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

import cn.estsh.i3plus.ext.mes.api.busi.IProductEncodeCfgExtService;
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.MesProductEncodeCfg;
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.web.bind.annotation.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "产品编码配置扩展控制层")
public class ProductEncodeCfgExtController extends MesBaseController {

    @Autowired
    private IProductEncodeCfgExtService productEncodeCfgExtService;

    @GetMapping(value = "/product-encode-cfg-ext/query")
    @ApiOperation(value = "按条件分页查询MES产品编码配置数据")
    public ResultBean queryMesProductEncodeCfgPager(MesProductEncodeCfg productEncodeCfg, Pager pager) {
        try {
            productEncodeCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(productEncodeCfgExtService.queryMesProductEncodeCfgByPager(productEncodeCfg, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/product-encode-cfg-ext/insert")
    @ApiOperation(value = "新增mes产品编码配置信息")
    public ResultBean insertMesProductEncodeCfg(MesProductEncodeCfg productEncodeCfg) {
        try {
            ValidatorBean.beginValid(productEncodeCfg)
                    .notNull(MesExtConstWords.MATCH_VALUE, productEncodeCfg.getMatchValue())
                    .notNull(MesExtConstWords.CODE_TYPE, productEncodeCfg.getCodeType())
                    .notNull(MesExtConstWords.MATCH_TYPE, productEncodeCfg.getMatchType())
                    .notNull(MesExtConstWords.RULE_CODE, productEncodeCfg.getRuleCode());

            productEncodeCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            productEncodeCfg.setCreateUser(AuthUtil.getSessionUser().getUserName());
            productEncodeCfgExtService.insertMesProductEncodeCfg(productEncodeCfg);

            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 = "/product-encode-cfg-ext/update")
    @ApiOperation(value = "更新mes产品编码配置信息")
    public ResultBean updateMesProductEncodeCfg(MesProductEncodeCfg productEncodeCfg) {
        try {
            ValidatorBean.beginValid(productEncodeCfg)
                    .notNull(MesExtConstWords.ID, productEncodeCfg.getId())
                    .notNull(MesExtConstWords.MATCH_VALUE, productEncodeCfg.getMatchValue())
                    .notNull(MesExtConstWords.CODE_TYPE, productEncodeCfg.getCodeType())
                    .notNull(MesExtConstWords.MATCH_TYPE, productEncodeCfg.getMatchType())
                    .notNull(MesExtConstWords.RULE_CODE, productEncodeCfg.getRuleCode());

            productEncodeCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            productEncodeCfg.setModifyUser(AuthUtil.getSessionUser().getUserName());
            productEncodeCfgExtService.updateMesProductEncodeCfg(productEncodeCfg);
            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 = "/product-encode-cfg-ext/status-update")
    @ApiOperation(value = "根据id启用或禁用产品编码配置信息")
    public ResultBean updateMesProductEncodeCfgStatusById(Long id, int status) {
        try {
            if (null == id) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            productEncodeCfgExtService.updateMesProductEncodeCfgStatusById(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);
        }
    }

}
