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


import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesNcProcessingService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesNcPersonModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesNcProcessingInputModel;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.mes.bean.nc.MesPartInspection;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "NC处理页面")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/nc-processing")
@Slf4j
public class MesNcProcessingController {

    @Autowired
    private IMesNcProcessingService ncProcessingService;


    @GetMapping("/query-part-inspection-pager")
    @ApiOperation(value = "查询NC-零件检测-单据")
    public ResultBean queryPartInspectionByPager(MesPartInspection partInspection, Pager pager) {

        try {
            ValidatorBean.checkNotNull(partInspection.getOrganizeCode(), "工厂不能为空");
            partInspection.setOrganizeCode(partInspection.getOrganizeCode());
            ListPager<MesPartInspection> partInspectionListPager = ncProcessingService.queryPartInspectionByPager(partInspection, pager);
            return ResultBean.success("查询成功").setListPager(partInspectionListPager);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query-part-inspection")
    @ApiOperation(value = "查询NC-零件检测-单据")
    public ResultBean queryPartInspection(MesPartInspection partInspection) {

        try {
            ValidatorBean.checkNotNull(partInspection.getOrganizeCode(), "工厂不能为空");
            return ResultBean.success("查询成功").setResultObject(ncProcessingService.queryPartInspection(partInspection));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query-person")
    @ApiOperation(value = "查询责任人")
    public ResultBean queryPerson(String organizeCode) {

        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂不能为空");
            return ResultBean.success("查询成功").setResultList(ncProcessingService.queryPerson(organizeCode));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @PostMapping("/save-nc")
    @ApiOperation(value = "NC处理")
    public ResultBean saveNc(@RequestBody MesNcProcessingInputModel model) {

        try {
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂不能为空");
            ValidatorBean.checkNotNull(model.getUserName(), "操作人不能为空");
            ValidatorBean.checkNotNull(model.getPartInspection(), "单据不能为空");
            ValidatorBean.checkNotNull(model.getPartInspectionDetailList(), "单据明细不能为空");
            if (model.getType() == MesExtEnumUtil.NC_TYPE.REWORK.getValue()
                    || model.getType() == MesExtEnumUtil.NC_TYPE.SCRAP.getValue()) {
                ValidatorBean.checkNotNull(model.getPerson(), "责任人不能为空");
                if (model.getPerson().getId() == null) {
                    throw new ImppBusiException("责任人不能为空");
                }
                ValidatorBean.checkNotNull(model.getInventoryLocationCode(), "区域代码不能为空");
            }

            String description = MesExtEnumUtil.NC_TYPE.valueOfDescription(model.getType());
            if (StringUtils.isEmpty(description)) {
                throw new ImppBusiException(String.format("【%s】类型不存在，请检查数据", model.getType()));

            }
            if (CollectionUtils.isEmpty(model.getPartInspectionDetailList())) {
                throw new ImppBusiException("不存在可疑信息，请检查数据");
            }
            ncProcessingService.saveNc(model, model.getOrganizeCode(), false);
            return ResultBean.success("NC处理成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/save-nc-lt")
    @ApiOperation(value = "NC处理")
    public ResultBean lotSaveNc(@RequestBody MesNcProcessingInputModel model) {

        try {
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂不能为空");
            ValidatorBean.checkNotNull(model.getUserName(), "操作人不能为空");
            ValidatorBean.checkNotNull(model.getPartInspection(), "单据不能为空");
            ValidatorBean.checkNotNull(model.getPartInspectionDetailList(), "单据明细不能为空");
            if (CollectionUtils.isEmpty(model.getPersonModelList())) {
                throw new ImppBusiException("责任人信息不能为空");
            }
            for (MesNcPersonModel mesNcPersonModel : model.getPersonModelList()) {
                if (mesNcPersonModel.getNcStatus() == MesExtEnumUtil.PART_INSPECTION_NC_STATUS.REWORK.getValue()
                        || mesNcPersonModel.getNcStatus() == MesExtEnumUtil.PART_INSPECTION_NC_STATUS.SCRAP.getValue()) {
                    ValidatorBean.checkNotNull(mesNcPersonModel.getOnlyPerson(), "责任人不能为空");
                }
            }
            if (CollectionUtils.isEmpty(model.getPartInspectionDetailList())) {
                throw new ImppBusiException("不存在可疑信息，请检查数据");
            }
            ncProcessingService.saveNc(model, model.getOrganizeCode(), false);
            return ResultBean.success("NC处理成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query-part-inspection-create-user")
    @ApiOperation(value = "查询检验人（NC-零件检测-单据 表的createUser）")
    public ResultBean queryPartInspectionCreateUser(String organizeCode) {
        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂不能为空");
            return ResultBean.success("查询成功").setResultList(
                    ncProcessingService.queryPartInspectionCreateUser(organizeCode));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @GetMapping("/sync-qms-scrap")
    @ApiOperation(value = "同步到qms中间表")
    public ResultBean syncQmsScrap(String organizeCode) {
        try {
            ValidatorBean.checkNotNull(organizeCode, "工厂不能为空");
            ncProcessingService.syncQmsScrap(organizeCode);
            return ResultBean.success("同步成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
