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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxEsopCenterRemainCfgService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEsopCenterRemainCfg;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesBaseController;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "作业指导书生产线切替数量配置控制层")
public class SxEsopCenterRemainCfgController extends MesBaseController {

    @Autowired
    private ISxEsopCenterRemainCfgService esopCenterRemainCfgService;

    @GetMapping(value = "/esop-center-remain-cfg/query")
    @ApiOperation(value = "按条件分页查询作业指导书生产线切替数量配置信息")
    public ResultBean queryEsopCenterRemainCfgByPager(MesEsopCenterRemainCfg esopCenterRemainCfg, Pager pager) {
        try {
            esopCenterRemainCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(esopCenterRemainCfgService.queryEsopCenterRemainCfgByPager(esopCenterRemainCfg, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/esop-center-remain-cfg/insert")
    @ApiOperation(value = "新增作业指导书生产线切替数量配置信息")
    public ResultBean insertEsopCenterRemainCfg(MesEsopCenterRemainCfg esopCenterRemainCfg) {
        try {
            ValidatorBean.beginValid(esopCenterRemainCfg)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, esopCenterRemainCfg.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_CENTER_NAME, esopCenterRemainCfg.getWorkCenterName())
                    .notNull(MesExtConstWords.REMAIN_QTY, esopCenterRemainCfg.getRemainQty());

            esopCenterRemainCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            esopCenterRemainCfg.setCreateUser(AuthUtil.getSessionUser().getUserName());
            esopCenterRemainCfgService.insertEsopCenterRemainCfg(esopCenterRemainCfg);

            return ResultBean.success("新增成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/esop-center-remain-cfg/update")
    @ApiOperation(value = "更新作业指导书生产线切替数量配置信息")
    public ResultBean updateEsopCenterRemainCfg(MesEsopCenterRemainCfg esopCenterRemainCfg) {
        try {
            ValidatorBean.beginValid(esopCenterRemainCfg)
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, esopCenterRemainCfg.getWorkCenterCode())
                    .notNull(MesExtConstWords.REMAIN_QTY, esopCenterRemainCfg.getRemainQty());

            esopCenterRemainCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            esopCenterRemainCfg.setModifyUser(AuthUtil.getSessionUser().getUserName());
            esopCenterRemainCfgService.updateEsopCenterRemainCfg(esopCenterRemainCfg);
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/esop-center-remain-cfg/status-update")
    @ApiOperation(value = "根据id启用或禁用作业指导书生产线切替数量配置信息")
    public ResultBean updateEsopCenterRemainCfgValidById(String idStr, Integer status) {
        try {
            if (StringUtils.isEmpty(idStr)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            esopCenterRemainCfgService.updateEsopCenterRemainCfgValidById(idStr, status, AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/esop-center-remain-cfg/delete")
    @ApiOperation(value = "根据id删除作业指导书生产线切替数量配置信息")
    public ResultBean deleteEsopCenterRemainCfgById(String idStr) {
        try {
            if (StringUtils.isEmpty(idStr)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            esopCenterRemainCfgService.deleteEsopCenterRemainCfgById(idStr, AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("删除成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
