Pre Merge pull request !373 from 试着奔跑的菜鸟/master

pull/373/MERGE
试着奔跑的菜鸟 6 months ago committed by Gitee
commit b910bfc838
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -3,6 +3,7 @@ package com.ruoyi.system.service.impl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -68,21 +69,20 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return * @return
*/ */
@Override @Override
public List<SysDept> buildDeptTree(List<SysDept> depts) public List<SysDept> buildDeptTree(List<SysDept> depts) {
{
List<SysDept> returnList = new ArrayList<SysDept>(); List<SysDept> returnList = new ArrayList<SysDept>();
List<Long> tempList = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList()); List<Long> tempList = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList());
for (SysDept dept : depts) // 按父级分组
{ Map<Long, List<SysDept>> groupByParentIdDepts = depts.stream().filter(dept -> dept.getParentId() != null)
.collect(Collectors.groupingBy(SysDept::getParentId));
for (SysDept dept : depts) {
// 如果是顶级节点, 遍历该父节点的所有子节点 // 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(dept.getParentId())) if (!tempList.contains(dept.getParentId())) {
{ recursionFn(groupByParentIdDepts, dept);
recursionFn(depts, dept);
returnList.add(dept); returnList.add(dept);
} }
} }
if (returnList.isEmpty()) if (returnList.isEmpty()) {
{
returnList = depts; returnList = depts;
} }
return returnList; return returnList;
@ -296,17 +296,17 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* *
*/ */
private void recursionFn(List<SysDept> list, SysDept t) private void recursionFn(Map<Long, List<SysDept>> groupByParentIdDepts, SysDept t) {
{
// 得到子节点列表 // 得到子节点列表
List<SysDept> childList = getChildList(list, t); List<SysDept> childList = groupByParentIdDepts.get(t.getDeptId());
t.setChildren(childList); if (childList != null) {
for (SysDept tChild : childList) t.setChildren(childList);
{ // 为每个子节点递归找到子节点
if (hasChild(list, tChild)) for (SysDept tChild : childList) {
{ recursionFn(groupByParentIdDepts, tChild);
recursionFn(list, tChild);
} }
} else {
t.setChildren(new ArrayList<>(0));
} }
} }

Loading…
Cancel
Save