package cn.estsh.i3plus.ext.mes.apiservice.controller.base;

import cn.estsh.i3plus.ext.mes.api.base.IMesNumberOfSafeOperationDaysService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesFourMChange;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesNumberOfSafeOperationDays;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesBaseController;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文件描述
 *
 * @author xiangming.liao
 * @date 2021年06月30日 14:39
 */
@RestController
@Api(tags = "初始安全运行天数")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/safe-days")
public class MesNumberOfSafeOperationDaysController extends MesBaseController {
    @Autowired
    private IMesNumberOfSafeOperationDaysService numberOfSafeOperationDaysService;

    @GetMapping(value = "/query")
    @ApiOperation(value = "查询初始安全运行天数")
    public ResultBean queryNumberOfSafeOperationDays(String organizeCode) {
        try {
            return ResultBean.success("查询成功").setResultObject(numberOfSafeOperationDaysService.queryNumberOfSafeOperationDays(organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update")
    @ApiOperation(value = "设置初始安全运行天数")
    public ResultBean update(@RequestBody MesNumberOfSafeOperationDays numberOfSafeOperationDays) {
        try {
            //条件验证
            ValidatorBean.beginValid(numberOfSafeOperationDays)
                    .notNull("initStartDate", numberOfSafeOperationDays.getInitStartDate())
                    .notNull("initEndDate", numberOfSafeOperationDays.getInitEndDate());
            return ResultBean.success("设置成功").setResultObject(numberOfSafeOperationDaysService.updateNumberOfSafeOperationDays(numberOfSafeOperationDays))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
