# EquipmentQueryManagerImpl.java

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

    import com.mycim.framework.utils.lang.BooleanUtils;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.ctx.exec.manager.ProcessOperationDescContextValueManager;
    import com.mycim.server.ems.manager.EntityGroupManager;
    import com.mycim.server.prp.manager.OperationManager;
    import com.mycim.server.spec.manager.ProcessSpecItemManager;
    import com.mycim.server.wip.dao.EquipmentQueryDAO;
    import com.mycim.server.wip.manager.EquipmentQueryManager;
    import com.mycim.server.wip.manager.LotQueryManager;
    import com.mycim.valueobject.bas.Relation;
    import com.mycim.valueobject.ems.Equipment;
    import com.mycim.valueobject.prp.Operation;
    import com.mycim.valueobject.security.Station;
    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 Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/24
     **/
    @Service
    @Transactional
    public class EquipmentQueryManagerImpl implements EquipmentQueryManager {

        private static final String FUTURE_KEY = "future";

        private static final String FUTURE_STEP_SEQ_KEY = "futureStepSeq";

        private static final String FUTURE_OPERATION_KEY = "future";

        @Autowired
        private EquipmentQueryDAO equipmentQueryDAO;

        @Autowired
        private NamedObjectManager namedObjectManager;

        @Autowired
        private OperationManager operationManager;

        @Autowired
        private EntityGroupManager entityGroupManager;

        @Autowired
        private LotQueryManager lotQueryManager;

        @Autowired
        private ProcessOperationDescContextValueManager operationDescContextValueManager;

        @Autowired
        private ProcessSpecItemManager processSpecItemManager;

        @Override
        public List<Station> getEntities4ExtByUser(long userRrn, String namedSpace, String filter) {
            return equipmentQueryDAO.getEntities4ExtByUser(userRrn, namedSpace, filter);
        }

        @Override
        public Map<String, Integer> countLotByStausForEntity(String lotStatus) {
            return equipmentQueryDAO.countLotByStausForEntity(lotStatus);
        }

        @Override
        public List<String> getLotsStatusEntity(Long entityRrn) {
            return equipmentQueryDAO.getLotsStatusEntity(entityRrn);
        }

        @Override
        public List<Equipment> getEquipment4Ext(long equipmentRrn, long stationRrn) {
            List<Equipment> equipmentList = equipmentQueryDAO.getEquipment4Ext(equipmentRrn, stationRrn);

            if (CollectionUtils.isNotEmpty(equipmentList)) {
                for (Equipment equipment : equipmentList) {
                    equipment.setProcessEngineerId(namedObjectManager.getNamedObjectId(equipment.getProcessEngineerRrn()));
                    equipment.setEquipmentModelId(namedObjectManager.getNamedObjectId(equipment.getEquipmentModelRrn()));

                    Long tmpRrn = equipment.getControlingStationRrn();
                    if ((tmpRrn != null) && (tmpRrn.longValue() > 0)) {
                        equipment.setControlingStationId(namedObjectManager.getNamedObjectId(tmpRrn));
                    }
                    tmpRrn = equipment.getProcessEngineerRrn();
                    if ((tmpRrn != null) && (tmpRrn > 0)) {
                        equipment.setProcessEngineerId(namedObjectManager.getNamedObjectId(tmpRrn));
                    }
                }
            }
            return equipmentList;
        }

        @Override
        public Long getDefaultEqpt(Long operationRrn) {

            Long defaultEqptRrn = null;
            // 1.get entity group

            Operation operation = operationManager.getOperation(operationRrn);

            // 2.get entitys in group
            List<Relation> entities = entityGroupManager.getAllEntities(operation.getEntityGroupRrn());

            long firstEqptRrn = 0L;
            for (Relation item : entities) {
                if (firstEqptRrn == 0L) {
                    firstEqptRrn = item.getFromRrn();
                }
                if (item.getAttributedata1() != null && BooleanUtils.toBoolean(item.getAttributedata1())) {
                    defaultEqptRrn = item.getFromRrn();
                    break;
                }
            }

            if (defaultEqptRrn != null) {
                return defaultEqptRrn;
            } else {
                return firstEqptRrn;
            }
        }

        @Override
        public String getEqpGroupIdsByEquipmentRrn(long equipmentRrn) {
            return equipmentQueryDAO.getEqpGroupIdsByEquipmentRrn(equipmentRrn);
        }

        @Override
        public String getMixedEqptGroupIds(Lot lot, long equipmentRrn) {
            //批次流程上的设备组
            String eqptGroupIdsForProcess = processSpecItemManager
                    .getEqptGroupIds(lot.getProcessRrn(), lot.getProcessVersion(), lot.getRouteRrn(),
                                     lot.getOperationRrn());
            //设备的设备组
            String eqptGroupIdsForEqpt = this.getEqpGroupIdsByEquipmentRrn(equipmentRrn);

            List mixed = new ArrayList();
            if (StringUtils.isNotEmpty(eqptGroupIdsForProcess) && StringUtils.isNotEmpty(eqptGroupIdsForEqpt)) {
                String[] eqptGroupIds1 = eqptGroupIdsForProcess.split(",");
                String[] eqptGroupIds2 = eqptGroupIdsForEqpt.split(",");
                Set<String> set = new HashSet<>(Arrays.asList(eqptGroupIds2));

                for (String id : eqptGroupIds1) {
                    if (set.contains(StringUtils.trim(id))) {
                        mixed.add(StringUtils.trim(id));
                    }
                }
            }

            return String.join(",", mixed);
        }

        @Override
        public String getEquipmentLastMoveOutTime(String equipmentId) {
            return equipmentQueryDAO.getEquipmentLastMoveOutTime(equipmentId);
        }

        @Override
        public boolean checkEquipmentHasRunningLot(Long eqpRrn) {
            return equipmentQueryDAO.checkEquipmentHasRunningLot(eqpRrn);
        }

        @Override
        public String getLastHoldTimeOnEqp(Long eqpRrn) {
            return equipmentQueryDAO.getLastHoldTimeOnEqp(eqpRrn);
        }

        @Override
        public String getLastProcessEndTime(String equipmentId, String chamberType) {
            return equipmentQueryDAO.getLastProcessEndTime(equipmentId, chamberType);
        }

        @Override
        public String getLastMoveInTimeByEqp(String equipmentId, Long eqpRrn) {
            return equipmentQueryDAO.getLastMoveInTimeByEqp(equipmentId, eqpRrn);
        }

        @Override
        public String getLastProcessStartTime(Long eqpRrn, String equipmentId, String chamberType) {
            return equipmentQueryDAO.getLastProcessStartTime(eqpRrn, equipmentId, chamberType);
        }

        @Override
        public Map<String,Object> getLastEquipmentRunRecipe(String equipmentId) {
            return equipmentQueryDAO.getLastEquipmentRunRecipe(equipmentId);
        }

        @Override
        public Map<String,Object> getLastChamberEquipmentRunRecipe(String equipmentId, Long equipmentRrn, String chamberType) {
            return equipmentQueryDAO.getLastChamberEquipmentRunRecipe(equipmentId, equipmentRrn,  chamberType);
        }

        @Override
        public String getLastEquipmentLogEvent(Long eqpRrn) {
            return equipmentQueryDAO.getLastEquipmentLogEvent(eqpRrn);
        }

    }