refactor: ✂️递归获取菜单、部门属性列表代码重构优化
This commit is contained in:
@@ -73,4 +73,9 @@ public class SysMenu extends BaseEntity {
|
||||
*/
|
||||
private String redirect;
|
||||
|
||||
/**
|
||||
* 父节点路径,以英文逗号(,)分割
|
||||
*/
|
||||
private String treePath;
|
||||
|
||||
}
|
||||
@@ -20,14 +20,11 @@ public class MenuVO {
|
||||
@Schema(description = "菜单名称")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "ICON")
|
||||
private String icon;
|
||||
@Schema(description="菜单类型")
|
||||
private MenuTypeEnum type;
|
||||
|
||||
@Schema(description = "路由名称")
|
||||
private String routeName;
|
||||
|
||||
@Schema(description = "路由相对路径")
|
||||
private String routePath;
|
||||
@Schema(description = "路由路径")
|
||||
private String path;
|
||||
|
||||
@Schema(description = "组件路径")
|
||||
private String component;
|
||||
@@ -38,12 +35,12 @@ public class MenuVO {
|
||||
@Schema(description = "菜单是否可见(1:显示;0:隐藏)")
|
||||
private Integer visible;
|
||||
|
||||
@Schema(description = "ICON")
|
||||
private String icon;
|
||||
|
||||
@Schema(description = "跳转路径")
|
||||
private String redirect;
|
||||
|
||||
@Schema(description="菜单类型")
|
||||
private MenuTypeEnum type;
|
||||
|
||||
@Schema(description="按钮权限标识")
|
||||
private String perm;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
@@ -9,22 +9,24 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.common.constant.SystemConstants;
|
||||
import com.youlai.system.common.enums.MenuTypeEnum;
|
||||
import com.youlai.system.common.enums.StatusEnum;
|
||||
import com.youlai.system.pojo.entity.SysDept;
|
||||
import com.youlai.system.pojo.form.MenuForm;
|
||||
import com.youlai.system.pojo.vo.Option;
|
||||
import com.youlai.system.converter.MenuConverter;
|
||||
import com.youlai.system.mapper.SysMenuMapper;
|
||||
import com.youlai.system.pojo.entity.SysMenu;
|
||||
import com.youlai.system.pojo.bo.RouteBO;
|
||||
import com.youlai.system.pojo.entity.SysMenu;
|
||||
import com.youlai.system.pojo.form.MenuForm;
|
||||
import com.youlai.system.pojo.query.MenuQuery;
|
||||
import com.youlai.system.pojo.vo.MenuVO;
|
||||
import com.youlai.system.pojo.vo.Option;
|
||||
import com.youlai.system.pojo.vo.RouteVO;
|
||||
import com.youlai.system.service.SysMenuService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -40,6 +42,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
|
||||
/**
|
||||
* 菜单列表
|
||||
*
|
||||
* @param queryParams {@link MenuQuery}
|
||||
*/
|
||||
@Override
|
||||
public List<MenuVO> listMenus(MenuQuery queryParams) {
|
||||
@@ -48,19 +52,20 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
.orderByAsc(SysMenu::getSort)
|
||||
);
|
||||
|
||||
Set<Long> cacheMenuIds = menus.stream()
|
||||
.map(menu -> menu.getId())
|
||||
Set<Long> parentIds = menus.stream()
|
||||
.map(SysMenu::getParentId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<MenuVO> list = menus.stream().map(menu -> {
|
||||
Long parentId = menu.getParentId();
|
||||
// parentId不在当前菜单ID的列表,说明为顶级菜单ID,根据此ID作为递归的开始节点
|
||||
if (!cacheMenuIds.contains(parentId)) {
|
||||
cacheMenuIds.add(parentId);
|
||||
return recurMenus(parentId, menus);
|
||||
}
|
||||
return new LinkedList<MenuVO>();
|
||||
}).collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);
|
||||
Set<Long> menuIds = menus.stream()
|
||||
.map(SysMenu::getId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<Long> rootIds = CollectionUtil.subtractToList(parentIds, menuIds); // 求差集,得到 parentIds 中 menuIds 没有的元素
|
||||
|
||||
List<MenuVO> list = new ArrayList<>();
|
||||
for (Long rootId : rootIds) {
|
||||
list.addAll(recurMenus(rootId, menus)); // 递归
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@@ -73,18 +78,20 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
|
||||
MenuTypeEnum menuType = menuForm.getType(); // 菜单类型
|
||||
switch (menuType) {
|
||||
case CATALOG: // 目录
|
||||
Assert.isTrue(path.startsWith("/"), "目录路由路径格式错误,必须以/开始");
|
||||
case CATALOG -> { // 目录
|
||||
if (NumberUtil.equals(menuForm.getParentId(), 0) && !path.startsWith("/")) {
|
||||
menuForm.setPath("/" + path); // 一级目录需以 / 开头
|
||||
}
|
||||
menuForm.setComponent("Layout");
|
||||
break;
|
||||
case EXTLINK: // 外链
|
||||
menuForm.setComponent(null);
|
||||
break;
|
||||
}
|
||||
case EXTLINK -> // 外链
|
||||
menuForm.setComponent(null);
|
||||
}
|
||||
SysMenu entity = menuConverter.form2Entity(menuForm);
|
||||
|
||||
boolean result = this.saveOrUpdate(entity);
|
||||
return result;
|
||||
String treePath = generateMenuTreePath(menuForm.getParentId());
|
||||
entity.setTreePath(treePath);
|
||||
return this.saveOrUpdate(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,8 +100,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
@Override
|
||||
public List<Option> listMenuOptions() {
|
||||
List<SysMenu> menuList = this.list(new LambdaQueryWrapper<SysMenu>().orderByAsc(SysMenu::getSort));
|
||||
List<Option> menus = recurMenuOptions(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
return menus;
|
||||
return recurMenuOptions(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,8 +110,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
@Cacheable(cacheNames = "system", key = "'routes'")
|
||||
public List<RouteVO> listRoutes() {
|
||||
List<RouteBO> menuList = this.baseMapper.listRoutes();
|
||||
List<RouteVO> routeList = recurRoutes(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
return routeList;
|
||||
return recurRoutes(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,14 +121,14 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
* @return
|
||||
*/
|
||||
private List<RouteVO> recurRoutes(Long parentId, List<RouteBO> menuList) {
|
||||
List<RouteVO> list = new ArrayList<>();
|
||||
Optional.ofNullable(menuList).ifPresent(menus -> menus.stream()
|
||||
return CollectionUtil.emptyIfNull(menuList).stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.forEach(menu -> {
|
||||
.map(menu -> {
|
||||
RouteVO routeVO = new RouteVO();
|
||||
MenuTypeEnum menuTypeEnum = menu.getType();
|
||||
if (MenuTypeEnum.MENU.equals(menuTypeEnum)) {
|
||||
routeVO.setName(menu.getPath()); // 根据name路由跳转 this.$router.push({name:xxx})
|
||||
String routeName = StringUtils.capitalize(StrUtil.toCamelCase(menu.getPath(), '-')); // 路由 name 需要驼峰,首字母大写
|
||||
routeVO.setName(routeName); // 根据name路由跳转 this.$router.push({name:xxx})
|
||||
}
|
||||
routeVO.setPath(menu.getPath()); // 根据path路由跳转 this.$router.push({path:xxx})
|
||||
routeVO.setRedirect(menu.getRedirectUrl());
|
||||
@@ -139,9 +144,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
|
||||
List<RouteVO> children = recurRoutes(menu.getId(), menuList);
|
||||
routeVO.setChildren(children);
|
||||
list.add(routeVO);
|
||||
}));
|
||||
return list;
|
||||
return routeVO;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
|
||||
@@ -154,11 +158,10 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
*/
|
||||
@Override
|
||||
public boolean updateMenuVisible(Long menuId, Integer visible) {
|
||||
boolean result = this.update(new LambdaUpdateWrapper<SysMenu>()
|
||||
return this.update(new LambdaUpdateWrapper<SysMenu>()
|
||||
.eq(SysMenu::getId, menuId)
|
||||
.set(SysMenu::getVisible, visible)
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,19 +214,15 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
* @return
|
||||
*/
|
||||
private List<MenuVO> recurMenus(Long parentId, List<SysMenu> menuList) {
|
||||
if (CollectionUtil.isEmpty(menuList)) {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
List<MenuVO> menus = menuList.stream()
|
||||
return CollectionUtil.emptyIfNull(menuList)
|
||||
.stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.map(entity -> {
|
||||
MenuVO menuVO = menuConverter.entity2Vo(entity);
|
||||
List<MenuVO> children = recurMenus(entity.getId(), menuList);
|
||||
menuVO.setChildren(children);
|
||||
return menuVO;
|
||||
}).collect(Collectors.toList());
|
||||
return menus;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -234,11 +233,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
* @return
|
||||
*/
|
||||
private static List<Option> recurMenuOptions(Long parentId, List<SysMenu> menuList) {
|
||||
if (CollectionUtil.isEmpty(menuList)) {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
List<Option> menus = menuList.stream()
|
||||
List<Option> menus = CollectionUtil.emptyIfNull(menuList).stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.map(menu -> new Option(menu.getId(), menu.getName(), recurMenuOptions(menu.getId(), menuList)))
|
||||
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
||||
@@ -246,4 +241,24 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 部门路径生成
|
||||
*
|
||||
* @param parentId 父ID
|
||||
* @return 父节点路径以英文逗号(, )分割,eg: 1,2,3
|
||||
*/
|
||||
private String generateMenuTreePath(Long parentId) {
|
||||
String treePath = null;
|
||||
if (SystemConstants.ROOT_NODE_ID.equals(parentId)) {
|
||||
treePath = String.valueOf(parentId);
|
||||
} else {
|
||||
SysMenu parent = this.getById(parentId);
|
||||
if (parent != null) {
|
||||
treePath = parent.getTreePath() + "," + parent.getId();
|
||||
}
|
||||
}
|
||||
return treePath;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user