# AutoMonitorLogEventTaskManagerImpl.java

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

    import com.fa.sesa.exception.Assert;
    import com.fa.sesa.exception.Errors;
    import com.fa.sesa.threadlocal.LocalContext;
    import com.fa.sesa.threadlocal.LocalContextNames;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.framework.utils.msg.JsonUtils;
    import com.mycim.server.automonitor.manager.LotAutoMonitorInqManager;
    import com.mycim.server.automonitor.manager.job.task.AutoMonitorLogEventTaskManager;
    import com.mycim.server.base.manager.EventManager;
    import com.mycim.server.ems.manager.EntityManager;
    import com.mycim.server.ems.manager.EquipmentInqManager;
    import com.mycim.server.ems.manager.EquipmentManager;
    import com.mycim.server.ems.manager.LogEventReqManager;
    import com.mycim.server.rcp.manager.RecipeManager;
    import com.mycim.server.system.manager.ReferenceFileManager;
    import com.mycim.server.wip.manager.DiffBatchQueryManager;
    import com.mycim.server.wip.manager.JobManager;
    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.server.wip.manager.job.RecipeCheckManager;
    import com.mycim.utils.WipUtils;
    import com.mycim.valueobject.MessageIdList;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.automonitor.dto.DispatchLotTransDataDTO;
    import com.mycim.valueobject.automonitor.entity.LotAutoMonitorInfo;
    import com.mycim.valueobject.automonitor.util.AutoMonitorOperationConstants;
    import com.mycim.valueobject.automonitor.util.AutoMonitorUtils;
    import com.mycim.valueobject.consts.*;
    import com.mycim.valueobject.ems.Entity;
    import com.mycim.valueobject.ems.Equipment;
    import com.mycim.valueobject.ems.Event;
    import com.mycim.valueobject.prp.Recipe;
    import com.mycim.valueobject.security.User;
    import com.mycim.valueobject.sys.ReferenceFileDetail;
    import com.mycim.valueobject.wip.Lot;
    import com.mycim.valueobject.wip.LotStatus;
    import com.mycim.valueobject.wip.dto.LotInfoDto;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.*;

    /**
     * @author finatice.yang
     * @date 2021/9/9
     **/
    @Service
    @Transactional
    public class AutoMonitorLogEventTaskManagerImpl implements AutoMonitorLogEventTaskManager {

        @Autowired
        LotInqManager lotInqManager;

        @Autowired
        EntityManager entityManager;

        @Autowired
        EquipmentManager equipmentManager;

        @Autowired
        EquipmentInqManager equipmentInqManager;

        @Autowired
        ReferenceFileManager referenceFileManager;

        @Autowired
        RecipeManager recipeManager;

        @Autowired
        RecipeCheckManager recipeCheckManager;

        @Autowired
        LotAutoMonitorInqManager lotAutoMonitorInqManager;

        @Autowired
        JobManager jobManager;

        @Autowired
        EventManager eventManager;

        @Autowired
        DiffBatchQueryManager diffBatchQueryManager;

        @Autowired
        LogEventReqManager logEventReqManager;

        @Override
        public Map doExecute(Map parameters) {

            Long entityRrn = MapUtils.getLongValue(parameters, "eqptRrn");

            List<LotInfoDto> lotInfoDtos = (List<LotInfoDto>) MapUtils.getObject(parameters, "transData");
            Assert.state(CollectionUtils.isNotEmpty(lotInfoDtos),
                         Errors.create().content("Parameters are not enough!").build());

            LotInfoDto lotInfoDto = lotInfoDtos.iterator().next();

            Lot lot = lotInqManager.getLot(lotInfoDto.getLotBaseInfo().getLotId());

            String trackFlag = lotInfoDto.getLotBaseInfo().getTrackFlag();

            String recipeId = lotInfoDto.getLotBaseInfo().getPpid();
            if (StringUtils.isBlank(recipeId)) {
                recipeId = lot.getRecipePhysicalId();
            }

            Equipment equipment = equipmentManager.getEquipment(entityRrn);

            logEventEqpt(equipment, lot, trackFlag, recipeId);

            parameters.put(SessionNames.RUNSTEP_FLAG, "0");
            return parameters;
        }

        private void logEventEqpt(Equipment equipment, Lot lot, String trackFlag, String recipeId) {
            Boolean isChamberEqpt = equipmentInqManager.checkIsChamberEquipment(equipment);
            if (StringUtils.equalsIgnoreCase(ActionPointList.MOVEIN_KEY, trackFlag)) {
                String targetEventId = EquipmentStatus.RUN;

                LotAutoMonitorInfo monitorInfo = lotAutoMonitorInqManager.getLotAutoMonitorInfo(lot.getLotRrn());
                if (monitorInfo != null) {
                    targetEventId = AutoMonitorUtils.checkStepIsMainProcessStep(
                            monitorInfo.getEqptType()) ? EquipmentStatus.MON_R : EquipmentStatus.RUN;
                }

                Boolean specialFlag = checkSpecialFlag(equipment, lot, trackFlag);

                if (Boolean.FALSE.equals(isChamberEqpt) && Boolean.FALSE.equals(specialFlag)) {
                    logEventReqManager.logEventEquipmentByMoveIn(equipment, targetEventId);
                }

                if (Boolean.TRUE.equals(isChamberEqpt)) {
                    logEventReqManager.logEventChamberByMoveIn(equipment, targetEventId, recipeId);
                }
            } else {
                logEventReqManager.logEventEquipmentByMoveOut(equipment, lot.getJobRrn());
            }
        }

        private Boolean checkSpecialFlag(Equipment equipment, Lot lot, String trackFlag) {
            if (StringUtils.equalsIgnoreCase(ActionPointList.MOVEIN_KEY, trackFlag)) {

                List<ReferenceFileDetail> detailList = referenceFileManager
                        .getRefFileValues(ReferenceDetailNames.AUTO_MONITOR_SPECIAL_EQPT, LocalContext.getFacilityRrn());

                for (ReferenceFileDetail detail : detailList) {
                    if (StringUtils.equals(detail.getData1Value(), equipment.getInstanceId())) {
                        return true;
                    }
                }
            } else {
                if (StringUtils.isNotBlank(lot.getBatchId())) {
                    return true;
                }
            }
            return false;
        }

    }