/*
|
* Copyright (c) 2018-2028, Chill Zhuang 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 dreamlu.net 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: Chill 庄骞 (smallchill@163.com)
|
*/
|
package org.springblade.modules.system.service.impl;
|
|
import com.alibaba.fastjson.JSON;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import lombok.AllArgsConstructor;
|
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springblade.common.cache.SysCache;
|
import org.springblade.core.log.exception.ServiceException;
|
import org.springblade.core.secure.BladeUser;
|
import org.springblade.core.secure.utils.AuthUtil;
|
import org.springblade.core.tool.constant.BladeConstant;
|
import org.springblade.core.tool.constant.RoleConstant;
|
import org.springblade.core.tool.node.ForestNodeMerger;
|
import org.springblade.core.tool.node.TreeNode;
|
import org.springblade.core.tool.support.Kv;
|
import org.springblade.core.tool.utils.Func;
|
import org.springblade.core.tool.utils.StringUtil;
|
import org.springblade.modules.house.dto.UserHouseLabelDTO;
|
import org.springblade.modules.house.service.IUserHouseLabelService;
|
import org.springblade.modules.system.dto.MenuDTO;
|
import org.springblade.modules.system.entity.*;
|
import org.springblade.modules.system.mapper.MenuMapper;
|
import org.springblade.modules.system.service.IMenuService;
|
import org.springblade.modules.system.service.IRoleMenuService;
|
import org.springblade.modules.system.service.IRoleScopeService;
|
import org.springblade.modules.system.service.ITopMenuSettingService;
|
import org.springblade.modules.system.vo.MenuVO;
|
import org.springblade.modules.system.wrapper.MenuWrapper;
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.io.Serializable;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
import static org.springblade.common.constant.CommonConstant.API_SCOPE_CATEGORY;
|
import static org.springblade.common.constant.CommonConstant.DATA_SCOPE_CATEGORY;
|
import static org.springblade.core.cache.constant.CacheConstant.MENU_CACHE;
|
|
/**
|
* 服务实现类
|
*
|
* @author Chill
|
*/
|
@Service
|
@AllArgsConstructor
|
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
|
|
private final IRoleMenuService roleMenuService;
|
private final IRoleScopeService roleScopeService;
|
private final ITopMenuSettingService topMenuSettingService;
|
private final static String PARENT_ID = "parentId";
|
private final static Integer MENU_CATEGORY = 1;
|
|
@Resource
|
private IUserHouseLabelService iUserHouseLabelService;
|
|
@Override
|
public List<MenuVO> lazyList(Long parentId, Map<String, Object> param) {
|
if (Func.isEmpty(Func.toStr(param.get(PARENT_ID)))) {
|
parentId = null;
|
}
|
List<MenuVO> menuVOS = baseMapper.lazyList(parentId, param);
|
for (MenuVO menuVO : menuVOS) {
|
if(StringUtils.isNotBlank(menuVO.getLabelStr())){
|
List<List<String>> lists = (List<List<String>>)JSON.parse(menuVO.getLabelStr() );
|
menuVO.setLabelList(lists);
|
}
|
}
|
return menuVOS;
|
}
|
|
@Override
|
public List<MenuVO> lazyMenuList(Long parentId, Map<String, Object> param) {
|
if (Func.isEmpty(Func.toStr(param.get(PARENT_ID)))) {
|
parentId = null;
|
}
|
List<MenuVO> menuVOS = baseMapper.lazyList(parentId, param);
|
for (MenuVO menuVO : menuVOS) {
|
if(StringUtils.isNotBlank(menuVO.getLabelStr())){
|
List<List<String>> lists = (List<List<String>>)JSON.parse(menuVO.getLabelStr() );
|
menuVO.setLabelList(lists);
|
}
|
}
|
return menuVOS;
|
}
|
|
public static final List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5, 6, 11, null);
|
public static final List<Integer> list2 = Arrays.asList(1, 11, null);
|
|
/**
|
* apache测试方法
|
*/
|
public static void main(String[] args) {
|
System.out.println("交集:" + CollectionUtils.intersection(list1, list2)); // 交集
|
System.out.println("补集:" + CollectionUtils.disjunction(list1, list2)); // 补集
|
System.out.println("并集:" + CollectionUtils.union(list1, list2)); // 并集
|
System.out.println("list1的差集:" + CollectionUtils.subtract(list1, list2)); // list1的差集
|
System.out.println("list2的差集:" + CollectionUtils.subtract(list2, list1)); // list2的差集
|
}
|
|
@Override
|
public List<MenuVO> routes(String roleId, Long topMenuId, Integer labelType) {
|
if (StringUtil.isBlank(roleId)) {
|
return null;
|
}
|
List<Menu> allMenus = baseMapper.allMenu();
|
List<Menu> roleMenus;
|
// 超级管理员并且不是顶部菜单请求则返回全部菜单
|
if (AuthUtil.isAdministrator() && Func.isEmpty(topMenuId)) {
|
roleMenus = allMenus;
|
}
|
// 非超级管理员并且不是顶部菜单请求则返回对应角色权限菜单
|
else if (!AuthUtil.isAdministrator() && Func.isEmpty(topMenuId)) {
|
roleMenus = tenantPackageMenu(baseMapper.roleMenuByRoleId(Func.toLongList(roleId)));
|
UserHouseLabelDTO userHouseLabelDTO = new UserHouseLabelDTO();
|
userHouseLabelDTO.setUserId(AuthUtil.getUserId());
|
userHouseLabelDTO.setLableType(labelType);
|
List<Integer> integers = iUserHouseLabelService.selectUserLabelList(userHouseLabelDTO);
|
Iterator<Menu> iterator = roleMenus.iterator();
|
while (iterator.hasNext()) {
|
Menu next = iterator.next();
|
if (!next.getParentId().equals(0)) {
|
if (StringUtils.isNotBlank(next.getLabelId())) {
|
String[] split = next.getLabelId().split(",");
|
List<Integer> integerList = Arrays.stream(split).map(Integer::valueOf).collect(Collectors.toList());
|
Collection<? extends Serializable> union = CollectionUtils.intersection(integerList, integers);
|
if (union.size() == 0) {
|
iterator.remove();
|
}
|
} else {
|
if (next.getName().trim().equals("标签报事")) {
|
iterator.remove();
|
}
|
if (next.getName().trim().equals("取保候审")) {
|
iterator.remove();
|
}
|
if (next.getName().trim().equals("打金店")) {
|
iterator.remove();
|
}
|
if (next.getName().trim().equals("旅馆安全")) {
|
iterator.remove();
|
}
|
if (next.getName().trim().equals("二手车交易")) {
|
iterator.remove();
|
}
|
if (next.getName().trim().equals("二手手机维修")) {
|
iterator.remove();
|
}
|
if (next.getName().trim().equals("校园安全")) {
|
iterator.remove();
|
}
|
}
|
}
|
}
|
}
|
// 顶部菜单请求返回对应角色权限菜单
|
else {
|
// 角色配置对应菜单
|
List<Menu> roleIdMenus = baseMapper.roleMenuByRoleId(Func.toLongList(roleId));
|
// 反向递归角色菜单所有父级
|
List<Menu> routes = new LinkedList<>(roleIdMenus);
|
roleIdMenus.forEach(roleMenu -> recursion(allMenus, routes, roleMenu));
|
// 顶部配置对应菜单
|
List<Menu> topIdMenus = baseMapper.roleMenuByTopMenuId(topMenuId);
|
// 筛选匹配角色对应的权限菜单
|
roleMenus = topIdMenus.stream().filter(x ->
|
routes.stream().anyMatch(route -> route.getId().longValue() == x.getId().longValue())
|
).collect(Collectors.toList());
|
}
|
|
return buildRoutes(allMenus, roleMenus);
|
}
|
|
@Override
|
public List<MenuVO> routesExt(String roleId, Long topMenuId) {
|
if (StringUtil.isBlank(roleId)) {
|
return null;
|
}
|
List<Menu> allMenus = baseMapper.allMenuExt();
|
List<Menu> roleMenus = baseMapper.roleMenuExt(Func.toLongList(roleId), topMenuId);
|
return buildRoutes(allMenus, roleMenus);
|
}
|
|
private List<MenuVO> buildRoutes(List<Menu> allMenus, List<Menu> roleMenus) {
|
List<Menu> routes = new LinkedList<>(roleMenus);
|
roleMenus.forEach(roleMenu -> recursion(allMenus, routes, roleMenu));
|
routes.sort(Comparator.comparing(Menu::getSort));
|
MenuWrapper menuWrapper = new MenuWrapper();
|
List<Menu> collect = routes.stream().filter(x -> Func.equals(x.getCategory(), 1)).collect(Collectors.toList());
|
return menuWrapper.listNodeVO(collect);
|
}
|
|
private void recursion(List<Menu> allMenus, List<Menu> routes, Menu roleMenu) {
|
Optional<Menu> menu = allMenus.stream().filter(x -> Func.equals(x.getId(), roleMenu.getParentId())).findFirst();
|
if (menu.isPresent() && !routes.contains(menu.get())) {
|
routes.add(menu.get());
|
recursion(allMenus, routes, menu.get());
|
}
|
}
|
|
@Override
|
public List<MenuVO> buttons(String roleId) {
|
List<Menu> buttons = (AuthUtil.isAdministrator()) ? baseMapper.allButtons() : baseMapper.buttons(Func.toLongList(roleId));
|
MenuWrapper menuWrapper = new MenuWrapper();
|
return menuWrapper.listNodeVO(buttons);
|
}
|
|
@Override
|
public List<TreeNode> tree() {
|
return ForestNodeMerger.merge(baseMapper.tree());
|
}
|
|
@Override
|
public List<TreeNode> grantTree(BladeUser user) {
|
return ForestNodeMerger.merge(AuthUtil.getUser().getRoleName().equals(RoleConstant.ADMINISTRATOR) ? baseMapper.grantTree() : baseMapper.grantTreeByRole(Func.toLongList(user.getRoleId())));
|
}
|
|
@Override
|
public List<TreeNode> grantTopTree(BladeUser user) {
|
List<TreeNode> menuTree = user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? baseMapper.grantTopTree() : baseMapper.grantTopTreeByRole(Func.toLongList(user.getRoleId()));
|
return ForestNodeMerger.merge(tenantPackageTree(menuTree, user.getTenantId()));
|
}
|
|
/**
|
* 租户菜单权限自定义筛选
|
*/
|
private List<TreeNode> tenantPackageTree(List<TreeNode> menuTree, String tenantId) {
|
TenantPackage tenantPackage = SysCache.getTenantPackage(tenantId);
|
if (!AuthUtil.isAdministrator() && Func.isNotEmpty(tenantPackage) && tenantPackage.getId() > 0L) {
|
List<Long> menuIds = Func.toLongList(tenantPackage.getMenuId());
|
// 筛选出两者菜单交集集合
|
List<TreeNode> collect = menuTree.stream().filter(x -> menuIds.contains(x.getId())).collect(Collectors.toList());
|
// 创建递归基础集合
|
List<TreeNode> packageTree = new LinkedList<>(collect);
|
// 递归筛选出菜单集合所有父级
|
collect.forEach(treeNode -> recursionParent(menuTree, packageTree, treeNode));
|
// 递归筛选出菜单集合所有子级
|
collect.forEach(treeNode -> recursionChild(menuTree, packageTree, treeNode));
|
// 合并在一起返回最终集合
|
return packageTree;
|
}
|
return menuTree;
|
}
|
|
/**
|
* 父节点递归
|
*/
|
public void recursionParent(List<TreeNode> menuTree, List<TreeNode> packageTree, TreeNode treeNode) {
|
Optional<TreeNode> node = menuTree.stream().filter(x -> Func.equals(x.getId(), treeNode.getParentId())).findFirst();
|
if (node.isPresent() && !packageTree.contains(node.get())) {
|
packageTree.add(node.get());
|
recursionParent(menuTree, packageTree, node.get());
|
}
|
}
|
|
/**
|
* 子节点递归
|
*/
|
public void recursionChild(List<TreeNode> menuTree, List<TreeNode> packageTree, TreeNode treeNode) {
|
List<TreeNode> nodes = menuTree.stream().filter(x -> Func.equals(x.getParentId(), treeNode.getId())).collect(Collectors.toList());
|
nodes.forEach(node -> {
|
if (!packageTree.contains(node)) {
|
packageTree.add(node);
|
recursionChild(menuTree, packageTree, node);
|
}
|
});
|
}
|
|
/**
|
* 租户菜单权限自定义筛选
|
*/
|
private List<Menu> tenantPackageMenu(List<Menu> menu) {
|
TenantPackage tenantPackage = SysCache.getTenantPackage(AuthUtil.getTenantId());
|
if (Func.isNotEmpty(tenantPackage) && tenantPackage.getId() > 0L) {
|
List<Long> menuIds = Func.toLongList(tenantPackage.getMenuId());
|
menu = menu.stream().filter(x -> menuIds.contains(x.getId())).collect(Collectors.toList());
|
}
|
return menu;
|
}
|
|
@Override
|
public List<TreeNode> grantDataScopeTree(BladeUser user) {
|
return ForestNodeMerger.merge(user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? baseMapper.grantDataScopeTree() : baseMapper.grantDataScopeTreeByRole(Func.toLongList(user.getRoleId())));
|
}
|
|
@Override
|
public List<TreeNode> grantApiScopeTree(BladeUser user) {
|
return ForestNodeMerger.merge(user.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? baseMapper.grantApiScopeTree() : baseMapper.grantApiScopeTreeByRole(Func.toLongList(user.getRoleId())));
|
}
|
|
@Override
|
public List<String> roleTreeKeys(String roleIds) {
|
List<RoleMenu> roleMenus = roleMenuService.list(Wrappers.<RoleMenu>query().lambda().in(RoleMenu::getRoleId, Func.toLongList(roleIds)));
|
return roleMenus.stream().map(roleMenu -> Func.toStr(roleMenu.getMenuId())).collect(Collectors.toList());
|
}
|
|
@Override
|
public List<String> topTreeKeys(String topMenuIds) {
|
List<TopMenuSetting> settings = topMenuSettingService.list(Wrappers.<TopMenuSetting>query().lambda().in(TopMenuSetting::getTopMenuId, Func.toLongList(topMenuIds)));
|
return settings.stream().map(setting -> Func.toStr(setting.getMenuId())).collect(Collectors.toList());
|
}
|
|
@Override
|
public List<String> dataScopeTreeKeys(String roleIds) {
|
List<RoleScope> roleScopes = roleScopeService.list(Wrappers.<RoleScope>query().lambda().eq(RoleScope::getScopeCategory, DATA_SCOPE_CATEGORY).in(RoleScope::getRoleId, Func.toLongList(roleIds)));
|
return roleScopes.stream().map(roleScope -> Func.toStr(roleScope.getScopeId())).collect(Collectors.toList());
|
}
|
|
@Override
|
public List<String> apiScopeTreeKeys(String roleIds) {
|
List<RoleScope> roleScopes = roleScopeService.list(Wrappers.<RoleScope>query().lambda().eq(RoleScope::getScopeCategory, API_SCOPE_CATEGORY).in(RoleScope::getRoleId, Func.toLongList(roleIds)));
|
return roleScopes.stream().map(roleScope -> Func.toStr(roleScope.getScopeId())).collect(Collectors.toList());
|
}
|
|
@Override
|
@Cacheable(cacheNames = MENU_CACHE, key = "'auth:routes:' + #user.roleId")
|
public List<Kv> authRoutes(BladeUser user) {
|
List<MenuDTO> routes = baseMapper.authRoutes(Func.toLongList(user.getRoleId()));
|
List<Kv> list = new ArrayList<>();
|
routes.forEach(route -> list.add(Kv.create().set(route.getPath(), Kv.create().set("authority", Func.toStrArray(route.getAlias())))));
|
return list;
|
}
|
|
@Override
|
public boolean removeMenu(String ids) {
|
Long cnt = baseMapper.selectCount(Wrappers.<Menu>query().lambda().in(Menu::getParentId, Func.toLongList(ids)));
|
if (cnt > 0L) {
|
throw new ServiceException("请先删除子节点!");
|
}
|
return removeByIds(Func.toLongList(ids));
|
}
|
|
@Override
|
public boolean submit(MenuDTO menu) {
|
LambdaQueryWrapper<Menu> menuQueryWrapper = Wrappers.lambdaQuery();
|
if (menu.getId() == null) {
|
menuQueryWrapper.eq(Menu::getCode, menu.getCode()).or(
|
wrapper -> wrapper.eq(Menu::getName, menu.getName()).eq(Menu::getCategory, MENU_CATEGORY)
|
);
|
} else {
|
menuQueryWrapper.ne(Menu::getId, menu.getId()).eq(Menu::getIsDeleted,0).and(
|
wrapper -> wrapper.eq(Menu::getCode, menu.getCode()).or(
|
o -> o.eq(Menu::getName, menu.getName()).eq(Menu::getCategory, MENU_CATEGORY)
|
)
|
);
|
}
|
Long cnt = baseMapper.selectCount(menuQueryWrapper);
|
if (cnt > 0L) {
|
throw new ServiceException("菜单名或编号已存在!");
|
}
|
if (menu.getParentId() == null && menu.getId() == null) {
|
menu.setParentId(BladeConstant.TOP_PARENT_ID);
|
}
|
if (menu.getParentId() != null && menu.getId() == null) {
|
Menu parentMenu = baseMapper.selectById(menu.getParentId());
|
if (parentMenu != null && parentMenu.getCategory() != 1) {
|
throw new ServiceException("父节点只可选择菜单类型!");
|
}
|
}
|
menu.setLabelStr(JSON.toJSONString(menu.getLabelList()));
|
List<List<String>> labelList = menu.getLabelList();
|
StringBuffer stringBuffer = new StringBuffer();
|
for (List<String> strings : labelList) {
|
stringBuffer.append(strings.get(strings.size()-1)).append(",");
|
}
|
menu.setLabelId(stringBuffer.toString());
|
menu.setIsDeleted(BladeConstant.DB_NOT_DELETED);
|
return saveOrUpdate(menu);
|
}
|
|
}
|