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.api.iservice.busi.IShippingDispatchService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.platform.common.util.MesPcnConstWords;
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卸托OR强制装托工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("epPutPalletDelPackOrForceCompleteStepService")
public class EpPutPalletDelPackOrForceCompleteStepService extends BaseStepService {

    @Autowired
    private EpPutPalletModuleService putPalletModuleService;

    @Autowired
    private IShippingDispatchService shippingDispatchService;

    //失败回到结束
    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("EpPutPalletDelPackOrForceCompleteStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();
        String err;

        if (StringUtils.isEmpty(reqBean.getOrganizeCode())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少工厂代码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        //删除包装条码
        String curWorkOrderNo = (String) putPalletModuleService.doHandleCurWorkOrderNo(reqBean, 1, null);
        MesWorkCenterContainerLocation curLocation = (MesWorkCenterContainerLocation) putPalletModuleService.doHandleCurLocation(reqBean, 1, null);
        EpPutPalletPlcDataModel ctNoModel = (EpPutPalletPlcDataModel) putPalletModuleService.doHandleCurCtNo(reqBean, 1, null);
        String packageNo = (String) putPalletModuleService.doHandleDeletePack(reqBean, 1, null);
        if (!StringUtils.isEmpty(packageNo)) {
            putPalletModuleService.doHandleDeletePack(reqBean, 3 , null);
            if (StringUtils.isEmpty(curWorkOrderNo)) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "删除包装条码[%s]失败,当前未锁定工单"), packageNo);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            if (null == curLocation) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "删除包装条码[%s]失败,当前未缓存位置"), packageNo);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            if (null == ctNoModel || !ctNoModel.getBoo()) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "删除包装条码[%s]失败,当前未固定容器代码"), packageNo);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            SxPutPalletModuleModel model = new SxPutPalletModuleModel();
            model.setPackageNo(packageNo);
            if ((Boolean) putPalletModuleService.doHandlePackageDataByCtNo(reqBean, ctNoModel.getCtNo(), 4, model)) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "包装条码[%s]删除成功"), packageNo);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

                //判断是否需要重新获取标准容量
                List<SxPutPalletModuleModel> putPalletModuleModelList = (List<SxPutPalletModuleModel>) putPalletModuleService.doHandlePackageDataByCtNo(reqBean, ctNoModel.getCtNo(), 1, null);
                if (CollectionUtils.isEmpty(putPalletModuleModelList)) {
                    EpPutPalletPlcDataModel curExecPackageNoModel = (EpPutPalletPlcDataModel) putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 1, null);
                    if (null == curExecPackageNoModel) {
                        putPalletModuleService.doHandleCurCtQty(reqBean, 3, null);
                    }
                }

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

                return StepResult.getNonComplete(err);
            } else {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "删除包装条码[%s]失败,容器代码[%s]未绑定此包装条码"), packageNo, ctNoModel.getCtNo());
                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(putPalletModuleService.doHandleResetCtNo(reqBean, 1, null))) {
            putPalletModuleService.doHandleResetCtNo(reqBean, 3 , null);
            if (StringUtils.isEmpty(curWorkOrderNo)) {
                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);
            }
            if (null == curLocation) {
                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);
            }
            if (null == ctNoModel || !ctNoModel.getBoo()) {
                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);
            }
            if (StringUtils.isEmpty(curProdType)) {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请先设置人工模式后再执行容器重置操作");
                this.sendMessage(reqBean,  null, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            if (MesPcnExtEnumUtil.EP_PUT_PALLET_PROD_TYPE.PLC_WORK.getCode().equals(curProdType)) {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PLC模式下不能执行容器重置操作");
                this.sendMessage(reqBean,  null, 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)) {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前存在已装托的产品,不能执行容器重置操作");
                this.sendMessage(reqBean,  null, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            EpPutPalletPlcDataModel curExecPackageNoModel = (EpPutPalletPlcDataModel) putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 1, null);
            if (null != curExecPackageNoModel) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前存在正在执行且未装托成功的包装条码[%s],不能执行容器重置操作"), curExecPackageNoModel.getPackageNo());
                this.sendMessage(reqBean,  null, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }

            putPalletModuleService.doHandleCurCtNo(reqBean, 3, null);
            putPalletModuleService.doHandleCurCtQty(reqBean, 3, null);

            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "容器重置成功");
            this.sendMessage(reqBean,  null, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getNonComplete(err);
        }

        //强制完成装托
        String forceSpecStepCode = (String) putPalletModuleService.doHandleForcePutPallet(reqBean, 1, null);
        if (!StringUtils.isEmpty(forceSpecStepCode)) {
            putPalletModuleService.doHandleForcePutPallet(reqBean, 3 , null);
            if (StringUtils.isEmpty(curWorkOrderNo)) {
                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);
            }
            if (null == curLocation) {
                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);
            }
            if (null == ctNoModel || !ctNoModel.getBoo()) {
                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);
            }
            if (StringUtils.isEmpty(curProdType)) {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请先设置人工模式后再执行强制装托操作");
                this.sendMessage(reqBean,  null, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            if (MesPcnExtEnumUtil.EP_PUT_PALLET_PROD_TYPE.PLC_WORK.getCode().equals(curProdType)) {
                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PLC模式下不能执行强制装托操作");
                this.sendMessage(reqBean,  null, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            EpPutPalletPlcDataModel packageNoModel = (EpPutPalletPlcDataModel) putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 1, null);
            if (null != packageNoModel) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前存在正在执行且未装托成功的包装条码[%s],请等待执行完成后再执行强制装托操作"), packageNoModel.getPackageNo());
                this.sendMessage(reqBean,  null, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }

            reqBean.setClientInfo(shippingDispatchService.getActorClientInfo(reqBean));
            reqBean.setInterfaceType(MesPcnConstWords.SHIPPING);
            reqBean.setBusiType(MesPcnConstWords.WS_CMD_DO_SCAN);
            reqBean.setForceSpecStepCode(forceSpecStepCode);
            shippingDispatchService.doSendScanQueueNextExec(reqBean);
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "即将执行强制完成装托...");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            foundExThrowNoShowMsg();
        }

        return StepResult.getSuccessComplete();
    }

}
