# EquipmentConstrainInfoManagerImpl.java

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

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.oid.IDGenerators;
    import com.mycim.framework.oid.type.IDNames;
    import com.mycim.framework.oid.type.SequenceNames;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.framework.utils.lang.time.DateUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.base.manager.RelationManager;
    import com.mycim.server.base.manager.TransactionLogManager;
    import com.mycim.server.constrain.dao.EquipmentConstrainInfoDAO;
    import com.mycim.server.constrain.manager.EquipmentConstrainInfoManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.LinkTypeList;
    import com.mycim.valueobject.consts.TransactionNames;
    import com.mycim.valueobject.prp.EquipmentConstrainInfo;
    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.*;
    import java.util.stream.Collectors;

    /**
     * 设备约束信息 managerImpl
     *
     * @author pinyan song
     * @version 6.0.0
     * @date 2019-8-30
     **/
    @Service
    @Transactional
    public class EquipmentConstrainInfoManagerImpl implements EquipmentConstrainInfoManager {

        @Autowired
        private NamedObjectManager namedObjectManager;

        @Autowired
        private RelationManager relationManager;

        @Autowired
        private EquipmentConstrainInfoDAO constrainInfoDAO;

        @Autowired
        private EquipmentConstrainInfoDAO equipmentConstrainInfoDAO;

        @Autowired
        private TransactionLogManager transactionLogManager;

        @Override
        public List<EquipmentConstrainInfo> qryEnableEquipmentConstrainInfos(List<String> rules, String equipmentId,
                                                                             String constrainAction) {
            return equipmentConstrainInfoDAO.qryEnableEquipmentConstrains(rules, equipmentId, constrainAction);
        }

        @Override
        public EquipmentConstrainInfo getEquipmentConstrainInfo(EquipmentConstrainInfo equipmentConstrainInfo,
                                                                Boolean approveFlag) {
            long constrainRrn = equipmentConstrainInfo.getConstrainRrn();

            if (approveFlag) {

                if (constrainRrn > 0) {
                    return equipmentConstrainInfoDAO.getEquipmentConstrainInfoForCheck(constrainRrn);
                } else {
                    return equipmentConstrainInfoDAO.getEquipmentConstrainInfoForCheck(equipmentConstrainInfo);
                }

            } else {
                if (constrainRrn > 0) {
                    return equipmentConstrainInfoDAO.getEquipmentConstrainInfo(constrainRrn);
                } else {
                    return equipmentConstrainInfoDAO.getEquipmentConstrainInfo(equipmentConstrainInfo);
                }
            }
        }

        @Override
        public void insertEquipmentConstrainInfo(EquipmentConstrainInfo equipmentConstrainInfo) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(equipmentConstrainInfo.getCreateBy(), TransactionNames.CREATE_KEY);
            equipmentConstrainInfo.setTransName(TransactionNames.CREATE_KEY);
            Long constrainRrn = Long
                    .valueOf(IDGenerators.get(IDNames.SEQUENCE).generateId(SequenceNames.MODULE_SEQ_OBJECT_RRN));
            equipmentConstrainInfo.setConstrainRrn(constrainRrn);
            equipmentConstrainInfoDAO.insertEquipmentConstrainInfoTmp(equipmentConstrainInfo);
            equipmentConstrainInfoDAO.insertEquipmentConstrainHistory(equipmentConstrainInfo, transactionLog.getTransRrn());
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public void modifyEquipmentConstrainInfo(EquipmentConstrainInfo equipmentConstrainInfo) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(LocalContext.getUserId(), TransactionNames.MODIFY_KEY);
            equipmentConstrainInfo.setTransName(TransactionNames.MODIFY_KEY);
            equipmentConstrainInfoDAO.modifyEquipmentConstrainInfoTmp(equipmentConstrainInfo);
            equipmentConstrainInfoDAO.insertEquipmentConstrainHistory(equipmentConstrainInfo, transactionLog.getTransRrn());
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public Page qryEquipmentConstrainInfos(Page lotConstrainPage, Map condtionInfo) {
            boolean checkFlag = MapUtils.getBooleanValue(condtionInfo, "checkFlag");

            if (checkFlag) {
                return equipmentConstrainInfoDAO.qryEquipmentConstrainsForCheck(lotConstrainPage, condtionInfo);
            } else {
                return equipmentConstrainInfoDAO.qryEquipmentConstrains(lotConstrainPage, condtionInfo);
            }
        }

        @Override
        public List<EquipmentConstrainInfo> validationEquipmentConstrainInfos(Map condtionMap) {
            return equipmentConstrainInfoDAO.validationEquipmentConstrains(condtionMap);
        }

        @Override
        public void deleteEquipmentConstrainInfo(EquipmentConstrainInfo equipmentConstrainInfo) {
            String user = equipmentConstrainInfo.getCreateBy();
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(user, TransactionNames.DELETE_KEY);
            equipmentConstrainInfo.setTransName(TransactionNames.DELETE_KEY);
            equipmentConstrainInfoDAO.insertEquipmentConstrainHistory(equipmentConstrainInfo, transactionLog.getTransRrn());
            equipmentConstrainInfoDAO.modifyEquipmentConstrainInfoTmp(equipmentConstrainInfo);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public void checkAndModifyEquipmentConstrainInfo(EquipmentConstrainInfo equipmentConstrainInfo) {

            String checkResult = equipmentConstrainInfo.getCheckResult();
            String user = equipmentConstrainInfo.getModifyBy();
            Long constrainInfoRrn = equipmentConstrainInfo.getConstrainRrn();
            TransactionLog transactionLog = transactionLogManager.startTransactionLog(user, checkResult);

            EquipmentConstrainInfo activeEquipmentConstrainInfo = equipmentConstrainInfoDAO
                    .getEquipmentConstrainInfo(constrainInfoRrn);
            EquipmentConstrainInfo equipmentConstrainInfoFromTmp = equipmentConstrainInfoDAO
                    .getEquipmentConstrainInfoForCheck(constrainInfoRrn);

            String transName = equipmentConstrainInfoFromTmp.getTransName();

            equipmentConstrainInfoFromTmp.setTransName(null);

            if (StringUtils.equalsIgnoreCase(checkResult, EquipmentConstrainInfo.APPROVE)) {

                equipmentConstrainInfoFromTmp.setEffectiveDate(DateUtils.formatDate(new Date(), DateUtils.DATE_FORMAT));
                if (StringUtils.equalsIgnoreCase(transName, TransactionNames.CREATE_KEY)) {// 新建
                    equipmentConstrainInfoDAO.insertEquipmentConstrainInfo(equipmentConstrainInfoFromTmp);
                    equipmentConstrainInfoDAO.modifyEquipmentConstrainInfoTmp(equipmentConstrainInfoFromTmp);
                    equipmentConstrainInfoDAO
                            .insertEquipmentConstrainHistory(equipmentConstrainInfo, transactionLog.getTransRrn());
                } else if (StringUtils.equalsIgnoreCase(transName, TransactionNames.MODIFY_KEY)) {
                    equipmentConstrainInfoDAO.modifyEquipmentConstrainInfo(equipmentConstrainInfoFromTmp);
                    equipmentConstrainInfoDAO.modifyEquipmentConstrainInfoTmp(equipmentConstrainInfoFromTmp);
                    equipmentConstrainInfoDAO
                            .insertEquipmentConstrainHistory(equipmentConstrainInfo, transactionLog.getTransRrn());
                } else if (StringUtils.equalsIgnoreCase(transName, TransactionNames.DELETE_KEY)) {
                    equipmentConstrainInfoDAO
                            .insertEquipmentConstrainHistory(equipmentConstrainInfo, transactionLog.getTransRrn());
                    equipmentConstrainInfoDAO.deleteEquipmentConstrainInfoTmp(constrainInfoRrn);
                    equipmentConstrainInfoDAO.deleteEquipmentConstrainInfo(constrainInfoRrn);
                }
            } else {
                if (StringUtils.equalsIgnoreCase(transName, TransactionNames.CREATE_KEY)) {// 新建
                    equipmentConstrainInfoDAO
                            .insertEquipmentConstrainHistory(equipmentConstrainInfo, transactionLog.getTransRrn());
                    equipmentConstrainInfoDAO.deleteEquipmentConstrainInfoTmp(constrainInfoRrn);
                } else if (StringUtils.equalsIgnoreCase(transName, TransactionNames.MODIFY_KEY)) {
                    activeEquipmentConstrainInfo.setModifyBy(equipmentConstrainInfoFromTmp.getModifyBy());
                    activeEquipmentConstrainInfo.setModifyTime(equipmentConstrainInfoFromTmp.getModifyTime());
                    equipmentConstrainInfoDAO.modifyEquipmentConstrainInfoTmp(activeEquipmentConstrainInfo);
                    equipmentConstrainInfoDAO
                            .insertEquipmentConstrainHistory(equipmentConstrainInfo, transactionLog.getTransRrn());
                } else if (StringUtils.equalsIgnoreCase(transName, TransactionNames.DELETE_KEY)) {
                    activeEquipmentConstrainInfo.setModifyBy(equipmentConstrainInfoFromTmp.getModifyBy());
                    activeEquipmentConstrainInfo.setModifyTime(equipmentConstrainInfoFromTmp.getModifyTime());
                    equipmentConstrainInfoDAO.modifyEquipmentConstrainInfoTmp(activeEquipmentConstrainInfo);
                    equipmentConstrainInfoDAO
                            .insertEquipmentConstrainHistory(equipmentConstrainInfo, transactionLog.getTransRrn());
                }
            }
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public Integer getMaxConstrainSeqByEqpt(String eqptId) {
            return equipmentConstrainInfoDAO.getMaxConstrainSeqByEqpt(eqptId);
        }

        @Override
        public Map<String, Object> getEqptConstrain4LotOperator(String equipmentId) {
            Map<String, Object> constrainMap = new HashMap<>(3);
            if (StringUtils.isNotBlank(equipmentId)) {
                List<String> constrainRule = Arrays
                        .asList(EquipmentConstrainInfo.NORMAL_EQUIPMENT_RULE, EquipmentConstrainInfo.FREE_EQUIPMENT_RULE);

                List<EquipmentConstrainInfo> equipmentConstrainInfos = this
                        .qryEnableEquipmentConstrainInfos(constrainRule, equipmentId, EquipmentConstrainInfo.REJECTSTOP);
                constrainMap.put(EquipmentConstrainInfo.REJECTSTOP, equipmentConstrainInfos);

                equipmentConstrainInfos = this
                        .qryEnableEquipmentConstrainInfos(constrainRule, equipmentId, EquipmentConstrainInfo.ACCEPT);
                constrainMap.put(EquipmentConstrainInfo.ACCEPT, equipmentConstrainInfos);
            }

            return constrainMap;
        }

        @Override
        public void changeEquipmentConstantToEnable() {
            equipmentConstrainInfoDAO.changeEquipmentConstantToEnable();
        }

        public boolean checkLotConstrain(Lot lot, EquipmentConstrainInfo equipmentConstrainInfo) {
            long operationRrn = equipmentConstrainInfo.getOperationRrn();
            long processRrn = equipmentConstrainInfo.getProcessRrn();
            long productRrn = equipmentConstrainInfo.getProductRrn();
            String lotId = equipmentConstrainInfo.getLotId();
            String recipeId = equipmentConstrainInfo.getRecipeId();
            String stageId = equipmentConstrainInfo.getStageId();
            String customerId = equipmentConstrainInfo.getCustomerId();

            long recipeRrn = namedObjectManager
                    .getNamedObjectRrn(recipeId, LocalContext.getFacilityRrn(), ObjectList.RECIPE_KEY);
            List<Map> recipeList = relationManager.getRelationFromRecipe(recipeRrn, LinkTypeList.RECIPEFAMILY_TO_RECIPE);

            List<String> recipeIds = recipeList.stream().map(map -> {
                return MapUtils.getString(map, "recipeId");
            }).collect(Collectors.toList());

            recipeIds.add(recipeId);

            if ((operationRrn == 0 || operationRrn == lot.getOperationRrn()) &&
                    (processRrn == 0 || processRrn == lot.getProcessRrn()) &&
                    (productRrn == 0 || productRrn == lot.getProductRrn()) &&
                    (StringUtils.isBlank(lotId) || lotId.equals(lot.getLotId())) &&
                    (StringUtils.isBlank(recipeId) || recipeIds.contains(lot.getRecipeId())) &&
                    (StringUtils.isBlank(stageId) || stageId.equals(lot.getStageId())) &&
                    (StringUtils.isBlank(customerId) || customerId.equals(lot.getCustomerId()))) {
                return true;
            }
            return false;
        }

    }