AutoMonitorAction.java

package com.mycim.webapp.actions;

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.beans.BeanUtils;
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.lang.math.NumberUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.framework.utils.msg.JsonUtils;
import com.mycim.server.automonitor.service.*;
import com.mycim.server.carrier.service.CarrierService;
import com.mycim.server.ctx.exec.service.CtxExecService;
import com.mycim.server.ems.service.EmsService;
import com.mycim.server.npw.service.EqpMonitorLotService;
import com.mycim.server.npw.service.EqpMonitorService;
import com.mycim.server.prp.service.PrpService;
import com.mycim.server.rcp.service.RecipeService;
import com.mycim.server.reticle.service.ReticleService;
import com.mycim.server.sorter.service.SorterQueryService;
import com.mycim.server.sorter.service.SorterService;
import com.mycim.server.spc.service.SpcService;
import com.mycim.server.wip.service.*;
import com.mycim.utils.WipUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.automonitor.dto.LotAutoMonitorQueryDTO;
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.EventName;
import com.mycim.valueobject.consts.ReferenceDetailNames;
import com.mycim.valueobject.consts.ReferenceFileConst;
import com.mycim.valueobject.ems.Equipment;
import com.mycim.valueobject.prp.Operation;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.Unit;
import com.mycim.webapp.forms.LotInfoFormAutoMonitor;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author finatice.yang
 * @date 2021/8/31
 **/
public abstract class AutoMonitorAction extends AbstractAction {

    protected LotInqService lotInqService = SpringContext.getBean(LotInqService.class);

    protected WipService wipService = SpringContext.getBean(WipService.class);

    protected EmsService emsService = SpringContext.getBean(EmsService.class);

    protected LotQueryService lotQueryService = SpringContext.getBean(LotQueryService.class);

    protected WipQueryService wipQueryService = SpringContext.getBean(WipQueryService.class);

    protected SpcService spcService = SpringContext.getBean(SpcService.class);

    protected PrpService prpService = SpringContext.getBean(PrpService.class);

    protected CarrierService carrierService = SpringContext.getBean(CarrierService.class);

    protected WipCheckService wipCheckService = SpringContext.getBean(WipCheckService.class);

    protected ReticleService reticleService = SpringContext.getBean(ReticleService.class);

    protected LotService lotService = SpringContext.getBean(LotService.class);

    protected RecipeService recipeService = SpringContext.getBean(RecipeService.class);

    protected LotAutoMonitorInqService lotAutoMonitorInqService = SpringContext.getBean(LotAutoMonitorInqService.class);

    protected LotAutoMonitorReqService lotAutoMonitorReqService = SpringContext.getBean(LotAutoMonitorReqService.class);

    protected AutoMonitorItemInqService autoMonitorItemInqService = SpringContext
            .getBean(AutoMonitorItemInqService.class);

    protected AutoMonitorItemReqService autoMonitorItemReqService = SpringContext
            .getBean(AutoMonitorItemReqService.class);

    protected SorterService sorterService = SpringContext.getBean(SorterService.class);

    protected SorterQueryService sorterQueryService = SpringContext.getBean(SorterQueryService.class);

    protected DiffBatchQueryService diffBatchQueryService = SpringContext.getBean(DiffBatchQueryService.class);

    public String initEmptyCarrier() {
        List<Map<String, Object>> emptyUnits = new ArrayList<>();
        // 默认槽位25片
        int number = 25;
        for (int i = 1; i <= number; i++) {
            Map<String, Object> unitMap = new HashMap<>();
            unitMap.put("position", i + "");
            unitMap.put("unitId", "");
            unitMap.put("unitRrn", "");
            unitMap.put("lotId", "");
            emptyUnits.add(unitMap);
        }
        return JsonUtils.toString(emptyUnits);
    }

    public String getChildLotId(String lotId, Long facilityRrn) {
        String childLotId = "";
        if (lotId.indexOf(".") == -1) {
            lotId = lotId + ".1";
        }
        lotId = lotQueryService.getMaxLotId(lotId.substring(0, lotId.indexOf(".") + 1));
        if (lotId.indexOf(".") == -1) {
            lotId = lotId + ".0";
        }

        String lotNumber = lotId.substring(lotId.indexOf(".") + 1);
        if (StringUtils.isNotBlank(lotNumber)) {
            lotNumber = String.valueOf(Integer.parseInt(lotNumber) + 1);
        } else {
            lotNumber = "1";
        }

        childLotId = lotId.substring(0, lotId.indexOf(".") + 1) + lotNumber;
        // holly modify 20080715 初级排除Carrier与max存在的情况.
        if (this.getInstanceRrn(childLotId, this.getNamedSpace(ObjectList.ENTITY_KEY, facilityRrn),
                                ObjectList.ENTITY_KEY, ObjectList.CARRIER_KEY) > 0) {
            childLotId = lotId.substring(0, lotId.indexOf(".") + 1) + String.valueOf(Integer.parseInt(lotNumber) + 1);
        }
        // MEMS要求个位数, 加0, 类似AAAA.01, 而非AAAA.1
        String newChildLotNumber = childLotId.substring(childLotId.indexOf(".") + 1);
        if (newChildLotNumber.length() == 1) {
            newChildLotNumber = "0" + newChildLotNumber;
        }
        childLotId = childLotId.substring(0, lotId.indexOf(".") + 1) + newChildLotNumber;

        return childLotId;
    }

    protected String getHotflagSplicingPriority(Lot lot) {
        return wipQueryService.getHotflagSplicingPriority(NumberUtils.toInt(lot.getHotFlag()), lot.getPriority(),
                                                          LocalContext.getFacilityRrn());
    }

    protected String parseToJsonString(List<Map> unitList) {
        List<Map> newUnits = new ArrayList<Map>(unitList);
        Collections.sort(newUnits, new Comparator<Map>() {

            // 将wafer重新排序
            @Override
            public int compare(Map map1, Map map2) {
                int position1 = MapUtils.getIntValue(map1, "position");
                int position2 = MapUtils.getIntValue(map2, "position");
                if (position1 > position2) {
                    return 1;
                } else if (position1 < position2) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        Iterator it = newUnits.iterator();
        Map unit = new HashMap();
        int number = 1;// 指wafer位置从1开始
        int seq = 1;
        List<Map> units = new ArrayList<Map>();
        while (it.hasNext()) {
            Map<String, Object> unitMap = null;
            unit = (HashMap) it.next();
            seq = MapUtils.getIntValue(unit, "position");
            while ((number < seq)) {
                unitMap = new HashMap();
                unitMap.put("position", number + "");
                unitMap.put("unitId", "");
                unitMap.put("unitRrn", "");
                unitMap.put("lotId", "");
                unitMap.put("chooseFlag", "");
                unitMap.put("markNumber", "");
                unitMap.put("markNumberComment", "");
                units.add(unitMap);
                number++;
            }
            unitMap = new HashMap();
            unitMap.put("position", number + "");
            unitMap.put("unitId", unit.get("unitId"));
            unitMap.put("unitRrn", unit.get("unitRrn"));
            unitMap.put("lotId", unit.get("lotId"));
            unitMap.put("chooseFlag", unit.get("chooseFlag"));
            unitMap.put("markNumber", unit.get("markNumber"));
            unitMap.put("markNumberComment", unit.get("markNumberComment"));
            units.add(unitMap);
            number++;
        }
        int listSize = units.size();// 已有
        for (int i = listSize + 1; i <= 25; i++) {
            Map<String, Object> unitMap = new HashMap();
            unitMap.put("position", i + "");
            unitMap.put("unitId", "");
            unitMap.put("unitRrn", "");
            unitMap.put("lotId", "");
            unitMap.put("chooseFlag", "");
            unitMap.put("markNumber", "");
            unitMap.put("markNumberComment", "");
            units.add(unitMap);
        }
        return JsonUtils.toString(units);
    }

    protected List<Unit> getUnitList(Long lotRrn) {
        List<Unit> unitList = wipQueryService.getUnitList(lotRrn);
        unitList.sort(new Comparator<Unit>() {
            @Override
            public int compare(Unit o1, Unit o2) {
                return o1.getPositionInCarrier().intValue() - o2.getPositionInCarrier().intValue();
            }
        });
        return unitList;
    }

    protected Map<String, Object> builidlotMapInfo(Lot lot, LotAutoMonitorInfo monitorInfo) {
        Map<String, Object> lotMapInfo = BeanUtils.copyBeanToMap(lot);
        String lotCategoryStr = sysService
                .getRefFileValue(ReferenceDetailNames.LOT_CREATE_CATEGORY, lot.getCreateCategory(),
                                 ReferenceFileConst.DATA_1_VALUE);
        lotMapInfo.put("lotCategoryStr", lotCategoryStr);
        String hotFlagString = "";
        if (StringUtils.isNotBlank(lot.getHotFlag()) && StringUtils.isNotBlank(lot.getPriority().toString())) {
            hotFlagString = getHotflagSplicingPriority(lot);
        }
        lotMapInfo.put("hotFlagString", hotFlagString);
        lotMapInfo.put("flowSeq", monitorInfo.getFlowSeq());
        lotMapInfo.put("operationDesc", monitorInfo.getOpeartionDesc());
        lotMapInfo.put("recipeId", monitorInfo.getRecipeId());
        lotMapInfo.put("lotStatus", monitorInfo.getLotStatus());
        lotMapInfo.put("workArea", monitorInfo.getEqptLocation());
        lotMapInfo.put("qty1", monitorInfo.getQty1().intValue());
        String consumedMaterial = wipQueryService.getConsumedMaterial(lot.getLotRrn());
        lotMapInfo.put("consumedMaterial", StringUtils.trimToEmpty(consumedMaterial));
        lotMapInfo.put("lotComments", getLotComments(lot));
        SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_FORMAT4DAYD);
        String duedate = df.format(lot.getDueDate());
        lotMapInfo.put("duedate", duedate);
        lotMapInfo.put("lotPurpose", lot.getLotComments());
        lotMapInfo.put("stageId", monitorInfo.getStageId());
        if(LotStatus.isAutoMonitorMerge(lot.getLotStatus())) {
            lotMapInfo.put("flowSeq", lot.getFlowSeq());
            lotMapInfo.put("stageId", lot.getStageId());
        }
        lotMapInfo.put("workflowRrn",monitorInfo.getWorkflowRrn());
        lotMapInfo.put("workflowVersion",monitorInfo.getWorkflowVersion());
        lotMapInfo.put("itemType",monitorInfo.getItemType());
        return lotMapInfo;
    }

    private String getLotComments(Lot lot) {
        List commentList = lotQueryService
                .qryLotStepHistoryComment(lot.getLotRrn(), lot.getStepSequence(), NumberUtils.INTEGER_ONE,
                                          Integer.MAX_VALUE);
        Iterator it = commentList.iterator();
        StringBuffer commentStr = new StringBuffer();
        while (it.hasNext()) {
            Map m = (HashMap) it.next();
            commentStr.append(MapUtils.getString(m, "commentSequence")).append(".")
                      .append(MapUtils.getString(m, "stepComment")).append(";");
        }

        return commentStr.toString();
    }

    public Map checkAllChildLotFinish(Map params) {
        Long baseLotRrn = MapUtils.getLong(params, "baseLotRrn");

        LotAutoMonitorQueryDTO queryDTO = new LotAutoMonitorQueryDTO();
        queryDTO.setBaseLotRrn(baseLotRrn);
        List<LotAutoMonitorInfo> monitorLotInfos = lotAutoMonitorInqService.getLotMonitorLotList(queryDTO);

        StringBuilder result = new StringBuilder();
        for (LotAutoMonitorInfo lotMonitorInfo : monitorLotInfos) {
            String childLotStatus = lotQueryService.getLotStatus(lotMonitorInfo.getLotRrn());
            if (!StringUtils.equals(LotStatus.AUTO_MONITOR_JOB_FINISH, childLotStatus)) {
                result.append(lotMonitorInfo.getLotId() + StringUtils.WRITE_SPACE);
            }
        }

        if (StringUtils.isNotEmpty(result)) {
            result.append(" not finish.");
        }

        Map resultMap = MapUtils.newHashMap();
        resultMap.put("msg", result);
        return resultMap;
    }

}