mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-19 17:58:17 +08:00
树结构使用hutool工具生成
This commit is contained in:
parent
4f4d33d750
commit
86359034e4
@ -1,50 +0,0 @@
|
|||||||
package com.ruoyi.common.core.domain;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
|
||||||
import com.ruoyi.common.core.domain.entity.SysDept;
|
|
||||||
import com.ruoyi.common.core.domain.entity.SysMenu;
|
|
||||||
import lombok.*;
|
|
||||||
import lombok.experimental.Accessors;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Treeselect树结构实体类
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@NoArgsConstructor
|
|
||||||
@Accessors(chain = true)
|
|
||||||
public class TreeSelect implements Serializable
|
|
||||||
{
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
/** 节点ID */
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
/** 节点名称 */
|
|
||||||
private String label;
|
|
||||||
|
|
||||||
/** 子节点 */
|
|
||||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
|
||||||
private List<TreeSelect> children;
|
|
||||||
|
|
||||||
public TreeSelect(SysDept dept)
|
|
||||||
{
|
|
||||||
this.id = dept.getDeptId();
|
|
||||||
this.label = dept.getDeptName();
|
|
||||||
this.children = dept.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public TreeSelect(SysMenu menu)
|
|
||||||
{
|
|
||||||
this.id = menu.getMenuId();
|
|
||||||
this.label = menu.getMenuName();
|
|
||||||
this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package com.ruoyi.common.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.tree.Tree;
|
||||||
|
import cn.hutool.core.lang.tree.TreeNodeConfig;
|
||||||
|
import cn.hutool.core.lang.tree.TreeUtil;
|
||||||
|
import cn.hutool.core.lang.tree.parser.NodeParser;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TreeUtils {
|
||||||
|
|
||||||
|
public static final TreeNodeConfig DEFAULT_TREE_NODE_CONFIG = new TreeNodeConfig();
|
||||||
|
|
||||||
|
static
|
||||||
|
{
|
||||||
|
DEFAULT_TREE_NODE_CONFIG.setNameKey("label");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> List<Tree<Long>> build(List<T> list, NodeParser<T, Long> nodeParser)
|
||||||
|
{
|
||||||
|
Long parentId = 0L;
|
||||||
|
return TreeUtil.build(list, parentId, DEFAULT_TREE_NODE_CONFIG, nodeParser);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
package com.ruoyi.system.service;
|
package com.ruoyi.system.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.tree.Tree;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.ruoyi.common.core.domain.TreeSelect;
|
|
||||||
import com.ruoyi.common.core.domain.entity.SysDept;
|
import com.ruoyi.common.core.domain.entity.SysDept;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -20,21 +20,13 @@ public interface ISysDeptService extends IService<SysDept> {
|
|||||||
*/
|
*/
|
||||||
public List<SysDept> selectDeptList(SysDept dept);
|
public List<SysDept> selectDeptList(SysDept dept);
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建前端所需要树结构
|
|
||||||
*
|
|
||||||
* @param depts 部门列表
|
|
||||||
* @return 树结构列表
|
|
||||||
*/
|
|
||||||
public List<SysDept> buildDeptTree(List<SysDept> depts);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建前端所需要下拉树结构
|
* 构建前端所需要下拉树结构
|
||||||
*
|
*
|
||||||
* @param depts 部门列表
|
* @param depts 部门列表
|
||||||
* @return 下拉树结构列表
|
* @return 下拉树结构列表
|
||||||
*/
|
*/
|
||||||
public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts);
|
public List<Tree<Long>> buildDeptTreeSelect(List<SysDept> depts);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据角色ID查询部门树信息
|
* 根据角色ID查询部门树信息
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package com.ruoyi.system.service;
|
package com.ruoyi.system.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.tree.Tree;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.ruoyi.common.core.domain.TreeSelect;
|
|
||||||
import com.ruoyi.common.core.domain.entity.SysMenu;
|
import com.ruoyi.common.core.domain.entity.SysMenu;
|
||||||
import com.ruoyi.system.domain.vo.RouterVo;
|
import com.ruoyi.system.domain.vo.RouterVo;
|
||||||
|
|
||||||
@ -63,21 +63,13 @@ public interface ISysMenuService extends IService<SysMenu> {
|
|||||||
*/
|
*/
|
||||||
public List<RouterVo> buildMenus(List<SysMenu> menus);
|
public List<RouterVo> buildMenus(List<SysMenu> menus);
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建前端所需要树结构
|
|
||||||
*
|
|
||||||
* @param menus 菜单列表
|
|
||||||
* @return 树结构列表
|
|
||||||
*/
|
|
||||||
public List<SysMenu> buildMenuTree(List<SysMenu> menus);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建前端所需要下拉树结构
|
* 构建前端所需要下拉树结构
|
||||||
*
|
*
|
||||||
* @param menus 菜单列表
|
* @param menus 菜单列表
|
||||||
* @return 下拉树结构列表
|
* @return 下拉树结构列表
|
||||||
*/
|
*/
|
||||||
public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus);
|
public List<Tree<Long>> buildMenuTreeSelect(List<SysMenu> menus);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据菜单ID查询信息
|
* 根据菜单ID查询信息
|
||||||
|
|||||||
@ -1,17 +1,18 @@
|
|||||||
package com.ruoyi.system.service.impl;
|
package com.ruoyi.system.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.lang.tree.Tree;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.ruoyi.common.annotation.DataScope;
|
import com.ruoyi.common.annotation.DataScope;
|
||||||
import com.ruoyi.common.constant.UserConstants;
|
import com.ruoyi.common.constant.UserConstants;
|
||||||
import com.ruoyi.common.core.domain.TreeSelect;
|
|
||||||
import com.ruoyi.common.core.domain.entity.SysDept;
|
import com.ruoyi.common.core.domain.entity.SysDept;
|
||||||
import com.ruoyi.common.core.domain.entity.SysRole;
|
import com.ruoyi.common.core.domain.entity.SysRole;
|
||||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||||
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
||||||
import com.ruoyi.common.exception.ServiceException;
|
import com.ruoyi.common.exception.ServiceException;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import com.ruoyi.common.utils.TreeUtils;
|
||||||
import com.ruoyi.system.mapper.SysDeptMapper;
|
import com.ruoyi.system.mapper.SysDeptMapper;
|
||||||
import com.ruoyi.system.mapper.SysRoleMapper;
|
import com.ruoyi.system.mapper.SysRoleMapper;
|
||||||
import com.ruoyi.system.mapper.SysUserMapper;
|
import com.ruoyi.system.mapper.SysUserMapper;
|
||||||
@ -19,10 +20,8 @@ import com.ruoyi.system.service.ISysDeptService;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门管理 服务实现
|
* 部门管理 服务实现
|
||||||
@ -50,32 +49,6 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
|
|||||||
return baseMapper.selectDeptList(dept);
|
return baseMapper.selectDeptList(dept);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建前端所需要树结构
|
|
||||||
*
|
|
||||||
* @param depts 部门列表
|
|
||||||
* @return 树结构列表
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public List<SysDept> buildDeptTree(List<SysDept> depts) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建前端所需要下拉树结构
|
* 构建前端所需要下拉树结构
|
||||||
*
|
*
|
||||||
@ -83,9 +56,14 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
|
|||||||
* @return 下拉树结构列表
|
* @return 下拉树结构列表
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts) {
|
public List<Tree<Long>> buildDeptTreeSelect(List<SysDept> depts)
|
||||||
List<SysDept> deptTrees = buildDeptTree(depts);
|
{
|
||||||
return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
|
return TreeUtils.build(depts, (dept, tree) -> {
|
||||||
|
tree.setId(dept.getDeptId());
|
||||||
|
tree.setParentId(dept.getParentId());
|
||||||
|
tree.setName(dept.getDeptName());
|
||||||
|
tree.setWeight(dept.getOrderNum());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -254,37 +232,4 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
|
|||||||
return baseMapper.deleteById(deptId);
|
return baseMapper.deleteById(deptId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 递归列表
|
|
||||||
*/
|
|
||||||
private void recursionFn(List<SysDept> list, SysDept t) {
|
|
||||||
// 得到子节点列表
|
|
||||||
List<SysDept> childList = getChildList(list, t);
|
|
||||||
t.setChildren(childList);
|
|
||||||
for (SysDept tChild : childList) {
|
|
||||||
if (hasChild(list, tChild)) {
|
|
||||||
recursionFn(list, tChild);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到子节点列表
|
|
||||||
*/
|
|
||||||
private List<SysDept> getChildList(List<SysDept> list, SysDept t) {
|
|
||||||
List<SysDept> tlist = new ArrayList<SysDept>();
|
|
||||||
for (SysDept n : list) {
|
|
||||||
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) {
|
|
||||||
tlist.add(n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tlist;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断是否有子节点
|
|
||||||
*/
|
|
||||||
private boolean hasChild(List<SysDept> list, SysDept t) {
|
|
||||||
return getChildList(list, t).size() > 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,16 @@
|
|||||||
package com.ruoyi.system.service.impl;
|
package com.ruoyi.system.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.tree.Tree;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.constant.Constants;
|
||||||
import com.ruoyi.common.constant.UserConstants;
|
import com.ruoyi.common.constant.UserConstants;
|
||||||
import com.ruoyi.common.core.domain.TreeSelect;
|
|
||||||
import com.ruoyi.common.core.domain.entity.SysMenu;
|
import com.ruoyi.common.core.domain.entity.SysMenu;
|
||||||
import com.ruoyi.common.core.domain.entity.SysRole;
|
import com.ruoyi.common.core.domain.entity.SysRole;
|
||||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||||
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
||||||
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.SecurityUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import com.ruoyi.common.utils.TreeUtils;
|
||||||
import com.ruoyi.system.domain.SysRoleMenu;
|
import com.ruoyi.system.domain.SysRoleMenu;
|
||||||
import com.ruoyi.system.domain.vo.MetaVo;
|
import com.ruoyi.system.domain.vo.MetaVo;
|
||||||
import com.ruoyi.system.domain.vo.RouterVo;
|
import com.ruoyi.system.domain.vo.RouterVo;
|
||||||
@ -21,7 +22,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 菜单 业务层处理
|
* 菜单 业务层处理
|
||||||
@ -169,32 +169,6 @@ public class SysMenuServiceImpl extends ServicePlusImpl<SysMenuMapper, SysMenu,
|
|||||||
return routers;
|
return routers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建前端所需要树结构
|
|
||||||
*
|
|
||||||
* @param menus 菜单列表
|
|
||||||
* @return 树结构列表
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public List<SysMenu> buildMenuTree(List<SysMenu> menus) {
|
|
||||||
List<SysMenu> returnList = new ArrayList<SysMenu>();
|
|
||||||
List<Long> tempList = new ArrayList<Long>();
|
|
||||||
for (SysMenu dept : menus) {
|
|
||||||
tempList.add(dept.getMenuId());
|
|
||||||
}
|
|
||||||
for (SysMenu menu : menus) {
|
|
||||||
// 如果是顶级节点, 遍历该父节点的所有子节点
|
|
||||||
if (!tempList.contains(menu.getParentId())) {
|
|
||||||
recursionFn(menus, menu);
|
|
||||||
returnList.add(menu);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (returnList.isEmpty()) {
|
|
||||||
returnList = menus;
|
|
||||||
}
|
|
||||||
return returnList;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建前端所需要下拉树结构
|
* 构建前端所需要下拉树结构
|
||||||
*
|
*
|
||||||
@ -202,9 +176,14 @@ public class SysMenuServiceImpl extends ServicePlusImpl<SysMenuMapper, SysMenu,
|
|||||||
* @return 下拉树结构列表
|
* @return 下拉树结构列表
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus) {
|
public List<Tree<Long>> buildMenuTreeSelect(List<SysMenu> menus)
|
||||||
List<SysMenu> menuTrees = buildMenuTree(menus);
|
{
|
||||||
return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
|
return TreeUtils.build(menus, (menu, tree) -> {
|
||||||
|
tree.setId(menu.getMenuId());
|
||||||
|
tree.setParentId(menu.getParentId());
|
||||||
|
tree.setName(menu.getMenuName());
|
||||||
|
tree.setWeight(menu.getOrderNum());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user