【8.1.8】【sys】增加角色取消绑定菜单时,对路径父级id的判断删除

dev-8.1.9
fengshuonan 2024-06-24 13:55:19 +08:00
parent f32482ed9f
commit 3dd4f4d392
4 changed files with 92 additions and 1 deletions

View File

@ -0,0 +1,63 @@
package cn.stylefeng.roses.kernel.sys.modular.menu.factory;
import cn.stylefeng.roses.kernel.rule.constants.TreeConstants;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
*
*
* @author fengshuonan
* @since 2024/6/24 12:52
*/
public class MenuPathCalcFactory {
/**
* idididid
*
* @return idmenuId
* @author fengshuonan
* @since 2024/6/24 12:52
*/
public static Set<Long> getMenuParentIds(Long menuId, Map<Long, Long> menuIdParentIdMap) {
Set<Long> uniquePathMenuIds = new HashSet<>();
uniquePathMenuIds.add(menuId);
findUniquePathMenuIdsRecursive(menuId, menuIdParentIdMap, uniquePathMenuIds);
return uniquePathMenuIds;
}
/**
*
*
* @author fengshuonan
* @since 2024/6/24 13:01
*/
private static void findUniquePathMenuIdsRecursive(Long currentMenuId, Map<Long, Long> menuParentMap, Set<Long> uniquePathMenuIds) {
// 获取当前菜单的父菜单ID
Long parentId = menuParentMap.get(currentMenuId);
// 如果父菜单ID为null或者-1说明已经到达顶级节点结束递归
if (parentId == null || TreeConstants.DEFAULT_PARENT_ID.equals(parentId)) {
return;
}
// 检查父菜单是否有其他子节点
boolean hasOtherChildren = menuParentMap.entrySet().stream()
.anyMatch(entry -> entry.getValue().equals(parentId) && !entry.getKey().equals(currentMenuId));
// 如果父菜单没有其他子节点,则继续递归处理父菜单
if (!hasOtherChildren) {
// 将当前菜单ID加入结果集合
uniquePathMenuIds.add(parentId);
// 继续递归向上找
findUniquePathMenuIdsRecursive(parentId, menuParentMap, uniquePathMenuIds);
}
}
}

View File

@ -7,6 +7,7 @@ import cn.stylefeng.roses.kernel.sys.modular.menu.pojo.response.AppGroupDetail;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
@ -124,4 +125,13 @@ public interface SysMenuService extends IService<SysMenu>, SysMenuServiceApi {
*/
List<String> getMenuCodeList(List<Long> menuIdList);
/**
* idid
*
* @return idid
* @author fengshuonan
* @since 2024/6/24 12:38
*/
Map<Long, Long> getMenuIdParentIdMap();
}

View File

@ -271,6 +271,19 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
return result;
}
@Override
public Map<Long, Long> getMenuIdParentIdMap() {
LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysMenuLambdaQueryWrapper.select(SysMenu::getMenuId, SysMenu::getMenuParentId);
List<SysMenu> sysMenuList = this.list(sysMenuLambdaQueryWrapper);
if (ObjectUtil.isEmpty(sysMenuList)) {
return new HashMap<>();
}
return sysMenuList.stream().collect(Collectors.toMap(SysMenu::getMenuId, SysMenu::getMenuParentId));
}
@Override
public List<AppGroupDetail> getAppMenuGroupDetail(SysMenuRequest sysMenuRequest) {

View File

@ -3,6 +3,7 @@ package cn.stylefeng.roses.kernel.sys.modular.role.service.impl;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.sys.modular.menu.entity.SysMenuOptions;
import cn.stylefeng.roses.kernel.sys.modular.menu.factory.MenuPathCalcFactory;
import cn.stylefeng.roses.kernel.sys.modular.menu.service.SysMenuOptionsService;
import cn.stylefeng.roses.kernel.sys.modular.menu.service.SysMenuService;
import cn.stylefeng.roses.kernel.sys.modular.role.action.RoleAssignOperateAction;
@ -67,9 +68,13 @@ public class RoleBindMenuImpl implements RoleAssignOperateAction, RoleBindLimitA
return;
}
// 【2024年6月24日】获取菜单的所有id和parentId后续判断删除的节点的父级没有其他自己节点时候使用
Map<Long, Long> menuIdParentIdMap = sysMenuService.getMenuIdParentIdMap();
Set<Long> needToDelete = MenuPathCalcFactory.getMenuParentIds(menuId, menuIdParentIdMap);
// 1. 先取消绑定角色和菜单
LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getMenuId, menuId);
sysRoleMenuLambdaQueryWrapper.in(SysRoleMenu::getMenuId, needToDelete);
sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRoleId, roleId);
this.sysRoleMenuService.remove(sysRoleMenuLambdaQueryWrapper);