LogEventAction.java

package com.mycim.webapp.actions.operation.run;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.context.spring.SpringContext;
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.framework.utils.lang.collections.MapUtils;
import com.mycim.server.ems.service.EmsInqService;
import com.mycim.server.ems.service.EmsReqService;
import com.mycim.server.prp.service.ProductService;
import com.mycim.utils.WipUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.alm.EenAction;
import com.mycim.valueobject.consts.EventName;
import com.mycim.valueobject.consts.LotInfoConstants;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.ems.Equipment;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.wip.Job;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.RootForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author Johnson.Wang
 * @version 6.0.0
 * @date 2019/10/4
 **/
public class LogEventAction extends WipSetupAction {

    protected EmsReqService emsReqService = SpringContext.getBean(EmsReqService.class);

    protected EmsInqService emsInqService = SpringContext.getBean(EmsInqService.class);

    @Override
    public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {

        RootForm theform = (RootForm) form;
        HashMap parameters = (HashMap) request.getAttribute(SessionNames.PARAMETERSINFO_KEY);
        if (Objects.isNull(parameters) && theform.getCacheParametersInfo() != null) {
            parameters = (HashMap) WebUtils.getCacheString2Obj(theform.getCacheParametersInfo());
            request.setAttribute(SessionNames.PARAMETERSINFO_KEY, parameters);
        }

        Assert.state(parameters != null, Errors.create().content("Parameters are not enough!").build());

        Long entityRrn = MapUtils.getLongValue(parameters,"eqptRrn");
        String eventId = MapUtils.getString(parameters,"eventId");
        HashMap maph = new HashMap();
        long jobRrn = MapUtils.getLongValue(parameters,"jobRrn");
        Long runRrn = new Long(0);
        Job job = new Job();
        job = wipQueryService.getJob(jobRrn);
        runRrn = job.getRunRrn();
        maph.put("jobRrn", jobRrn);
        maph.put("runRrn", runRrn);
        Long eventRrn = getInstanceRrn(eventId, LocalContext.getFacilityRrn(), ObjectList.EVENT_KEY);

        // to do: if such event does not exist, throw error invalid event id;
        Assert.state(entityRrn != null && eventRrn != null,
                     Errors.create().key(MessageIdList.SYSTEM_MISSING_PARAMETER).content("Parameters not enough!")
                           .build());

        // diff机台, dummy lot 需要跳过logevent 。
        Map<String, Object> equipmentExtInfo = emsService.getEquipmentExtMap(entityRrn);
        if (BooleanUtils.toBoolean(MapUtils.getIntValue(equipmentExtInfo, "diffFlag"))) {
            List<Lot> jobLotList = lotInqService.getLotListByJobRrn(jobRrn);
            Boolean needSkip = true;
            for (Lot lot : jobLotList) {
                if (!LotInfoConstants.isCategoryC(lot.getCreateCategory())) {
                    needSkip = false;
                }
            }
            if (needSkip) {
                parameters.put(SessionNames.RUNSTEP_FLAG, "0");
                request.getRequestDispatcher(mapping.findForward("workflow").getPath()).forward(request, response);
                return null;
            }
        }

        if (entityRrn != 0) {
            String entityStatus = emsService.getEntityCurrentStatus(entityRrn);
            if (wipCheckService.isSkipLogEvent(jobRrn, entityRrn, entityStatus)) {
                parameters.put(SessionNames.RUNSTEP_FLAG, "0");
                return mapping.findForward("workflow");
            }

            logEvent(eventId, entityRrn, jobRrn, maph);
        }

        parameters.put(SessionNames.RUNSTEP_FLAG, "0");
        request.getRequestDispatcher(mapping.findForward("workflow").getPath()).forward(request, response);
        return null;
    }

    public void logEvent(String eventId, Long entityRrn, Long jobRrn, HashMap maph) throws Exception {
        Long eventRrn = getInstanceRrn(eventId, LocalContext.getFacilityRrn(), ObjectList.EVENT_KEY);

        String eqptId = emsInqService.getEquipmentId(entityRrn);

        Equipment equipment = emsInqService.getEquipment(eqptId);
        if (Objects.isNull(equipment)) {
            return;
        }
        if (EventName.IDLE_TO_RUN.equalsIgnoreCase(eventId)) {

            String recipeId = StringUtils.EMPTY;
            List<Lot> jobLotList = lotInqService.getLotListByJobRrn(jobRrn);
            if (CollectionUtils.isNotEmpty(jobLotList)) {
                Lot lot = jobLotList.iterator().next();
                recipeId = lot.getRecipePhysicalId();
            }

            emsReqService.logEventEquipmentByMoveIn(equipment, EventName.RUN, recipeId);
        } else if (EventName.RUN_TO_IDLE.equalsIgnoreCase(eventId)) {
            emsReqService.logEventEquipmentByMoveOut(equipment, jobRrn);
        } else {
            emsService.logEvent(entityRrn, eventRrn, LocalContext.getUserId(), maph);
        }
    }

    public Long checkEventAndEntity(Long entityRrn, String eventId) {
        Long eventRrn = getInstanceRrn(eventId, LocalContext.getFacilityRrn(), ObjectList.EVENT_KEY);
        Assert.isFalse(entityRrn == null || eventRrn == null,
                       Errors.create().content("Required parameters do not exists!").build());
        return eventRrn;
    }

}