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

import cn.estsh.i3plus.ext.mes.api.busi.ISxOqcManageService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOqcCheckDetail;
import cn.estsh.i3plus.ext.mes.pojo.model.SxOqcBatchModel;
import cn.estsh.i3plus.ext.mes.pojo.model.SxOqcManageModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
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 + "/sx/oqc-manage")
@Api(tags = "出货检验管理")
public class SxOqcManageController extends MesBaseController {

    @Autowired
    private ISxOqcManageService sxOqcManageService;

    @GetMapping(value = "/oqc-check/query-pager")
    @ApiOperation(value = "出货检验单查询")
    public ResultBean queryOqcCheckPager(SxOqcManageModel model, Pager pager) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.PAGE_TYPE, model.getPageType());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(sxOqcManageService.queryOqcCheckPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/oqc-check/check-result/update")
    @ApiOperation(value = "出货检验单结果变更")
    public ResultBean updateOqcCheckResult(SxOqcManageModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.ID, model.getId())
                    .notNull(MesExtConstWords.CHECK_RESULT, model.getCheckResult());
            sxOqcManageService.updateOqcCheckResult(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);
        }
    }

    @GetMapping(value = "/oqc-check-detail/query-pager")
    @ApiOperation(value = "出货检验单项目明细查询")
    public ResultBean queryOqcCheckDetailPager(MesOqcCheckDetail detail, Pager pager) {
        try {
            ValidatorBean.beginValid(detail)
                    .notNull(MesExtConstWords.OQC_NO, detail.getOqcNo());
            detail.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(sxOqcManageService.queryOqcCheckDetailPager(detail, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/oqc-check-detail/remark/update")
    @ApiOperation(value = "出货检验单项目明细备注修改")
    public ResultBean updateOqcCheckDetailRemark(MesOqcCheckDetail detail) {
        try {
            ValidatorBean.beginValid(detail)
                    .notNull(MesExtConstWords.ID, detail.getId())
                    .notNull(MesExtConstWords.REMARK, detail.getRemark());
            sxOqcManageService.updateOqcCheckDetailRemark(detail, 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);
        }
    }

    @GetMapping(value = "/oqc-package-detail/query")
    @ApiOperation(value = "出货检验单产品明细查询")
    public ResultBean queryOqcPackageDetailList(SxOqcManageModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.ID, model.getId());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(sxOqcManageService.queryOqcPackageDetailList(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/oqc-package-detail/check-result/update")
    @ApiOperation(value = "出货检验产品结果变更")
    public ResultBean updateOqcPackageDetailResult(@RequestBody SxOqcBatchModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.OQC_NO, model.getOqcNo())
                    .notNull(MesExtConstWords.CHECK_RESULT, model.getCheckResult());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            sxOqcManageService.updateOqcPackageDetailResult(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);
        }
    }

}
