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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.ep.EpPutPalletModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCenterContainerLocation;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxPutPalletModuleModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpPutPalletPlcDataModel;
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.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description : 工步业务-EP装托校验满托工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("epPutPalletCheckCompleteStepService")
public class EpPutPalletCheckCompleteStepService extends BaseStepService {

    @Autowired
    private EpPutPalletModuleService putPalletModuleService;

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

        StationResultBean resultBean = new StationResultBean();
        String err;

        Boolean forceCompleteFlag = !StringUtils.isEmpty(reqBean.getDataMap().get("FORCE:" + reqBean.getForceSpecStepCode())) ? true : false;

        try {
            String curWorkOrderNo = (String) putPalletModuleService.doHandleCurWorkOrderNo(reqBean, 1, null);
            if (StringUtils.isEmpty(curWorkOrderNo)) {
                if (forceCompleteFlag) {
                    err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "强制完成执行失败,当前未锁定工单");
                } else {
                    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 curProdType = (String) putPalletModuleService.doHandleCurProdType(reqBean, 1, null);
            if (StringUtils.isEmpty(curProdType)) {
                if (forceCompleteFlag) {
                    err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "强制完成执行失败,请先设置工作模式");
                } else {
                    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);
            }

            MesWorkCenterContainerLocation curLocation = (MesWorkCenterContainerLocation) putPalletModuleService.doHandleCurLocation(reqBean, 1, null);
            if (null == curLocation) {
                if (forceCompleteFlag) {
                    err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "强制完成执行失败,当前未缓存位置");
                } else {
                    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);
            }

            EpPutPalletPlcDataModel ctNoModel = (EpPutPalletPlcDataModel) putPalletModuleService.doHandleCurCtNo(reqBean, 1, null);
            if (null == ctNoModel || !ctNoModel.getBoo()) {
                if (forceCompleteFlag) {
                    err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "强制完成执行失败,当前未固定容器代码");
                } else {
                    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);
            }
            Integer ctQty = (Integer) putPalletModuleService.doHandleCurCtQty(reqBean, 1, null);
            if (StringUtils.isEmpty(ctQty) || ctQty.compareTo(0) == 0) {
                if (forceCompleteFlag) {
                    err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "强制完成执行失败,当前未获取到容器容量");
                } else {
                    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);
            }

            List<SxPutPalletModuleModel> putPalletModuleModelList = (List<SxPutPalletModuleModel>) putPalletModuleService.doHandlePackageDataByCtNo(reqBean, ctNoModel.getCtNo(), 1, null);
            if (CollectionUtils.isEmpty(putPalletModuleModelList) && forceCompleteFlag) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "强制完成执行失败,容器代码[%s]未绑定包装条码"), ctNoModel.getCtNo());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }

            putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 3, null);
            //判断当前是否存在必要完托信号
            String cmdPlcPutPallet = (String) putPalletModuleService.doHandleCmdPlcPutPallet(reqBean, 1, null);
            if (!StringUtils.isEmpty(cmdPlcPutPallet)) {
                putPalletModuleService.doHandleCmdPlcPutPallet(reqBean, 3, null);
            }

            if (forceCompleteFlag) {
                //强制装托或完托信号
                return StepResult.getSuccessComplete();
            } else {
                Integer completeQty = CollectionUtils.isEmpty(putPalletModuleModelList) ? 0 : putPalletModuleModelList.size();
                if (completeQty.compareTo(ctQty) >= 0) {
                    if (MesPcnExtEnumUtil.EP_PUT_PALLET_PROD_TYPE.PLC_WORK.getCode().equals(curProdType)) {
                        //PLC模式缓存必要完托指令
                        putPalletModuleService.doHandleCmdPlcPutPallet(reqBean, 2, "1");
                        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PLC模式验证已经满托,请PLC写入完托组地址信号"), ctNoModel.getCtNo());
                        this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                        return StepResult.getNonComplete();
                    }
                    //人工满托
                    return StepResult.getSuccessComplete();
                } else {
                    //未满托
                    return StepResult.getNonComplete();
                }
            }
        } finally {
            if (forceCompleteFlag) {
                reqBean.getDataMap().remove("FORCE:" + reqBean.getForceSpecStepCode());
                reqBean.setForceSpecStepCode(null);
            }
        }
    }

}
