修改路由菜单属性

This commit is contained in:
zk
2026-03-12 14:35:39 +08:00
parent 28b118f975
commit aa16d63c40
3 changed files with 92 additions and 48 deletions
@@ -96,6 +96,23 @@ public class RouteMenuServer {
* @return 树形路由菜单
*/
public List<RouteMenuVo> getUserRoutes(Long userId) {
// 一次查出所有启用的菜单
List<RouteMenu> allMenus = routeMenuMapper.selectList(
new LambdaQueryWrapper<RouteMenu>().eq(RouteMenu::getStatus, 1));
if (allMenus.isEmpty()) {
return Collections.emptyList();
}
Map<Long, RouteMenu> menuMap = allMenus.stream()
.collect(Collectors.toMap(RouteMenu::getId, m -> m));
// 收集公开菜单ID
Set<Long> needIds = allMenus.stream()
.filter(m -> m.getOpenAccess() != null && m.getOpenAccess() == 1)
.map(RouteMenu::getId)
.collect(Collectors.toSet());
// 查询用户有效库存(不限时 或 未过期)
Instant now = Instant.now();
List<UserRouteMenuStock> stocks = userRouteMenuStockMapper.selectList(
@@ -108,46 +125,36 @@ public class RouteMenuServer {
)
);
if (stocks == null || stocks.isEmpty()) {
// 合并库存中有权限的菜单ID
if (stocks != null && !stocks.isEmpty()) {
stocks.stream()
.map(UserRouteMenuStock::getRouteId)
.forEach(needIds::add);
}
if (needIds.isEmpty()) {
return Collections.emptyList();
}
Set<Long> validRouteIds = stocks.stream()
.map(UserRouteMenuStock::getRouteId)
.collect(Collectors.toSet());
if (validRouteIds.isEmpty()) {
return Collections.emptyList();
}
// 一次查出所有启用的菜单
List<RouteMenu> allMenus = routeMenuMapper.selectList( new LambdaQueryWrapper<RouteMenu>().eq(RouteMenu::getStatus, 1));
if (allMenus.isEmpty()) {
return Collections.emptyList();
}
// 收集有权限的节点及其所有父节点ID
Map<Long, RouteMenu> menuMap = allMenus.stream().collect(Collectors.toMap(RouteMenu::getId, m -> m));
Set<Long> needIds = new HashSet<>(validRouteIds);
for (Long routeId : validRouteIds) {
// 补充父节点
Set<Long> allNeedIds = new HashSet<>(needIds);
for (Long routeId : needIds) {
RouteMenu current = menuMap.get(routeId);
while (current != null && current.getParentId() != 0) {
needIds.add(current.getParentId());
allNeedIds.add(current.getParentId());
current = menuMap.get(current.getParentId());
}
}
// 转VO
List<RouteMenuVo> voList = allMenus.stream()
.filter(menu -> needIds.contains(menu.getId()))
.filter(menu -> allNeedIds.contains(menu.getId()))
.map(menu -> {
RouteMenuVo vo = new RouteMenuVo();
BeanUtils.copyProperties(menu, vo);
return vo;
})
.collect(Collectors.toList());
.toList();
// 构建树形结构
Map<Long, List<RouteMenuVo>> parentMap = voList.stream()