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

import cn.estsh.i3plus.ext.mes.api.base.ISxKpsnRuleService;
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.i3plus.pojo.mes.bean.MesKpsnRule;
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 SxKpsnRuleController extends MesBaseController {

    @Autowired
    private ISxKpsnRuleService kpsnRuleService;

    @GetMapping(value = "/sx-kpsn-rule/query")
    @ApiOperation(value = "按条件分页查询关键件条码校验规则信息")
    public ResultBean queryKpsnRuleByPager(MesKpsnRule kpsnRule, Pager pager) {
        try {
            kpsnRule.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(kpsnRuleService.queryKpsnRuleByPager(kpsnRule, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx-kpsn-rule/insert")
    @ApiOperation(value = "新增关键件条码校验规则信息")
    public ResultBean insertKpsnRule(MesKpsnRule kpsnRule) {
        try {
            ValidatorBean.beginValid(kpsnRule)
                    .notNull(MesExtConstWords.PART_NO, kpsnRule.getPartNo())
                    .notNull(MesExtConstWords.LENGTH, kpsnRule.getLength())
                    .notNull(MesExtConstWords.FROM1, kpsnRule.getFrom1())
                    .notNull(MesExtConstWords.TO1, kpsnRule.getTo1())
                    .notNull(MesExtConstWords.FIX1, kpsnRule.getFix1())
                    .minCheck(MesExtConstWords.LENGTH, kpsnRule.getLength(), 1)
                    .minCheck(MesExtConstWords.FROM1, kpsnRule.getFrom1(), 1)
                    .maxCheck(MesExtConstWords.FROM1, kpsnRule.getFrom1(), kpsnRule.getLength())
                    .maxCheck(MesExtConstWords.FROM1, kpsnRule.getFrom1(), kpsnRule.getTo1())
                    .minCheck(MesExtConstWords.TO1, kpsnRule.getTo1(), 1)
                    .maxCheck(MesExtConstWords.TO1, kpsnRule.getTo1(), kpsnRule.getLength());
            if (!StringUtils.isEmpty(kpsnRule.getFrom2()) && !StringUtils.isEmpty(kpsnRule.getTo2()) && kpsnRule.getFrom2().compareTo(0) > 0 && kpsnRule.getTo2().compareTo(0) > 0) {
                ValidatorBean.beginValid(kpsnRule)
                        .minCheck(MesExtConstWords.FROM2, kpsnRule.getFrom2(), 1)
                        .maxCheck(MesExtConstWords.FROM2, kpsnRule.getFrom2(), kpsnRule.getLength())
                        .maxCheck(MesExtConstWords.FROM2, kpsnRule.getFrom2(), kpsnRule.getTo2())
                        .minCheck(MesExtConstWords.TO2, kpsnRule.getTo2(), 1)
                        .maxCheck(MesExtConstWords.TO2, kpsnRule.getTo2(), kpsnRule.getLength());
            }

            kpsnRule.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            kpsnRule.setCreateUser(AuthUtil.getSessionUser().getUserName());
            kpsnRuleService.insertKpsnRule(kpsnRule);

            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 = "/sx-kpsn-rule/update")
    @ApiOperation(value = "更新关键件条码校验规则信息")
    public ResultBean updateKpsnRule(MesKpsnRule kpsnRule) {
        try {
            ValidatorBean.beginValid(kpsnRule)
                    .notNull(MesExtConstWords.ID, kpsnRule.getId())
                    .notNull(MesExtConstWords.PART_NO, kpsnRule.getPartNo())
                    .notNull(MesExtConstWords.LENGTH, kpsnRule.getLength())
                    .notNull(MesExtConstWords.FROM1, kpsnRule.getFrom1())
                    .notNull(MesExtConstWords.TO1, kpsnRule.getTo1())
                    .notNull(MesExtConstWords.FIX1, kpsnRule.getFix1())
                    .minCheck(MesExtConstWords.LENGTH, kpsnRule.getLength(), 1)
                    .minCheck(MesExtConstWords.FROM1, kpsnRule.getFrom1(), 1)
                    .maxCheck(MesExtConstWords.FROM1, kpsnRule.getFrom1(), kpsnRule.getLength())
                    .maxCheck(MesExtConstWords.FROM1, kpsnRule.getFrom1(), kpsnRule.getTo1())
                    .minCheck(MesExtConstWords.TO1, kpsnRule.getTo1(), 1)
                    .maxCheck(MesExtConstWords.TO1, kpsnRule.getTo1(), kpsnRule.getLength());
            if (!StringUtils.isEmpty(kpsnRule.getFrom2()) && !StringUtils.isEmpty(kpsnRule.getTo2()) && kpsnRule.getFrom2().compareTo(0) > 0 && kpsnRule.getTo2().compareTo(0) > 0) {
                ValidatorBean.beginValid(kpsnRule)
                        .minCheck(MesExtConstWords.FROM2, kpsnRule.getFrom2(), 1)
                        .maxCheck(MesExtConstWords.FROM2, kpsnRule.getFrom2(), kpsnRule.getLength())
                        .maxCheck(MesExtConstWords.FROM2, kpsnRule.getFrom2(), kpsnRule.getTo2())
                        .minCheck(MesExtConstWords.TO2, kpsnRule.getTo2(), 1)
                        .maxCheck(MesExtConstWords.TO2, kpsnRule.getTo2(), kpsnRule.getLength());
            }

            kpsnRule.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            kpsnRule.setModifyUser(AuthUtil.getSessionUser().getUserName());
            kpsnRuleService.updateKpsnRule(kpsnRule);
            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 = "/sx-kpsn-rule/status-update")
    @ApiOperation(value = "根据id启用或禁用关键件条码校验规则信息")
    public ResultBean updateKpsnRuleValidById(Long id, Integer status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            kpsnRuleService.updateKpsnRuleValidById(id, 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);
        }
    }

}
