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.bean.MesWorkCenterContainerLocation;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description : 工步业务-本体装托PLC模式通知机械臂装托工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxPutPalletPlcWorkTellMechanicalArmStepService")
public class SxPutPalletPlcWorkTellMechanicalArmStepService extends BaseStepService {

    @Autowired
    private SxPutPalletModuleService putPalletModuleService;

    @Autowired
    private IStepCommonMethodService stepCommonMethodService;

    @Autowired
    private SxPutPalletInitProdTypeStepService putPalletInitProdTypeStepService;

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

        StationResultBean resultBean = new StationResultBean();
        String err;

        Map<String, MesProdRouteOptParam> stepParamMap = stepCommonMethodService.getStepCfgInfo(reqBean);
        if (CollectionUtils.isEmpty(stepParamMap)) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[本体装托PLC模式通知机械臂装托工步]未配置必要工步参数[%s][%s][%s]"),
                    MesPcnExtConstWords.PLC_PUT_PALLET_HAND_SHAKE_SIGNAL, MesPcnExtConstWords.PCN_PUT_PALLET_HAND_SHAKE_SIGNAL, MesPcnExtConstWords.PCN_PUT_PALLET_LOCATION);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        String pcnPutPalletHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PCN_PUT_PALLET_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        String pcnPutPalletLocation = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PCN_PUT_PALLET_LOCATION, reqBean, resultBean);
        String plcPutPalletHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.PLC_PUT_PALLET_HAND_SHAKE_SIGNAL, reqBean, resultBean);

        MesPlc plcPutPalletHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(plcPutPalletHandShakeSignal, MesPcnExtConstWords.PLC_PUT_PALLET_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        MesPlc pcnPutPalletHandShakeSignalPlc = putPalletInitProdTypeStepService.getPlc(pcnPutPalletHandShakeSignal, MesPcnExtConstWords.PCN_PUT_PALLET_HAND_SHAKE_SIGNAL, reqBean, resultBean);
        MesPlc pcnPutPalletLocationPlc = putPalletInitProdTypeStepService.getPlc(pcnPutPalletLocation, MesPcnExtConstWords.PCN_PUT_PALLET_LOCATION, reqBean, resultBean);

        Integer locationType = null;
        String curLockLocationNo = (String) putPalletModuleService.doHandleCurLockLocationNo(reqBean, 1, null);
        List<MesWorkCenterContainerLocation> locationList = (List<MesWorkCenterContainerLocation>) putPalletModuleService.doHandleSelectLocationList(reqBean, 1, null);
        Optional<MesWorkCenterContainerLocation> wcclOptional = locationList.stream().filter(o -> (null != o && o.getCtLocationNo().equals(curLockLocationNo))).findFirst();
        if (null != wcclOptional && wcclOptional.isPresent()) {
            locationType = wcclOptional.get().getLocationType();
        }

        Integer value1, value2, value3;
        try {
            value1 = Integer.valueOf(putPalletModuleService.doReadOpcParamValue(plcPutPalletHandShakeSignalPlc).toString());
            value2 = Integer.valueOf(putPalletModuleService.doReadOpcParamValue(pcnPutPalletLocationPlc).toString());
            value3 = Integer.valueOf(putPalletModuleService.doReadOpcParamValue(pcnPutPalletHandShakeSignalPlc).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]"),
                    plcPutPalletHandShakeSignalPlc.getTagAddress(), 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);
        }

        Boolean boo1, boo2;
        if (value1 == 0 && value2 == 0 && value3 == 0) {
            try {
                boo1 = putPalletModuleService.doWriteOpcParamValue(pcnPutPalletLocationPlc, locationType.toString());
            } catch (Exception e) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN修改PLC地址位值为[%s]时发生异常:[%s]"), locationType, 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]"),
                        pcnPutPalletLocationPlc.getTagAddress(), locationType, 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) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN修改PLC地址位[%s]值为[%s]时失败"), locationType);
                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]时失败"),
                        pcnPutPalletLocationPlc.getTagAddress(), locationType);
                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]值为[%s]成功,即将修改PLC地址位[%s]值为[1]"),
                        pcnPutPalletLocationPlc.getTagAddress(), locationType, pcnPutPalletHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
            return StepResult.getNonComplete(err);
        } else if (value1 == 0 && value2 == locationType && value3 == 0) {
            try {
                boo2 = putPalletModuleService.doWriteOpcParamValue(pcnPutPalletHandShakeSignalPlc, "1");
            } catch (Exception e) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PCN修改PLC地址位[%s]值为[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]"),
                        pcnPutPalletHandShakeSignalPlc.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 (!boo2) {
                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]时失败"),
                        pcnPutPalletHandShakeSignalPlc.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接收装托信号..."),
                        pcnPutPalletHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
            return StepResult.getNonComplete(err);
        } else if (value1 == 1 && value2 == locationType && value3 == 1) {
            try {
                boo2 = putPalletModuleService.doWriteOpcParamValue(pcnPutPalletHandShakeSignalPlc, "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]"),
                        pcnPutPalletHandShakeSignalPlc.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 (!boo2) {
                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]时失败"),
                        pcnPutPalletHandShakeSignalPlc.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地址位已经接收到装托信号,正在监听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]值为[2]成功,正在监听PLC复位..."),
                        pcnPutPalletHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
            return StepResult.getNonComplete(err);
        } else if (value1 == 0 && value2 == locationType && value3 == 2) {
            try {
                boo1 = 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]值为[0]时发生异常:[%s]"),
                        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 (!boo1) {
                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]时失败"),
                        pcnPutPalletLocationPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            } else {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "正在复位PCN握手信号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地址位[%s]值为[0]"),
                        pcnPutPalletLocationPlc.getTagAddress(), locationType, pcnPutPalletHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
            return StepResult.getNonComplete(err);
        } else if (value1 == 0 && value2 == 0 && value3 == 2) {
            try {
                boo2 = putPalletModuleService.doWriteOpcParamValue(pcnPutPalletHandShakeSignalPlc, "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]"),
                        pcnPutPalletHandShakeSignalPlc.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 (!boo2) {
                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]时失败"),
                        pcnPutPalletHandShakeSignalPlc.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地址位复位成功"),
                        pcnPutPalletHandShakeSignalPlc.getTagAddress());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }

            putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 3, null);
            return StepResult.getSuccessComplete();
        } else {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "正在监听PLC接收装托信号...");
            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;
    }


}
