# DiffBatchQueryManagerImpl.java

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

    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.framework.utils.lang.time.DateUtils;
    import com.mycim.server.automonitor.manager.LotAutoMonitorInqManager;
    import com.mycim.server.wip.dao.DiffBatchQueryDAO;
    import com.mycim.server.wip.manager.DiffBatchQueryManager;
    import com.mycim.server.wip.manager.LotInqManager;
    import com.mycim.server.wip.manager.LotQueryManager;
    import com.mycim.server.wip.manager.job.EquipmentCheckManager;
    import com.mycim.valueobject.ems.Equipment;
    import com.mycim.valueobject.wip.BatchLotStore;
    import com.mycim.valueobject.wip.EqpRcpDiffBatchSetupInfo;
    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.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;

    /**
     * @author finatice.yang
     * @date 2021/6/30
     **/
    @Service
    @Transactional
    public class DiffBatchQueryManagerImpl implements DiffBatchQueryManager {

        @Autowired
        DiffBatchQueryDAO diffBatchQueryDAO;

        @Autowired
        LotInqManager lotInqManager;

        @Autowired
        LotAutoMonitorInqManager lotAutoMonitorInqManager;

        @Autowired
        EquipmentCheckManager equipmentCheckManager;

        @Override
        public List<BatchLotStore> getBatchStoreListByLot(Long lotRrn) {
            List<BatchLotStore> batchList = new ArrayList<BatchLotStore>();
            BatchLotStore store = diffBatchQueryDAO.getBatchLotStoreByToRrn(lotRrn);
            if (store != null) {
                batchList = diffBatchQueryDAO.getBatchLotStore(store.getBatchId());
            } else {
                batchList = diffBatchQueryDAO.getBatchLotStore(lotRrn);
            }
            return batchList;
        }

        @Override
        public List<BatchLotStore> getBatchStoreListByBatchId(String batchId) {
            return diffBatchQueryDAO.getBatchLotStore(batchId);
        }

        @Override
        public List<Lot> getBatchLotList(Long lotRrn) {
            List<Lot> lotList = new ArrayList<>();

            List<BatchLotStore> batchLotStoreList = getBatchStoreListByLot(lotRrn);
            if (CollectionUtils.isNotEmpty(batchLotStoreList)) {
                for (BatchLotStore batchLotStore : batchLotStoreList) {
                    lotList.add(lotInqManager.getLot(batchLotStore.getLotRrn()));
                }
                Long monitorLotRrn = batchLotStoreList.iterator().next().getMonitorLotRrn();
                if (Objects.nonNull(monitorLotRrn) && monitorLotRrn.longValue() > 0 &&
                        lotAutoMonitorInqManager.checkLotIsAutoMonitorLot(monitorLotRrn)) {
                    lotList.add(lotInqManager.getLot(monitorLotRrn));
                }
            }

            return lotList;
        }

        @Override
        public Boolean checkLotInBatch(Long lotRrn) {
            return diffBatchQueryDAO.checkLotInBatch(lotRrn);
        }

        @Override
        public Boolean checkLotIsBatchMonitorLot(Long lotRrn) {
            return diffBatchQueryDAO.checkLotIsBatchMonitorLot(lotRrn);
        }

        @Override
        public Boolean checkLotIsBatchMonitorLot(Long lotRrn, Equipment equipment) {
            Boolean isMonitorLot = checkLotIsBatchMonitorLot(lotRrn);

            if (isMonitorLot) {
                if (lotAutoMonitorInqManager.checkLotIsAutoMonitorLot(lotRrn)) {
                    return checkLotCanDiffBatchRun(lotRrn, equipment);
                }
                return Boolean.TRUE;
            }

            return Boolean.FALSE;
        }

        @Override
        public String getLotBatchId(Long lotRrn) {
            return diffBatchQueryDAO.getBatchIdByLotRrn(lotRrn);
        }

        @Override
        public String buildBatchId(String prefix) {
            String batchPrefix = prefix + DateUtils.getNowTime("yyyyMMdd") + ".";
            String count = (diffBatchQueryDAO.getPrefixBatchIdCount(batchPrefix) + 1) + "";
            int length = 3 - count.length();
            for (int i = 0; i < length; i++) {
                count = NumberUtils.INTEGER_ZERO.toString() + count;
            }
            return batchPrefix + count;
        }

        @Override
        public List<Map<String, Object>> getBatchLotStoreHistory(BatchLotStore storeCondition, Boolean showAll) {
            return diffBatchQueryDAO.getBatchLotStoreHistory(storeCondition, showAll);
        }

        @Override
        public List<EqpRcpDiffBatchSetupInfo> getEqpRecipeDiffBatchSet(Long eqptRrn) {
            return diffBatchQueryDAO.getEqpRecipeDiffBatchSet(eqptRrn);
        }

        @Override
        public Page getEqpRcpBatchSetupHistory(Map<String, Object> condition, Page page) {
            return diffBatchQueryDAO.getEqpRcpBatchSetupHistory(condition, page);
        }

        private Boolean checkLotCanDiffBatchRun(Long lotRrn, Equipment equipment) {
            Boolean canDiffBatchRun = true;

            List<Lot> batchLotList = getBatchLotList(lotRrn);

            if (CollectionUtils.isNotEmpty(batchLotList)) {
                for (Lot lot : batchLotList) {
                    Boolean canTrackInThisEqpt = equipmentCheckManager.checkLotCanDispatchEquipment(lot, equipment);
                    canDiffBatchRun = canDiffBatchRun ? canTrackInThisEqpt : canDiffBatchRun;
                }
            }
            return canDiffBatchRun;
        }

    }