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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base.ModulePackageTaskService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.WorkCenterContainerLocationService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.SxPutPalletModuleService;
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.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesContainer;
import cn.estsh.i3plus.pojo.mes.bean.MesPartContainerCapacity;
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.StringUtils;

/**
 * @Description : 工步业务-本体装托人工模式扫描容器代码工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxPutPalletHandWorkScanCtNoStepService")
public class SxPutPalletHandWorkScanCtNoStepService extends BaseStepService {

    @Autowired
    private SxPutPalletModuleService putPalletModuleService;

    @Autowired
    private WorkCenterContainerLocationService workCenterContainerLocationService;
    @Autowired
    private ModulePackageTaskService modulePackageTaskService;

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

        try {
            StationResultBean resultBean = new StationResultBean();
            String err;

            SxPutPalletPlcDataModel curCtNoModel = (SxPutPalletPlcDataModel) putPalletModuleService.doHandleCurCtNo(reqBean, 1, null);
            if (null == curCtNoModel || !curCtNoModel.getBoo()) {
                if (StringUtils.isEmpty(reqBean.getScanInfo())) {
                    err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请扫描容器代码");
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                    foundExThrowNoShowMsg();
                }

                MesContainer container = workCenterContainerLocationService.getContainerByCtNo(reqBean.getOrganizeCode(), reqBean.getScanInfo());
                if (null == container) {
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "容器代码[%s]无效"), reqBean.getScanInfo());
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    putPalletModuleService.doHandleCurCtNo(reqBean, 3, null);
                    return StepResult.getNonComplete(err);
                }

                if (String.valueOf(MesPcnEnumUtil.MES_CONTAINER_STATUS.OCCUPY.getValue()).equals(container.getUseStatus()) || modulePackageTaskService.queryWmsCtSn(container.getCtCode(), reqBean.getOrganizeCode(), reqBean.getScanInfo())) {
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "容器代码[%s]当前状态[占用]"), container.getCtNo());
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    putPalletModuleService.doHandleCurCtNo(reqBean, 3, null);
                    return StepResult.getNonComplete(err);
                }

                String curPartNo = (String) putPalletModuleService.doHandleCurPartNo(reqBean, 1, null);
                MesPartContainerCapacity partContainerCapacity = workCenterContainerLocationService.getPartContainerCapacity(reqBean.getOrganizeCode(), container.getCtCode(), curPartNo);
                if (null != partContainerCapacity) {
                    if (StringUtils.isEmpty(partContainerCapacity.getCtQty()) || partContainerCapacity.getCtQty().compareTo(0) == 0) {
                        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),
                                "容器代码[%s]对应的容器类型[%s]与物料编码[%s]在产品容器容量配置信息中未配置容量"), container.getCtNo(), container.getCtCode(), curPartNo);
                        this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                        putPalletModuleService.doHandleCurCtNo(reqBean, 3, null);
                        return StepResult.getNonComplete(err);
                    }
                    putPalletModuleService.doHandleCurCtQty(reqBean, 2, partContainerCapacity.getCtQty());
                } else {
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "容器代码[%s]对应的容器类型[%s]与物料编码[%s]未配置产品容器容量信息"),
                            container.getCtNo(), container.getCtCode(), curPartNo);
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    putPalletModuleService.doHandleCurCtNo(reqBean, 3, null);
                    return StepResult.getNonComplete(err);
                }

                SxPutPalletPlcDataModel ctNoModel = new SxPutPalletPlcDataModel();
                ctNoModel.setCtNo(container.getCtNo());
                ctNoModel.setBoo(true);
                ctNoModel.setReadDatetime(TimeTool.getNowTime(true));
                putPalletModuleService.doHandleCurCtNo(reqBean, 2, ctNoModel);

                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "容器编码[%s]扫描成功"), container.getCtNo());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            } else {

                //判断是否需要重新获取标准容量
                Integer ctQty = (Integer) putPalletModuleService.doHandleCurCtQty(reqBean, 1, null);
                if (StringUtils.isEmpty(ctQty) || ctQty.compareTo(0) == 0) {
                    String curPartNo = (String) putPalletModuleService.doHandleCurPartNo(reqBean, 1, null);

                    MesContainer container = workCenterContainerLocationService.getContainerByCtNo(reqBean.getOrganizeCode(), curCtNoModel.getCtNo());
                    if (null == container) {
                        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "人工重新获取标准容量:当前缓存的容器代码[%s]无效"), curCtNoModel.getCtNo());
                        this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                        putPalletModuleService.doHandleCurCtNo(reqBean, 3, null);
                        return StepResult.getNonComplete(err);
                    }
                    if (String.valueOf(MesPcnEnumUtil.MES_CONTAINER_STATUS.OCCUPY.getValue()).equals(container.getUseStatus()) || modulePackageTaskService.queryWmsCtSn(container.getCtCode(), reqBean.getOrganizeCode(), reqBean.getScanInfo())) {
                        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "人工重新获取标准容量:当前缓存的容器代码[%s]的当前状态为[占用]"), container.getCtNo());
                        this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                        putPalletModuleService.doHandleCurCtNo(reqBean, 3, null);
                        return StepResult.getNonComplete(err);
                    }
                    MesPartContainerCapacity partContainerCapacity = workCenterContainerLocationService.getPartContainerCapacity(reqBean.getOrganizeCode(), container.getCtCode(), curPartNo);
                    if (null != partContainerCapacity) {
                        if (StringUtils.isEmpty(partContainerCapacity.getCtQty()) || partContainerCapacity.getCtQty().compareTo(0) == 0) {
                            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(),
                                    "人工重新获取标准容量:当前缓存的容器代码[%s]对应的容器类型[%s]与物料编码[%s]在产品容器容量配置信息中未配置容量"), container.getCtNo(), container.getCtCode(), curPartNo);
                            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                            putPalletModuleService.doHandleCurCtNo(reqBean, 3, null);
                            return StepResult.getNonComplete(err);
                        }
                        putPalletModuleService.doHandleCurCtQty(reqBean, 2, partContainerCapacity.getCtQty());
                    } else {
                        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "人工重新获取标准容量:当前缓存的容器代码[%s]对应的容器类型[%s]与物料编码[%s]未配置产品容器容量信息"),
                                container.getCtNo(), container.getCtCode(), curPartNo);
                        this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                        putPalletModuleService.doHandleCurCtNo(reqBean, 3, null);
                        return StepResult.getNonComplete(err);
                    }
                }
            }
            return StepResult.getSuccessComplete();
        } finally {
            reqBean.setScanInfo(null);
        }
    }

}
