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

import cn.estsh.i3plus.ext.mes.api.busi.jx.IJxOqcReportFormService;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.*;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
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.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(MesExtConstWords.BASE_URL_MES_JX + "/report")
@Api(tags = "JX出货检验报表")
public class JxOqcReportFormController extends MesBaseController {

    @Autowired
    private IJxOqcReportFormService oqcReportFormService;

    @GetMapping(value = "/oqc-check/query-pager")
    @ApiOperation(value = "出货检验结果明细报表")
    public ResultBean queryOqcCheckReportPager(JxOqcCheckReportModel model, Pager pager) {
        try {

            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.WORK_TIME_START, model.getWorkTimeStart())
                    .notNull(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd());

            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(oqcReportFormService.queryOqcCheckReportPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/oqc-check/badness-update")
    @ApiOperation(value = "出货检验结果明细不合格不良结果变更")
    public ResultBean queryOqcCheckReportPager(JxOqcCheckReportModel model) {
        try {

            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.ID, model.getId())
                    .notNull(MesExtConstWords.BADNESS_TYPE_CODE, model.getBadnessTypeCode())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_CODE, model.getBadnessClassifyCode())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_DETAIL_CODE, model.getBadnessClassifyDetailCode())
                    .notNull(MesExtConstWords.BADNESS_CLASSIFY_DETAIL_NAME, model.getBadnessClassifyDetailName());

            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            oqcReportFormService.updateOqcCheckReport(model);
            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 queryOqcCheckDetailReportPager(JxOqcCheckDetailReportModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(oqcReportFormService.queryOqcCheckDetailReportPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/oqc-check-detail/batch-confirm")
    @ApiOperation(value = "出货检验项目明细报表批量确认")
    public ResultBean updateOqcCheckDetailConfirmUser(String[] idStr) {
        try {

            if (null == idStr) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            oqcReportFormService.updateOqcCheckDetailConfirmUser(idStr, 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);
        }
    }

    @PostMapping(value = "/oqc-check-detail/edit")
    @ApiOperation(value = "出货检验项目明细报表编辑")
    public ResultBean updateOqcCheckDetailEdit(@RequestBody JxOqcCheckDetailReportModel model) {
        try {

            if (null == model.getId()) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            oqcReportFormService.updateOqcCheckDetailEdit(model);

            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 = "/oqc-check/quality-early-warning/query")
    @ApiOperation(value = "出货检验质量预警")
    public ResultBean queryOqcCheckQualityEarlyWarningReport(@RequestBody JxOqcCheckQualityEarlyWarningReportModel model) {
        try {

            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.HAPPEN_TIME_START, model.getHappenTimeStart())
                    .notNull(MesExtConstWords.HAPPEN_TIME_END, model.getHappenTimeEnd())
                    .notNull(MesExtConstWords.DIMENSION, model.getDimension())
                    .notNull(MesExtConstWords.DIMENSION_1, model.getDimension1());

            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());

            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(oqcReportFormService.queryOqcCheckQualityEarlyWarningReport(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/oqc-last-check-detail/query-pager")
    @ApiOperation(value = "出货总检明细报表")
    public ResultBean queryOqcLastCheckDetailReportPager(@RequestBody JxOqcLastCheckDetailReportModel model) {
        try {

            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.WORK_TIME_START, model.getWorkTimeStart())
                    .notNull(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd());

            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(oqcReportFormService.queryOqcLastCheckDetailReportPager(model, model.getPager()));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/oqc-check/fre-confirm/query-pager")
    @ApiOperation(value = "出货检验缺件确认")
    public ResultBean queryOqcCheckFreConfirmReportPager(JxOqcCheckFreConfirmReportModel model, Pager pager) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.OUT_WC_TIME_START, model.getOutWcTimeStart())
                    .notNull(MesExtConstWords.OUT_WC_TIME_END, model.getOutWcTimeEnd());

            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(oqcReportFormService.queryOqcCheckFreConfirmReportPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/oqc-check/fre-confirm/detail/query")
    @ApiOperation(value = "出货检验缺件确认明细")
    public ResultBean queryOqcCheckFreConfirmDetailReport(JxOqcCheckFreConfirmDetailReportModel model) {
        try {

            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.ID_STR, model.getIdStr())
                    .notNull(MesExtConstWords.PART_NO, model.getPartNo())
                    .notNull(MesExtConstWords.PART_NAME_RDD, model.getPartNameRdd());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());

            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(oqcReportFormService.queryOqcCheckFreConfirmDetailReport(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/oqc-check-statistics/query")
    @ApiOperation(value = "出货检验月报日报")
    public ResultBean queryOqcCheckStatisticsReport(@RequestBody JxOqcCheckStatisticsReportModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.WORK_TIME_START, model.getWorkTimeStart())
                    .notNull(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd())
                    .notNull(MesExtConstWords.DIMENSION, model.getDimension())
                    .notNull(MesExtConstWords.DIMENSION_1, model.getDimension1());

            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(oqcReportFormService.queryOqcCheckStatisticsReport(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /******************* 成品出货检验管理 Start ***********************/
    @GetMapping(value = "/oqc-check-page-type/query-pager")
    @ApiOperation(value = "出货检验单查询")
    public ResultBean queryOqcCheckPager(JxOqcCheckReportModel model, Pager pager) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.SN_TYPE, model.getSnType());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(oqcReportFormService.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(JxOqcCheckReportModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.ID, model.getId())
                    .notNull(MesExtConstWords.CHECK_RESULT, model.getCheckResult());
            oqcReportFormService.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);
        }
    }

    @PostMapping(value = "/oqc-check/oqc-qty/update")
    @ApiOperation(value = "出货检验单检查范围变更")
    public ResultBean updateOqcCheckOqcqty(@RequestBody JxOqcCheckReportModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.ID, model.getId())
                    .notNull(MesExtConstWords.OQC_QTY, model.getOqcQty());
            oqcReportFormService.updateOqcCheckReportOqcQty(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-page-type/query-pager")
    @ApiOperation(value = "出货检验项目明细数据")
    public ResultBean queryOqcCheckDetailPager(JxOqcCheckDetailReportModel model, Pager pager) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.OQC_NO, model.getOqcNo());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(oqcReportFormService.queryOqcCheckDetailReportPager(model, 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(JxOqcCheckDetailReportModel detail) {
        try {
            ValidatorBean.beginValid(detail)
                    .notNull(MesExtConstWords.ID_STR, detail.getIdStr())
                    .notNull(MesExtConstWords.REMARK, detail.getRemark());
            oqcReportFormService.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(JxOqcCheckReportModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.ID, model.getId());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(oqcReportFormService.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 JxOqcBatchModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.OQC_NO, model.getOqcNo())
                    .notNull(MesExtConstWords.CHECK_RESULT, model.getCheckResult());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            oqcReportFormService.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);
        }
    }

    /******************* 成品出货检验管理 End ***********************/

    /******************* 出货检验操作履历 START *********************/
    //@GetMapping(value = "/oqc-check-travel/query-pager")
    //@ApiOperation(value = "出货检验单操作履历查询")
    //public ResultBean queryOqcCheckTravelPager(SxQmsOqcCheckTravel travel, Pager pager) {
    //    try {
    //        travel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
    //        return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
    //                .setListPager(oqcReportFormService.queryOqcCheckTravelPager(travel, pager));
    //    } catch (ImppBusiException busExcep) {
    //        return ResultBean.fail(busExcep);
    //    } catch (Exception e) {
    //        return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
    //    }
    //}
    /******************* 出货检验操作履历 END *********************/

}
