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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IQualityDecisionService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxSyncBusiService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesBadCellDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.RepairRequestModel;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesPcnBaseController;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@Api(tags = "质量判定")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN+ "/quality")
public class QualityController extends MesPcnBaseController {

    @Autowired
    private IQualityDecisionService qualityDecisionService;

    @Autowired
    private ISxSyncBusiService sxSyncBusiService;

    /**
     * 扫描条码验证
     *
     * @param serialNumber
     * @param organizeCode
     * @return
     */
    @GetMapping(value = "/check-serial-number/do")
    @ApiOperation(value = "校验条码是否存在")
    public ResultBean doCheckSerialNumber(String serialNumber, String organizeCode, String workCenterCode) {
        try {
            return ResultBean.success("查询成功")
                    .setResultObject(qualityDecisionService.doCheckSerialNumberExt(serialNumber, organizeCode, workCenterCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 获取不良信息中的数据
     *
     * @param organizeCode
     * @return
     */
    @GetMapping(value = "/mes-defect/find")
    @ApiOperation(value = "获取不良信息中的数据")
    public ResultBean findMesDefect(String organizeCode) {
        try {
            return ResultBean.success("查询成功")
                    .setResultMap(qualityDecisionService.findMesDefect(organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 新增不良明细数据
     *
     * @param mesBadCellDetailList
     * @param serialNumber
     * @param organizeCode
     * @param userName
     * @return
     */
    @PostMapping(value = "/mes-bad-cell-and-detail/insert")
    @ApiOperation(value = "新增不良明细数据")
    public ResultBean insertMesBadCellAndDetail(@RequestBody List<MesBadCellDetail> mesBadCellDetailList,
                                                String serialNumber, String organizeCode, String workCenterCode,
                                                String workCellCode, String processCode, String userName) {
        try {
            RepairRequestModel repairRequestModel = new RepairRequestModel();
            repairRequestModel.setMesDefectRecordList(mesBadCellDetailList);
            repairRequestModel.setSerialNumber(serialNumber);
            repairRequestModel.setOrganizeCode(organizeCode);
            repairRequestModel.setWorkCenterCode(workCenterCode);
            repairRequestModel.setWorkCellCode(workCellCode);
            repairRequestModel.setProcessCode(processCode);
            repairRequestModel.setModifyUser(userName);
            sxSyncBusiService.doRepairOrQualityDecision(repairRequestModel, 1);
            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 = "/do-unfreeze")
    @ApiOperation(value = "解冻")
    public ResultBean doUnfreeze(RepairRequestModel repairRequestModel) {
        try {
            qualityDecisionService.doUnfreeze(repairRequestModel);
            return ResultBean.success("解冻成功!").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 获取工位工序关系的数据
     *
     * @param workCenterCode
     * @param workCellCode
     * @param organizeCode
     * @return
     */
    @GetMapping(value = "/mes-process-cell/get")
    @ApiOperation(value = "获取工位工序关系的数据")
    public ResultBean getMesProcessCell(String workCenterCode, String workCellCode, String organizeCode) {
        try {
            return ResultBean.success("查询成功")
                    .setResultObject(qualityDecisionService.getMesProcessCell(workCenterCode, workCellCode, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
