diff --git a/pom.xml b/pom.xml index ddc287527..723aed730 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,7 @@ 4.1.2 2.3 0.9.1 + 5.7.13 @@ -199,6 +200,12 @@ ${ruoyi.version} + + cn.hutool + hutool-core + ${hutool.version} + + diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDeptController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDeptController.java index 6ec2f3ef0..d2c98807c 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDeptController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDeptController.java @@ -2,6 +2,8 @@ package com.ruoyi.web.controller.system; import java.util.Iterator; import java.util.List; + +import com.ruoyi.common.core.domain.TreeSelect; import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; @@ -82,10 +84,10 @@ public class SysDeptController extends BaseController * 获取部门下拉树列表 */ @GetMapping("/treeselect") - public AjaxResult treeselect(SysDept dept) - { + public AjaxResult treeselect(SysDept dept){ List depts = deptService.selectDeptList(dept); - return AjaxResult.success(deptService.buildDeptTreeSelect(depts)); + List treeSelects = deptService.buildDeptTreeSelect(depts); + return AjaxResult.success(treeSelects); } /** diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index e18104ca1..a45a74bef 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -125,6 +125,11 @@ javax.servlet-api + + cn.hutool + hutool-core + + \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/TreeSelect.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/TreeSelect.java index 4a59e402a..f4d665016 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/TreeSelect.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/TreeSelect.java @@ -3,6 +3,8 @@ package com.ruoyi.common.core.domain; import java.io.Serializable; import java.util.List; import java.util.stream.Collectors; + +import cn.hutool.core.lang.tree.Tree; import com.fasterxml.jackson.annotation.JsonInclude; import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.core.domain.entity.SysMenu; @@ -44,6 +46,16 @@ public class TreeSelect implements Serializable this.label = menu.getMenuName(); this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList()); } + public TreeSelect(Tree dept) { + this.id = dept.getId(); + this.label = dept.getName().toString(); + List> children = dept.getChildren(); + if (children != null && !children.isEmpty()) { + this.children = children.stream().map(TreeSelect::new).collect(Collectors.toList()); + } + + } + public Long getId() { diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java index 1ac8ccb27..5383750b6 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java @@ -1,6 +1,8 @@ package com.ruoyi.system.service; import java.util.List; + +import cn.hutool.core.lang.tree.Tree; import com.ruoyi.common.core.domain.TreeSelect; import com.ruoyi.common.core.domain.entity.SysDept; @@ -25,7 +27,7 @@ public interface ISysDeptService * @param depts 部门列表 * @return 树结构列表 */ - public List buildDeptTree(List depts); + public List> buildDeptTree(List depts, Long parentId); /** * 构建前端所需要下拉树结构 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java index 32ec0077f..157ab9f07 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java @@ -4,6 +4,10 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; + +import cn.hutool.core.lang.tree.Tree; +import cn.hutool.core.lang.tree.TreeNodeConfig; +import cn.hutool.core.lang.tree.TreeUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ruoyi.common.annotation.DataScope; @@ -55,28 +59,20 @@ public class SysDeptServiceImpl implements ISysDeptService * @return 树结构列表 */ @Override - public List buildDeptTree(List depts) + public List> buildDeptTree(List depts,Long parentId) { - List returnList = new ArrayList(); - List tempList = new ArrayList(); - for (SysDept dept : depts) - { - tempList.add(dept.getDeptId()); - } - for (SysDept dept : depts) - { - // 如果是顶级节点, 遍历该父节点的所有子节点 - if (!tempList.contains(dept.getParentId())) - { - recursionFn(depts, dept); - returnList.add(dept); - } - } - if (returnList.isEmpty()) - { - returnList = depts; - } - return returnList; + //配置 + TreeNodeConfig treeNodeConfig = new TreeNodeConfig(); + treeNodeConfig.setNameKey("label"); + // 自定义属性名 都要默认值的 + List> treeList = TreeUtil.build(depts, parentId, treeNodeConfig, + (treeNode, tree) -> { + tree.setId(treeNode.getDeptId()); + tree.setParentId(treeNode.getParentId()); + tree.setName(treeNode.getDeptName()); + tree.putExtra("value", treeNode.getDeptId()); + }); + return treeList; } /** @@ -88,7 +84,7 @@ public class SysDeptServiceImpl implements ISysDeptService @Override public List buildDeptTreeSelect(List depts) { - List deptTrees = buildDeptTree(depts); + List> deptTrees = buildDeptTree(depts, 0L); return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList()); }