# PiLotInqManagerImpl.java

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

    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.utils.lang.ObjectUtils;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.server.ems.manager.EquipmentManager;
    import com.mycim.server.security.manager.UserManager;
    import com.mycim.server.wip.dao.PiLotInqDAO;
    import com.mycim.server.wip.manager.PiLotInqManager;
    import com.mycim.valueobject.consts.PiLotStatusEnum;
    import com.mycim.valueobject.consts.PiLotTypeEnum;
    import com.mycim.valueobject.ems.Equipment;
    import com.mycim.valueobject.ems.pilot.PiLotRcpGrp;
    import com.mycim.valueobject.ems.pilot.PiLotSetup;
    import com.mycim.valueobject.ems.pilot.PiLotView;
    import com.mycim.valueobject.ems.pilot.dto.PiLotRcpGrpQueryDTO;
    import com.mycim.valueobject.ems.pilot.dto.PiLotSetupQueryDTO;
    import com.mycim.valueobject.ems.pilot.dto.PiLotViewQueryDTO;
    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.Objects;
    import java.util.stream.Collectors;

    /**
     * @author songpy
     * @version 1.0.0
     * @date 2021/8/18
     **/
    @Service
    @Transactional
    public class PiLotInqManagerImpl implements PiLotInqManager {
        @Autowired
        PiLotInqDAO queryDAO;

        @Autowired
        UserManager userManager;

        @Autowired
        EquipmentManager equipmentManager;

        @Override
        public Page getPiLotSetups(PiLotSetupQueryDTO queryDTO) {
            Page page = new Page(queryDTO.getPage(), queryDTO.getLimit());
            page = queryDAO.getPiLotSetups(page, queryDTO);
            if(CollectionUtils.isNotEmpty(page.getResults())){
                page.getResults().forEach(pilotSetup -> buildPiLotSetupDetail((PiLotSetup)pilotSetup));
            }
            return page;
        }

        @Override
        public PiLotSetup getPiLotSetupByRrn(Long piLotSetupRrn) {
            PiLotSetupQueryDTO queryDTO = new PiLotSetupQueryDTO();
            queryDTO.setPiLotRrn(piLotSetupRrn);
            return buildPiLotSetupDetail(queryDAO.getPiLotSetup(queryDTO));
        }

        @Override
        public PiLotSetup getPiLotSetupIdleByEquipment(String eqptId, long eqptRrn) {
            PiLotSetupQueryDTO queryDTO = new PiLotSetupQueryDTO();
            queryDTO.setEqptId(eqptId);
            queryDTO.setEqptRrn(eqptRrn);
            queryDTO.setType(PiLotTypeEnum.IDLETIME.toString());
            queryDTO.setEnableFlag(PiLotStatusEnum.ON.toString());
            queryDTO.addStatus(PiLotStatusEnum.CREATED.toString());
            return buildPiLotSetupDetail(queryDAO.getPiLotSetup(queryDTO));
        }

        @Override
        public PiLotView getPiLotViewByRrn(Long viewRrn) {
            PiLotViewQueryDTO queryDTO = new PiLotViewQueryDTO();
            queryDTO.setViewRrn(viewRrn);
            return queryDAO.getPiLotView(queryDTO);
        }

        @Override
        public Page getPiLotViews(PiLotViewQueryDTO queryDTO) {
            Page page = new Page(queryDTO.getPage(), queryDTO.getLimit());
            return queryDAO.getPiLotViews(page, queryDTO);
        }

        @Override
        public Integer getCountPiLotViewBySetupRrn(Long setupRrn) {
            return queryDAO.getCountPiLotViewBySetupRrn(setupRrn);
        }

        @Override
        public List<PiLotView> getSplitBeforePiLotViewByEqpt(String eqptId, long eqptRrn) {
            PiLotViewQueryDTO queryDTO = new PiLotViewQueryDTO();
            queryDTO.setEqptId(eqptId);
            queryDTO.setEqptRrn(eqptRrn);
            queryDTO.addStatus(PiLotStatusEnum.WAITSPLIT.toString())
                    .addStatus(PiLotStatusEnum.WAITMERGE.toString());
            return queryDAO.getPiLotViews(queryDTO);
        }

        @Override
        public PiLotView getSplitAfterPiLotViewByEqpt(String eqptId, long eqptRrn) {
            PiLotViewQueryDTO queryDTO = new PiLotViewQueryDTO();
            queryDTO.setEqptId(eqptId);
            queryDTO.setEqptRrn(eqptRrn);
            queryDTO.addStatus(PiLotStatusEnum.WAITMERGE.toString());
            return queryDAO.getPiLotView(queryDTO);
        }

        @Override
        public PiLotView getPiLotViewByEqpt(String eqptId, long eqptRrn) {
            PiLotViewQueryDTO queryDTO = new PiLotViewQueryDTO();
            queryDTO.setEqptId(eqptId);
            queryDTO.setEqptRrn(eqptRrn);
            queryDTO.addStatus(PiLotStatusEnum.ONGOING.toString()).addStatus(PiLotStatusEnum.WAITSPLIT.toString())
                    .addStatus(PiLotStatusEnum.WAITMERGE.toString()).addStatus(PiLotStatusEnum.WAITCOMPLETE.toString());
            return queryDAO.getPiLotView(queryDTO);
        }

        @Override
        public PiLotView getSingleLotPiLotViewByEqpt(String eqptId, long eqptRrn) {
            PiLotViewQueryDTO queryDTO = new PiLotViewQueryDTO();
            queryDTO.setEqptId(eqptId);
            queryDTO.setEqptRrn(eqptRrn);
            queryDTO.addStatus(PiLotStatusEnum.WAITCOMPLETE.toString());
            return queryDAO.getPiLotView(queryDTO);
        }

        @Override
        public String getPiLotViewIdByLotRrn(Long basedLotRrn) {
            PiLotView view = getPiLotViewByLotRrn(basedLotRrn);
            if (Objects.nonNull(view)) {
                return view.getViewId();
            } else {
                return StringUtils.EMPTY;
            }
        }

        @Override
        public String getPiLotViewIdByChildLotRrn(Long lotRrn) {
            PiLotView view = getPiLotViewByChildLotRrn(lotRrn);
            if (Objects.nonNull(view)) {
                return view.getViewId();
            } else {
                return StringUtils.EMPTY;
            }
        }

        @Override
        public Page getPiLotRcpGrps(PiLotRcpGrpQueryDTO queryDTO) {
            Page page = new Page(queryDTO.getPage(), queryDTO.getLimit());
            return queryDAO.getPiLotRcpGrps(page, queryDTO);
        }

        @Override
        public PiLotRcpGrp getPiLotRcpGrpByRrn(Long rcpGrpRrn) {
            PiLotRcpGrpQueryDTO queryDTO = new PiLotRcpGrpQueryDTO();
            queryDTO.setRcpGrpRrn(rcpGrpRrn);
            return queryDAO.getPiLotRcpGrp(queryDTO);
        }

        @Override
        public Page getPiLotRcps(PiLotRcpGrpQueryDTO queryDTO) {
            Page page = new Page(queryDTO.getPage(), queryDTO.getLimit());
            return queryDAO.getPiLotRcps(page, queryDTO);
        }

        @Override
        public PiLotSetup getPiLotSetup(PiLotSetupQueryDTO queryDTO) {
            return buildPiLotSetupDetail(queryDAO.getPiLotSetup(queryDTO));
        }

        @Override
        public PiLotView getPiLotViewByLotRrn(Long lotRrn) {
            PiLotViewQueryDTO queryDTO = new PiLotViewQueryDTO();
            queryDTO.setLotRrn(lotRrn);
            queryDTO.addStatus(PiLotStatusEnum.WAITSPLIT.toString())
                    .addStatus(PiLotStatusEnum.WAITMERGE.toString())
                    .addStatus(PiLotStatusEnum.ONGOING.toString())
                    .addStatus(PiLotStatusEnum.WAITCOMPLETE.toString());
            return queryDAO.getPiLotView(queryDTO);
        }

        @Override
        public Page querySetupHistorys(PiLotSetupQueryDTO queryDTO) {
            Page page = new Page(queryDTO.getPage(), queryDTO.getLimit());
            return queryDAO.querySetupHistorys(page, queryDTO);
        }

        @Override
        public Page queryViewHistorys(PiLotViewQueryDTO queryDTO) {
            Page page = new Page(queryDTO.getPage(), queryDTO.getLimit());
            return queryDAO.queryViewHistorys(page, queryDTO);
        }

        @Override
        public PiLotSetup getPiLotSetupByTypeAndEqptId(String type, String eqptId) {
            PiLotSetupQueryDTO queryDTO = new PiLotSetupQueryDTO();
            queryDTO.setType(type);
            queryDTO.setEqptId(eqptId);
            return queryDAO.getPiLotSetup(queryDTO);
        }

        @Override
        public PiLotView getPiLotViewByChildLotRrn(long lotRrn) {
            PiLotViewQueryDTO queryDTO = new PiLotViewQueryDTO();
            queryDTO.setChildLotRrn(lotRrn);
            queryDTO.addStatus(PiLotStatusEnum.WAITMERGE.toString());
            return queryDAO.getPiLotView(queryDTO);
        }

        @Override
        public PiLotSetup getBasePilotSetup(Long piLotSetupRrn) {
            PiLotSetup piLotSetup = getPiLotSetupByRrn(piLotSetupRrn);
            Equipment equipment = equipmentManager.getEquipment(piLotSetup.getEqptRrn());
            if (ObjectUtils.isEmpty(equipment.getParentEntityRrn())){
                piLotSetup.setEqptRrn(equipment.getInstanceRrn());
            }else {
                piLotSetup.setEqptRrn(equipment.getParentEntityRrn());
                Equipment parentEquipment = equipmentManager.getEquipment(equipment.getParentEntityRrn());
                piLotSetup.setChamberMode(parentEquipment.getChamberMode());
            }
            return piLotSetup;
        }

        private PiLotSetup buildPiLotSetupDetail(PiLotSetup piLotSetup) {
            if (Objects.isNull(piLotSetup)) {
                return piLotSetup;
            }

            switch (PiLotTypeEnum.valueOf(piLotSetup.getType())) {
                case IDLETIME:
                    piLotSetup.setIdle(queryDAO.getPiLotSetupIdle(piLotSetup.getPiLotRrn()));
                    break;
                case PM:
                    piLotSetup.setPm(queryDAO.getPiLotSetupPM(piLotSetup.getPiLotRrn()));
                    break;
                case COUNT:
                    piLotSetup.setCount(queryDAO.getPiLotSetupCount(piLotSetup.getPiLotRrn()));
                    break;
                case RECIPEGROUPCHANGE:
                    piLotSetup.setRcpGrp(queryDAO.getPiLotSetupRcpGrp(piLotSetup.getPiLotRrn()));
                    break;
            }
            return piLotSetup;
        }

        @Override
        public List<PiLotSetup> getPilotSetupIdle() {
            PiLotSetupQueryDTO queryDTO = new PiLotSetupQueryDTO();
            queryDTO.setType(PiLotTypeEnum.IDLETIME.toString());
            queryDTO.setEnableFlag(PiLotStatusEnum.ON.toString());
            queryDTO.addStatus(PiLotStatusEnum.CREATED.toString());
            List<PiLotSetup> pilotSetups = queryDAO.getPilotSetups(queryDTO);
            return pilotSetups.stream().map(piLotSetup -> buildPiLotSetupDetail(piLotSetup)).collect(Collectors.toList());
        }

    }