package cn.iocoder.yudao.module.system;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthMenuRespVO;
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT;
public class Test {
public static List<MenuDO> menuList;
public static Map<Long, AuthMenuRespVO> treeNodeMap;
static{
menuList = new ArrayList<MenuDO>();
MenuDO m1 = new MenuDO();
m1.setId(1L);
m1.setParentId(0L);
m1.setName("系统管理");
m1.setPath("[p1]");
m1.setComponent("[c1]");
m1.setComponentName("[cName1]");
m1.setIcon("[icon1]");
m1.setVisible(true);
m1.setKeepAlive(true);
m1.setAlwaysShow(true);
m1.setSort(10);
MenuDO m11 = new MenuDO();
m11.setId(1224L);
m11.setParentId(1L);
m11.setName("租户管理");
m11.setPath("[p11]");
m11.setComponent("[c11]");
m11.setComponentName("[cName11]");
m11.setIcon("[icon11]");
m11.setVisible(true);
m11.setKeepAlive(true);
m11.setAlwaysShow(true);
m11.setSort(0);
MenuDO m111_1 = new MenuDO();
m111_1.setId(1138L);
m111_1.setParentId(1224L);
m111_1.setName("租户列表");
m111_1.setPath("[p111_1]");
m111_1.setComponent("[c111_1]");
m111_1.setComponentName("[cName111_1]");
m111_1.setIcon("[icon111_1]");
m111_1.setVisible(true);
m111_1.setKeepAlive(true);
m111_1.setAlwaysShow(true);
m111_1.setSort(0);
MenuDO m111_2 = new MenuDO();
m111_2.setId(1225L);
m111_2.setParentId(1224L);
m111_2.setName("租户套餐");
m111_2.setPath("[p111_2]");
m111_2.setComponent("[c111_2]");
m111_2.setComponentName("[cName111_2]");
m111_2.setIcon("[icon111_2]");
m111_2.setVisible(true);
m111_2.setKeepAlive(true);
m111_2.setAlwaysShow(true);
m111_2.setSort(0);
MenuDO m2 = new MenuDO();
m2.setId(2L);
m2.setParentId(0L);
m2.setName("基础设施");
m2.setPath("[p2]");
m2.setComponent("[c2]");
m2.setComponentName("[cName2]");
m2.setIcon("[icon2]");
m2.setVisible(true);
m2.setKeepAlive(true);
m2.setAlwaysShow(true);
m2.setSort(20);
MenuDO m3 = new MenuDO();
m3.setId(1185L);
m3.setParentId(0L);
m3.setName("工作流程");
m3.setPath("[p3]");
m3.setComponent("[c3]");
m3.setComponentName("[cName3]");
m3.setIcon("[icon3]");
m3.setVisible(true);
m3.setKeepAlive(true);
m3.setAlwaysShow(true);
m3.setSort(50);
MenuDO m4 = new MenuDO();
m4.setId(1281L);
m4.setParentId(0L);
m4.setName("报表管理");
m4.setPath("[p4]");
m4.setComponent("[c4]");
m4.setComponentName("[cName4]");
m4.setIcon("[icon4]");
m4.setVisible(true);
m4.setKeepAlive(true);
m4.setAlwaysShow(true);
m4.setSort(40);
menuList.add(m1);
menuList.add(m11);
menuList.add(m111_1);
menuList.add(m111_2);
menuList.add(m2);
menuList.add(m3);
menuList.add(m4);
/** 示例数据(真来自 芋道)
"MenuDO(id=1, name=系统管理, permission=, type=1, sort=10, parentId=0, path=/system, icon=system, component=null, componentName=null, status=0, visible=true, keepAlive=true, alwaysShow=true)"
"MenuDO(id=1224, name=租户管理, permission=, type=2, sort=0, parentId=1, path=tenant, icon=peoples, component=null, componentName=null, status=0, visible=true, keepAlive=true, alwaysShow=true)"
"MenuDO(id=1138, name=租户列表, permission=, type=2, sort=0, parentId=1224, path=list, icon=peoples, component=system/tenant/index, componentName=null, status=0, visible=true, keepAlive=true, alwaysShow=true)"
"MenuDO(id=1225, name=租户套餐, permission=, type=2, sort=0, parentId=1224, path=package, icon=eye, component=system/tenantPackage/index, componentName=null, status=0, visible=true, keepAlive=true, alwaysShow=true)"
"MenuDO(id=2, name=基础设施, permission=, type=1, sort=20, parentId=0, path=/infra, icon=monitor, component=null, componentName=null, status=0, visible=true, keepAlive=true, alwaysShow=true)"
"MenuDO(id=1185, name=工作流程, permission=, type=1, sort=50, parentId=0, path=/bpm, icon=tool, component=null, componentName=null, status=0, visible=true, keepAlive=true, alwaysShow=true)"
"MenuDO(id=1281, name=报表管理, permission=, type=1, sort=40, parentId=0, path=/report, icon=chart, component=null, componentName=null, status=0, visible=true, keepAlive=true, alwaysShow=true)"
*/
treeNodeMap = new LinkedHashMap<>();
menuList.forEach(menu -> treeNodeMap.put(menu.getId(), AuthConvert.INSTANCE.convertTreeNode(menu)));
}
// menuList.sort(Comparator.comparing(MenuDO::getSort));
public static void main1(String[] args) {
menuList.sort(Comparator.comparing(MenuDO::getSort));
for(int i=0;i<menuList.size();i++){
System.out.println(menuList.get(i).toString());
}
/** 输出结果:
MenuDO(id=1224, name=租户管理, permission=null, type=null, sort=0, parentId=1, path=[p11], icon=[icon11], component=[c11], componentName=[cName11], status=null, visible=true, keepAlive=true, alwaysShow=true)
MenuDO(id=1138, name=租户列表, permission=null, type=null, sort=0, parentId=1224, path=[p111_1], icon=[icon111_1], component=[c111_1], componentName=[cName111_1], status=null, visible=true, keepAlive=true, alwaysShow=true)
MenuDO(id=1225, name=租户套餐, permission=null, type=null, sort=0, parentId=1224, path=[p111_2], icon=[icon111_2], component=[c111_2], componentName=[cName111_2], status=null, visible=true, keepAlive=true, alwaysShow=true)
MenuDO(id=1, name=系统管理, permission=null, type=null, sort=10, parentId=0, path=[p1], icon=[icon1], component=[c1], componentName=[cName1], status=null, visible=true, keepAlive=true, alwaysShow=true)
MenuDO(id=2, name=基础设施, permission=null, type=null, sort=20, parentId=0, path=[p2], icon=[icon2], component=[c2], componentName=[cName2], status=null, visible=true, keepAlive=true, alwaysShow=true)
MenuDO(id=1281, name=报表管理, permission=null, type=null, sort=40, parentId=0, path=[p4], icon=[icon4], component=[c4], componentName=[cName4], status=null, visible=true, keepAlive=true, alwaysShow=true)
MenuDO(id=1185, name=工作流程, permission=null, type=null, sort=50, parentId=0, path=[p3], icon=[icon3], component=[c3], componentName=[cName3], status=null, visible=true, keepAlive=true, alwaysShow=true)
*/
}
// menuList.forEach(menu -> treeNodeMap.put(menu.getId(), AuthConvert.INSTANCE.convertTreeNode(menu)));
public static void main2(String[] args) {
Map<Long, AuthMenuRespVO> treeNodeMap2 = new LinkedHashMap<>();
menuList.forEach(menu -> treeNodeMap2.put(menu.getId(), AuthConvert.INSTANCE.convertTreeNode(menu)));
for (Long key : treeNodeMap2.keySet()) {
System.out.println(treeNodeMap2.get(key).toString());
}
/** 输出结果:
1 = AuthMenuRespVO(id=1, parentId=0, name=系统管理, path=[p1], component=[c1], componentName=[cName1], icon=[icon1], visible=true, keepAlive=true, alwaysShow=true, children=null)
1224 = AuthMenuRespVO(id=1224, parentId=1, name=租户管理, path=[p11], component=[c11], componentName=[cName11], icon=[icon11], visible=true, keepAlive=true, alwaysShow=true, children=null)
1138 = AuthMenuRespVO(id=1138, parentId=1224, name=租户列表, path=[p111_1], component=[c111_1], componentName=[cName111_1], icon=[icon111_1], visible=true, keepAlive=true, alwaysShow=true, children=null)
1225 = AuthMenuRespVO(id=1225, parentId=1224, name=租户套餐, path=[p111_2], component=[c111_2], componentName=[cName111_2], icon=[icon111_2], visible=true, keepAlive=true, alwaysShow=true, children=null)
2 = AuthMenuRespVO(id=2, parentId=0, name=基础设施, path=[p2], component=[c2], componentName=[cName2], icon=[icon2], visible=true, keepAlive=true, alwaysShow=true, children=null)
1185 = AuthMenuRespVO(id=1185, parentId=0, name=工作流程, path=[p3], component=[c3], componentName=[cName3], icon=[icon3], visible=true, keepAlive=true, alwaysShow=true, children=null)
1281 = AuthMenuRespVO(id=1281, parentId=0, name=报表管理, path=[p4], component=[c4], componentName=[cName4], icon=[icon4], visible=true, keepAlive=true, alwaysShow=true, children=null)
*/
}
// treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(ID_ROOT)).forEach(childNode -> {...}
public static void main3(String[] args) {
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(ID_ROOT)).forEach(childNode -> {
System.out.println(childNode.toString());
});
/** 输出结果:
AuthMenuRespVO(id=1224, parentId=1, name=租户管理, path=[p11], component=[c11], componentName=[cName11], icon=[icon11], visible=true, keepAlive=true, alwaysShow=true, children=null)
AuthMenuRespVO(id=1138, parentId=1224, name=租户列表, path=[p111_1], component=[c111_1], componentName=[cName111_1], icon=[icon111_1], visible=true, keepAlive=true, alwaysShow=true, children=null)
AuthMenuRespVO(id=1225, parentId=1224, name=租户套餐, path=[p111_2], component=[c111_2], componentName=[cName111_2], icon=[icon111_2], visible=true, keepAlive=true, alwaysShow=true, children=null)
*/
}
/*treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(ID_ROOT)).forEach(childNode -> {
// 获得父节点
AuthMenuRespVO parentNode = treeNodeMap.get(childNode.getParentId());
if (parentNode == null) {
LoggerFactory.getLogger(getClass()).error("[buildRouterTree][resource({}) 找不到父资源({})]",
childNode.getId(), childNode.getParentId());
return;
}
// 将自己添加到父节点中
if (parentNode.getChildren() == null) {
parentNode.setChildren(new ArrayList<>());
}
parentNode.getChildren().add(childNode);
});*/
public static void main4(String[] args) {
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(ID_ROOT)).forEach(childNode -> {
// 获得父节点
AuthMenuRespVO parentNode = treeNodeMap.get(childNode.getParentId());
if (parentNode == null) {
return;
}
// 将自己添加到父节点中
if (parentNode.getChildren() == null) {
parentNode.setChildren(new ArrayList<>());
}
parentNode.getChildren().add(childNode);
});
for (Long key : treeNodeMap.keySet()) {
System.out.println(treeNodeMap.get(key).toString());
}
/** 输出结果:
AuthMenuRespVO(id=1, parentId=0, name=系统管理, path=[p1], component=[c1], componentName=[cName1], icon=[icon1], visible=true, keepAlive=true, alwaysShow=true, children=[AuthMenuRespVO(id=1224, parentId=1, name=租户管理, path=[p11], component=[c11], componentName=[cName11], icon=[icon11], visible=true, keepAlive=true, alwaysShow=true, children=[AuthMenuRespVO(id=1138, parentId=1224, name=租户列表, path=[p111_1], component=[c111_1], componentName=[cName111_1], icon=[icon111_1], visible=true, keepAlive=true, alwaysShow=true, children=null), AuthMenuRespVO(id=1225, parentId=1224, name=租户套餐, path=[p111_2], component=[c111_2], componentName=[cName111_2], icon=[icon111_2], visible=true, keepAlive=true, alwaysShow=true, children=null)])])
AuthMenuRespVO(id=1224, parentId=1, name=租户管理, path=[p11], component=[c11], componentName=[cName11], icon=[icon11], visible=true, keepAlive=true, alwaysShow=true, children=[AuthMenuRespVO(id=1138, parentId=1224, name=租户列表, path=[p111_1], component=[c111_1], componentName=[cName111_1], icon=[icon111_1], visible=true, keepAlive=true, alwaysShow=true, children=null), AuthMenuRespVO(id=1225, parentId=1224, name=租户套餐, path=[p111_2], component=[c111_2], componentName=[cName111_2], icon=[icon111_2], visible=true, keepAlive=true, alwaysShow=true, children=null)])
AuthMenuRespVO(id=1138, parentId=1224, name=租户列表, path=[p111_1], component=[c111_1], componentName=[cName111_1], icon=[icon111_1], visible=true, keepAlive=true, alwaysShow=true, children=null)
AuthMenuRespVO(id=1225, parentId=1224, name=租户套餐, path=[p111_2], component=[c111_2], componentName=[cName111_2], icon=[icon111_2], visible=true, keepAlive=true, alwaysShow=true, children=null)
AuthMenuRespVO(id=2, parentId=0, name=基础设施, path=[p2], component=[c2], componentName=[cName2], icon=[icon2], visible=true, keepAlive=true, alwaysShow=true, children=null)
AuthMenuRespVO(id=1185, parentId=0, name=工作流程, path=[p3], component=[c3], componentName=[cName3], icon=[icon3], visible=true, keepAlive=true, alwaysShow=true, children=null)
AuthMenuRespVO(id=1281, parentId=0, name=报表管理, path=[p4], component=[c4], componentName=[cName4], icon=[icon4], visible=true, keepAlive=true, alwaysShow=true, children=null)
*/
}
// from.stream().filter(predicate).collect(Collectors.toList());
public static void main(String[] args) {
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(ID_ROOT)).forEach(childNode -> {
// 获得父节点
AuthMenuRespVO parentNode = treeNodeMap.get(childNode.getParentId());
if (parentNode == null) {
return;
}
// 将自己添加到父节点中
if (parentNode.getChildren() == null) {
parentNode.setChildren(new ArrayList<>());
}
parentNode.getChildren().add(childNode);
});
List<AuthMenuRespVO> collect = treeNodeMap.values().stream().filter(node -> ID_ROOT.equals(node.getParentId())).collect(Collectors.toList());
for (AuthMenuRespVO v : collect) {
System.out.println(v.toString());
}
/** 输出结果:
AuthMenuRespVO(id=1, parentId=0, name=系统管理, path=[p1], component=[c1], componentName=[cName1], icon=[icon1], visible=true, keepAlive=true, alwaysShow=true, children=[AuthMenuRespVO(id=1224, parentId=1, name=租户管理, path=[p11], component=[c11], componentName=[cName11], icon=[icon11], visible=true, keepAlive=true, alwaysShow=true, children=[AuthMenuRespVO(id=1138, parentId=1224, name=租户列表, path=[p111_1], component=[c111_1], componentName=[cName111_1], icon=[icon111_1], visible=true, keepAlive=true, alwaysShow=true, children=null), AuthMenuRespVO(id=1225, parentId=1224, name=租户套餐, path=[p111_2], component=[c111_2], componentName=[cName111_2], icon=[icon111_2], visible=true, keepAlive=true, alwaysShow=true, children=null)])])
AuthMenuRespVO(id=2, parentId=0, name=基础设施, path=[p2], component=[c2], componentName=[cName2], icon=[icon2], visible=true, keepAlive=true, alwaysShow=true, children=null)
AuthMenuRespVO(id=1185, parentId=0, name=工作流程, path=[p3], component=[c3], componentName=[cName3], icon=[icon3], visible=true, keepAlive=true, alwaysShow=true, children=null)
AuthMenuRespVO(id=1281, parentId=0, name=报表管理, path=[p4], component=[c4], componentName=[cName4], icon=[icon4], visible=true, keepAlive=true, alwaysShow=true, children=null)
*/
}
}
文章评论