顾挽11.13

This commit is contained in:
顾挽 2024-11-13 18:25:40 +08:00
parent 06c042b523
commit fe76b12b8c
1 changed files with 30 additions and 2 deletions

View File

@ -9,7 +9,10 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import service.SystemUsersService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author 12455
@ -77,7 +80,7 @@ public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper, Syste
// System.out.println("systemMenus = " + systemMenus);
systemMenus.stream().filter(systemMenu -> !systemMenu.getDeleted() && systemMenu.getVisible())
/* systemMenus.stream().filter(systemMenu -> !systemMenu.getDeleted() && systemMenu.getVisible())
.map(systemMenu -> {
// 角色码列表
@ -93,11 +96,36 @@ public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper, Syste
return null;
}).distinct().toList();
}).distinct().toList();*/
System.out.println("systemMenus = " + buildMenuTree(systemMenus));
}
// 方法将菜单列表构建为树形结构
public static List<SystemMenu> buildMenuTree(List<SystemMenu> menuList) {
// parentId 分组菜单
Map<Long, List<SystemMenu>> parentIdMap = menuList.stream()
.collect(Collectors.groupingBy(SystemMenu::getParentId));
// 从根节点parentId null 0递归构建树
return buildTree(parentIdMap, 0L); // 假设顶级节点 parentId 0
}
// 递归构建树形结构
private static List<SystemMenu> buildTree(Map<Long, List<SystemMenu>> parentIdMap, Long parentId) {
List<SystemMenu> children = parentIdMap.getOrDefault(parentId, new ArrayList<>());
for (SystemMenu menu : children) {
// 递归设置子菜单
menu.setChildren(buildTree(parentIdMap, menu.getId()));
}
return children;
}
}