#
Junjie
昨天 4351d2b4db890de9f5f456f16a68a270788c6434
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,
@@ -1250,42 +1217,7 @@
                                           StationTrafficSnapshot trafficSnapshot,
                                           LoopMergeGuardContext loopMergeGuardContext,
                                           Set<LoopMergeEntry> mandatoryLoopMergeEntrySet) {
        if (path == null || path.size() < 2 || loopMergeGuardContext == null || loopMergeGuardContext.loopStationIdSet.isEmpty()) {
            return true;
        }
        List<Integer> stationIdList = extractStationIdList(path);
        if (stationIdList.size() < 2) {
            return true;
        }
        for (int i = 1; i < stationIdList.size(); i++) {
            Integer prevStationId = stationIdList.get(i - 1);
            Integer currentStationId = stationIdList.get(i);
            if (prevStationId == null || currentStationId == null) {
                continue;
            }
            if (loopMergeGuardContext.loopStationIdSet.contains(prevStationId)
                    || !loopMergeGuardContext.loopStationIdSet.contains(currentStationId)) {
                continue;
            }
            Set<Integer> trunkNeighborSet = loopMergeGuardContext.loopNeighborMap.getOrDefault(currentStationId, Collections.emptySet());
            if (trunkNeighborSet.size() < 2) {
                continue;
            }
            LoopMergeEntry currentEntry = new LoopMergeEntry(prevStationId, currentStationId);
            for (Integer trunkNeighborStationId : trunkNeighborSet) {
                if (isStationOccupiedForLoopMerge(trunkNeighborStationId, statusMap, trafficSnapshot)) {
                    return false;
                }
            }
            boolean mandatoryEntry = mandatoryLoopMergeEntrySet != null && mandatoryLoopMergeEntrySet.contains(currentEntry);
            if (!mandatoryEntry && !isLoopTrunkVeryIdle(currentStationId, loopMergeGuardContext, statusMap, trafficSnapshot)) {
                return false;
            }
        }
        // 环线并入保护已停用:允许候选路径直接参与后续评分。
        return true;
    }
@@ -1812,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;