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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.SxNoumenonPackageModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.api.iservice.busi.IShippingDispatchService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.platform.common.util.MesPcnConstWords;
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.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description : 本体包装前置校验跳号码时验证是否重选工单校验工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2020-05-27
 * @Modify:
 **/
@Slf4j
@Service("sxNoumenonPackagePreScanProdSeqCheckReselectOrderWithGuideStepService")
public class SxNoumenonPackagePreScanProdSeqCheckReselectOrderWithGuideStepService extends BaseStepService {

    @Autowired
    private SxNoumenonPackageModuleService noumenonPackageModuleService;

    @Autowired
    private IShippingDispatchService shippingDispatchService;

    @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 execute(StationRequestBean reqBean) {
        log.info("SxNoumenonPackagePreScanProdSeqCheckReselectOrderWithGuideStepService[execute] 参数：{}", reqBean);
        StationResultBean resultBean = new StationResultBean();
        String 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);
        }

        if (StringUtils.isEmpty(reqBean.getSerialNumber())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少条码信息 SerialNumber");
            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(), "入参缺少工单信息 WorkOrderNo");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        if (StringUtils.isEmpty(reqBean.getDataMap().get(MesPcnExtConstWords.WORK_ORDER_NO))) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参[DataMap]缺少工单信息 WorkOrderNo");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        if (!reqBean.getWorkOrderNo().equals(reqBean.getDataMap().get(MesPcnExtConstWords.WORK_ORDER_NO))) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单已变更为[%s]"), reqBean.getWorkOrderNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            noumenonPackageModuleService.putLastSelectOrder(reqBean);

            //跳过整个工序
            reqBean.setClientInfo(shippingDispatchService.getActorClientInfo(reqBean));
            reqBean.setInterfaceType(MesPcnExtConstWords.SHIPPING);
            reqBean.setBusiType(MesPcnConstWords.WS_CMD_DO_SCAN);
            reqBean.setForceJumpProcess(true);
            shippingDispatchService.doSendScanQueueNextExec(reqBean);

            foundExThrowNoShowMsg();
        }

        return StepResult.getSuccessComplete();
    }

}
