From 4351d2b4db890de9f5f456f16a68a270788c6434 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期六, 21 三月 2026 13:57:08 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/common/utils/NavigateUtils.java |  330 ++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 229 insertions(+), 101 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/NavigateUtils.java b/src/main/java/com/zy/common/utils/NavigateUtils.java
index 7f91b77..6219dbc 100644
--- a/src/main/java/com/zy/common/utils/NavigateUtils.java
+++ b/src/main/java/com/zy/common/utils/NavigateUtils.java
@@ -84,110 +84,36 @@
         return calcByStationId(startStationId, endStationId, null, true);
     }
 
+    public synchronized List<List<NavigateNode>> calcCandidatePathByStationId(Integer startStationId,
+                                                                              Integer endStationId,
+                                                                              Integer currentTaskNo) {
+        StationPathSearchContext context = buildStationPathSearchContext(startStationId, endStationId);
+        if (context.allList.isEmpty()) {
+            return new ArrayList<>();
+        }
+
+        List<List<NavigateNode>> orderedPathList = orderStationPathCandidates(
+                context.allList,
+                context.resolvedPolicy,
+                currentTaskNo,
+                startStationId,
+                endStationId
+        );
+        return normalizeCandidatePaths(orderedPathList);
+    }
+
     private synchronized List<NavigateNode> calcByStationId(Integer startStationId,
                                                             Integer endStationId,
                                                             Integer currentTaskNo,
                                                             boolean reachabilityOnly) {
-        BasStation startStation = basStationService.getById(startStationId);
-        if (startStation == null) {
-            throw new CoolException("鏈壘鍒拌 璧风偣 瀵瑰簲鐨勭珯鐐规暟鎹�");
-        }
-        Integer lev = startStation.getStationLev();
-
-        NavigateSolution navigateSolution = new NavigateSolution();
-        List<List<NavigateNode>> stationMap = navigateSolution.getStationMap(lev);
-
-        NavigateNode startNode = navigateSolution.findStationNavigateNode(stationMap, startStationId);
-        if (startNode == null) {
-            throw new CoolException("鏈壘鍒拌 璧风偣 瀵瑰簲鐨勮妭鐐�");
-        }
-
-        NavigateNode endNode = navigateSolution.findStationNavigateNode(stationMap, endStationId);
-        if (endNode == null) {
-            throw new CoolException("鏈壘鍒拌 缁堢偣 瀵瑰簲鐨勮妭鐐�");
-        }
-
-        StationPathResolvedPolicy resolvedPolicy = resolveStationPathPolicy(startStationId, endStationId);
-        StationPathProfileConfig profileConfig = resolvedPolicy.getProfileConfig() == null
-                ? StationPathProfileConfig.defaultConfig()
-                : resolvedPolicy.getProfileConfig();
-
-        long startTime = System.currentTimeMillis();
-        News.info("[WCS Debug] 绔欑偣璺緞寮�濮嬭绠�,startStationId={},endStationId={}", startStationId, endStationId);
-        int calcMaxDepth = safeInt(profileConfig.getCalcMaxDepth(), 120);
-        int calcMaxPaths = safeInt(profileConfig.getCalcMaxPaths(), 500);
-        int calcMaxCost = safeInt(profileConfig.getCalcMaxCost(), 300);
-        List<Integer> guideStationSequence = buildGuideStationSequence(startStationId, endStationId, resolvedPolicy.getRuleConfig());
-        List<List<NavigateNode>> allList = navigateSolution.allSimplePaths(
-                stationMap,
-                startNode,
-                endNode,
-                calcMaxDepth,
-                calcMaxPaths,
-                calcMaxCost,
-                guideStationSequence
-        );
-        if (allList.isEmpty()) {
-//            throw new CoolException("鏈壘鍒拌璺緞");
+        StationPathSearchContext context = buildStationPathSearchContext(startStationId, endStationId);
+        if (context.allList.isEmpty()) {
             return new ArrayList<>();
         }
-        Map<Integer, StationProtocol> statusMap = loadStationStatusMap();
-        allList = filterNonAutoStationPaths(allList, statusMap);
-        if (allList.isEmpty()) {
-            News.info("[WCS Debug] 绔欑偣璺緞鍊欓�夊叏閮ㄨ杩囨护锛屽瓨鍦ㄩ潪鑷姩绔欑偣,startStationId={},endStationId={}", startStationId, endStationId);
-            return new ArrayList<>();
-        }
-        News.info("[WCS Debug] 绔欑偣璺緞璁$畻瀹屾垚锛岃�楁椂锛歿}ms", System.currentTimeMillis() - startTime);
-
-        startTime = System.currentTimeMillis();
-        News.info("[WCS Debug] 绔欑偣璺緞鏉冮噸寮�濮嬪垎鏋�,startStationId={},endStationId={}", startStationId, endStationId);
         List<NavigateNode> list = reachabilityOnly
-                ? findStationReachablePath(allList, resolvedPolicy, startStationId, endStationId)
-                : findStationBestPathTwoStage(allList, resolvedPolicy, currentTaskNo, startStationId, endStationId);
-        News.info("[WCS Debug] 绔欑偣璺緞鏉冮噸鍒嗘瀽瀹屾垚锛岃�楁椂锛歿}ms", System.currentTimeMillis() - startTime);
-
-        //鍘婚噸
-        HashSet<Integer> set = new HashSet<>();
-        List<NavigateNode> fitlerList = new ArrayList<>();
-        for (NavigateNode navigateNode : list) {
-            JSONObject valuObject = JSON.parseObject(navigateNode.getNodeValue());
-            if (valuObject.containsKey("rgvCalcFlag")) {
-                continue;
-            }
-            if (set.add(valuObject.getInteger("stationId"))) {
-                fitlerList.add(navigateNode);
-            }
-        }
-
-        for (int i = 0; i < fitlerList.size(); i++) {
-            NavigateNode currentNode = fitlerList.get(i);
-            currentNode.setIsInflectionPoint(false);
-            currentNode.setIsLiftTransferPoint(false);
-
-            try {
-                JSONObject valueObject = JSON.parseObject(currentNode.getNodeValue());
-                if (valueObject != null) {
-                    Object isLiftTransfer = valueObject.get("isLiftTransfer");
-                    if (isLiftTransfer != null) {
-                        String isLiftTransferStr = isLiftTransfer.toString();
-                        if ("1".equals(isLiftTransferStr) || "true".equalsIgnoreCase(isLiftTransferStr)) {
-                            currentNode.setIsLiftTransferPoint(true);
-                        }
-                    }
-                }
-            } catch (Exception ignore) {}
-
-            NavigateNode nextNode = (i + 1 < fitlerList.size()) ? fitlerList.get(i + 1) : null;
-            NavigateNode prevNode = (i - 1 >= 0) ? fitlerList.get(i - 1) : null;
-
-            HashMap<String, Object> result = searchInflectionPoint(currentNode, nextNode, prevNode);
-            if (Boolean.parseBoolean(result.get("result").toString())) {
-                currentNode.setIsInflectionPoint(true);
-                currentNode.setDirection(result.get("direction").toString());
-            }
-        }
-
-        return fitlerList;
+                ? findStationReachablePath(context.allList, context.resolvedPolicy, startStationId, endStationId)
+                : findStationBestPathTwoStage(context.allList, context.resolvedPolicy, currentTaskNo, startStationId, endStationId);
+        return normalizeStationPath(list);
     }
 
     public synchronized List<NavigateNode> calcByTrackSiteNo(int lev, Integer startTrackSiteNo, Integer endTrackSiteNo) {
@@ -337,6 +263,18 @@
                                                            Integer currentTaskNo,
                                                            Integer startStationId,
                                                            Integer endStationId) {
+        List<List<NavigateNode>> orderedPathList = orderStationPathCandidates(allList, resolvedPolicy, currentTaskNo, startStationId, endStationId);
+        if (orderedPathList.isEmpty()) {
+            return new ArrayList<>();
+        }
+        return orderedPathList.get(0);
+    }
+
+    private List<List<NavigateNode>> orderStationPathCandidates(List<List<NavigateNode>> allList,
+                                                                StationPathResolvedPolicy resolvedPolicy,
+                                                                Integer currentTaskNo,
+                                                                Integer startStationId,
+                                                                Integer endStationId) {
         if (allList == null || allList.isEmpty()) {
             return new ArrayList<>();
         }
@@ -423,12 +361,41 @@
         }
 
         int topK = safeInt(profileConfig.getS1TopK(), 5);
-        if (topK > 0 && stage1Selected.size() > topK) {
-            stage1Selected = new ArrayList<>(stage1Selected.subList(0, topK));
+        List<PathCandidateMetrics> primaryMetrics = new ArrayList<>(stage1Selected);
+        List<PathCandidateMetrics> secondaryMetrics = new ArrayList<>();
+        if (topK > 0 && primaryMetrics.size() > topK) {
+            secondaryMetrics.addAll(primaryMetrics.subList(topK, primaryMetrics.size()));
+            primaryMetrics = new ArrayList<>(primaryMetrics.subList(0, topK));
         }
 
-        stage1Selected.sort((a, b) -> compareDouble(a.dynamicCost, b.dynamicCost, a.pathLen, b.pathLen, a.turnCount, b.turnCount));
-        return stage1Selected.get(0).path;
+        primaryMetrics.sort((a, b) -> compareDouble(a.dynamicCost, b.dynamicCost, a.pathLen, b.pathLen, a.turnCount, b.turnCount));
+        secondaryMetrics.sort((a, b) -> compareDouble(a.dynamicCost, b.dynamicCost, a.pathLen, b.pathLen, a.turnCount, b.turnCount));
+
+        List<PathCandidateMetrics> remainingMetrics = new ArrayList<>();
+        for (PathCandidateMetrics metrics : metricsList) {
+            if (!stage1Selected.contains(metrics)) {
+                remainingMetrics.add(metrics);
+            }
+        }
+        remainingMetrics.sort((a, b) -> compareDouble(a.dynamicCost, b.dynamicCost, a.pathLen, b.pathLen, a.turnCount, b.turnCount));
+
+        List<List<NavigateNode>> orderedPathList = new ArrayList<>();
+        appendCandidatePathList(orderedPathList, primaryMetrics);
+        appendCandidatePathList(orderedPathList, secondaryMetrics);
+        appendCandidatePathList(orderedPathList, remainingMetrics);
+        return orderedPathList;
+    }
+
+    private void appendCandidatePathList(List<List<NavigateNode>> orderedPathList,
+                                         List<PathCandidateMetrics> metricsList) {
+        if (orderedPathList == null || metricsList == null) {
+            return;
+        }
+        for (PathCandidateMetrics metrics : metricsList) {
+            if (metrics != null && metrics.path != null && !metrics.path.isEmpty()) {
+                orderedPathList.add(metrics.path);
+            }
+        }
     }
 
     private List<List<NavigateNode>> applyRuleFilters(List<List<NavigateNode>> allList,
@@ -1777,10 +1744,171 @@
         return text != null && !text.trim().isEmpty();
     }
 
+    private StationPathSearchContext buildStationPathSearchContext(Integer startStationId, Integer endStationId) {
+        BasStation startStation = basStationService.getById(startStationId);
+        if (startStation == null) {
+            throw new CoolException("鏈壘鍒拌 璧风偣 瀵瑰簲鐨勭珯鐐规暟鎹�");
+        }
+        Integer lev = startStation.getStationLev();
+
+        NavigateSolution navigateSolution = new NavigateSolution();
+        List<List<NavigateNode>> stationMap = navigateSolution.getStationMap(lev);
+
+        NavigateNode startNode = navigateSolution.findStationNavigateNode(stationMap, startStationId);
+        if (startNode == null) {
+            throw new CoolException("鏈壘鍒拌 璧风偣 瀵瑰簲鐨勮妭鐐�");
+        }
+
+        NavigateNode endNode = navigateSolution.findStationNavigateNode(stationMap, endStationId);
+        if (endNode == null) {
+            throw new CoolException("鏈壘鍒拌 缁堢偣 瀵瑰簲鐨勮妭鐐�");
+        }
+
+        StationPathResolvedPolicy resolvedPolicy = resolveStationPathPolicy(startStationId, endStationId);
+        StationPathProfileConfig profileConfig = resolvedPolicy.getProfileConfig() == null
+                ? StationPathProfileConfig.defaultConfig()
+                : resolvedPolicy.getProfileConfig();
+
+        long startTime = System.currentTimeMillis();
+        News.info("[WCS Debug] 绔欑偣璺緞寮�濮嬭绠�,startStationId={},endStationId={}", startStationId, endStationId);
+        int calcMaxDepth = safeInt(profileConfig.getCalcMaxDepth(), 120);
+        int calcMaxPaths = safeInt(profileConfig.getCalcMaxPaths(), 500);
+        int calcMaxCost = safeInt(profileConfig.getCalcMaxCost(), 300);
+        List<Integer> guideStationSequence = buildGuideStationSequence(startStationId, endStationId, resolvedPolicy.getRuleConfig());
+        List<List<NavigateNode>> allList = navigateSolution.allSimplePaths(
+                stationMap,
+                startNode,
+                endNode,
+                calcMaxDepth,
+                calcMaxPaths,
+                calcMaxCost,
+                guideStationSequence
+        );
+        if (allList.isEmpty()) {
+            return StationPathSearchContext.empty(resolvedPolicy);
+        }
+        Map<Integer, StationProtocol> statusMap = loadStationStatusMap();
+        allList = filterNonAutoStationPaths(allList, statusMap);
+        if (allList.isEmpty()) {
+            News.info("[WCS Debug] 绔欑偣璺緞鍊欓�夊叏閮ㄨ杩囨护锛屽瓨鍦ㄩ潪鑷姩绔欑偣,startStationId={},endStationId={}", startStationId, endStationId);
+            return StationPathSearchContext.empty(resolvedPolicy);
+        }
+        News.info("[WCS Debug] 绔欑偣璺緞璁$畻瀹屾垚锛岃�楁椂锛歿}ms", System.currentTimeMillis() - startTime);
+        return new StationPathSearchContext(allList, resolvedPolicy);
+    }
+
+    private List<List<NavigateNode>> normalizeCandidatePaths(List<List<NavigateNode>> orderedPathList) {
+        List<List<NavigateNode>> result = new ArrayList<>();
+        if (orderedPathList == null || orderedPathList.isEmpty()) {
+            return result;
+        }
+        Set<String> seenPathSignatures = new LinkedHashSet<>();
+        for (List<NavigateNode> path : orderedPathList) {
+            List<NavigateNode> normalizedPath = normalizeStationPath(path);
+            String pathSignature = buildPathSignature(normalizedPath);
+            if (pathSignature.isEmpty() || !seenPathSignatures.add(pathSignature)) {
+                continue;
+            }
+            result.add(normalizedPath);
+        }
+        return result;
+    }
+
+    private List<NavigateNode> normalizeStationPath(List<NavigateNode> path) {
+        HashSet<Integer> stationIdSet = new HashSet<>();
+        List<NavigateNode> filterList = new ArrayList<>();
+        for (NavigateNode navigateNode : safeList(path)) {
+            if (navigateNode == null) {
+                continue;
+            }
+            JSONObject valueObject;
+            try {
+                valueObject = JSON.parseObject(navigateNode.getNodeValue());
+            } catch (Exception ignore) {
+                continue;
+            }
+            if (valueObject == null || valueObject.containsKey("rgvCalcFlag")) {
+                continue;
+            }
+            Integer stationId = valueObject.getInteger("stationId");
+            if (stationId == null || !stationIdSet.add(stationId)) {
+                continue;
+            }
+            NavigateNode clonedNode = navigateNode.clone();
+            if (clonedNode == null) {
+                continue;
+            }
+            filterList.add(clonedNode);
+        }
+
+        for (int i = 0; i < filterList.size(); i++) {
+            NavigateNode currentNode = filterList.get(i);
+            currentNode.setIsInflectionPoint(false);
+            currentNode.setIsLiftTransferPoint(false);
+
+            try {
+                JSONObject valueObject = JSON.parseObject(currentNode.getNodeValue());
+                if (valueObject != null) {
+                    Object isLiftTransfer = valueObject.get("isLiftTransfer");
+                    if (isLiftTransfer != null) {
+                        String isLiftTransferStr = isLiftTransfer.toString();
+                        if ("1".equals(isLiftTransferStr) || "true".equalsIgnoreCase(isLiftTransferStr)) {
+                            currentNode.setIsLiftTransferPoint(true);
+                        }
+                    }
+                }
+            } catch (Exception ignore) {
+            }
+
+            NavigateNode nextNode = (i + 1 < filterList.size()) ? filterList.get(i + 1) : null;
+            NavigateNode prevNode = (i - 1 >= 0) ? filterList.get(i - 1) : null;
+
+            HashMap<String, Object> searchResult = searchInflectionPoint(currentNode, nextNode, prevNode);
+            if (Boolean.parseBoolean(searchResult.get("result").toString())) {
+                currentNode.setIsInflectionPoint(true);
+                currentNode.setDirection(searchResult.get("direction").toString());
+            }
+        }
+        return filterList;
+    }
+
+    private String buildPathSignature(List<NavigateNode> path) {
+        List<Integer> stationIdList = extractStationIdList(path);
+        if (stationIdList.isEmpty()) {
+            return "";
+        }
+        StringBuilder builder = new StringBuilder();
+        for (Integer stationId : stationIdList) {
+            if (stationId == null) {
+                continue;
+            }
+            if (builder.length() > 0) {
+                builder.append("->");
+            }
+            builder.append(stationId);
+        }
+        return builder.toString();
+    }
+
     private <T> List<T> safeList(List<T> list) {
         return list == null ? Collections.emptyList() : list;
     }
 
+    private static class StationPathSearchContext {
+        private final List<List<NavigateNode>> allList;
+        private final StationPathResolvedPolicy resolvedPolicy;
+
+        private StationPathSearchContext(List<List<NavigateNode>> allList,
+                                         StationPathResolvedPolicy resolvedPolicy) {
+            this.allList = allList == null ? new ArrayList<>() : allList;
+            this.resolvedPolicy = resolvedPolicy == null ? new StationPathResolvedPolicy() : resolvedPolicy;
+        }
+
+        private static StationPathSearchContext empty(StationPathResolvedPolicy resolvedPolicy) {
+            return new StationPathSearchContext(new ArrayList<>(), resolvedPolicy);
+        }
+    }
+
     private static class PathCandidateMetrics {
         private List<NavigateNode> path;
         private int pathLen;

--
Gitblit v1.9.1