package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.step.jx;

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.jx.JxSnFinalInspectionModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description : 嘉兴条码终检初始化工单工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("jxSnFinalInspectionInitOrderStepService")
public class JxSnFinalInspectionInitOrderStepService extends BaseStepService {

    @Autowired
    private JxSnFinalInspectionModuleService snFinalInspectionModuleService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        log.info("工厂{}生产线{}工位{}: --- STEP EXECUTE --- JxSnFinalInspectionInitOrderStepService --- START --- ", reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());

        StepResult stepResult = StepResult.getSuccessComplete();

        StationResultBean resultBean = new StationResultBean();

        String selectWorkOrderNo = (String) snFinalInspectionModuleService.doHandleSelectWorkOrderNo(reqBean, 1, null);
        String curExecWorkOrderNo = (String) snFinalInspectionModuleService.doHandleCurExecWorkOrder(reqBean, 1, null);

        if (!StringUtils.isEmpty(selectWorkOrderNo)) snFinalInspectionModuleService.doHandleSelectWorkOrderNo(reqBean, 3, null);
        if (!StringUtils.isEmpty(selectWorkOrderNo) && !StringUtils.isEmpty(curExecWorkOrderNo)) {
            if (selectWorkOrderNo.equals(curExecWorkOrderNo)) selectWorkOrderNo = null;
            else curExecWorkOrderNo = null;
        }

        MesWorkOrderExt workOrderExt = null;

        if (!StringUtils.isEmpty(selectWorkOrderNo)) {
            workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), selectWorkOrderNo);
            checkWorkOrderExtIsValid(reqBean, resultBean, stepResult, workOrderExt, String.format("选择的工单号[%s]", selectWorkOrderNo));
            doCacheCurExecWorkOrderNo(reqBean, resultBean, stepResult, selectWorkOrderNo);
        }

        if (!StringUtils.isEmpty(curExecWorkOrderNo)) {
            workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), curExecWorkOrderNo);
            checkWorkOrderExtIsValid(reqBean, resultBean, stepResult, workOrderExt, String.format("此前操作的工单号[%s]", curExecWorkOrderNo));
        }

        if (null == workOrderExt || !stepResult.isCompleted()) execSendGuideAndThrowEx(reqBean, resultBean, "请选择启动状态的生产工单!");

        cacheOrderModel(reqBean, workOrderExt);

        log.info("工厂{}生产线{}工位{}: --- STEP EXECUTE --- JxSnFinalInspectionInitOrderStepService --- SUCCESS --- ", reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());

        return stepResult;
    }

    private void doCacheCurExecWorkOrderNo(StationRequestBean reqBean, StationResultBean resultBean, StepResult stepResult, String selectWorkOrderNo) {
        Boolean flag = (Boolean) snFinalInspectionModuleService.doHandleCurExecWorkOrder(reqBean, 2, selectWorkOrderNo);
        if (!flag) execNonCompleteAndSendMsg(reqBean, resultBean, stepResult, String.format("选择的工单号[%s]初始化失败!", selectWorkOrderNo));
        else {
            snFinalInspectionModuleService.execStateModule(reqBean, null, null);
            if (stepResult.isCompleted()) this.sendMessage(reqBean, resultBean, String.format("选择的工单号[%s]初始化成功!", selectWorkOrderNo), MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        }
    }

    private void checkWorkOrderExtIsValid(StationRequestBean reqBean, StationResultBean resultBean, StepResult stepResult, MesWorkOrderExt workOrderExt, String suffix) {

        if (null == workOrderExt) execNonCompleteAndSendMsg(reqBean, resultBean, stepResult, String.format("%s对应的工单信息不存在!", suffix));

        if (stepResult.isCompleted() && MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue() != workOrderExt.getWorkOrderStatus())
            execNonCompleteAndSendMsg(reqBean, resultBean, stepResult, String.format("%s状态[%s],不支持在此工位操作!", suffix, MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderExt.getWorkOrderStatus())));

        if (stepResult.isCompleted() && MathOperation.compareTo(workOrderExt.getQty(), new Double(0)) <= 0)
            execNonCompleteAndSendMsg(reqBean, resultBean, stepResult, String.format("%s计划数量为[0],不支持在此工位操作!", suffix));

        if (!stepResult.isCompleted()) return;

    }

    private void cacheOrderModel(StationRequestBean reqBean, MesWorkOrderExt workOrderExt) {
        WorkOrderExtModel orderModel = new WorkOrderExtModel();
        BeanUtils.copyProperties(workOrderExt, orderModel, MesPcnExtConstWords.ID);
        reqBean.getDataMap().put(MesPcnExtConstWords.WORK_ORDER, orderModel);
    }

}
