From a208fd3f884369089b31c5eb31bf952601db1646 Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Mon, 13 Jan 2020 01:11:30 +0800 Subject: [PATCH] add:menu --- menu/pom.xml | 16 ++++ .../demo/Application.java | 87 +++++++++++++++++++ .../com/duanledexianxianxian/demo/Menu.java | 18 ++++ .../com/duanledexianxianxian/demo/MenuVO.java | 21 +++++ pom.xml | 7 ++ 5 files changed, 149 insertions(+) create mode 100644 menu/pom.xml create mode 100644 menu/src/main/java/com/duanledexianxianxian/demo/Application.java create mode 100644 menu/src/main/java/com/duanledexianxianxian/demo/Menu.java create mode 100644 menu/src/main/java/com/duanledexianxianxian/demo/MenuVO.java diff --git a/menu/pom.xml b/menu/pom.xml new file mode 100644 index 0000000..6aba2a9 --- /dev/null +++ b/menu/pom.xml @@ -0,0 +1,16 @@ + + + + demo-parent + com.duanledexianxianxian.demo + 1.0-SNAPSHOT + + 4.0.0 + + menu + + + + \ No newline at end of file diff --git a/menu/src/main/java/com/duanledexianxianxian/demo/Application.java b/menu/src/main/java/com/duanledexianxianxian/demo/Application.java new file mode 100644 index 0000000..7291625 --- /dev/null +++ b/menu/src/main/java/com/duanledexianxianxian/demo/Application.java @@ -0,0 +1,87 @@ +package com.duanledexianxianxian.demo; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.Lists; +import org.springframework.beans.BeanUtils; +import org.springframework.util.CollectionUtils; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @author duanledexianxianxian + * @date 2020/1/12 23:51 + * @since 1.0.0 + */ +public class Application { + private static String[][] menus = { + {null, "0001", "用户管理", "/urlMgr"}, + {"0001", "00010001", "用户管理-添加用户", "/urlMgr/add"}, + {"0001", "00010002", "用户管理-修改用户", "/urlMgr/edit"}, + {"0001", "00010003", "用户管理-删除用户", "/urlMgr/delete"}, + {"0001", "00010004", "用户管理-查询用户", "/urlMgr/query"}, + {null, "0002", "系统管理", "/systemMgr"}, + {"0002", "00020001", "系统管理-系统管理0001", "/systemMgr/0001"}, + {"00020001", "000200010001", "系统管理-系统管理0001-系统管理0001", "/systemMgr/0001/0001"}, + {"000200010001", "0002000100010001", "系统管理-系统管理0001-系统管理0001-系统管理0001", "/systemMgr/0001/0001/0001"}, + }; + + public static void main(String[] args) { + List menuList = Lists.newArrayList(); + Arrays.asList(menus).forEach(x -> { + Menu menu = new Menu(); + menu.setParentMenuCode(x[0]); + menu.setMenuCode(x[1]); + menu.setMenuTitle(x[2]); + menu.setMenuUrl(x[3]); + menuList.add(menu); + }); + + menuList.forEach(x -> System.out.println(x.toString())); + MenuVO menuVO = getTreeMenuVO(menuList); + ObjectMapper mapper = new ObjectMapper(); + try { + System.out.println(mapper.writeValueAsString(menuVO)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + } + + private static MenuVO getTreeMenuVO(List menuList) { + if (CollectionUtils.isEmpty(menuList)) { + return null; + } + List menuVOList = Lists.newArrayList(); + menuList.forEach(x -> { + MenuVO menuVO = new MenuVO(); + BeanUtils.copyProperties(x, menuVO); + menuVOList.add(menuVO); + }); + // 转成map + Map menuMap = menuVOList.stream().collect(Collectors.toMap(MenuVO::getMenuCode, x -> x)); + // 构建根节点 + MenuVO menuVO = new MenuVO(); + menuVO.setParentMenuCode(null); + menuVO.setMenuCode("root"); + menuVO.setMenuTitle("根节点"); + + for (MenuVO menu : menuVOList) { + if (menuMap.containsKey(menu.getParentMenuCode())) { + if (menuMap.get(menu.getParentMenuCode()).getChildren() == null) { + menuMap.get(menu.getParentMenuCode()).setChildren(Lists.newArrayList()); + } + menuMap.get(menu.getParentMenuCode()).getChildren().add(menu); + + } else { + if (menuVO.getChildren() == null) { + menuVO.setChildren(Lists.newArrayList()); + } + menuVO.getChildren().add(menu); + } + } + return menuVO; + } +} diff --git a/menu/src/main/java/com/duanledexianxianxian/demo/Menu.java b/menu/src/main/java/com/duanledexianxianxian/demo/Menu.java new file mode 100644 index 0000000..b5dac88 --- /dev/null +++ b/menu/src/main/java/com/duanledexianxianxian/demo/Menu.java @@ -0,0 +1,18 @@ +package com.duanledexianxianxian.demo; + +import lombok.Data; + +/** + * 菜单 + * + * @author duanledexianxianxian + * @date 2020/1/12 23:49 + * @since 1.0.0 + */ +@Data +public class Menu { + private String parentMenuCode; + private String menuCode; + private String menuTitle; + private String menuUrl; +} diff --git a/menu/src/main/java/com/duanledexianxianxian/demo/MenuVO.java b/menu/src/main/java/com/duanledexianxianxian/demo/MenuVO.java new file mode 100644 index 0000000..ece311a --- /dev/null +++ b/menu/src/main/java/com/duanledexianxianxian/demo/MenuVO.java @@ -0,0 +1,21 @@ +package com.duanledexianxianxian.demo; + +import lombok.Data; + +import java.util.List; + +/** + * 菜单 + * + * @author duanledexianxianxian + * @date 2020/1/12 23:49 + * @since 1.0.0 + */ +@Data +public class MenuVO { + private String parentMenuCode; + private String menuCode; + private String menuTitle; + private String menuUrl; + private List children; +} diff --git a/pom.xml b/pom.xml index 89c8ba3..06796d7 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,7 @@ 1.0-SNAPSHOT kafka + menu @@ -39,6 +40,12 @@ json-path test + + + com.google.guava + guava + 23.0 + -- GitLab