WipUtils.java

package com.mycim.utils;

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.valueobject.prp.RecipeDto;
import com.mycim.valueobject.wip.Lot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author Johnson Wang
 * @date 2019/3/25 10:12
 **/
public class WipUtils {
    public static final String AUTO="AUTO";

    public static final String MANUAL="MANUAL";

    public static final Pattern PCDPATERN = Pattern.compile("(\\w{1,}\\-\\w{1,})");

    public static final String CSTTYPE_SEPERATER_KEY = "-";

    private static final int NUMBER_THREE = 3;

    /**
     * get route_id by processStepIdVersion
     *
     * @param processStepIdVersion 版本号
     * @return
     */
    @Deprecated
    public static String getRouteIdByProcessStep(String processStepIdVersion) {
        if (StringUtils.isBlank(processStepIdVersion)) {
            return null;
        }
        int firstSep = processStepIdVersion.indexOf("|");
        int secondSep = processStepIdVersion.indexOf("|", firstSep + 1);
        if (secondSep >= 0) {
            return processStepIdVersion.substring(firstSep + 1, processStepIdVersion.indexOf(",", firstSep));
        } else {
            return processStepIdVersion.substring(0, processStepIdVersion.indexOf(","));
        }
    }

    /**
     * get route_rrn by processStepVersion
     *
     * @param processStepVersion 版本号
     * @return String
     * @throws Exception
     */
    @Deprecated
    public static Long getRouteRrnByProcessStep(String processStepVersion) {
        if (StringUtils.isBlank(processStepVersion)) {
            return null;
        }

        int firstSep = processStepVersion.indexOf("|");
        int secondSep = processStepVersion.indexOf("|", firstSep + 1);
        if (secondSep >= 0) {
            return new Long(processStepVersion.substring(firstSep + 1, processStepVersion.indexOf(",", firstSep)));
        } else {
            return new Long(processStepVersion.substring(0, processStepVersion.indexOf(",")));
        }
    }

    /***
     * get route_id by processStepIdVersion
     *
     * @param processStepIdVersion 版本号
     * @return String
     * @throws Exception
     */
    @Deprecated
    public static int getRouteVersion(String processStepIdVersion) {
        if (StringUtils.isBlank(processStepIdVersion)) {
            return 0;
        }
        String version = "0";
        String[] split = processStepIdVersion.split("\\|");
        if (split.length == NUMBER_THREE) {
            String firstSep = StringUtils.substringAfter(processStepIdVersion, "|");
            String centreSep = StringUtils.substringBefore(firstSep, "|");
            version = StringUtils.substringAfter(centreSep, ",");
        } else {
            String firstSep = StringUtils.substringBefore(processStepIdVersion, "|");
            version = StringUtils.substringAfter(firstSep, ",");
        }

        if (StringUtils.isNotEmpty(version)) {
            return NumberUtils.toInt(version);
        } else {
            return 0;
        }
    }

    public static Long parseRecipeRrn(String recipeString) {
        if (StringUtils.isNotEmpty(recipeString)) {
            int i = recipeString.indexOf(",");

            if (i == -1) {
                i = recipeString.length();
            }

            int j = recipeString.indexOf("|");

            if (j == -1) {
                recipeString = recipeString.substring(0, i);
            } else {
                recipeString = recipeString.substring(0, j);
            }
            return NumberUtils.toLong(recipeString);
        }
        return null;
    }

    /**
     * @param processStepVersion
     * @return 工序版本
     */
    @Deprecated
    public static Integer getRouteVersionByProcessStep(String processStepVersion) {
        if (processStepVersion == null) {
            return null;
        }
        int firstSep = processStepVersion.indexOf("|");
        int secondSep = processStepVersion.indexOf("|", firstSep + 1);
        if (secondSep >= 0) {
            return new Integer(processStepVersion.substring(processStepVersion.indexOf(",", firstSep) + 1, secondSep));
        } else {
            return new Integer(processStepVersion.substring(processStepVersion.indexOf(",") + 1, firstSep));
        }
    }

    public static List<Map<String, Object>> parseWflTree(List wflTree) {
        List<Map<String, Object>> wflInfos = new ArrayList<Map<String, Object>>();
        String routeSeq = null;
        String routeId = null;
        String routeDesc = null;
        long routeRrn = 0;
        for (int i = 0; i < wflTree.size(); i++) {
            Map<String, Object> node = new HashMap<String, Object>();
            String[] treeNodes = (String[]) wflTree.get(i);
            if ("0".equals(treeNodes[7])) {
                routeSeq = StringUtils.substringBefore(treeNodes[2], "---");
                routeId = StringUtils.substringAfter(treeNodes[2], "---");
                routeDesc = treeNodes[5];
                node.put("routeSeq", routeSeq);
                node.put("routeId", routeId);
                node.put("routeDesc", routeDesc);
                node.put("isRoute", new Boolean(true));
                routeRrn = NumberUtils.toLong(treeNodes[4], 0);
                // nodes.add(node);不用显示工序了
            } else if ("1".equals(treeNodes[7])) {
                String operationSeq = StringUtils.substringBefore(treeNodes[2], "---");
                node.put("routeSeq", routeSeq);
                node.put("operationSeq", operationSeq);
                node.put("routeId", routeId);
                node.put("routeDesc", routeDesc);
                String operationId = StringUtils.substringAfter(treeNodes[2], "---");
                node.put("operationId", operationId);
                long operationRrn = NumberUtils.toLong(treeNodes[4], 0);

                String wflStep = treeNodes[6];
                node.put("stepPath", wflStep);
                node.put("stepNumber", wflStep);

                String[] wflStepArray = StringUtils.split(wflStep, ".");
                if (wflStepArray.length < 3) {
                    // rework流程的stepPath中,没有process信息
                    node.put("processRrn", "");
                    node.put("processVersion", "");
                } else {
                    node.put("processRrn", NumberUtils.toLong(wflStepArray[0], 0));
                    node.put("processVersion", NumberUtils.toInt(wflStepArray[1], 0));
                }
                node.put("routeRrn", routeRrn);
                node.put("operationRrn", operationRrn);

                wflInfos.add(node);
            }
        }
        return wflInfos;
    }

    @Deprecated
    public static Long parseRouteRrn(String processStepString) {
        Long routeRrn = null;

        if (StringUtils.isNotEmpty(processStepString)) {
            int firstSep = processStepString.indexOf("|");
            int secondSep = processStepString.indexOf("|", firstSep + 1);

            if (secondSep >= 0) {
                routeRrn = Long
                        .valueOf(processStepString.substring(firstSep + 1, processStepString.indexOf(",", firstSep)));
            } else {
                routeRrn = Long.valueOf(processStepString.substring(0, processStepString.indexOf(",")));
            }
        }

        return routeRrn;
    }

    @Deprecated
    public static Long getOperationRrnByProcessStep(String processStepVersion) {
        if (processStepVersion == null) {
            return null;
        }
        int firstSep = processStepVersion.indexOf("|");
        int secondSep = processStepVersion.indexOf("|", firstSep + 1);
        if (secondSep >= 0) {
            return new Long(processStepVersion.substring(secondSep + 1, processStepVersion.indexOf(",", secondSep)));
        } else {
            return new Long(processStepVersion.substring(firstSep + 1, processStepVersion.indexOf(",", firstSep)));
        }
    }

    private static String[] parseProcessStepVersion(String processStepVersion) {
        return StringUtils.split(processStepVersion, "|");
    }

    public static String[] getRouteByProcessStepVersion(String processStepVersion) {
        if (StringUtils.isEmpty(processStepVersion)) {
            return null;
        }
        String[] process = parseProcessStepVersion(processStepVersion);

        String processStr = process.length > 2 ? process[1] : process[0];

        return StringUtils.split(processStr, ",");
    }

    public static String[] getOperationByProcessStepVersion(String processStepVersion) {
        if (StringUtils.isEmpty(processStepVersion)) {
            return null;
        }
        String[] operation = parseProcessStepVersion(processStepVersion);

        String operationStr = operation[operation.length - 1];

        return StringUtils.split(operationStr, ",");
    }

    /**
     * 过滤掉已到流程最后一步的lot
     *
     * @param lotInfos
     * @return
     */
    public static List<Map> filterEndLotInfos(List<Map> lotInfos) {
        return lotInfos.stream().filter(lotInfo -> {
            return "0".equals(MapUtils.getString(lotInfo, "wflend"));
        }).collect(Collectors.toList());
    }

    public static List<Map> reBuildWflTree(List<String[]> oldWflTree) {
        List<Map> wflTree = new ArrayList<>();

        String routeRrn = "";
        String routeId = "";
        String routeSeq = "";
        String routeDesc = "";
        boolean oprationIsFirstStep = true;

        for (String[] node : oldWflTree) {
            if ("0".equals(node[7])) {
                Map routeNode = new HashMap();

                String stepInfo = node[6] + "";
                String stepRrn = node[1];

                routeNode.put("stepRrn", stepRrn);
                routeNode.put("stepInfo", stepInfo);

                routeRrn = node[4] + "";
                routeNode.put("routeRrn", routeRrn);

                String route = node[2] + "";
                routeId = StringUtils.substringAfterLast(route, "-");
                routeNode.put("routeId", routeId);

                routeSeq = StringUtils.substringBefore(route, "-");
                routeNode.put("routeSeq", routeSeq);

                routeDesc = node[5];
                routeNode.put("routeDesc", routeDesc);

                routeNode.put("nodeType", "ROUTE");
                wflTree.add(routeNode);

                oprationIsFirstStep = true;
            } else if ("1".equals(node[7])) {
                Map operationNode = new HashMap();

                String stepInfo = node[6] + "";
                String stepRrn = node[1];

                operationNode.put("stepRrn", stepRrn);
                operationNode.put("stepInfo", stepInfo);

                String operationRrn = node[4] + "";
                operationNode.put("operationRrn", operationRrn);

                String operation = node[2] + "";
                String operationId = StringUtils.substringAfterLast(operation, "-");
                operationNode.put("operationId", operationId);

                String operationSeq = StringUtils.substringBefore(operation, "-");
                operationNode.put("operationSeq", operationSeq);

                String operationDesc = node[5];
                operationDesc = StringUtils.substringAfterLast(operationDesc, "-");
                operationNode.put("operationDesc", operationDesc);

                operationNode.put("routeRrn", routeRrn);

                operationNode.put("routeId", routeId);

                operationNode.put("routeSeq", routeSeq);

                operationNode.put("routeDesc", routeDesc);

                operationNode.put("nodeType", "OPERATION");

                if (oprationIsFirstStep) {
                    operationNode.put("oprationIsFirstStep", "1");
                } else {
                    operationNode.put("oprationIsFirstStep", "0");
                }

                oprationIsFirstStep = false;
                wflTree.add(operationNode);
            }
        }

        return wflTree;
    }

    public static String buildEventNameByStatus(String currentStatus, String targetStatus) {
        if (StringUtils.equals(currentStatus, targetStatus)) {
            return "";
        }
        String eventId = "EQP." + currentStatus + "_TO_" + targetStatus;
        return eventId;
    }

    public static boolean checkLotIsMonitorLot(Lot lot) {
        String category = lot.getCreateCategory();
        String lotType = lot.getLotType();
        if (StringUtils.equalsIgnoreCase("C", category) &&
                (StringUtils.equalsIgnoreCase("C", lotType) || StringUtils.equalsIgnoreCase("D", lotType))) {
            return true;
        }
        return false;
    }

    public static void getRecipeLot(Lot lot, String recipeLogicalId, String recipePhysicalId, Long recipeLogicalRrn,
                                    String ppid) {
        // lot.setReticleGroupRrn(recipeLogicalRrn);
        // lot.setReticleGroupId(recipeLogicalId);
        lot.setRecipeLogicalRrn(recipeLogicalRrn);
        lot.setRecipePhysicalId(recipePhysicalId);
        lot.setPpid(ppid);
    }

    public static RecipeDto getRecipeDto(Long recipeGroupRrn, String recipeGroupId, String recipePhysicalId,
                                         Long recipeLogicalRrn, String ppid) {
        RecipeDto recipeDto = new RecipeDto();
        recipeDto.setRecipeGroupRrn(recipeGroupRrn);
        recipeDto.setRecipeGroupId(recipeGroupId);
        recipeDto.setRecipeLogicalRrn(recipeLogicalRrn);
        recipeDto.setRecipePhysicalId(recipePhysicalId);
        recipeDto.setPpid(ppid);
        return recipeDto;
    }

}