lyh
2025-06-16 24e0bdd24a10449c98013cdb5bcc5e37735f5a91
lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/utils/TreeBuilder.java
@@ -1,10 +1,14 @@
package org.jeecg.modules.dnc.utils;
import it.unimi.dsi.fastutil.longs.*;
import it.unimi.dsi.fastutil.longs.Long2IntMap;
import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import org.jeecg.modules.dnc.entity.ProductMix;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Collectors;
public class TreeBuilder {
@@ -46,19 +50,19 @@
        // 1. 构建邻接表(父节点 -> 子节点列表)
        Long2ObjectMap<List<Long>> adjacencyList = new Long2ObjectOpenHashMap<>(nodes.size());
        nodes.forEach(node -> {
            long parentId = node.getParentId();
            Long parentId = node.getParentId();
            adjacencyList.computeIfAbsent(parentId, k -> new ArrayList<>())
                    .add(node.getId());
        });
        // 2. 初始化入度计数器
        Long2IntMap inDegree = new Long2IntOpenHashMap();
        nodes.forEach(node -> inDegree.put(node.getId(), 0));
        nodes.forEach(node -> inDegree.put(node.getId().longValue(), 0));
        // 3. 计算入度
        nodes.forEach(node -> {
            if (node.getParentId() != 0 && nodeMap.containsKey(node.getParentId())) {
                inDegree.put(node.getId(), inDegree.get(node.getId()) + 1);
                inDegree.put(node.getId().longValue(), inDegree.get(node.getId()) + 1);
            }
        });
@@ -87,11 +91,11 @@
            });
        }
        // 6. 循环检测
        if (sorted.size() != nodes.size()) {
            handleSortingFailure(nodes, sorted, nodeMap);
            return Collections.emptyList();
        }
//        // 6. 循环检测
//        if (sorted.size() != nodes.size()) {
//            handleSortingFailure(nodes, sorted, nodeMap);
//            return Collections.emptyList();
//        }
        return sorted;
    }
@@ -104,9 +108,10 @@
            ProductMix newNode = new ProductMix(
                    node.getId(),
                    node.getParentId(),
                    node.getName(),
                    node.getCode(),
                    node.getType()
                    node.getTreeName(),
                    node.getTreeCode(),
                    node.getType(),
                    node.getCreateTime()
            );
            treeMap.put(newNode.getId(), newNode);
        });
@@ -178,4 +183,4 @@
        public Long2ObjectMap<ProductMix> getNodeMap() { return nodeMap; }
        public List<ProductMix> getValidNodes() { return validNodes; }
    }
}
}