♻️ #2585 基于 Hutool TreeUtils 重构目前系统自定义使用的TreeUtil

This commit is contained in:
lbw committed 2021-05-25 11:35:28 +08:00
1 parent c2c2ad928a
commit f595cda94e
18 files changed
+98 -577

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
-- 更新历史菜单排序避免空指针
UPDATE `sys_menu` SET `sort`=1 WHERE ISNULL(`sort`)
@@ -144,7 +144,7 @@ public class PigxTokenDealServiceImpl {
try {
cursor.close();
}
catch (IOException e) {
catch (Exception e) {
log.error("关闭cursor 失败");
}
return result;
@@ -1,4 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pigx.common.log.LogAutoConfiguration
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.env.EnvironmentPostProcessor=\
com.pig4cloud.pigx.common.log.init.ApplicationLoggerInitializer
@@ -1,46 +0,0 @@
/*
*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*
*/
package com.pig4cloud.pigx.admin.api.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 部门树对象
*
* @author lengleng
* @date 2020-06-19
*/
@Data
@ApiModel(value = "部门树")
@EqualsAndHashCode(callSuper = true)
public class DeptTree extends TreeNode {
@ApiModelProperty(value = "部门名称")
private String name;
/**
* 是否显示被锁定
*/
private Boolean isLock = true;
}
@@ -1,126 +0,0 @@
/*
*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*
*/
package com.pig4cloud.pigx.admin.api.dto;
import com.pig4cloud.pigx.admin.api.vo.MenuVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/**
* @author lengleng
* @date 2017年11月9日23:33:27
*/
@Data
@ApiModel(value = "菜单树")
@EqualsAndHashCode(callSuper = true)
public class MenuTree extends TreeNode implements Serializable {
/**
* 菜单图标
*/
@ApiModelProperty(value = "菜单图标")
private String icon;
/**
* 菜单名称
*/
@ApiModelProperty(value = "菜单名称")
private String name;
private boolean spread = false;
/**
* 前端路由标识路径
*/
@ApiModelProperty(value = "前端路由标识路径")
private String path;
/**
* 路由缓冲
*/
@ApiModelProperty(value = "路由缓冲")
private String keepAlive;
/**
* 权限编码
*/
@ApiModelProperty(value = "权限编码")
private String permission;
/**
* 菜单类型 (0菜单 1按钮)
*/
@ApiModelProperty(value = "菜单类型,0:菜单 1:按钮")
private String type;
/**
* 菜单标签
*/
@ApiModelProperty(value = "菜单标签")
private String label;
/**
* 排序值
*/
@ApiModelProperty(value = "排序值")
private Integer sort;
/**
* 是否包含子节点
*
* @since 3.7
*/
private Boolean hasChildren;
public MenuTree() {
}
public MenuTree(int id, String name, int parentId) {
this.id = id;
this.parentId = parentId;
this.name = name;
this.label = name;
}
public MenuTree(int id, String name, MenuTree parent) {
this.id = id;
this.parentId = parent.getId();
this.name = name;
this.label = name;
}
public MenuTree(MenuVO menuVo) {
this.id = menuVo.getMenuId();
this.parentId = menuVo.getParentId();
this.icon = menuVo.getIcon();
this.name = menuVo.getName();
this.path = menuVo.getPath();
this.type = menuVo.getType();
this.permission = menuVo.getPermission();
this.label = menuVo.getName();
this.sort = menuVo.getSort();
this.keepAlive = menuVo.getKeepAlive();
}
}
@@ -1,50 +0,0 @@
/*
*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*
*/
package com.pig4cloud.pigx.admin.api.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* @author lengleng
* @date 2017年11月9日23:33:45
*/
@Data
@ApiModel(value = "树形节点")
public class TreeNode {
@ApiModelProperty(value = "当前节点id")
protected int id;
@ApiModelProperty(value = "父节点id")
protected int parentId;
@ApiModelProperty(value = "子节点列表")
protected List<TreeNode> children = new ArrayList<TreeNode>();
public void add(TreeNode node) {
children.add(node);
}
}
@@ -1,123 +0,0 @@
/*
*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*
*/
package com.pig4cloud.pigx.admin.api.util;
import com.pig4cloud.pigx.admin.api.dto.MenuTree;
import com.pig4cloud.pigx.admin.api.dto.TreeNode;
import com.pig4cloud.pigx.admin.api.entity.SysMenu;
import lombok.experimental.UtilityClass;
import java.util.ArrayList;
import java.util.List;
/**
* @author lengleng
* @date 2020-02-09
*/
@UtilityClass
public class TreeUtil {
/**
* 两层循环实现建树
* @param treeNodes 传入的树节点列表
* @return
*/
public <T extends TreeNode> List<T> build(List<T> treeNodes, Object root) {
List<T> trees = new ArrayList<>();
for (T treeNode : treeNodes) {
if (root.equals(treeNode.getParentId())) {
trees.add(treeNode);
}
for (T it : treeNodes) {
if (it.getParentId() == treeNode.getId()) {
if (treeNode.getChildren() == null) {
treeNode.setChildren(new ArrayList<>());
}
treeNode.add(it);
}
}
}
return trees;
}
/**
* 使用递归方法建树
* @param treeNodes
* @return
*/
public <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes, Object root) {
List<T> trees = new ArrayList<T>();
for (T treeNode : treeNodes) {
if (root.equals(treeNode.getParentId())) {
trees.add(findChildren(treeNode, treeNodes));
}
}
return trees;
}
/**
* 递归查找子节点
* @param treeNodes
* @return
*/
public <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes) {
for (T it : treeNodes) {
if (treeNode.getId() == it.getParentId()) {
if (treeNode.getChildren() == null) {
treeNode.setChildren(new ArrayList<>());
}
treeNode.add(findChildren(it, treeNodes));
}
}
return treeNode;
}
/**
* 通过sysMenu创建树形节点
* @param menus
* @param root
* @return
*/
public List<MenuTree> buildTree(List<SysMenu> menus, int root) {
List<MenuTree> trees = new ArrayList<>();
MenuTree node;
for (SysMenu menu : menus) {
node = new MenuTree();
node.setId(menu.getMenuId());
node.setParentId(menu.getParentId());
node.setName(menu.getName());
node.setPath(menu.getPath());
node.setPermission(menu.getPermission());
node.setLabel(menu.getName());
node.setIcon(menu.getIcon());
node.setType(menu.getType());
node.setSort(menu.getSort());
node.setHasChildren(true);
node.setKeepAlive(menu.getKeepAlive());
trees.add(node);
}
return TreeUtil.build(trees, root);
}
}
@@ -1,134 +0,0 @@
/*
*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*
*/
package com.pig4cloud.pigx.admin.api.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
* 菜单权限表
* </p>
*
* @author lengleng
* @since 2017-11-08
*/
@Data
@ApiModel(value = "菜单展示对象")
public class MenuVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 菜单ID
*/
@ApiModelProperty(value = "菜单id")
private Integer menuId;
/**
* 菜单名称
*/
@ApiModelProperty(value = "菜单名称")
private String name;
/**
* 菜单权限标识
*/
@ApiModelProperty(value = "菜单权限标识")
private String permission;
/**
* 父菜单ID
*/
@ApiModelProperty(value = "父菜单id")
private Integer parentId;
/**
* 图标
*/
@ApiModelProperty(value = "图标")
private String icon;
/**
* 前端路由标识路径
*/
@ApiModelProperty(value = "前端路由标识路径")
private String path;
/**
* 排序值
*/
@ApiModelProperty(value = "排序值")
private Integer sort;
/**
* 菜单类型 (0菜单 1按钮)
*/
@ApiModelProperty(value = "菜单类型,0:菜单 1:按钮")
private String type;
/**
* 是否缓冲
*/
@ApiModelProperty(value = "路由缓冲")
private String keepAlive;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
/**
* 更新时间
*/
@ApiModelProperty(value = "更新时间")
private LocalDateTime updateTime;
/**
* 0--正常 1--删除
*/
@ApiModelProperty(value = "删除标记,1:已删除,0:正常")
private String delFlag;
@Override
public int hashCode() {
return menuId.hashCode();
}
/**
* menuId 相同则相同
* @param obj
* @return
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof MenuVO) {
Integer targetMenuId = ((MenuVO) obj).getMenuId();
return menuId.equals(targetMenuId);
}
return super.equals(obj);
}
}
@@ -20,7 +20,6 @@
package com.pig4cloud.pigx.admin.controller;
import com.pig4cloud.pigx.admin.api.entity.SysMenu;
import com.pig4cloud.pigx.admin.api.vo.MenuVO;
import com.pig4cloud.pigx.admin.service.SysMenuService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
@@ -57,7 +56,7 @@ public class SysMenuController {
public R getUserMenu(String type, Integer parentId) {
// 获取符合条件的菜单
Set<MenuVO> all = new HashSet<>();
Set<SysMenu> all = new HashSet<>();
SecurityUtils.getRoles().forEach(roleId -> all.addAll(sysMenuService.findMenuByRoleId(roleId)));
return R.ok(sysMenuService.filterMenu(all, type, parentId));
}
@@ -81,7 +80,7 @@ public class SysMenuController {
@GetMapping("/tree/{roleId}")
public R getRoleTree(@PathVariable Integer roleId) {
return R.ok(
sysMenuService.findMenuByRoleId(roleId).stream().map(MenuVO::getMenuId).collect(Collectors.toList()));
sysMenuService.findMenuByRoleId(roleId).stream().map(SysMenu::getMenuId).collect(Collectors.toList()));
}
/**
@@ -20,7 +20,6 @@
package com.pig4cloud.pigx.admin.mapper;
import com.pig4cloud.pigx.admin.api.entity.SysMenu;
import com.pig4cloud.pigx.admin.api.vo.MenuVO;
import com.pig4cloud.pigx.common.data.datascope.PigxBaseMapper;
import org.apache.ibatis.annotations.Mapper;
@@ -42,7 +41,7 @@ public interface SysMenuMapper extends PigxBaseMapper<SysMenu> {
* @param roleId 角色ID
* @return
*/
List<MenuVO> listMenusByRoleId(Integer roleId);
List<SysMenu> listMenusByRoleId(Integer roleId);
/**
* 通过角色ID查询权限
@@ -19,8 +19,8 @@
package com.pig4cloud.pigx.admin.service;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pigx.admin.api.dto.DeptTree;
import com.pig4cloud.pigx.admin.api.entity.SysDept;
import java.util.List;
@@ -39,7 +39,7 @@ public interface SysDeptService extends IService<SysDept> {
* 查询部门树菜单
* @return 树
*/
List<DeptTree> selectTree();
List<Tree<Integer>> selectTree();
/**
* 添加信息部门
@@ -19,10 +19,9 @@
package com.pig4cloud.pigx.admin.service;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pigx.admin.api.dto.MenuTree;
import com.pig4cloud.pigx.admin.api.entity.SysMenu;
import com.pig4cloud.pigx.admin.api.vo.MenuVO;
import com.pig4cloud.pigx.common.core.util.R;
import java.util.List;
@@ -43,7 +42,7 @@ public interface SysMenuService extends IService<SysMenu> {
* @param roleId 角色ID
* @return 菜单列表
*/
List<MenuVO> findMenuByRoleId(Integer roleId);
List<SysMenu> findMenuByRoleId(Integer roleId);
/**
* 级联删除菜单
@@ -65,7 +64,7 @@ public interface SysMenuService extends IService<SysMenu> {
* @param parentId 父节点ID
* @return
*/
List<MenuTree> treeMenu(boolean lazy, Integer parentId);
List<Tree<Integer>> treeMenu(boolean lazy, Integer parentId);
/**
* 查询菜单
@@ -73,6 +72,6 @@ public interface SysMenuService extends IService<SysMenu> {
* @param parentId
* @return
*/
List<MenuTree> filterMenu(Set<MenuVO> voSet, String type, Integer parentId);
List<Tree<Integer>> filterMenu(Set<SysMenu> voSet, String type, Integer parentId);
}
@@ -20,12 +20,14 @@
package com.pig4cloud.pigx.admin.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.dto.DeptTree;
import com.pig4cloud.pigx.admin.api.entity.SysDept;
import com.pig4cloud.pigx.admin.api.entity.SysDeptRelation;
import com.pig4cloud.pigx.admin.api.util.TreeUtil;
import com.pig4cloud.pigx.admin.mapper.SysDeptMapper;
import com.pig4cloud.pigx.admin.service.SysDeptRelationService;
import com.pig4cloud.pigx.admin.service.SysDeptService;
@@ -115,7 +117,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
* @return 树
*/
@Override
public List<DeptTree> selectTree() {
public List<Tree<Integer>> selectTree() {
// 查询全部部门
List<SysDept> deptAllList = deptMapper.selectList(Wrappers.emptyWrapper());
// 查询数据权限内部门
@@ -123,19 +125,20 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
.map(SysDept::getDeptId).collect(Collectors.toList());
// 权限内部门
List<DeptTree> collect = deptAllList.stream().filter(dept -> dept.getDeptId().intValue() != dept.getParentId())
List<TreeNode<Integer>> collect = deptAllList.stream()
.filter(dept -> dept.getDeptId().intValue() != dept.getParentId())
.sorted(Comparator.comparingInt(SysDept::getSort)).map(dept -> {
DeptTree node = new DeptTree();
node.setId(dept.getDeptId());
node.setParentId(dept.getParentId());
node.setName(dept.getName());
TreeNode<Integer> treeNode = new TreeNode();
treeNode.setId(dept.getDeptId());
treeNode.setParentId(dept.getParentId());
treeNode.setName(dept.getName());
// 有权限不返回标识
if (deptOwnIdList.contains(dept.getDeptId())) {
node.setIsLock(Boolean.FALSE);
treeNode.setExtra(MapUtil.of("isLock", Boolean.FALSE));
}
return node;
return treeNode;
}).collect(Collectors.toList());
return TreeUtil.build(collect, 0);
}
@@ -20,13 +20,13 @@
package com.pig4cloud.pigx.admin.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.dto.MenuTree;
import com.pig4cloud.pigx.admin.api.entity.SysMenu;
import com.pig4cloud.pigx.admin.api.entity.SysRoleMenu;
import com.pig4cloud.pigx.admin.api.vo.MenuVO;
import com.pig4cloud.pigx.admin.api.util.TreeUtil;
import com.pig4cloud.pigx.admin.mapper.SysMenuMapper;
import com.pig4cloud.pigx.admin.mapper.SysRoleMenuMapper;
import com.pig4cloud.pigx.admin.service.SysMenuService;
@@ -35,14 +35,17 @@ import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.enums.MenuTypeEnum;
import com.pig4cloud.pigx.common.core.util.R;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -62,7 +65,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
@Override
@Cacheable(value = CacheConstants.MENU_DETAILS, key = "#roleId", unless = "#result.isEmpty()")
public List<MenuVO> findMenuByRoleId(Integer roleId) {
public List<SysMenu> findMenuByRoleId(Integer roleId) {
return baseMapper.listMenusByRoleId(roleId);
}
@@ -94,18 +97,23 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
* @return
*/
@Override
public List<MenuTree> treeMenu(boolean lazy, Integer parentId) {
public List<Tree<Integer>> treeMenu(boolean lazy, Integer parentId) {
if (!lazy) {
return TreeUtil.buildTree(
baseMapper.selectList(Wrappers.<SysMenu>lambdaQuery().orderByAsc(SysMenu::getSort)),
CommonConstants.MENU_TREE_ROOT_ID);
List<TreeNode<Integer>> collect = baseMapper
.selectList(Wrappers.<SysMenu>lambdaQuery().orderByAsc(SysMenu::getSort)).stream()
.map(getNodeFunction()).collect(Collectors.toList());
return TreeUtil.build(collect, CommonConstants.MENU_TREE_ROOT_ID);
}
Integer parent = parentId == null ? CommonConstants.MENU_TREE_ROOT_ID : parentId;
return TreeUtil.buildTree(
baseMapper.selectList(
Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getParentId, parent).orderByAsc(SysMenu::getSort)),
parent);
List<TreeNode<Integer>> collect = baseMapper
.selectList(
Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getParentId, parent).orderByAsc(SysMenu::getSort))
.stream().map(getNodeFunction()).collect(Collectors.toList());
return TreeUtil.build(collect, parent);
}
/**
@@ -116,11 +124,33 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
* @return
*/
@Override
public List<MenuTree> filterMenu(Set<MenuVO> all, String type, Integer parentId) {
List<MenuTree> menuTreeList = all.stream().filter(menuTypePredicate(type)).map(MenuTree::new)
.sorted(Comparator.comparingInt(MenuTree::getSort)).collect(Collectors.toList());
public List<Tree<Integer>> filterMenu(Set<SysMenu> all, String type, Integer parentId) {
List<TreeNode<Integer>> collect = all.stream().filter(menuTypePredicate(type)).map(getNodeFunction())
.collect(Collectors.toList());
Integer parent = parentId == null ? CommonConstants.MENU_TREE_ROOT_ID : parentId;
return TreeUtil.build(menuTreeList, parent);
return TreeUtil.build(collect, parent);
}
@NotNull
private Function<SysMenu, TreeNode<Integer>> getNodeFunction() {
return menu -> {
TreeNode<Integer> node = new TreeNode<>();
node.setId(menu.getMenuId());
node.setName(menu.getName());
node.setParentId(menu.getParentId());
node.setWeight(menu.getSort());
// 扩展属性
Map<String, Object> extra = new HashMap<>();
extra.put("icon", menu.getIcon());
extra.put("path", menu.getPath());
extra.put("type", menu.getType());
extra.put("permission", menu.getPermission());
extra.put("label", menu.getName());
extra.put("sort", menu.getSort());
extra.put("keepAlive", menu.getKeepAlive());
node.setExtra(extra);
return node;
};
}
/**
@@ -128,7 +158,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
* @param type 类型
* @return Predicate
*/
private Predicate<MenuVO> menuTypePredicate(String type) {
private Predicate<SysMenu> menuTypePredicate(String type) {
return vo -> {
if (MenuTypeEnum.TOP_MENU.getDescription().equals(type)) {
return MenuTypeEnum.TOP_MENU.getType().equals(vo.getType());
@@ -16,12 +16,9 @@
*/
package com.pig4cloud.pigx.admin.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.dto.MenuTree;
import com.pig4cloud.pigx.admin.api.entity.*;
import com.pig4cloud.pigx.admin.api.util.TreeUtil;
import com.pig4cloud.pigx.admin.mapper.SysRoleMenuMapper;
import com.pig4cloud.pigx.admin.mapper.SysTenantMapper;
import com.pig4cloud.pigx.admin.service.*;
@@ -30,7 +27,6 @@ import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.data.resolver.ParamResolver;
import com.pig4cloud.pigx.common.data.tenant.TenantBroker;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@@ -159,8 +155,7 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
userRole.setRoleId(role.getRoleId());
userRoleService.save(userRole);
// 插入新的菜单
saveTenantMenu(TreeUtil.buildTree(menuList, CommonConstants.MENU_TREE_ROOT_ID),
CommonConstants.MENU_TREE_ROOT_ID);
saveTenantMenu(menuList, CommonConstants.MENU_TREE_ROOT_ID, CommonConstants.MENU_TREE_ROOT_ID);
List<SysMenu> newMenuList = menuService.list();
// 查询全部菜单,构造角色菜单关系
@@ -189,21 +184,19 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
/**
* 保存新的租户菜单,维护成新的菜单
* @param nodeList 节点树
* @param parent 上级
* @param menuList 菜单列表
* @param originParentId 原始上级
* @param targetParentId 目标上级
*/
private void saveTenantMenu(List<MenuTree> nodeList, Integer parent) {
for (MenuTree node : nodeList) {
SysMenu menu = new SysMenu();
BeanUtils.copyProperties(node, menu, "parentId");
menu.setParentId(parent);
private void saveTenantMenu(List<SysMenu> menuList, Integer originParentId, Integer targetParentId) {
menuList.stream().filter(menu -> menu.getParentId().equals(originParentId)).forEach(menu -> {
// 保存菜单原始menuId, 方便查询子节点使用
Integer originMenuId = menu.getMenuId();
menu.setParentId(targetParentId);
menuService.save(menu);
if (CollUtil.isNotEmpty(node.getChildren())) {
List<MenuTree> childrenList = node.getChildren().stream().map(treeNode -> (MenuTree) treeNode)
.collect(Collectors.toList());
saveTenantMenu(childrenList, menu.getMenuId());
}
}
// 查找此节点的子节点,然后子节点的重新插入父节点更改为新的menuId
saveTenantMenu(menuList, originMenuId, menu.getMenuId());
});
}
}
@@ -19,12 +19,6 @@
package com.pig4cloud.pigx.admin.service.impl;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -33,20 +27,10 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.dto.UserDTO;
import com.pig4cloud.pigx.admin.api.dto.UserInfo;
import com.pig4cloud.pigx.admin.api.entity.SysDept;
import com.pig4cloud.pigx.admin.api.entity.SysDeptRelation;
import com.pig4cloud.pigx.admin.api.entity.SysRole;
import com.pig4cloud.pigx.admin.api.entity.SysUser;
import com.pig4cloud.pigx.admin.api.entity.SysUserRole;
import com.pig4cloud.pigx.admin.api.vo.MenuVO;
import com.pig4cloud.pigx.admin.api.entity.*;
import com.pig4cloud.pigx.admin.api.vo.UserVO;
import com.pig4cloud.pigx.admin.mapper.SysUserMapper;
import com.pig4cloud.pigx.admin.service.SysDeptRelationService;
import com.pig4cloud.pigx.admin.service.SysDeptService;
import com.pig4cloud.pigx.admin.service.SysMenuService;
import com.pig4cloud.pigx.admin.service.SysRoleService;
import com.pig4cloud.pigx.admin.service.SysUserRoleService;
import com.pig4cloud.pigx.admin.service.SysUserService;
import com.pig4cloud.pigx.admin.service.*;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.util.R;
@@ -61,6 +45,12 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author lengleng
* @date 2017/10/31
@@ -122,7 +112,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
Set<String> permissions = new HashSet<>();
roleIds.forEach(roleId -> {
List<String> permissionList = sysMenuService.findMenuByRoleId(roleId).stream()
.filter(menuVo -> StrUtil.isNotEmpty(menuVo.getPermission())).map(MenuVO::getPermission)
.filter(menu -> StrUtil.isNotEmpty(menu.getPermission())).map(SysMenu::getPermission)
.collect(Collectors.toList());
permissions.addAll(permissionList);
});
@@ -37,23 +37,8 @@
<result column="del_flag" property="delFlag"/>
</resultMap>
<resultMap id="MenuVoResultMap" type="com.pig4cloud.pigx.admin.api.vo.MenuVO">
<id column="menu_id" property="menuId"/>
<result column="name" property="name"/>
<result column="permission" property="permission"/>
<result column="parent_id" property="parentId"/>
<result column="icon" property="icon"/>
<result column="path" property="path"/>
<result column="sort" property="sort"/>
<result column="keep_alive" property="keepAlive"/>
<result column="type" property="type"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="del_flag" property="delFlag"/>
</resultMap>
<!--通过角色查询菜单信息-->
<select id="listMenusByRoleId" resultMap="MenuVoResultMap">
<select id="listMenusByRoleId" resultMap="BaseResultMap">
SELECT
sys_menu.*
FROM
+1 -1
View File
@@ -33,7 +33,7 @@
</organization>
<properties>
<spring-boot.version>2.4.5</spring-boot.version>
<spring-boot.version>2.5.0</spring-boot.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>