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.api.busi.IProduceSnRepairService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.ep.EpSnLineOffModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnRepair;
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.mes.pcn.util.LocaleUtil;
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("epSnLineOffCheckSnStepService")
public class EpSnLineOffCheckSnStepService extends BaseStepService {

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private EpSnLineOffModuleService snLineOffModuleService;

    @Autowired
    private IProduceSnRepairService produceSnRepairService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Override
    public StepResult guide(StationRequestBean reqBean) {
        StationResultBean resultBean = new StationResultBean();
        String guide = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请扫描产品条码");
        this.sendMessage(reqBean, resultBean, guide, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        return super.guide(reqBean);
    }

    @Override
    public StepResult init(StationRequestBean reqBean) {
        StationResultBean resultBean = new StationResultBean();
        resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_CONTENT.getValue());
        this.sendMessage(reqBean, resultBean);
        return super.init(reqBean);
    }

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("EpSnLineOffCheckSnStepService[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);
            return StepResult.getNonComplete(err);
        }

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

        String curWorkOrderNo = (String) snLineOffModuleService.doHandleCurWorkOrderNo(reqBean, 1, null);
        if (StringUtils.isEmpty(curWorkOrderNo)) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单已经失效,请重新选择工单");
            this.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, curWorkOrderNo);
        }
        if (null == orderModel) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产工单号[%s]无效,请重新选择工单"), curWorkOrderNo);
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }
        if (!curWorkOrderNo.equals(orderModel.getOrderNo())) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单[%s]与缓存工单号[%s]不一致,请重新选择工单"), curWorkOrderNo, orderModel.getOrderNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), reqBean.getScanInfo());
        if (null == produceSnExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]无效"), reqBean.getScanInfo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        if (MesPcnExtEnumUtil.PRODUCE_SN_STATUS.ERR_OFFLINE.getValue() == produceSnExt.getSnStatus() ||
                MesPcnExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getValue() == produceSnExt.getSnStatus()) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]状态为[%s],不能操作"),
                    produceSnExt.getSerialNumber(), MesPcnExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(produceSnExt.getSnStatus()));
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        if (MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.QUALIFIED.getValue() != produceSnExt.getQcStatus()) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]质量状态为[%s],不能操作"),
                    produceSnExt.getSerialNumber(), MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.valueOfDescription(produceSnExt.getQcStatus()));
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() == orderModel.getWorkOrderType()) {
            if (!produceSnExt.getLastWorkOrderNo().equals(curWorkOrderNo)) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]当前关联的返工工单[%s]与当前执行的返工工单[%s]不一致,不能操作"),
                        produceSnExt.getSerialNumber(), produceSnExt.getLastWorkOrderNo(), curWorkOrderNo);
                sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            MesProduceSnRepair produceSnRepair = produceSnRepairService.getProduceSnRepairBySnAndOrderNo(reqBean.getOrganizeCode(), produceSnExt.getSerialNumber(), curWorkOrderNo);
            if (null == produceSnRepair) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]关联返工工单[%s]的记录不存在,不能操作"),
                        produceSnExt.getSerialNumber(), curWorkOrderNo);
                sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
        } else {
            if (!produceSnExt.getLastWorkOrderNo().equals(curWorkOrderNo)) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]未关联生产工单[%s],不能操作"),
                        produceSnExt.getSerialNumber(), curWorkOrderNo);
                sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
        }

        reqBean.getDataMap().put(MesPcnExtConstWords.WORK_ORDER_NO, reqBean.getWorkOrderNo());

        reqBean.setSerialNumber(produceSnExt.getSerialNumber());
        reqBean.setPartNo(orderModel.getPartNo());
        reqBean.setPartName(orderModel.getPartNameRdd());
        StepResult stepResult = StepResult.getSuccessComplete();
        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() == orderModel.getWorkOrderType()) {
            stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.name());
        } else {
            stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.WORK_ORDER_TYPE.STANDARD_ORDER.name());
        }

        return stepResult;
    }

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

}
