# LotConstrainManagerImpl.java

    package com.mycim.server.constrain.manager.impl;

    import com.fa.sesa.i18n.I18nUtils;
    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.framework.utils.lang.time.DateUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.constrain.manager.EquipmentConstrainInfoManager;
    import com.mycim.server.constrain.manager.LotConstrainManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.server.wip.manager.DiffBatchQueryManager;
    import com.mycim.server.wip.manager.EquipmentQueryManager;
    import com.mycim.server.wip.manager.LotQueryManager;
    import com.mycim.server.wip.manager.RunCardQueryManager;
    import com.mycim.valueobject.MessageIdList;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.bas.ErrorMsg;
    import com.mycim.valueobject.consts.LotInfoConstants;
    import com.mycim.valueobject.ems.Equipment;
    import com.mycim.valueobject.prp.EquipmentConstrainInfo;
    import com.mycim.valueobject.prp.RecipeContextValue;
    import com.mycim.valueobject.runcard.util.RunCardUtils;
    import com.mycim.valueobject.wip.BatchLotStore;
    import com.mycim.valueobject.wip.Lot;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.*;

    /**
     * @author yanbing.chen
     * @version 6.0.0
     * @date 2019/9/4
     **/
    @Service
    @Transactional
    public class LotConstrainManagerImpl implements LotConstrainManager {

        @Autowired
        EquipmentConstrainInfoManager equipmentConstrainInfoManager;

        @Autowired
        NamedObjectManager namedObjectManager;

        @Autowired
        EquipmentQueryManager equipmentQueryManager;

        @Autowired
        RunCardQueryManager runCardQueryManager;

        @Autowired
        LotQueryManager lotQueryManager;

        @Autowired
        DiffBatchQueryManager diffBatchQueryManager;

        @Autowired
        ContextValueManager contextValueManager;

        @Override
        public ErrorMsg checkLotConstrain(Equipment equipment, Lot lot) {
            ErrorMsg errorMsg = new ErrorMsg();
            errorMsg.setError(false);
            errorMsg.setErrorMsg("OK");

            if (runCardQueryManager.getSplitRunCardLotStore(lot.getLotRrn()) != null) {
                return errorMsg;
            }

            String equipmentId = namedObjectManager.getNamedObjectId(equipment.getInstanceRrn());
            equipment.setInstanceId(equipmentId);
            //取情景值上的Recipe
            String recipeId = this.getRecipeIdForLotProcess(lot);
            //获取批次流程和设备设置的设备组交集
            String mixedEqptGroupIds = equipmentQueryManager.getMixedEqptGroupIds(lot, equipment.getInstanceRrn());

            Lot _lot = lotQueryManager.getLot(lot.getLotRrn());
            _lot.setRecipeId(recipeId);
            _lot.setEqptGroupIds(mixedEqptGroupIds);

            // 检查批次上的工艺限制
            List<String> constrainRule = Arrays.asList(new String[]{EquipmentConstrainInfo.NORMAL_EQUIPMENT_RULE,
                                                                    EquipmentConstrainInfo.FREE_EQUIPMENT_RULE});

            List<EquipmentConstrainInfo> equipmentConstrainInfos = equipmentConstrainInfoManager
                    .qryEnableEquipmentConstrainInfos(constrainRule, equipment.getInstanceId(),
                                                      EquipmentConstrainInfo.REJECTSTOP);
            for (EquipmentConstrainInfo equipmentConstrainInfo : equipmentConstrainInfos) {
                if (!checkConstrainInfo(equipmentConstrainInfo, equipment, _lot)) {
                    errorMsg.setError(true);
                    errorMsg.setErrorMsg(I18nUtils.getMessage(MessageIdList.EQPT_CONSTRAINT_ADD, new Object[]{equipmentConstrainInfo.getEquipmentId(), equipmentConstrainInfo.getConstrainSeq()}));
                    errorMsg.setEqpIds(equipmentId);
                    return errorMsg;
                }
            }

            equipmentConstrainInfos = equipmentConstrainInfoManager
                    .qryEnableEquipmentConstrainInfos(constrainRule, equipmentId, EquipmentConstrainInfo.ACCEPT);

            //过滤 已过期的设定
            List<EquipmentConstrainInfo> acceptEquipmentConstrainInfos = new ArrayList<>();
            for (EquipmentConstrainInfo equipmentConstrainInfo : equipmentConstrainInfos) {
                if (!equipmentConstrainInfo.areadyExpire()) {
                    acceptEquipmentConstrainInfos.add(equipmentConstrainInfo);
                }
            }

            boolean acceptConstrain = !acceptEquipmentConstrainInfos.isEmpty();
            for (EquipmentConstrainInfo equipmentConstrainInfo : acceptEquipmentConstrainInfos) {
                if (equipmentConstrainInfo.constraintsMatch(equipment, _lot)) {
                    acceptConstrain = false;
                    break;
                }
            }
            if (acceptConstrain) {
                errorMsg.setError(true);
                errorMsg.setErrorMsg(
                        I18nUtils.getMessage(MessageIdList.EQPT_CONSTRAINT_MATCH, new Object[]{equipmentId}));
                errorMsg.setEqpIds(equipmentId);
                return errorMsg;
            }
            errorMsg.setEqpIds(equipment.getInstanceId());
            return errorMsg;
        }

        @Override
        public ErrorMsg checkLotConstrain(Map<String, Object> constrainMap, String equipmentId, Lot lot) {
            ErrorMsg errorMsg = new ErrorMsg();
            errorMsg.setError(false);
            errorMsg.setErrorMsg("OK");
            if (MapUtils.isNotEmpty(constrainMap)) {
                Equipment equipment = new Equipment();
                equipment.setInstanceId(equipmentId);
                long eqptRrn = namedObjectManager
                        .getNamedObjectRrn(equipmentId, LocalContext.getFacilityRrn(), ObjectList.ENTITY_KEY);
                equipment.setInstanceRrn(eqptRrn);

                //取情景值上的Recipe
                String recipeId = this.getRecipeIdForLotProcess(lot);
                //获取流程和设备设置的设备组交集
                String mixedEqptGroupIds = equipmentQueryManager.getMixedEqptGroupIds(lot, eqptRrn);

                Lot _lot = lotQueryManager.getLot(lot.getLotRrn());
                _lot.setRecipeId(recipeId);
                _lot.setEqptGroupIds(mixedEqptGroupIds);

                Collection rejectStopConstrainColl = (Collection) MapUtils
                        .getObject(constrainMap, EquipmentConstrainInfo.REJECTSTOP);
                if (CollectionUtils.isNotEmpty(rejectStopConstrainColl)) {
                    for (Iterator iterator = rejectStopConstrainColl.iterator(); iterator.hasNext(); ) {
                        EquipmentConstrainInfo equipmentConstrainInfo = (EquipmentConstrainInfo) iterator.next();
                        if (!checkConstrainInfo(equipmentConstrainInfo, equipment, _lot)) {
                            errorMsg.setError(true);
                            errorMsg.setErrorMsg(
                                    "EQP constraint are added!<br>" + "EQP ID :" + equipmentConstrainInfo.getEquipmentId() +
                                            ".<br>Rules of use:RN, CKORD is " + equipmentConstrainInfo.getConstrainSeq() +
                                            " !<br>");
                            errorMsg.setEqpIds(equipmentId);
                            return errorMsg;
                        }
                    }
                }

                Collection acceptContrainColl = (Collection) MapUtils
                        .getObject(constrainMap, EquipmentConstrainInfo.ACCEPT);
                boolean acceptConstrain = CollectionUtils.isNotEmpty(acceptContrainColl);
                for (Iterator iterator = acceptContrainColl.iterator(); iterator.hasNext(); ) {
                    EquipmentConstrainInfo equipmentConstrainInfo = (EquipmentConstrainInfo) iterator.next();
                    if (equipmentConstrainInfo.constraintsMatch(equipment, _lot)) {
                        acceptConstrain = false;
                        break;
                    }
                }
                if (acceptConstrain) {// 有accepte 但是没有匹配de
                    errorMsg.setError(true);
                    errorMsg.setErrorMsg(
                            "EQP ID :" + equipmentId + ".<br>Rules of use:PN are added,but no constraint matched!");
                    errorMsg.setEqpIds(equipmentId);
                    return errorMsg;

                }
                errorMsg.setEqpIds(equipment.getInstanceId());
                return errorMsg;
            }
            return errorMsg;
        }

        @Override
        public ErrorMsg checkLotConstrainCountInfo(Equipment equipment, List<Lot> lotList) {
            ErrorMsg errorMsg = new ErrorMsg();
            errorMsg.setError(false);
            errorMsg.setErrorMsg("OK");

            String equipmentId = namedObjectManager.getNamedObjectId(equipment.getInstanceRrn());
            equipment.setInstanceId(equipmentId);
            Lot _lot = lotQueryManager.getLot(lotList.iterator().next().getLotRrn());
            //取情景值上的Recipe
            String recipeId = this.getRecipeIdForLotProcess(_lot);
            //获取流程和设备定义的设备组交集
            String mixedEqptGroupIds = equipmentQueryManager.getMixedEqptGroupIds(_lot, equipment.getInstanceRrn());

            _lot.setRecipeId(recipeId);
            _lot.setEqptGroupIds(mixedEqptGroupIds);

            // 检查批次上的工艺限制
            List<String> constrainRule = Arrays.asList(new String[]{EquipmentConstrainInfo.NORMAL_EQUIPMENT_RULE,
                                                                    EquipmentConstrainInfo.FREE_EQUIPMENT_RULE});

            List<EquipmentConstrainInfo> equipmentConstrainInfos = equipmentConstrainInfoManager
                    .qryEnableEquipmentConstrainInfos(constrainRule, equipment.getInstanceId(),
                                                      EquipmentConstrainInfo.REJECTSTOP);
            boolean rejectConstrain = false;
            EquipmentConstrainInfo rejectConstrainInfo = null;
            for (EquipmentConstrainInfo equipmentConstrainInfo : equipmentConstrainInfos) {
                if (equipmentConstrainInfo.constraintsMatch(equipment, _lot)) {
                    if (equipmentConstrainInfo.getDailyLotCount() > 0 || equipmentConstrainInfo.getTotalLotCount() > 0 ||
                            equipmentConstrainInfo.getDailyWaferCount() > 0 ||
                            equipmentConstrainInfo.getTotalWaferCount() > 0) {

                        int jobLotSize = lotList.size();
                        int jobWaferSize = 0;
                        for (Lot lot : lotList) {
                            if (lot.getInt_qty1() == null || lot.getInt_qty1() == null) {
                                lot = lotQueryManager.getLot(lot.getLotRrn());
                            }
                            jobWaferSize += lot.getInt_qty1();
                        }

                        Map<String, Object> conditionMap = new HashMap<String, Object>();
                        conditionMap.put("type", "eqptConstrain");
                        conditionMap.put("constrainInfo", equipmentConstrainInfo);
                        if (equipmentConstrainInfo.getDailyLotCount() > 0) {
                            Date date = DateUtils
                                    .parse(DateUtils.getNowTime(DateUtils.DATE_FORMAT4DAYD), DateUtils.DATE_FORMAT4DAYD);
                            if (date.before(equipmentConstrainInfo.getModifyTime())) {
                                conditionMap.put("startTime", DateUtils
                                        .formatDate(equipmentConstrainInfo.getModifyTime(), DateUtils.DATE_FORMAT4DATE));
                            } else {
                                conditionMap.put("startTime", DateUtils.getNowTime(DateUtils.DATE_FORMAT4DAYD));
                            }
                            if (equipmentConstrainInfo.getDailyLotCount() > 0) {
                                conditionMap.put("level", EquipmentConstrainInfo.LOT);
                                long currentDailyLotCount = lotQueryManager.getLotCountFormHistoryByCondition(conditionMap);
                                if ((jobLotSize + currentDailyLotCount) >= equipmentConstrainInfo.getDailyLotCount()) {
                                    rejectConstrain = true;
                                    rejectConstrainInfo = equipmentConstrainInfo;
                                    break;
                                }
                            }
                            if (equipmentConstrainInfo.getDailyWaferCount() > 0) {
                                conditionMap.put("level", EquipmentConstrainInfo.WAFER);
                                long currentDailyWaferCount = lotQueryManager
                                        .getLotCountFormHistoryByCondition(conditionMap);
                                if ((jobWaferSize + currentDailyWaferCount) >=
                                        equipmentConstrainInfo.getDailyWaferCount()) {
                                    rejectConstrain = true;
                                    rejectConstrainInfo = equipmentConstrainInfo;
                                    break;
                                }
                            }

                            if (equipmentConstrainInfo.getTotalLotCount() > 0 ||
                                    equipmentConstrainInfo.getTotalWaferCount() > 0) {
                                conditionMap.put("startTime", DateUtils
                                        .formatDate(equipmentConstrainInfo.getModifyTime(), DateUtils.DATE_FORMAT4DATE));
                                if (equipmentConstrainInfo.getTotalLotCount() > 0) {
                                    conditionMap.put("level", EquipmentConstrainInfo.LOT);
                                    long currentTotalLotCount = lotQueryManager
                                            .getLotCountFormHistoryByCondition(conditionMap);
                                    if ((jobLotSize + currentTotalLotCount) >= equipmentConstrainInfo.getTotalLotCount()) {
                                        rejectConstrain = true;
                                        rejectConstrainInfo = equipmentConstrainInfo;
                                        break;
                                    }
                                }
                                if (equipmentConstrainInfo.getTotalWaferCount() > 0) {
                                    conditionMap.put("level", EquipmentConstrainInfo.WAFER);
                                    long currentTotalWaferCount = lotQueryManager
                                            .getLotCountFormHistoryByCondition(conditionMap);
                                    if ((jobWaferSize + currentTotalWaferCount) >=
                                            equipmentConstrainInfo.getTotalWaferCount()) {
                                        rejectConstrain = true;
                                        rejectConstrainInfo = equipmentConstrainInfo;
                                        break;
                                    }
                                }
                            }
                        }
                    } else {
                        rejectConstrain = true;
                        rejectConstrainInfo = equipmentConstrainInfo;
                        break;
                    }
                }
            }

            if (rejectConstrain) {// 有reject
                errorMsg.setError(true);
                errorMsg.setErrorMsg("EQP constraint are added!<br>" + "EQP ID :" + rejectConstrainInfo.getEquipmentId() +
                                             ".<br>Rules of use:" + rejectConstrainInfo.getConstrainActionForShow() +
                                             ", CKORD is " + rejectConstrainInfo.getConstrainSeq() + ", Comment is " +
                                             rejectConstrainInfo.getRemark() + " !<br>");
                errorMsg.setEqpIds(equipmentId);
            }
            return errorMsg;
        }

        @Override
        public ErrorMsg checkConstrainCountInfo(Equipment equipment, List<Lot> lotList) {

            // 检查批次上的工艺限制
            List<String> constrainRule = Arrays.asList(new String[]{EquipmentConstrainInfo.NORMAL_EQUIPMENT_RULE,
                                                                    EquipmentConstrainInfo.FREE_EQUIPMENT_RULE});
            List<EquipmentConstrainInfo> equipmentConstrainInfos = equipmentConstrainInfoManager
                    .qryEnableEquipmentConstrainInfos(constrainRule, equipment.getInstanceId(),
                                                      EquipmentConstrainInfo.REJECTSTOP);

            int allQty = 0;
            for(Lot lot:lotList) {
                if(LotInfoConstants.SysType.isNormalLot(lot.getSysType())) {
                    allQty += lot.getInt_qty1().intValue();
                }
            }

            for (EquipmentConstrainInfo equipmentConstrainInfo : equipmentConstrainInfos) {
                boolean checkFlag = false;
                for (Lot lot : lotList) {
                    if (equipmentConstrainInfo.constraintsMatch(equipment, lot)) {
                        checkFlag = true;
                        break;
                    }
                }

                if(checkFlag) {
                    if(!checkConstrainInfoCount(allQty,equipmentConstrainInfo)) {
                        ErrorMsg errorMsg = new ErrorMsg();
                        errorMsg.setError(true);
                        errorMsg.setErrorMsg(I18nUtils.getMessage(MessageIdList.EQPT_CONSTRAINT_ADD,
                                                                  new Object[]{equipmentConstrainInfo.getEquipmentId(),
                                                                               equipmentConstrainInfo.getConstrainSeq()}));
                        errorMsg.setEqpIds(equipment.getInstanceId());
                        return errorMsg;
                    }
                }
            }
            ErrorMsg errorMsg = new ErrorMsg();
            errorMsg.setError(false);
            errorMsg.setErrorMsg("OK");
            return errorMsg;
        }

        private boolean checkConstrainInfoCount(int allQty, EquipmentConstrainInfo equipmentConstrainInfo) {
            if (equipmentConstrainInfo.getDailyLotCount() > 0 || equipmentConstrainInfo.getTotalLotCount() > 0 ||
                    equipmentConstrainInfo.getDailyWaferCount() > 0 ||
                    equipmentConstrainInfo.getTotalWaferCount() > 0) {

                Map<String, Object> conditionMap = new HashMap<String, Object>();
                conditionMap.put("type", "eqptConstrain");
                conditionMap.put("constrainInfo", equipmentConstrainInfo);
                if (equipmentConstrainInfo.getDailyLotCount() > 0 || equipmentConstrainInfo.getDailyWaferCount() > 0) {
                    Date date = DateUtils
                            .parse(DateUtils.getNowTime(DateUtils.DATE_FORMAT4DAYD), DateUtils.DATE_FORMAT4DAYD);
                    if (date.before(equipmentConstrainInfo.getModifyTime())) {
                        conditionMap.put("startTime", DateUtils
                                .formatDate(equipmentConstrainInfo.getModifyTime(), DateUtils.DATE_FORMAT4DATE));
                    } else {
                        conditionMap.put("startTime", DateUtils.getNowTime(DateUtils.DATE_FORMAT4DAYD));
                    }
                    if (equipmentConstrainInfo.getDailyLotCount() > 0) {
                        conditionMap.put("level", EquipmentConstrainInfo.LOT);
                        long currentDailyLotCount = lotQueryManager.getLotCountFormHistoryByCondition(conditionMap);
                        if ((allQty + currentDailyLotCount) >= equipmentConstrainInfo.getDailyLotCount()) {
                            return false;
                        }
                    }
                    if (equipmentConstrainInfo.getDailyWaferCount() > 0) {
                        conditionMap.put("level", EquipmentConstrainInfo.WAFER);
                        long currentDailyWaferCount = lotQueryManager.getLotCountFormHistoryByCondition(conditionMap);
                        if ((allQty + currentDailyWaferCount) >= equipmentConstrainInfo.getDailyWaferCount()) {
                            return false;
                        }
                    }
                }
                if (equipmentConstrainInfo.getTotalLotCount() > 0 || equipmentConstrainInfo.getTotalWaferCount() > 0) {
                    conditionMap.put("startTime", DateUtils
                            .formatDate(equipmentConstrainInfo.getModifyTime(), DateUtils.DATE_FORMAT4DATE));
                    if (equipmentConstrainInfo.getTotalLotCount() > 0) {
                        conditionMap.put("level", EquipmentConstrainInfo.LOT);
                        long currentTotalLotCount = lotQueryManager.getLotCountFormHistoryByCondition(conditionMap);
                        if ((allQty + currentTotalLotCount) >= equipmentConstrainInfo.getTotalLotCount()) {
                            return false;
                        }
                    }
                    if (equipmentConstrainInfo.getTotalWaferCount() > 0) {
                        conditionMap.put("level", EquipmentConstrainInfo.WAFER);
                        long currentTotalWaferCount = lotQueryManager.getLotCountFormHistoryByCondition(conditionMap);
                        if ((allQty + currentTotalWaferCount) >= equipmentConstrainInfo.getTotalWaferCount()) {
                            return false;
                        }
                    }
                }
            } else {
                return false;
            }
            return true;
        }

        private boolean checkConstrainInfo(EquipmentConstrainInfo equipmentConstrainInfo, Equipment equipment, Lot lot) {
            if (equipmentConstrainInfo.constraintsMatch(equipment, lot)) {
                if (equipmentConstrainInfo.getDailyLotCount() > 0 || equipmentConstrainInfo.getTotalLotCount() > 0 ||
                        equipmentConstrainInfo.getDailyWaferCount() > 0 ||
                        equipmentConstrainInfo.getTotalWaferCount() > 0) {

                    int jobLotSize = 1;
                    int jobWaferSize = lot.getInt_qty1();
                    if (lot.getInt_qty1() == null || lot.getInt_qty1() == 0) {
                        lot = lotQueryManager.getLot(lot.getLotRrn());
                    }
                    if (lot.getJobRrn() != null && lot.getJobRrn() > 0) {
                        Collection lots = lotQueryManager.getLotsByJobRrn(lot.getJobRrn());
                        if (lots != null && lots.size() >= 1) {
                            jobLotSize = lots.size();
                            if (equipmentConstrainInfo.getDailyWaferCount() > 0 ||
                                    equipmentConstrainInfo.getTotalWaferCount() > 0) {
                                Lot jobLot = new Lot();
                                jobWaferSize = 0;
                                for (Iterator<Lot> it = lots.iterator(); it.hasNext(); ) {
                                    jobLot = it.next();
                                    jobWaferSize += jobLot.getInt_qty1();
                                }
                            }
                        }
                    } else {
                        List<BatchLotStore> batchList = diffBatchQueryManager.getBatchStoreListByLot(lot.getLotRrn());
                        if (batchList != null && batchList.size() >= 1) {
                            if (equipmentConstrainInfo.getDailyWaferCount() > 0 ||
                                    equipmentConstrainInfo.getTotalWaferCount() > 0) {
                                Lot jobLot = new Lot();
                                jobWaferSize = 0;
                                jobLotSize = 0;
                                for (BatchLotStore batch : batchList) {
                                    jobLot = lotQueryManager.getLot(batch.getLotRrn());
                                    if(!RunCardUtils.checkLotIdIsRunCardLot(jobLot.getLotId())) {
                                        jobWaferSize += jobLot.getInt_qty1();
                                        jobLotSize++;
                                    }
                                }
                            }
                        }
                    }
                    Map<String, Object> conditionMap = new HashMap<String, Object>();
                    conditionMap.put("type", "eqptConstrain");
                    conditionMap.put("constrainInfo", equipmentConstrainInfo);
                    if (equipmentConstrainInfo.getDailyLotCount() > 0 || equipmentConstrainInfo.getDailyWaferCount() > 0) {
                        Date date = DateUtils
                                .parse(DateUtils.getNowTime(DateUtils.DATE_FORMAT4DAYD), DateUtils.DATE_FORMAT4DAYD);
                        if (date.before(equipmentConstrainInfo.getModifyTime())) {
                            conditionMap.put("startTime", DateUtils
                                    .formatDate(equipmentConstrainInfo.getModifyTime(), DateUtils.DATE_FORMAT4DATE));
                        } else {
                            conditionMap.put("startTime", DateUtils.getNowTime(DateUtils.DATE_FORMAT4DAYD));
                        }
                        if (equipmentConstrainInfo.getDailyLotCount() > 0) {
                            conditionMap.put("level", EquipmentConstrainInfo.LOT);
                            long currentDailyLotCount = lotQueryManager.getLotCountFormHistoryByCondition(conditionMap);
                            if ((jobLotSize + currentDailyLotCount) >= equipmentConstrainInfo.getDailyLotCount()) {
                                return false;
                            }
                        }
                        if (equipmentConstrainInfo.getDailyWaferCount() > 0) {
                            conditionMap.put("level", EquipmentConstrainInfo.WAFER);
                            long currentDailyWaferCount = lotQueryManager.getLotCountFormHistoryByCondition(conditionMap);
                            if ((jobWaferSize + currentDailyWaferCount) >= equipmentConstrainInfo.getDailyWaferCount()) {
                                return false;
                            }
                        }
                    }
                    if (equipmentConstrainInfo.getTotalLotCount() > 0 || equipmentConstrainInfo.getTotalWaferCount() > 0) {
                        conditionMap.put("startTime", DateUtils
                                .formatDate(equipmentConstrainInfo.getModifyTime(), DateUtils.DATE_FORMAT4DATE));
                        if (equipmentConstrainInfo.getTotalLotCount() > 0) {
                            conditionMap.put("level", EquipmentConstrainInfo.LOT);
                            long currentTotalLotCount = lotQueryManager.getLotCountFormHistoryByCondition(conditionMap);
                            if ((jobLotSize + currentTotalLotCount) >= equipmentConstrainInfo.getTotalLotCount()) {
                                return false;
                            }
                        }
                        if (equipmentConstrainInfo.getTotalWaferCount() > 0) {
                            conditionMap.put("level", EquipmentConstrainInfo.WAFER);
                            long currentTotalWaferCount = lotQueryManager.getLotCountFormHistoryByCondition(conditionMap);
                            if ((jobWaferSize + currentTotalWaferCount) >= equipmentConstrainInfo.getTotalWaferCount()) {
                                return false;
                            }
                        }
                    }
                } else {
                    return false;
                }
            }
            return true;
        }

        private String getRecipeIdForLotProcess(Lot lot) {
            RecipeContextValue contextValue = new RecipeContextValue();
            contextValue.setContextRrn(namedObjectManager.getNamedObjectRrn(contextValue.getContextId(),
                                                                            LocalContext.getFacilityRrn(),
                                                                            ObjectList.CONTEXT_KEY));
            contextValue.setOperationRrn(lot.getOperationRrn());
            contextValue.setRouteRrn(lot.getRouteRrn());
            contextValue.setProcessRrn(lot.getProcessRrn());
            contextValue.setProcessVersion(lot.getProcessVersion());
            contextValue.setProductRrn(lot.getProductRrn());
            contextValueManager.simulateContextValue(contextValue);
            String recipeId = lot.getRecipeId();
            if (contextValue != null && NumberUtils.isParsable(contextValue.getResultValue1())) {
                recipeId = namedObjectManager.getInstanceId(NumberUtils.toLong(contextValue.getResultValue1()));
            }
            return recipeId;
        }

    }