From 6f3d7dc3a2157e46924e3b12a4f665de10af4bb4 Mon Sep 17 00:00:00 2001 From: C-ccc <2170639886@qq.com> Date: Thu, 25 Jul 2024 08:36:58 +0000 Subject: [PATCH] =?UTF-8?q?update=20system/department/manager/DepartmentCa?= =?UTF-8?q?cheManager.java.=20=E4=BF=AE=E6=94=B9=20recursiveBuildTree?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E4=BD=BF=E8=BF=94=E5=9B=9E=E5=80=BC?= =?UTF-8?q?=E4=B8=AD=E7=9A=84setSelfAndAllChildrenIdList=E6=98=AF=E4=BB=A5?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E8=8A=82=E7=82=B9=E4=B8=BA=E6=A0=B9=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=E7=BB=93?= =?UTF-8?q?=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: C-ccc <2170639886@qq.com> --- .../manager/DepartmentCacheManager.java | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/smart-admin-api/sa-admin/src/main/java/net/lab1024/sa/admin/module/system/department/manager/DepartmentCacheManager.java b/smart-admin-api/sa-admin/src/main/java/net/lab1024/sa/admin/module/system/department/manager/DepartmentCacheManager.java index 3ed6f350..5a4ecc4d 100644 --- a/smart-admin-api/sa-admin/src/main/java/net/lab1024/sa/admin/module/system/department/manager/DepartmentCacheManager.java +++ b/smart-admin-api/sa-admin/src/main/java/net/lab1024/sa/admin/module/system/department/manager/DepartmentCacheManager.java @@ -141,13 +141,15 @@ public class DepartmentCacheManager { return treeVOList; } - /** + /** * 构建所有根节点的下级树形结构 - * + * 返回值为层序遍历结果 + * [由于departmentDao中listAll给出数据根据Sort降序 所以同一层中Sort值较大的优先遍历] */ - private void recursiveBuildTree(List nodeList, List allDepartmentList) { + private List recursiveBuildTree(List nodeList, List allDepartmentList) { int nodeSize = nodeList.size(); - for (int i = 0; i < nodeSize; i++) { + List childIdList = new ArrayList<>(); + for(int i = 0; i < nodeSize; i++) { int preIndex = i - 1; int nextIndex = i + 1; DepartmentTreeVO node = nodeList.get(i); @@ -158,16 +160,34 @@ public class DepartmentCacheManager { node.setNextId(nodeList.get(nextIndex).getDepartmentId()); } - ArrayList selfAndAllChildrenIdList = Lists.newArrayList(); - selfAndAllChildrenIdList.add(node.getDepartmentId()); - node.setSelfAndAllChildrenIdList(selfAndAllChildrenIdList); - List children = getChildren(node.getDepartmentId(), allDepartmentList); + + List tempChildIdList = new ArrayList<>(); if (CollectionUtils.isNotEmpty(children)) { node.setChildren(children); - this.recursiveBuildTree(children, allDepartmentList); + tempChildIdList = this.recursiveBuildTree(children, allDepartmentList); } + + if(CollectionUtils.isEmpty(node.getSelfAndAllChildrenIdList())) { + node.setSelfAndAllChildrenIdList( + new ArrayList<>() + ); + } + node.getSelfAndAllChildrenIdList().add(node.getDepartmentId()); + + if(CollectionUtils.isNotEmpty(tempChildIdList)) { + node.getSelfAndAllChildrenIdList().addAll(tempChildIdList); + childIdList.addAll(tempChildIdList); + } + } + + // 保证本层遍历顺序 + for(int i = nodeSize - 1; i >= 0; i--) { + childIdList.add(0, nodeList.get(i).getDepartmentId()); + } + + return childIdList; }