package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.base.ep;

import cn.estsh.i3plus.ext.mes.pcn.api.base.ep.IEpSxRepairWorkService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ep.IEpSxSyncBusiService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesBadCellDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesRepairRecordExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.RepairRequestModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpBadCellDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpDefectModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.LocaleUtils;
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.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-16 16:14
 */
@RestController
@Api(tags = "EP返修作业")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/ep-ext/module-repair-data")
public class EpRepairJobController {

    @Autowired
    private IEpSxSyncBusiService sxSyncBusiService;

    @Autowired
    private IEpSxRepairWorkService sxRepairWorkService;


    @PostMapping(value = "/insert-defect-records")
    @ApiOperation(value = "本体/组件返修-不良明细-新增", notes = "本体/组件返修-不良明细-新增")
    public ResultBean insertMesDefectRecord(@RequestBody EpBadCellDetailModel mesDefectRecord) {
        List<EpDefectModel> mesDefectList = mesDefectRecord.getMesDefectList();
        if (CollectionUtils.isEmpty(mesDefectList)) {
            return ResultBean.fail("入参缺失缺陷集合");
        }
        try {
            ValidatorBean.beginValid(mesDefectRecord)
                    .notNull(MesPcnExtConstWords.PART_NO, mesDefectRecord.getPartNo())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, mesDefectRecord.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.WORK_CELL_CODE, mesDefectRecord.getWorkCellCode());
            for (EpDefectModel mesDefect : mesDefectList) {
                ValidatorBean.beginValid(mesDefect)
                        .notNull(MesPcnExtConstWords.DEFECT_CODE, mesDefect.getDefectCode())
                        .notNull(MesPcnExtConstWords.DEFECT_NAME, mesDefect.getDefectName())
                        .notNull(MesPcnExtConstWords.ITEM_PART_NO, mesDefect.getItemPartNo())
                        .notNull(MesPcnExtConstWords.ITEM_PART_NAME, mesDefect.getItemPartName())
                        .notNull(MesPcnExtConstWords.HAPPEN_STATION, mesDefect.getHappenStation());
            }
            sxSyncBusiService.doRepairOrQualityDecision(mesDefectRecord, 2);
            return ResultBean.success(LocaleUtils.getLocaleRes("新增成功")).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert-repair-record")
    @ApiOperation(value = "本体/组件返修-维修明细-新增", notes = "本体/组件返修-维修明细-新增")
    public ResultBean insertMesRepairRecord(MesRepairRecordExt mesRepairRecord) {
        try {
            ValidatorBean.beginValid(mesRepairRecord)
                    .notNull(MesPcnExtConstWords.PART_NO, mesRepairRecord.getPartNo())
                    .notNull(MesPcnExtConstWords.PART_NAME, mesRepairRecord.getPartName())
                    .notNull(MesPcnExtConstWords.DEFECT_CODE, mesRepairRecord.getDefectCode())
                    .notNull(MesPcnExtConstWords.DEFECT_NAME, mesRepairRecord.getDefectName())
                    .notNull(MesPcnExtConstWords.REPAIR_CODE, mesRepairRecord.getRepairCode())
                    .notNull(MesPcnExtConstWords.REPAIR_JUDGE_CODE, mesRepairRecord.getRepairJudgeCode())
                    .notNull(MesPcnExtConstWords.REPAIR_NAME, mesRepairRecord.getRepairName())
                    .notNull(MesPcnExtConstWords.DEFECT_RECORD_ID, mesRepairRecord.getDefectRecordId())
                    .notNull(MesPcnExtConstWords.WORK_CELL_CODE, mesRepairRecord.getWorkCellCode());
            sxRepairWorkService.insertMesRepairRecord(mesRepairRecord);
            return ResultBean.success(LocaleUtils.getLocaleRes(LocaleUtils.getLocaleRes("操作成功")))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update-repair-record")
    @ApiOperation(value = "本体/组件返修-维修明细-修改", notes = "本体/组件返修-维修明细-修改")
    public ResultBean updateMesRepairRecord(@RequestBody MesRepairRecordExt repairRecord) {
        try {
            sxRepairWorkService.updateMesRepairRecord(repairRecord);
            return ResultBean.success(LocaleUtils.getLocaleRes("修改成功")).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert-repair-defect-record")
    @ApiOperation(value = "本体/组件返修-一步操作", notes = "本体/组件返修-一步操作")
    public ResultBean insertMesRepairDefectRecord(@RequestBody RepairRequestModel requestModel) {
        List<MesBadCellDetail> mesDefectRecordList = requestModel.getMesDefectRecordList();
        if (CollectionUtils.isEmpty(mesDefectRecordList)) {
            return ResultBean.fail("请求必要参数信息为空");
        }
        List<MesRepairRecordExt> mesRepairRecordList = requestModel.getMesRepairRecordList();
        if (CollectionUtils.isEmpty(mesRepairRecordList)) {
            return ResultBean.fail("请求必要参数信息为空");
        }
        try {
            ValidatorBean.beginValid(requestModel)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, requestModel.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, requestModel.getModifyUser())
                    .notNull(MesPcnExtConstWords.WORK_CELL_CODE, requestModel.getWorkCellCode());
            for (MesBadCellDetail mesDefectRecord : mesDefectRecordList) {
                ValidatorBean.beginValid(mesDefectRecord)
                        .notNull(MesPcnExtConstWords.PART_NO, mesDefectRecord.getPartNo())
                        .notNull(MesPcnExtConstWords.DEFECT_CODE, mesDefectRecord.getDefectCode())
                        .notNull(MesPcnExtConstWords.DEFECT_NAME, mesDefectRecord.getDefectName())
                        .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, mesDefectRecord.getWorkCenterCode())
                        .notNull(MesPcnExtConstWords.ITEM_PART_NO, mesDefectRecord.getItemPartNo())
                        .notNull(MesPcnExtConstWords.ITEM_PART_NAME, mesDefectRecord.getItemPartName())
                        .notNull(MesPcnExtConstWords.HAPPEN_STATION, mesDefectRecord.getHappenStation());
            }
            for (MesRepairRecordExt mesRepairRecord : mesRepairRecordList) {
                ValidatorBean.beginValid(mesRepairRecord)
                        .notNull(MesPcnExtConstWords.PART_NO, mesRepairRecord.getPartNo())
                        .notNull(MesPcnExtConstWords.PART_NAME, mesRepairRecord.getPartName())
                        .notNull(MesPcnExtConstWords.DEFECT_CODE, mesRepairRecord.getDefectCode())
                        .notNull(MesPcnExtConstWords.DEFECT_NAME, mesRepairRecord.getDefectName())
                        .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, mesRepairRecord.getWorkCenterCode())
                        .notNull(MesPcnExtConstWords.REPAIR_JUDGE_CODE, mesRepairRecord.getRepairJudgeCode())
                        .notNull(MesPcnExtConstWords.REPAIR_CODE, mesRepairRecord.getRepairCode())
                        .notNull(MesPcnExtConstWords.REPAIR_NAME, mesRepairRecord.getRepairName());
            }
            sxSyncBusiService.doRepairOrQualityDecision(requestModel, 3);
            return ResultBean.success(LocaleUtils.getLocaleRes(LocaleUtils.getLocaleRes("操作成功")))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update-repair-complete")
    @ApiOperation(value = "本体返修-维修完成", notes = "本体返修-维修完成")
    public ResultBean updateRepairComplete(String serialNumber, String defectType, String organizeCode, String modifyUser) {
        try {
            sxRepairWorkService.updateRepairComplete(serialNumber, defectType, organizeCode, modifyUser);
            return ResultBean.success(LocaleUtils.getLocaleRes("操作成功")).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update-batch-repair-complete")
    @ApiOperation(value = "组件返修-维修完成", notes = "组件返修-维修完成")
    public ResultBean updateRepairBatchComplete(String workOrderNo, String defectType, Double defectQty, String organizeCode, String modifyUser) {
        try {
            sxRepairWorkService.updateRepairBatchComplete(workOrderNo, defectType, defectQty, organizeCode, modifyUser);
            return ResultBean.success(LocaleUtils.getLocaleRes("操作成功")).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
