# LotAutoMonitorInqManagerImpl.java

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

    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.server.automonitor.dao.LotAutoMonitorInqDao;
    import com.mycim.server.automonitor.manager.AutoMonitorItemInqManager;
    import com.mycim.server.automonitor.manager.LotAutoMonitorInqManager;
    import com.mycim.server.ems.manager.EquipmentManager;
    import com.mycim.valueobject.automonitor.dto.LotAutoMonitorQueryDTO;
    import com.mycim.valueobject.automonitor.dto.MonitorJobQueryDTO;
    import com.mycim.valueobject.automonitor.entity.*;
    import com.mycim.valueobject.automonitor.util.AutoMonitorOperationConstants;
    import com.mycim.valueobject.automonitor.util.AutoMonitorUtils;
    import com.mycim.valueobject.ems.Equipment;
    import com.mycim.valueobject.wip.EquipmentRunHistory;
    import com.mycim.valueobject.wip.Lot;
    import com.mycim.valueobject.wip.LotStatus;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.List;
    import java.util.Objects;

    /**
     * @author finatice.yang
     * @date 2021/8/31
     **/
    @Service
    public class LotAutoMonitorInqManagerImpl implements LotAutoMonitorInqManager {

        @Autowired
        LotAutoMonitorInqDao lotAutoMonitorInqDao;

        @Autowired
        AutoMonitorItemInqManager itemInqManager;

        @Autowired
        EquipmentManager equipmentManager;

        @Override
        public Lot fillLotAutoMonitorSpec(Lot lot) {
            LotMonitorJobStore lotStore =  getLotMonitorJobStore(lot.getLotRrn());

            if(Objects.isNull(lotStore)) {
                return lot;
            }

            Long workflowRrn = lotStore.getWorkflowRrn();
            Integer verison = lotStore.getWorkflowVersion();
            Long operationRrn = lot.getOperationRrn();

            Long equipmentRrn = lotStore.getEqptRrn();
            Equipment equipment = equipmentManager.getEquipment(equipmentRrn);

            AutoMonitorItemStep stepInfo = itemInqManager.getAutoMonitorItemStep(workflowRrn, verison, operationRrn);
            if (Objects.nonNull(stepInfo)) {
                lot.setStepType(stepInfo.getEqptType());
                lot.setOperationDesc(stepInfo.getOperationDesc());
                lot.setWorkArea(equipment.getEquipmentLocation());

                lot.setRecipeId(stepInfo.getRecipeId());

                if (!LotStatus.isAutoMonitorMerge(lot.getLotStatus())) {
                    lot.setFlowSeq(stepInfo.getFlowSeq());
                    lot.setStageId(stepInfo.getStageId());
                }
            }

            return lot;
        }

        @Override
        public boolean checkLotIsAutoMonitorLot(Long lotRrn) {
            return lotAutoMonitorInqDao.checkLotIsAutoMonitorLot(lotRrn);
        }

        @Override
        public boolean checkLotInAutoMonitorBaseLot(Long lotRrn) {
            return lotAutoMonitorInqDao.checkLotInAutoMonitorBaseLot(lotRrn);
        }

        @Override
        public List<MonitorCarrierMapping> getMonitorCarrierMappings(Long monitorCarrierRrn) {
            return lotAutoMonitorInqDao.getMonitorCarrierMappings(monitorCarrierRrn);
        }

        @Override
        public LotMonitorJobStore getLotMonitorJobStore(Long lotRrn) {
            return lotAutoMonitorInqDao.getLotMonitorJobStore(lotRrn);
        }

        @Override
        public Page getMonitorJobListPage(Page page, MonitorJobQueryDTO queryDTO) {
            return lotAutoMonitorInqDao.getMonitorJobListPage(page, queryDTO);
        }

        @Override
        public LotAutoMonitorInfo getLotActiveAutoMonitorInfo(Long lotRrn) {
            return lotAutoMonitorInqDao.getLotActiveAutoMonitorInfo(lotRrn);
        }

        @Override
        public LotAutoMonitorInfo getLotAutoMonitorInfo(Long lotRrn) {
            return lotAutoMonitorInqDao.getLotAutoMonitorInfo(lotRrn);
        }

        @Override
        public Page getLotMonitorLotPage(Page page, LotAutoMonitorQueryDTO queryDTO) {
            return lotAutoMonitorInqDao.getLotMonitorLotPage(page, queryDTO);
        }

        @Override
        public List<LotAutoMonitorInfo> getLotMonitorLotList(LotAutoMonitorQueryDTO queryDTO) {
            return lotAutoMonitorInqDao.getLotMonitorLotList(queryDTO);
        }

        @Override
        public boolean checkAutoMonitorLotCanCreateSortJob(LotAutoMonitorInfo lotAutoMonitorInfo) {
            if (Objects.isNull(lotAutoMonitorInfo)) {
                return false;
            }
            if(LotStatus.isAutoMonitorMerge(lotAutoMonitorInfo.getLotStatus())) {
                return false;
            }
            if (Objects.nonNull(lotAutoMonitorInfo.getMonitorCarrierRrn()) &&
                    lotAutoMonitorInfo.getCarrierRrn().longValue()==lotAutoMonitorInfo.getMonitorCarrierRrn().longValue()) {
                return false;
            }
            if (lotAutoMonitorInfo.getStartSplitFlag() &&
                    lotAutoMonitorInfo.getWorkflowStepSequence().longValue() == NumberUtils.LONG_ONE.longValue()) {
                return true;
            } else if (!lotAutoMonitorInfo.getStartSplitFlag() &&
                    StringUtils.equals(lotAutoMonitorInfo.getEqptType(), AutoMonitorOperationConstants.STEP_TYPE_MAIN) &&
                    lotAutoMonitorInfo.getMonitorCarrierRrn() != null &&
                    lotAutoMonitorInfo.getMonitorCarrierRrn().longValue() > 0) {
                return true;
            }
            return false;
        }

        @Override
        public List<LotMonitorJobStepEdcInfo> getStepEdcInfoList(Long transRrn) {
            return lotAutoMonitorInqDao.getStepEdcInfoList(transRrn);
        }

        @Override
        public Boolean checkAutomonitorLotNeedSkipEdc(Long lotRrn, Long stepSequence) {
            Integer count = lotAutoMonitorInqDao.getLotStepEdcCount(lotRrn, stepSequence);
            if (Objects.nonNull(count) && count.intValue() > 0) {
                return Boolean.TRUE;
            }

            return Boolean.FALSE;
        }

        @Override
        public List<String> getFinishLotIdList(Long baseLotRrn) {
            return lotAutoMonitorInqDao.getFinishLotIdList(baseLotRrn);
        }

        @Override
        public List<AutoMonitorUnitInfo> getAutoMonitorUnitInfoList(Long lotRrn) {
            return lotAutoMonitorInqDao.getAutoMonitorUnitInfoList(lotRrn);
        }

        @Override
        public Page getAutoMonitorJobHistoryPage(Page page, MonitorJobQueryDTO queryDTO) {
            return lotAutoMonitorInqDao.getAutoMonitorJobHistoryPage(page, queryDTO);
        }
    }