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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.SxPutPalletModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxPutPalletModuleModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxPutPalletPlcDataModel;
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.api.iservice.busi.IStepCommonMethodService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPlc;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRouteOptParam;
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.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @Description : 工步业务-本体装托PLC模式读取包装条码工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxPutPalletPlcWorkReadPackageNoStepService")
public class SxPutPalletPlcWorkReadPackageNoStepService extends BaseStepService {

    @Autowired
    private SxPutPalletModuleService putPalletModuleService;

    @Autowired
    private IStepCommonMethodService stepCommonMethodService;

    @Autowired
    private SxPutPalletInitProdTypeStepService putPalletInitProdTypeStepService;

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

        StationResultBean resultBean = new StationResultBean();
        String err;

        //从PLC读取到包装条码并校验成功,直接跳转通知机械臂搬运
        SxPutPalletPlcDataModel curPacakgeNoModel = (SxPutPalletPlcDataModel) putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 1, null);
        if (null != curPacakgeNoModel && curPacakgeNoModel.getBoo()) {
            StepResult stepResult = StepResult.getNonComplete();
            stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.PUT_PALLET_PROD_TYPE.PLC_WORK.name());
            return stepResult;
        }

        Map<String, MesProdRouteOptParam> stepParamMap = stepCommonMethodService.getStepCfgInfo(reqBean);
        if (CollectionUtils.isEmpty(stepParamMap)) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]未配置必要工步参数[%s][%s][%s]"),
                    MesPcnExtConstWords.PLC_PROD_HAND_SHAKE_SIGNAL, MesPcnExtConstWords.PLC_PACK_SN, MesPcnExtConstWords.PCN_PROD_HAND_SHAKE_SIGNAL);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        String plcProdHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PLC_PROD_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        String plcPackSn = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PLC_PACK_SN, reqBean, resultBean);
        String pcnProdHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PCN_PROD_HAND_SHAKE_SIGNAL, reqBean, resultBean);

        MesPlc plcProdHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(plcProdHandShakeSignal, MesPcnExtConstWords.PLC_PROD_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        MesPlc plcPackSnPlc = putPalletInitProdTypeStepService.getPlc(plcPackSn, MesPcnExtConstWords.PLC_PACK_SN, reqBean, resultBean);
        MesPlc pcnProdHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(pcnProdHandShakeSignal, MesPcnExtConstWords.PCN_PROD_HAND_SHAKE_SIGNAL, reqBean, resultBean);


        Integer value1, value3;
        String value2;
        try {
            value1 = Integer.valueOf(putPalletModuleService.doReadOpcParamValue(plcProdHandShakeSignalPlc).toString());
            value2 = (String) putPalletModuleService.doReadOpcParamValue(plcPackSnPlc);
            value3 = Integer.valueOf(putPalletModuleService.doReadOpcParamValue(pcnProdHandShakeSignalPlc).toString());
        } catch (Exception e) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN读取PLC地址位发生异常:[%s]"), e.getMessage());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN读取PLC地址位[%s][%s][%s]发生异常:[%s]"),
                    plcProdHandShakeSignalPlc.getTagAddress(), plcPackSnPlc.getTagAddress(),
                    pcnProdHandShakeSignalPlc.getTagAddress(), e.getMessage());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        Boolean boo;
        if (value1 == 0 && value2.equals("0") && value3 == 0) {
            SxPutPalletPlcDataModel curCtNoModel = (SxPutPalletPlcDataModel) putPalletModuleService.doHandleCurCtNo(reqBean, 1, null);
            if (null != curCtNoModel && curCtNoModel.getBoo()) {
                if (CollectionUtils.isEmpty((List<SxPutPalletModuleModel>) putPalletModuleService.doHandlePackageDataByCtNo(reqBean, curCtNoModel.getCtNo(), 1, null))) {
                    if (null == putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 1, null)) {
                        putPalletModuleService.doHandleDisUnlockLocation(reqBean, 3, null);
                    }
                }
            }

            try {
                boo = putPalletModuleService.doWriteOpcParamValue(pcnProdHandShakeSignalPlc, "1");
            } catch (Exception e) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN修改PLC地址位值为[1]时发生异常:[%s]"), e.getMessage());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN修改PLC地址位[%s]值为[1]时发生异常:[%s]"),
                        pcnProdHandShakeSignalPlc.getTagAddress(), e.getMessage());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            if (!boo) {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN修改PLC地址位值为[1]时失败");
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN修改PLC地址位[%s]值为[1]时失败"),
                        pcnProdHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            } else {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "正在监听PLC读取包装条码...");
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN修改PLC地址位[%s]值为[1]成功,正在监听PLC读取包装条码..."),
                        pcnProdHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "正在监听PLC读取包装条码...");
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
            return StepResult.getNonComplete(err);
        } else if (value1 != 0 && !value2.equals("0") && value3 == 1) {
            curPacakgeNoModel = new SxPutPalletPlcDataModel();
            curPacakgeNoModel.setPackageNo(value2);
            curPacakgeNoModel.setBoo(false);
            curPacakgeNoModel.setReadDatetime(TimeTool.getNowTime(true));
            putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 2, curPacakgeNoModel);
            putPalletModuleService.doHandleDisUnlockLocation(reqBean, 2, true);
            try {
                boo = putPalletModuleService.doWriteOpcParamValue(pcnProdHandShakeSignalPlc, "2");
            } catch (Exception e) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN修改PLC地址位值为[2]时发生异常:[%s]"), e.getMessage());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN修改PLC地址位[%s]值为[2]时发生异常:[%s]"),
                        pcnProdHandShakeSignalPlc.getTagAddress(), e.getMessage());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            if (!boo) {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN修改PLC地址位值为[2]时失败");
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN修改PLC地址位[%s]值为[2]时失败"),
                        pcnProdHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            } else {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "已经读取到PLC中的包装条码[%s],正在监听PLC复位..."), value2);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN修改PLC地址位[%s]值为[2]成功,已经读取到PLC中的包装条码[%s],正在监听PLC复位..."),
                        pcnProdHandShakeSignalPlc.getTagAddress(), value2);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "已经读取到PLC中的包装条码[%s],正在监听PLC复位..."), value2);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
            return StepResult.getNonComplete(err);
        } else if (value1 == 0 && value2.equals("0") && value3 == 2) {
            try {
                boo = putPalletModuleService.doWriteOpcParamValue(pcnProdHandShakeSignalPlc, "0");
            } catch (Exception e) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN修改PLC地址位值为[0]时发生异常:[%s]"), e.getMessage());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN修改PLC地址位[%s]值为[0]时发生异常:[%s]"),
                        pcnProdHandShakeSignalPlc.getTagAddress(), e.getMessage());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            if (!boo) {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN修改PLC地址位值为[0]时失败");
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN修改PLC地址位[%s]值为[0]时失败"),
                        pcnProdHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            } else {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PLC地址位复位成功");
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]PCN修改PLC地址位[%s]值为[0]成功,PLC地址位复位成功"),
                        pcnProdHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PLC地址位复位成功");
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }

            return StepResult.getSuccessComplete();
        } else {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "正在监听中...");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]正在监听PLC地址位读取包装条码...");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getNonComplete();
        }

    }

    private String getValueByStepParamMap(Map<String, MesProdRouteOptParam> stepParamMap, String code, StationRequestBean reqBean, StationResultBean resultBean) {
        String value = null;
        if (!CollectionUtils.isEmpty(stepParamMap) && stepParamMap.containsKey(code)
                && !StringUtils.isEmpty(stepParamMap.get(code).getParamValue())) {
            value = stepParamMap.get(code).getParamValue();
        }
        if (StringUtils.isEmpty(value)) {
            String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式读取包装条码工步]未配置必要工步参数[%s]"), code);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }
        return value;
    }


}
