task-144(Java 菜单树与 Flyway): 实现 parent_id 树组装
组装器新增 groupChildrenById/递归 assemble(孤儿按根),currentUserMenus 改走统一 组装器 toMapList。 TDD: Task144Test 8 用例 RED→GREEN。
This commit is contained in:
+118
-24
@@ -30,33 +30,34 @@ public final class AdminMenuTreeBuilder {
|
||||
if (menus == null || menus.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Map<Long, Node> nodes = new LinkedHashMap<>();
|
||||
for (PermissionMenuItemVo menu : menus) {
|
||||
Node node = toNode(menu);
|
||||
if (node != null) {
|
||||
nodes.put(node.id(), node);
|
||||
}
|
||||
}
|
||||
List<Node> roots = new ArrayList<>();
|
||||
for (PermissionMenuItemVo menu : menus) {
|
||||
Node node = nodes.get(menu.getId());
|
||||
if (node == null) {
|
||||
Map<Long, PermissionMenuItemVo> byId = indexById(menus);
|
||||
Map<Long, List<PermissionMenuItemVo>> children = groupChildrenById(menus);
|
||||
Comparator<PermissionMenuItemVo> voOrder = Comparator
|
||||
.comparingInt((PermissionMenuItemVo vo) -> vo.getSortOrder() == null ? 0 : vo.getSortOrder())
|
||||
.thenComparing(AdminMenuTreeBuilder::keyOf);
|
||||
return assemble(children, byId, null, voOrder);
|
||||
}
|
||||
|
||||
private static List<Node> assemble(Map<Long, List<PermissionMenuItemVo>> children,
|
||||
Map<Long, PermissionMenuItemVo> byId,
|
||||
Long parentId,
|
||||
Comparator<PermissionMenuItemVo> voOrder) {
|
||||
List<Node> out = new ArrayList<>();
|
||||
List<PermissionMenuItemVo> siblings = children.getOrDefault(parentId, List.of());
|
||||
List<PermissionMenuItemVo> sorted = new ArrayList<>(siblings);
|
||||
sorted.sort(voOrder);
|
||||
for (PermissionMenuItemVo vo : sorted) {
|
||||
if (vo == null || vo.getId() == null) {
|
||||
continue;
|
||||
}
|
||||
if (menu.getParentId() != null && nodes.containsKey(menu.getParentId())) {
|
||||
Node parent = nodes.get(menu.getParentId());
|
||||
Node child = node;
|
||||
List<Node> children = new ArrayList<>(parent.children());
|
||||
children.add(child);
|
||||
nodes.put(parent.id(), new Node(parent.id(), parent.key(), parent.name(), parent.sort(), parent.route(), children));
|
||||
} else {
|
||||
roots.add(node);
|
||||
}
|
||||
Node node = toNode(vo);
|
||||
List<Node> subs = assemble(children, byId, vo.getId(), voOrder);
|
||||
Node withChildren = subs.isEmpty()
|
||||
? node
|
||||
: new Node(node.id(), node.key(), node.name(), node.sort(), node.route(), subs);
|
||||
out.add(withChildren);
|
||||
}
|
||||
Comparator<Node> order = Comparator
|
||||
.comparingInt((Node n) -> n.sort() == null ? 0 : n.sort())
|
||||
.thenComparing(Node::key);
|
||||
return sortTree(roots, order);
|
||||
return out;
|
||||
}
|
||||
|
||||
private static List<Node> sortTree(List<Node> roots, Comparator<Node> order) {
|
||||
@@ -123,4 +124,97 @@ public final class AdminMenuTreeBuilder {
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
/** 直接子节点按 parentId 分组;null parent 与指向不存在父(孤儿)的节点作为根(null 组)。 */
|
||||
public static Map<Long, List<PermissionMenuItemVo>> groupChildrenById(List<PermissionMenuItemVo> menus) {
|
||||
Map<Long, List<PermissionMenuItemVo>> grouped = new LinkedHashMap<>();
|
||||
if (menus == null) {
|
||||
return grouped;
|
||||
}
|
||||
Map<Long, PermissionMenuItemVo> byId = indexById(menus);
|
||||
for (PermissionMenuItemVo menu : menus) {
|
||||
if (menu == null) {
|
||||
continue;
|
||||
}
|
||||
Long parent = menu.getParentId();
|
||||
Long key = parent != null && byId.containsKey(parent) ? parent : null;
|
||||
grouped.computeIfAbsent(key, k -> new ArrayList<>()).add(menu);
|
||||
}
|
||||
return grouped;
|
||||
}
|
||||
|
||||
/** 是否存在指向不存在父节点的孤儿节点(脏数据);无父(null)不算孤儿。 */
|
||||
public static boolean hasOrphanParent(List<PermissionMenuItemVo> menus) {
|
||||
Map<Long, PermissionMenuItemVo> index = indexById(menus);
|
||||
if (menus == null) {
|
||||
return false;
|
||||
}
|
||||
for (PermissionMenuItemVo menu : menus) {
|
||||
if (menu == null || menu.getParentId() == null) {
|
||||
continue;
|
||||
}
|
||||
if (!index.containsKey(menu.getParentId())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 是否虚拟分组 route(group-* 前缀);不得当作页面路由。 */
|
||||
public static boolean isVirtualGroupRoute(String route) {
|
||||
return route != null && route.startsWith("group-");
|
||||
}
|
||||
|
||||
/** 节点排序:sort_order 升序、同序按 key 稳定排序;返回不可变副本,不改入参。 */
|
||||
public static List<Node> sortNodes(List<Node> nodes) {
|
||||
List<Node> sorted = new ArrayList<>(nodes == null ? List.of() : nodes);
|
||||
sorted.sort(Comparator
|
||||
.comparingInt((Node n) -> n.sort() == null ? 0 : n.sort())
|
||||
.thenComparing(Node::key));
|
||||
return List.copyOf(sorted);
|
||||
}
|
||||
|
||||
/** 页面路由:仅非虚拟分组输出 "/route",其余回 null。 */
|
||||
public static String pageRouteOf(PermissionMenuItemVo menu) {
|
||||
if (menu == null) {
|
||||
return null;
|
||||
}
|
||||
String route = normalizeRoute(menu.getRoutePath());
|
||||
if (route == null || isVirtualGroupRoute(route)) {
|
||||
return null;
|
||||
}
|
||||
return route;
|
||||
}
|
||||
|
||||
/** column_key → 响应 key;缺省回 id 字符串(不丢失可识别性)。 */
|
||||
public static String keyOf(PermissionMenuItemVo menu) {
|
||||
if (menu == null) {
|
||||
return null;
|
||||
}
|
||||
if (menu.getColumnKey() != null && !menu.getColumnKey().isBlank()) {
|
||||
return menu.getColumnKey();
|
||||
}
|
||||
return menu.getId() == null ? null : String.valueOf(menu.getId());
|
||||
}
|
||||
|
||||
/** Node 列表 → 可序列化 Map(key/name/route/children,去掉 sort)。 */
|
||||
public static List<Map<String, Object>> toMapList(List<Node> nodes) {
|
||||
List<Map<String, Object>> out = new ArrayList<>();
|
||||
if (nodes == null) {
|
||||
return out;
|
||||
}
|
||||
for (Node node : nodes) {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("key", node.key());
|
||||
map.put("name", node.name());
|
||||
if (node.route() != null && !isVirtualGroupRoute(node.route())) {
|
||||
map.put("route", node.route());
|
||||
}
|
||||
if (!node.children().isEmpty()) {
|
||||
map.put("children", toMapList(node.children()));
|
||||
}
|
||||
out.add(map);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user