CarrierSetupAction.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.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.framework.utils.msg.JsonUtils;
import com.mycim.server.automonitor.service.LotAutoMonitorInqService;
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.sorter.service.SorterQueryService;
import com.mycim.server.status.service.StatusService;
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.SystemConstant;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.valueobject.consts.ReferenceDetailNames;
import com.mycim.valueobject.sys.ReferenceFileDetail;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.WebUtils;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CarrierSetupAction extends AbstractAction {

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

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

    protected StatusService statusService = SpringContext.getBean(StatusService.class);

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

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

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

    protected CtxExecService ctxExecService = SpringContext.getBean(CtxExecService.class);

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

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

    protected LotRunCardQueryService lotRunCardQueryService = SpringContext.getBean(LotRunCardQueryService.class);

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

    public void initStatusByEvent(Long facilityRrn, String user, String entityId, String entityType, String eventId) {
        Map commentMap = new HashMap(10);
        commentMap.put("comments", "Create " + entityType);

        long pcdRrn = baseService
                .getNamedObjectRrn(entityId, baseService.getNamedSpace(facilityRrn, ObjectList.FACILITY_KEY),
                                   ObjectList.ENTITY_KEY, entityType);
        long eventRrn = baseService
                .getNamedObjectRrn(eventId, baseService.getNamedSpace(facilityRrn, ObjectList.EVENT_KEY),
                                   ObjectList.EVENT_KEY);

        emsService.initializeEntityStatusOnly(pcdRrn, entityType, eventRrn, user, commentMap);
    }

    protected ActionForward getCommentHistory(HttpServletRequest request, ActionMapping mapping, Long facilityRrn,
                                              String user, String transAction, String actionOp) {
        String entity = request.getParameter("eqptId");
        int pageNo = WebUtils.getParameterInt("pageNo", request);
        int pageSize = WebUtils.getParameterInt("pageSize", request);

        if (StringUtils.isNotBlank(entity)) {
            String namedSpace = baseService.getNamedSpace(facilityRrn, ObjectList.ENTITY_KEY);
            NamedObject no = new NamedObject();
            no.setInstanceId(entity);
            no.setNamedSpace(namedSpace);
            no.setObject(ObjectList.ENTITY_KEY);
            Long eqptRrn = baseService.getNamedObjectRrn(no);
            if (eqptRrn > 0) {
                Map<String, Object> map = emsService.getEqptCommentHistoryByPage(eqptRrn, pageNo, pageSize);
                int totalCount = MapUtils.getIntValue(map, "totalCount");
                List<Map<String, Object>> commentList = (List<Map<String, Object>>) MapUtils
                        .getObject(map, "commentList");
                request.setAttribute("entityId", entity);
                request.setAttribute("pageNo", pageNo);
                request.setAttribute("reason", "");
                request.setAttribute("comments", commentList);
                if (commentList != null && commentList.size() > 0) {
                    request.setAttribute("totalCount", totalCount);
                } else {
                    request.setAttribute("totalCount", -1);
                }
            }
        }
        request.setAttribute("transAction", transAction);
        request.setAttribute("actionOp", actionOp);
        return mapping.findForward("eqptCommentHistory");
    }

    // protected void checkAndCreateLotsTransLock(Long userRrn, String transId, List<Lot> lots, String comments) {
    //     List<LotTransLock> locks = new ArrayList<LotTransLock>();
    //     if (lots != null && lots.size() > 0) {
    //         for (Lot lot : lots) {
    //             LotTransLock lock = new LotTransLock();
    //             lock.setLotRrn(lot.getLotRrn());
    //             lock.setUserRrn(userRrn);
    //             lock.setTransId(transId);
    //             lock.setLotStatus(lot.getLotStatus());
    //             locks.add(lock);
    //         }
    //         lotService.checkAndCreateLotsTransLock(locks, comments);
    //     }
    // }

    protected 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);
    }

    /**
     * 未执行的任务 检查源晶舟和目标晶舟是否存在未执行的Sort任务
     */
    protected void checkWaitJobs(long sourceCarrierRrn, long targetCarrierRrn, Long targetCarrierRrn2) {
        Assert.isFalse(sorterQueryService.haveWaitJobForCarrier(sourceCarrierRrn),
                       Errors.create().key(MessageIdList.SOURCE_CARRIER_BE_OCCUPIED).build());
        if (targetCarrierRrn >0){
            Assert.isFalse(sorterQueryService.haveWaitJobForCarrier(targetCarrierRrn),
                           Errors.create().key(MessageIdList.TARGET_CARRIER_BE_OCCUPIED).build());
        }
        if (targetCarrierRrn2 != null && targetCarrierRrn2 >0){
            Assert.isFalse(sorterQueryService.haveWaitJobForCarrier(targetCarrierRrn2),
                           Errors.create().key(MessageIdList.TARGET_CARRIER_BE_OCCUPIED).build());
        }
    }

    protected List<ReferenceFileDetail> buiildPcdType(List<ReferenceFileDetail> comboData) {
        ReferenceFileDetail referenceFileDetail = new ReferenceFileDetail(ReferenceDetailNames.PROCESS_LOCATION_AUTO_CHANGE_CASSETTE_TYPE,
                                                                          getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                                        LocalContext.getFacilityRrn()),
                                                                          ObjectList.REFERENCE_FILE_KEY);
        List<ReferenceFileDetail> referenceFileDetails = sysService
                .getReferenceFileDetails(referenceFileDetail, LocalContext.getFacilityRrn());
        String removePcdType = "";
        for (ReferenceFileDetail ref: referenceFileDetails) {
            Matcher mathcher = WipUtils.PCDPATERN.matcher(ref.getData1Value());
            if(mathcher.matches()) {
                removePcdType = StringUtils.split(ref.getData1Value(), "-")[1];
            }
        }
        List<ReferenceFileDetail> newComboData = new ArrayList<>();
        for (ReferenceFileDetail ref1 : comboData) {
            if (!StringUtils.equalsIgnoreCase(ref1.getData1Value(),StringUtils.trimToUpperCase(removePcdType))) {
                newComboData.add(ref1);
            }
        }
        return newComboData;
    }


    protected String generatePCDId(String pcdType, String createType){
        String pcdId;
        ReferenceFileDetail pcdNameDetail = new ReferenceFileDetail(ReferenceDetailNames.PCD_NAME_RULE,
                                                                          getNamedSpace(ObjectList.REFERENCE_FILE_KEY, LocalContext.getFacilityRrn()), ObjectList.REFERENCE_FILE_KEY);
        if (StringUtils.isEmpty(createType)){
            return StringUtils.EMPTY;
        }
        pcdNameDetail.setKey1Value(pcdType);
        pcdNameDetail.setKey2Value(createType);
        pcdNameDetail = sysService.getReferenceFileDetail(pcdNameDetail);
        if (pcdNameDetail == null){//没有配置这种类型的 NamingRule
            return StringUtils.EMPTY;
        }
        String prefix = pcdNameDetail.getData1Value();
        if (StringUtils.isEmpty(prefix)){//未配置这种类型的 NamingRule 的前缀
            return StringUtils.EMPTY;
        }
        String ignorePrefix = pcdNameDetail.getData2Value();
        String lengthStr = pcdNameDetail.getData3Value();
        String autoIncrementFlag = pcdNameDetail.getData4Value();
        if (StringUtils.isEmpty(autoIncrementFlag)){//配置中不需要自增
            return StringUtils.EMPTY;
        }
        int length = NumberUtils.toInt(lengthStr, 4);
        pcdId = carrierService.generatePcdIdByPcdType(pcdType, prefix, ignorePrefix, length);
        Assert.isFalse(StringUtils.contains(pcdId, "-1"),
                       Errors.create().key(MessageIdList.PCD_CASSETTE_OUT_OF_MAX).content("Error, Maximum upper limit")
                             .build());
        return pcdId;
    }

    protected List<ReferenceFileDetail> queryComboDataList(String type){
        long facilityRrn = LocalContext.getFacilityRrn();
        List<ReferenceFileDetail> comboData;
        String fileType;
        if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.CARRIER_TYPE, type)
                || StringUtils.equalsIgnoreCase(SystemConstant.Pcd.DOOR_TYPE, type)
                || StringUtils.equalsIgnoreCase(SystemConstant.Pcd.POD_TYPE, type)) {//统一为使用 CARRIER_TYPE
            fileType = "$CARRIER_TYPE";
        }
        // else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.DOOR_TYPE, type)) {
        //     fileType = "$DOOR_TYPE";
        // } else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.POD_TYPE, type)) {
        //     fileType = "$POD_TYPE";
        // }
        else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.CARRIER_COLOR, type)) {
            fileType = "$CARRIER_COLOR";
        } else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.POD_COLOR, type)) {
            fileType = "$POD_COLOR";
        } else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.DOOR_COLOR, type)) {
            fileType = "$DOOR_COLOR";
        }
        else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.BUILD_CARRIER_TYPE, type)
                || StringUtils.equalsIgnoreCase(SystemConstant.Pcd.BUILD_DOOR_TYPE, type)
                || StringUtils.equalsIgnoreCase(SystemConstant.Pcd.BUILD_POD_TYPE, type)) {//统一为使用 CARRIER_TYPE
            fileType = "$CARRIER_TYPE";
        }
        // else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.BUILD_DOOR_TYPE, type)) {
        //     fileType = "$DOOR_TYPE";
        // } else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.BUILD_POD_TYPE, type)) {
        //     fileType = "$POD_TYPE";
        // }
        else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.PCD_STATUS, type)) {
            fileType = "$PCD_STATUS";
        } else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.PCD_CATEGORY, type)) {
            fileType = "$PCD_CATEGORY";
        } else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.WAFER_SIZE, type)) {
            fileType = "$WAFER_SIZE";
        } else if (StringUtils.equalsIgnoreCase(SystemConstant.Pcd.CARRIER_SIZE, type)) {
            fileType = "$CARRIER_SIZE";
        } else {
          return new ArrayList<>();
        }

        comboData = sysService.getRefFileValues(fileType, facilityRrn);
        if(StringUtils.equalsIgnoreCase(SystemConstant.Pcd.BUILD_DOOR_TYPE, type)
                || StringUtils.equalsIgnoreCase(SystemConstant.Pcd.BUILD_POD_TYPE, type)
                || StringUtils.equalsIgnoreCase(SystemConstant.Pcd.BUILD_CARRIER_TYPE, type)) {
            comboData = buiildPcdType(comboData);
        }

        return comboData;
    }

    protected List<Map> putDefaultData() {
        List<Map> list = new ArrayList<>();
        Map resultMap = new HashMap(20);
        resultMap.put("value", "");
        resultMap.put("key", "--Choice--");
        list.add(resultMap);
        return list;
    }

    protected void checkNamedObjectIdRule(String podOrDoorId, String type){
        String regex = "[0-9a-zA-Z]+$";
        Assert.isFalse(!Pattern.matches(regex, podOrDoorId) || StringUtils.length(podOrDoorId) > 64,
                       Errors.create().key(MessageIdList.POD_OR_DOOR_ID_RULE).content("{}号长度必须小于{}位,且只能是字母加数字组合!")
                             .args(type, SystemConstant.Pcd.PCD_ID_LENGTH).build());
    }

    protected void checkIsRunCardLot(Lot lot) {
        //先检查是不是RC的批次
        Assert.state(!lotRunCardQueryService.checkLotInRunCard(lot.getLotRrn()),
                     Errors.create().key(MessageIdList.IS_RC_LOT).content("{} is a RunCard Lot! Can't operate here!").args(lot.getLotId()).build());
    }

}