From f3abed3a723c810d5ac9150e6c67dc5f5194ad8f Mon Sep 17 00:00:00 2001 From: zhenxianyimeng <1920405993@qq.com> Date: Tue, 9 Jul 2019 23:50:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E6=89=80?= =?UTF-8?q?=E6=9C=89=E9=83=A8=E9=97=A8=E6=A0=91=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admins/DeptmentController.java | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/system/system-application/src/main/java/cn/iocoder/mall/admin/application/controller/admins/DeptmentController.java b/system/system-application/src/main/java/cn/iocoder/mall/admin/application/controller/admins/DeptmentController.java index f91493a2a..132f520dc 100644 --- a/system/system-application/src/main/java/cn/iocoder/mall/admin/application/controller/admins/DeptmentController.java +++ b/system/system-application/src/main/java/cn/iocoder/mall/admin/application/controller/admins/DeptmentController.java @@ -10,6 +10,7 @@ import cn.iocoder.mall.admin.api.dto.depetment.DeptmentPageDTO; import cn.iocoder.mall.admin.api.dto.depetment.DeptmentUpdateDTO; import cn.iocoder.mall.admin.application.convert.DeptmentConvert; import cn.iocoder.mall.admin.application.vo.deptment.DeptmentVO; +import cn.iocoder.mall.admin.application.vo.resource.ResourceTreeNodeVO; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContext; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder; import io.swagger.annotations.Api; @@ -21,6 +22,7 @@ import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -42,6 +44,19 @@ public class DeptmentController { @Autowired private DeptmentService deptmentService; + @GetMapping("tree/all") + @ApiOperation(value = "根部门的部门树") + public CommonResult> treeAll(){ + List list = deptmentService.getAllDeptments(); + List voList = DeptmentConvert.INSTANCE.convert(list); + Map nodeMap = calaNodeMap(voList); + // 获得到所有的根节点 + List rootNodes = nodeMap.values().stream() + .filter(node -> node.getPid().equals(ResourceConstants.PID_ROOT)) + .collect(Collectors.toList()); + return success(rootNodes); + } + @GetMapping("tree/page") @ApiOperation(value = "根部门分页的部门树") public CommonResult> treePage(@Validated DeptmentPageDTO deptmentPageDTO){ @@ -49,20 +64,7 @@ public class DeptmentController { PageResult voPageResult = DeptmentConvert.INSTANCE.convert(pageResult); List list = deptmentService.getAllDeptments(); List voList = DeptmentConvert.INSTANCE.convert(list); - Map nodeMap = voList.stream().collect(Collectors.toMap(e->e.getId(), e->e)); - - nodeMap.values().stream() - .filter(node -> !node.getPid().equals(ResourceConstants.PID_ROOT)) - .forEach((childNode) -> { - // 获得父节点 - DeptmentVO parentNode = nodeMap.get(childNode.getPid()); - if (parentNode.getChildren() == null) { // 初始化 children 数组 - parentNode.setChildren(new ArrayList<>()); - } - // 将自己添加到父节点中 - parentNode.getChildren().add(childNode); - }); - + Map nodeMap = calaNodeMap(voList); voPageResult.getList().forEach(d->{ d.setChildren(nodeMap.get(d.getId()).getChildren()); }); @@ -95,5 +97,22 @@ public class DeptmentController { )); } + private Map calaNodeMap(List voList){ + Map nodeMap = voList.stream().collect(Collectors.toMap(e->e.getId(), e->e)); + + nodeMap.values().stream() + .filter(node -> !node.getPid().equals(ResourceConstants.PID_ROOT)) + .forEach((childNode) -> { + // 获得父节点 + DeptmentVO parentNode = nodeMap.get(childNode.getPid()); + if (parentNode.getChildren() == null) { // 初始化 children 数组 + parentNode.setChildren(new ArrayList<>()); + } + // 将自己添加到父节点中 + parentNode.getChildren().add(childNode); + }); + return nodeMap; + } + }