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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
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.model.ep.EpProduceSnExtModel;
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.mes.pcn.util.LocaleUtil;
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 : 工步业务-EP打包下线校验是否需要报工工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("epSnLineOffCheckIsNeedReportWorkStepService")
public class EpSnLineOffCheckIsNeedReportWorkStepService extends BaseStepService {

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("EpSnLineOffCheckIsNeedReportWorkStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();
        String err;

        if (StringUtils.isEmpty(reqBean.getOrganizeCode())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少工厂代码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        if (StringUtils.isEmpty(reqBean.getWorkOrderNo())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少生产工单号");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        if (StringUtils.isEmpty(reqBean.getSerialNumber())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少产品条码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        EpProduceSnExtModel snModel = (EpProduceSnExtModel) reqBean.getDataMap().get(MesPcnExtConstWords.PRODUCE_SN);
        if (null == snModel || !snModel.getSerialNumber().equals(reqBean.getSerialNumber())) {
            snModel = getEpProduceSnExtModel(reqBean);
        }
        if (null == snModel) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]无效"), reqBean.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        WorkOrderExtModel orderModel = (WorkOrderExtModel) reqBean.getDataMap().get(MesPcnExtConstWords.WORK_ORDER);
        if (null == orderModel) {
            orderModel = getWorkOrderExtModel(reqBean);
        }
        if (null == orderModel) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产工单号[%s]无效"), reqBean.getWorkOrderNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        Boolean flag = true;
        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.ATTEMPT_ORDER.getValue() == orderModel.getWorkOrderType()) {
            if (reqBean.getDataMap().containsKey(MesPcnExtConstWords.INFO)) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]类型为[试制工单],产品条码[%s]不执行报工!【%s】"),
                        reqBean.getWorkOrderNo(), reqBean.getSerialNumber(), reqBean.getDataMap().get(MesPcnExtConstWords.INFO));
                if (reqBean.getDataMap().containsKey(MesPcnExtConstWords.ERROR)) {
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                } else {
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                }
            } else {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]类型为[试制工单],产品条码[%s]不执行报工"),
                        reqBean.getWorkOrderNo(), reqBean.getSerialNumber());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
            flag = false;
        } else {
            if (MesPcnExtEnumUtil.PRODUCE_SN_STATUS.ERR_OFFLINE.getValue() == snModel.getSnStatus()) {
                if (reqBean.getDataMap().containsKey(MesPcnExtConstWords.INFO)) {
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]因缺件异常下线,不执行报工!【%s】"),
                            reqBean.getSerialNumber(), reqBean.getDataMap().get(MesPcnExtConstWords.INFO));
                } else {
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]因缺件异常下线,不执行报工!"),
                            reqBean.getSerialNumber());
                }
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                flag = false;
            }
        }

        if (flag) {
            return StepResult.getSuccessComplete();
        } else {
            StepResult stepResult = StepResult.getNonComplete();
            if (MathOperation.compareTo(orderModel.getCompleteQty(), orderModel.getQty()) == 0) {
                stepResult = StepResult.getSuccessComplete();
                stepResult.setNextTriggerEvent(MesPcnExtConstWords.END);
                this.sendMessage(reqBean, resultBean, "", MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
            return stepResult;
        }

    }

    private WorkOrderExtModel getWorkOrderExtModel(StationRequestBean reqBean) {
        WorkOrderExtModel orderModel = null;
        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        if (null != workOrderExt) {
            orderModel = new WorkOrderExtModel();
            BeanUtils.copyProperties(workOrderExt, orderModel, MesPcnExtConstWords.ID);
        }
        return orderModel;
    }

    private EpProduceSnExtModel getEpProduceSnExtModel(StationRequestBean reqBean) {
        EpProduceSnExtModel snModel = null;
        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), reqBean.getSerialNumber());
        if (null != produceSnExt) {
            snModel = new EpProduceSnExtModel();
            BeanUtils.copyProperties(produceSnExt, snModel, MesPcnExtConstWords.ID);
        }
        return snModel;
    }
}
