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

import cn.estsh.i3plus.ext.mes.api.base.bu3.IBuQManageService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessQManage;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/18 13:33 下午
 * @Description:
 **/
@RestController
@Api(tags = "Q管理")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/bu/q-data")
public class BuQManageController {
    public static final Logger LOGGER = LoggerFactory.getLogger(BuQManageController.class);

    @Autowired
    private IBuQManageService qManageService;

    @PostMapping(value = "/mes-process-q-manage/insert")
    @ApiOperation(value = "添加MES_工序Q管理项目信息")
    public ResultBean insertMesProcessQManage(MesProcessQManage mesProcessQManage) {
        try {

            ValidatorBean.checkNotNull(mesProcessQManage.getCheckType());
            //条件验证
            ValidatorBean.beginValid(mesProcessQManage)
                    .notNull(MesExtConstWords.PROCESS_CODE,mesProcessQManage.getProcessCode())
                    .notNull("manageProjects", mesProcessQManage.getManageProjects())
                    .notNull("screwType", mesProcessQManage.getScrewType())
                    .notNull("tighteningTorque",mesProcessQManage.getTighteningTorque())
                    .notNull(MesExtConstWords.CHECK_TYPE, mesProcessQManage.getCheckType());
            if(mesProcessQManage.getCheckType().equals(MesExtEnumUtil.MES_Q_MANAGER_CHECK_TYPE.ORDER_CHECK.getValue())){
                ValidatorBean.checkNotNull(mesProcessQManage.getPartNo());
            }

            mesProcessQManage.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            qManageService.insertMesProcessQManage(mesProcessQManage);
            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {
            LOGGER.error(imppException.getErrorMsg() + "：{}", imppException.getErrorDetail(), imppException);
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-process-q-manage/update")
    @ApiOperation(value = "更新MES_工序Q管理项目")
    public ResultBean updateMesProcessQManage(MesProcessQManage mesProcessQManage) {
        try {
            //条件验证
            ValidatorBean.beginValid(mesProcessQManage)
                    .notNull(MesExtConstWords.PROCESS_CODE,mesProcessQManage.getProcessCode())
                    .notNull("manageProjects", mesProcessQManage.getManageProjects())
                    .notNull("screwType", mesProcessQManage.getScrewType())
                    .notNull("tighteningTorque",mesProcessQManage.getTighteningTorque());

            if (mesProcessQManage.getId() == null || mesProcessQManage.getId().longValue() < 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            qManageService.updateMesProcessQManage(mesProcessQManage, 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);
        }
    }
}
