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.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.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.Map;

/**
 * @Description : 工步业务-本体装托判断是否完成初始化PLC工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxPutPalletCheckFinishInitPlcStepService")
public class SxPutPalletCheckFinishInitPlcStepService extends BaseStepService {

    @Autowired
    private SxPutPalletModuleService putPalletModuleService;

    @Autowired
    private IStepCommonMethodService stepCommonMethodService;

    @Autowired
    private SxPutPalletInitProdTypeStepService putPalletInitProdTypeStepService;

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

        StationResultBean resultBean = new StationResultBean();
        String err;

        String curProdType = (String) putPalletModuleService.doHandleCurProdType(reqBean, 1, null);
        if (StringUtils.isEmpty(curProdType)) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请设置本体装托生产模式");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getNonComplete(err);
        }

        if (StringUtils.isEmpty(putPalletModuleService.doHandleInitPlcFlag(reqBean, 1, null))) {
            return StepResult.getSuccessComplete();
        }

        Map<String, MesProdRouteOptParam> stepParamMap = stepCommonMethodService.getStepCfgInfo(reqBean);
        if (CollectionUtils.isEmpty(stepParamMap)) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托判断是否完成初始化PLC工步]未配置必要工步参数[%s][%s][%s][%s][%s][%s][%s][%s][%s]"),
                    MesPcnExtConstWords.PLC_PROD_HAND_SHAKE_SIGNAL, MesPcnExtConstWords.PLC_PACK_SN, MesPcnExtConstWords.PCN_PROD_HAND_SHAKE_SIGNAL,
                    MesPcnExtConstWords.PLC_LEFT_LOCATION_HAND_SHAKE_SIGNAL, MesPcnExtConstWords.PLC_LEFT_LOCATION_CT_NO, MesPcnExtConstWords.PCN_LEFT_LOCATION_HAND_SHAKE_SIGNAL,
                    MesPcnExtConstWords.PLC_RIGHT_LOCATION_HAND_SHAKE_SIGNAL, MesPcnExtConstWords.PLC_RIGHT_LOCATION_CT_NO, MesPcnExtConstWords.PCN_RIGHT_LOCATION_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);
        String plcLeftLocationHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PLC_LEFT_LOCATION_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        String plcLeftLocationCtNo = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PLC_LEFT_LOCATION_CT_NO, reqBean, resultBean);
        String pcnLeftLocationHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PCN_LEFT_LOCATION_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        String plcRightLocationHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PLC_RIGHT_LOCATION_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        String plcRightLocationCtNo = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PLC_RIGHT_LOCATION_CT_NO, reqBean, resultBean);
        String pcnRightLocationHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PCN_RIGHT_LOCATION_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        String pcnPutPalletHandShakeSignal = null;
        String pcnPutPalletLocation = null;
        String plcPutPalletHandShakeSignal = null;
        if (MesPcnExtEnumUtil.PUT_PALLET_PROD_TYPE.AUTO_WORK.getCode().equals(curProdType)) {
            pcnPutPalletHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PCN_PUT_PALLET_HAND_SHAKE_SIGNAL, reqBean, resultBean);
            pcnPutPalletLocation = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PCN_PUT_PALLET_LOCATION, reqBean, resultBean);
            plcPutPalletHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PLC_PUT_PALLET_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);
        MesPlc plcLeftLocationHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(plcLeftLocationHandShakeSignal, MesPcnExtConstWords.PLC_LEFT_LOCATION_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        MesPlc plcLeftLocationCtNoPlc = putPalletInitProdTypeStepService.getPlc(plcLeftLocationCtNo, MesPcnExtConstWords.PLC_LEFT_LOCATION_CT_NO, reqBean, resultBean);
        MesPlc pcnLeftLocationHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(pcnLeftLocationHandShakeSignal, MesPcnExtConstWords.PCN_LEFT_LOCATION_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        MesPlc plcRightLocationHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(plcRightLocationHandShakeSignal, MesPcnExtConstWords.PLC_RIGHT_LOCATION_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        MesPlc plcRightLocationCtNoPlc = putPalletInitProdTypeStepService.getPlc(plcRightLocationCtNo, MesPcnExtConstWords.PLC_RIGHT_LOCATION_CT_NO, reqBean, resultBean);
        MesPlc pcnRightLocationHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(pcnRightLocationHandShakeSignal, MesPcnExtConstWords.PCN_RIGHT_LOCATION_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        MesPlc plcPutPalletHandShakeSignalPlc = null;
        MesPlc pcnPutPalletHandShakeSignalPlc = null;
        MesPlc pcnPutPalletLocationPlc = null;
        if (MesPcnExtEnumUtil.PUT_PALLET_PROD_TYPE.AUTO_WORK.getCode().equals(curProdType)) {
            plcPutPalletHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(plcPutPalletHandShakeSignal, MesPcnExtConstWords.PLC_PUT_PALLET_HAND_SHAKE_SIGNAL, reqBean, resultBean);
            pcnPutPalletHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(pcnPutPalletHandShakeSignal, MesPcnExtConstWords.PCN_PUT_PALLET_HAND_SHAKE_SIGNAL, reqBean, resultBean);
            pcnPutPalletLocationPlc = putPalletInitProdTypeStepService.getPlc(pcnPutPalletLocation, MesPcnExtConstWords.PCN_PUT_PALLET_LOCATION, reqBean, resultBean);
        }

        Boolean boo1, boo2, boo3, boo4, boo5, boo6, boo7;
        try {
            boo1 = putPalletModuleService.doReadOpcParamValueIsEquDefaultVal(plcLeftLocationHandShakeSignalPlc, "0", true);
            boo2 = putPalletModuleService.doReadOpcParamValueIsEquDefaultVal(plcLeftLocationCtNoPlc, "0", true);

            boo3 = putPalletModuleService.doReadOpcParamValueIsEquDefaultVal(plcRightLocationHandShakeSignalPlc, "0", true);
            boo4 = putPalletModuleService.doReadOpcParamValueIsEquDefaultVal(plcRightLocationCtNoPlc, "0", true);

            boo5 = putPalletModuleService.doReadOpcParamValueIsEquDefaultVal(plcProdHandShakeSignalPlc, "0", true);
            boo6 = putPalletModuleService.doReadOpcParamValueIsEquDefaultVal(plcPackSnPlc, "0", true);
        } 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][%s][%s]发生异常:[%s]"),
                    plcLeftLocationHandShakeSignalPlc.getTagAddress(), plcLeftLocationCtNoPlc.getTagAddress(),
                    plcRightLocationHandShakeSignalPlc.getTagAddress(), plcRightLocationCtNoPlc.getTagAddress(),
                    plcProdHandShakeSignalPlc.getTagAddress(), plcPackSnPlc.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 (!boo1 || !boo2 || !boo3 || !boo4 || !boo5 || !boo6) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PLC地址位中存在未初始化的地址位,持续监听中...");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托判断是否完成初始化PLC工步]PLC地址位[%s][%s][%s][%s][%s][%s]中存在未初始化的地址位,持续监听中..."),
                    plcLeftLocationHandShakeSignalPlc.getTagAddress(), plcLeftLocationCtNoPlc.getTagAddress(),
                    plcRightLocationHandShakeSignalPlc.getTagAddress(), plcRightLocationCtNoPlc.getTagAddress(),
                    plcProdHandShakeSignalPlc.getTagAddress(), plcPackSnPlc.getTagAddress());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getNonComplete(err);
        }
        if (MesPcnExtEnumUtil.PUT_PALLET_PROD_TYPE.AUTO_WORK.getCode().equals(curProdType)) {
            try {
                boo7 = putPalletModuleService.doReadOpcParamValueIsEquDefaultVal(plcPutPalletHandShakeSignalPlc, "0", true);
            } 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]"),
                        plcPutPalletHandShakeSignalPlc.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 (!boo7) {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PLC地址位中存在未初始化的地址位,持续监听中...");
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托判断是否完成初始化PLC工步]PLC地址位[%s]未初始化,持续监听中..."),
                        plcPutPalletHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                return StepResult.getNonComplete(err);
            }
        }

        Boolean boo8, boo9, boo10, boo11, boo12;
        try {
            boo8 = putPalletModuleService.doWriteOpcParamValue(pcnLeftLocationHandShakeSignalPlc, "0");
            boo9 = putPalletModuleService.doWriteOpcParamValue(pcnRightLocationHandShakeSignalPlc, "0");
            boo10 = 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][%s][%s]值为[0]时发生异常:[%s]"),
                    pcnLeftLocationHandShakeSignalPlc.getTagAddress(), pcnRightLocationHandShakeSignalPlc.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);
        }
        if (!boo8 || !boo9 || !boo10) {
            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][%s][%s]值为[0]时失败"),
                    pcnLeftLocationHandShakeSignalPlc.getTagAddress(), pcnRightLocationHandShakeSignalPlc.getTagAddress(), pcnProdHandShakeSignalPlc.getTagAddress());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (MesPcnExtEnumUtil.PUT_PALLET_PROD_TYPE.AUTO_WORK.getCode().equals(curProdType)) {
            try {
                boo11 = putPalletModuleService.doWriteOpcParamValue(pcnPutPalletHandShakeSignalPlc, "0");
                boo12 = putPalletModuleService.doWriteOpcParamValue(pcnPutPalletLocationPlc, "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][%s]值为[0]时发生异常:[%s]"),
                        pcnPutPalletHandShakeSignalPlc.getTagAddress(), pcnPutPalletLocationPlc.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 (!boo11 || !boo12) {
                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][%s]值为[0]时失败"),
                        pcnPutPalletHandShakeSignalPlc.getTagAddress(), pcnPutPalletLocationPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
        }

        err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PLC初始化成功...");
        this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

        putPalletModuleService.doHandleInitPlcFlag(reqBean, 3, null);

        return StepResult.getSuccessComplete();
    }

    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;
    }

}
