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.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 : 工步业务-本体装托校验满托工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxPutPalletCheckCompleteStepService")
public class SxPutPalletCheckCompleteStepService extends BaseStepService {

    @Autowired
    private SxPutPalletModuleService putPalletModuleService;

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

        StationResultBean resultBean = new StationResultBean();
        String err;

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

        try {
            if (StringUtils.isEmpty(reqBean.getOrganizeCode())) {
                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 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 curLockLocationNo = (String) putPalletModuleService.doHandleCurLockLocationNo(reqBean, 1, null);
            if (StringUtils.isEmpty(curLockLocationNo)) {
                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);
            }
            SxPutPalletPlcDataModel ctNoModel = (SxPutPalletPlcDataModel) 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);
            }

            if (forceCompleteFlag) {
                return StepResult.getSuccessComplete();
            } else {
                Integer completeQty = CollectionUtils.isEmpty(putPalletModuleModelList) ? 0 : putPalletModuleModelList.size();
                if (completeQty.compareTo(ctQty) >= 0) {
                    return StepResult.getSuccessComplete();
                } else {
                    return StepResult.getNonComplete();
                }
            }
        } finally {
            if (forceCompleteFlag) {
                reqBean.getDataMap().remove("FORCE:" + reqBean.getForceSpecStepCode());
                reqBean.setForceSpecStepCode(null);
            }
        }
    }

}
