refactor: ✂️递归获取菜单、部门属性列表代码重构优化

This commit is contained in:
haoxr
2023-05-21 15:11:57 +08:00
parent 222607fd30
commit fcb509007e
4 changed files with 109 additions and 116 deletions

View File

@@ -1,25 +1,25 @@
package com.youlai.system.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.constant.SystemConstants;
import com.youlai.system.common.enums.StatusEnum;
import com.youlai.system.pojo.vo.Option;
import com.youlai.system.converter.DeptConverter;
import com.youlai.system.mapper.SysDeptMapper;
import com.youlai.system.pojo.entity.SysDept;
import com.youlai.system.pojo.form.DeptForm;
import com.youlai.system.pojo.query.DeptQuery;
import com.youlai.system.pojo.vo.DeptVO;
import com.youlai.system.pojo.vo.Option;
import com.youlai.system.service.SysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -52,35 +52,20 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
.orderByAsc(SysDept::getSort)
);
Set<Long> deptIds = deptList.stream()
.map(SysDept::getId)
.collect(Collectors.toSet());
Set<Long> parentIds = deptList.stream()
.map(SysDept::getParentId)
.collect(Collectors.toSet());
List<Long> rootIds = CollectionUtil.subtractToList(parentIds, deptIds);
List<DeptVO> list = new ArrayList<>();
if (CollectionUtil.isNotEmpty(deptList)) {
Set<Long> cacheDeptIds = deptList.stream()
.map(SysDept::getId)
.collect(Collectors.toSet());
for (SysDept dept : deptList) {
Long parentId = dept.getParentId();
// 不在缓存ID列表的parentId是顶级节点ID以此作为递归开始
if (!cacheDeptIds.contains(parentId)) {
list.addAll(recurDeptList(parentId, deptList));
cacheDeptIds.add(parentId); // 避免重复递归
}
}
for (Long rootId : rootIds) {
list.addAll(recurDeptList(rootId, deptList));
}
// 列表为空说明所有的节点都是独立的
if (list.isEmpty()) {
return deptList.stream().map(item -> {
DeptVO deptVO = new DeptVO();
BeanUtil.copyProperties(item, deptVO);
return deptVO;
})
.collect(Collectors.toList());
}
return list;
}
@@ -92,7 +77,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
* @return
*/
public List<DeptVO> recurDeptList(Long parentId, List<SysDept> deptList) {
List<DeptVO> list = deptList.stream()
return deptList.stream()
.filter(dept -> dept.getParentId().equals(parentId))
.map(dept -> {
DeptVO deptVO = deptConverter.entity2Vo(dept);
@@ -100,10 +85,8 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
deptVO.setChildren(children);
return deptVO;
}).collect(Collectors.toList());
return list;
}
/**
* 部门下拉选项
*
@@ -111,7 +94,6 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
*/
@Override
public List<Option> listDeptOptions() {
List<Option> options =new ArrayList<>();
List<SysDept> deptList = this.list(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getStatus, StatusEnum.ENABLE.getValue())
@@ -119,19 +101,21 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
.orderByAsc(SysDept::getSort)
);
Set<Long> parentIds = deptList.stream().map(SysDept::getParentId)
Set<Long> parentIds = deptList.stream()
.map(SysDept::getParentId)
.collect(Collectors.toSet());
Set<Long> childrenIds = deptList.stream().map(SysDept::getId)
Set<Long> deptIds = deptList.stream()
.map(SysDept::getId)
.collect(Collectors.toSet());
for (Long parentId : parentIds) {
if(!childrenIds.contains(parentId)){
options.addAll(recurDeptTreeOptions(parentId, deptList));
}
List<Long> rootIds = CollectionUtil.subtractToList(parentIds, deptIds);
List<Option> list = new ArrayList<>();
for (Long rootId : rootIds) {
list.addAll(recurDeptTreeOptions(rootId, deptList));
}
return options;
return list;
}
@Override
@@ -166,11 +150,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
* @return
*/
public static List<Option> recurDeptTreeOptions(long parentId, List<SysDept> deptList) {
if (CollectionUtil.isEmpty(deptList)) {
return Collections.EMPTY_LIST;
}
List<Option> list = deptList.stream()
List<Option> list = CollectionUtil.emptyIfNull(deptList).stream()
.filter(dept -> dept.getParentId().equals(parentId))
.map(dept -> {
Option option = new Option(dept.getId(), dept.getName());
@@ -181,7 +161,6 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
return option;
})
.collect(Collectors.toList());
return list;
}
@@ -226,21 +205,20 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
SysDept::getSort
));
DeptForm deptForm = deptConverter.entity2Form(entity);
return deptForm;
return deptConverter.entity2Form(entity);
}
/**
* 部门路径生成
*
* @param parentId
* @return
* @param parentId 父ID
* @return 父节点路径以英文逗号(, )分割eg: 1,2,3
*/
private String generateDeptTreePath(Long parentId) {
String treePath = null;
if (SystemConstants.ROOT_NODE_ID.equals(parentId)) {
treePath = parentId + "";
treePath = String.valueOf(parentId);
} else {
SysDept parent = this.getById(parentId);
if (parent != null) {
@@ -249,6 +227,4 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
}
return treePath;
}
}