From 8414fcb0103b2844bb1a396b6d87516f6b0521e5 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 9 Sep 2023 18:40:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20MenuUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/common/util/MenuUtil.java | 61 ------------------- 1 file changed, 61 deletions(-) delete mode 100644 plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/common/util/MenuUtil.java diff --git a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/common/util/MenuUtil.java b/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/common/util/MenuUtil.java deleted file mode 100644 index 55f0918..0000000 --- a/plusone-system/plusone-system-application/src/main/java/xyz/zhouxy/plusone/system/application/common/util/MenuUtil.java +++ /dev/null @@ -1,61 +0,0 @@ -package xyz.zhouxy.plusone.system.application.common.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import xyz.zhouxy.plusone.domain.IWithOrderNumber; -import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject; -import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType; - -/** - * 菜单工具类 - * - * @author ZhouXY - */ -public class MenuUtil { - private MenuUtil() { - throw new IllegalStateException("Utility class"); - } - - /** - * 构建菜单树 - * - * @param allMenus 菜单列表 - * @return 菜单树 - */ - public static List buildMenuTree(Collection allMenus) { - // 先排序,保证添加到 rootMenus 中的顺序,以及 addChild 添加的子菜单的顺序 - allMenus = allMenus.stream() - .sorted(Comparator.comparing(IWithOrderNumber::getOrderNumber)) - .toList(); - - // 一级菜单 - List rootMenus = new ArrayList<>(); - // key: 菜单 id; value: 菜单对象. 方便根据 id 查找相应对象。 - Map menuListMap = new HashMap<>(); - - for (var menu : allMenus) { - // 添加 MENU_LIST 到 map 中,方便后面调用对象的方法 - if (menu.getType() == MenuType.MENU_LIST.ordinal()) { - menuListMap.put(menu.getId(), menu); - } - // 一级菜单 - if (menu.getParentId() == 0) { - rootMenus.add(menu); - } - } - for (var menu : allMenus) { - var parent = menuListMap.getOrDefault(menu.getParentId(), null); - // 父菜单存在于 map 中,调用父菜单的 addChild 方法将当前菜单添加为父菜单的子菜单。 - if (parent != null) { - parent.addChild(menu); - } - } - - return rootMenus; - } -}