Pre Merge pull request !435 from zszdevelop/fix_dept_recursion_bug

This commit is contained in:
zszdevelop 2022-05-01 12:24:19 +00:00 committed by Gitee
commit 7886dbcf86
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 50 additions and 26 deletions

View File

@ -33,6 +33,7 @@
<poi.version>4.1.2</poi.version>
<velocity.version>2.3</velocity.version>
<jwt.version>0.9.1</jwt.version>
<hutool.version>5.7.13</hutool.version>
</properties>
<!-- 依赖声明 -->
@ -199,6 +200,12 @@
<version>${ruoyi.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -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<SysDept> depts = deptService.selectDeptList(dept);
return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
List<TreeSelect> treeSelects = deptService.buildDeptTreeSelect(depts);
return AjaxResult.success(treeSelects);
}
/**

View File

@ -125,6 +125,11 @@
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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<Long> dept) {
this.id = dept.getId();
this.label = dept.getName().toString();
List<Tree<Long>> children = dept.getChildren();
if (children != null && !children.isEmpty()) {
this.children = children.stream().map(TreeSelect::new).collect(Collectors.toList());
}
}
public Long getId()
{

View File

@ -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<SysDept> buildDeptTree(List<SysDept> depts);
public List<Tree<Long>> buildDeptTree(List<SysDept> depts, Long parentId);
/**
* 构建前端所需要下拉树结构

View File

@ -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<SysDept> buildDeptTree(List<SysDept> depts)
public List<Tree<Long>> buildDeptTree(List<SysDept> depts,Long parentId)
{
List<SysDept> returnList = new ArrayList<SysDept>();
List<Long> tempList = new ArrayList<Long>();
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<Tree<Long>> 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<TreeSelect> buildDeptTreeSelect(List<SysDept> depts)
{
List<SysDept> deptTrees = buildDeptTree(depts);
List<Tree<Long>> deptTrees = buildDeptTree(depts, 0L);
return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
}