cuijian
2025-06-16 ec1bf4658e36a17f971a54007920a44c5378b7dc
lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/ProductMixServiceImpl.java
@@ -48,4 +48,67 @@
        result.sort(Comparator.comparing(ProductMix::getCreateTime, Comparator.nullsLast(Date::compareTo)));
        return result;
    }
    @Override
    public List<ProductMix> getParentList(String id) {
        List<ProductMix> parentList = new ArrayList<>();
        // 1. 根据ID查询当前节点
        ProductMix current = this.getById(id);
        if (current == null) {
            return parentList; // 节点不存在时返回空列表
        }
        // 2. 从当前节点开始向上查找父节点
        Long parentId = current.getParentId();
        while ( parentId != 0L) {
            ProductMix parent = this.getById(parentId.toString());
            if (parent == null) {
                break;
            }
            parentList.add(parent);
            parentId = parent.getParentId();
        }
        return parentList;
    }
    @Override
    public List<ProductMix> getChildrenList(String id) {
        List<ProductMix> childrenList = new ArrayList<>();
        ProductMix current = this.getById(id);
        if (current == null) {
            return childrenList;
        }
        // 使用队列进行BFS
        Queue<ProductMix> queue = new LinkedList<>();
        queue.add(current); // 加入当前节点作为起点
        // 记录已访问节点的ID,避免循环引用
        Set<String> visited = new HashSet<>();
        visited.add(id); // 起始节点已访问
        while (!queue.isEmpty()) {
            ProductMix node = queue.poll();
            // 跳过起始节点(即传入的节点),不加入结果列表
            if (!node.getId().toString().equals(id)) {
                childrenList.add(node);
            }
            // 查询当前节点的直接子节点
            List<ProductMix> directChildren = this.lambdaQuery().eq(ProductMix::getParentId, node.getId()).list();
            if (directChildren != null && !directChildren.isEmpty()) {
                for (ProductMix child : directChildren) {
                    String childId = child.getId().toString();
                    // 如果该子节点还未访问过
                    if (!visited.contains(childId)) {
                        visited.add(childId);
                        queue.add(child);
                    }
                    // 否则忽略,避免循环引用导致的死循环
                }
            }
        }
        return childrenList;
    }
}