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

import cn.estsh.i3plus.ext.mes.api.base.INumberRuleExtService;
import cn.estsh.i3plus.ext.mes.api.base.ep.IEpNumberRuleExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesNumberRuleExt;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
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.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;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-27 13:39
 */
@RestController
@Api(tags = "条码生成规则")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/ep/number-rule-ext")
public class EpNumberRuleExtController {

    @Autowired
    private IEpNumberRuleExtService numberRuleExtService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增条码生成规则信息")
    public ResultBean insertNumberRule(MesNumberRuleExt numberRuleExt) {
        try {
            ValidatorBean.beginValid(numberRuleExt)
                    .notNull(MesExtConstWords.RULE_CODE, numberRuleExt.getRuleCode())
                    .notNull(MesExtConstWords.NUMBER_RULE, numberRuleExt.getNumberRule())
                    .notNull(MesExtConstWords.SERIALNO_LENGTH, numberRuleExt.getSerialnoLength())
                    .notNull(MesExtConstWords.SERIALNO_INCREMENT, numberRuleExt.getSerialnoIncrement())
                    .notNull(MesExtConstWords.BEGIN_VAL, numberRuleExt.getBeginVal())
                    .notNull(MesExtConstWords.IS_CYCLE, numberRuleExt.getIsCycle());

            numberRuleExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            numberRuleExt.setCreateUser(AuthUtil.getSessionUser().getUserName());
            numberRuleExtService.insertNumberRule(numberRuleExt);

            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 = "/update")
    @ApiOperation(value = "更新条码生成规则信息")
    public ResultBean updateNumberRule(MesNumberRuleExt numberRuleExt) {
        try {
            ValidatorBean.beginValid(numberRuleExt)
                    .notNull(MesExtConstWords.ID, numberRuleExt.getId())
                    .notNull(MesExtConstWords.RULE_CODE, numberRuleExt.getRuleCode())
                    .notNull(MesExtConstWords.NUMBER_RULE, numberRuleExt.getNumberRule())
                    .notNull(MesExtConstWords.SERIALNO_LENGTH, numberRuleExt.getSerialnoLength())
                    .notNull(MesExtConstWords.SERIALNO_INCREMENT, numberRuleExt.getSerialnoIncrement())
                    .notNull(MesExtConstWords.BEGIN_VAL, numberRuleExt.getBeginVal())
                    .notNull(MesExtConstWords.IS_CYCLE, numberRuleExt.getIsCycle());

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