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

import cn.estsh.i3plus.ext.mes.api.base.jx.IJxOqcProjectService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOqcRuleNoteDetail;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxOqcProjectModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
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.*;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/5 15:55
 **/
@RestController
@Api(tags = "出货检验项目")
@RequestMapping(MesExtConstWords.BASE_URL_MES_JX + "/oqc-project")
public class JxOqcProjectController {

    @Autowired
    private IJxOqcProjectService oqcProjectService;

    @GetMapping(value = "/query")
    @ApiOperation(value = "分页查询出货检验项目")
    public ResultBean queryOqcProjectPager(MesOqcRuleNoteDetail oqcProject, Pager pager) {
        try {
            oqcProject.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(oqcProjectService.queryOqcProjectPager(oqcProject, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增出货检验项目")
    public ResultBean insertOqcProject(JxOqcProjectModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.OQC_NO_TYPE, model.getOqcNoType())
                    .notNull(MesExtConstWords.CHECK_ITEM, model.getCheckItem())
                    .notNull(MesExtConstWords.CHECK_TYPE, model.getCheckType())
                    .notNull(MesExtConstWords.SEQ, model.getSeq());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            model.setCreateUser(AuthUtil.getSessionUser().getUserName());
            oqcProjectService.insertOqcProject(model);
            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 updateOqcProject( JxOqcProjectModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.ID, model.getId())
                    .notNull(MesExtConstWords.OQC_NO_TYPE, model.getOqcNoType())
                    .notNull(MesExtConstWords.CHECK_ITEM, model.getCheckItem())
                    .notNull(MesExtConstWords.CHECK_TYPE, model.getCheckType())
                    .notNull(MesExtConstWords.SEQ, model.getSeq());
            model.setModifyUser(AuthUtil.getSessionUser().getUserName());
            oqcProjectService.updateOqcProject(model);
            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 = "/status-update")
    @ApiOperation(value = "根据ID启用或禁用出货检验项目")
    public ResultBean updateOqcProjectValidById(JxOqcProjectModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.STATUS_LOWER_CASE, model.getStatus());
            oqcProjectService.updateOqcProjectValidById(model, 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);
        }
    }

}
