顾挽-11月13日

This commit is contained in:
ovo 2024-11-13 23:23:37 +08:00
parent fe76b12b8c
commit b9d9722067
4 changed files with 120 additions and 14 deletions

View File

@ -6,6 +6,9 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
/**
@ -129,6 +132,13 @@ public class SystemMenu implements Serializable {
@TableField(value = "deleted")
private Boolean deleted;
// 添加 children 属性表示子菜单
@TableField(exist = false)
private List<SystemMenu> children = new ArrayList<>();
@TableField(exist = false)
private static final long serialVersionUID = 1L;

View File

@ -2,6 +2,7 @@ package com.guwan.controller.login;
import com.guwan.RBAC.TempVo;
import com.guwan.common.R;
import com.guwan.service.SystemUsersService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -12,13 +13,14 @@ import org.springframework.web.bind.annotation.RestController;
public class DemoController {
@Resource
private service.SystemUsersService systemUsersService;
private SystemUsersService systemUsersService;
@GetMapping("1")
public R test(){
systemUsersService.getRouterAsync();
// systemUsersService.getRouterAsync();
return R.ok().put("data", new TempVo().setName("1111"));
return R.ok().put("data", systemUsersService.getRouterAsync());
}
}

View File

@ -1,4 +1,4 @@
package service;
package com.guwan.service;
import com.guwan.RBAC.SystemUsers;
import com.baomidou.mybatisplus.extension.service.IService;
@ -11,6 +11,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface SystemUsersService extends IService<SystemUsers> {
public void getRouterAsync();
public String getRouterAsync();
}

View File

@ -2,16 +2,17 @@ package com.guwan.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.guwan.RBAC.*;
import com.guwan.mapper.*;
import com.guwan.service.SystemUsersService;
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.*;
import java.util.stream.Collectors;
/**
@ -22,7 +23,7 @@ import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper, SystemUsers>
implements SystemUsersService{
implements SystemUsersService {
private final SystemUsersMapper systemUsersMapper;
@ -35,7 +36,7 @@ public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper, Syste
@Override
public void getRouterAsync() {
public String getRouterAsync() {
/* List<String> roleList = ;
@ -98,12 +99,105 @@ public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper, Syste
}).distinct().toList();*/
System.out.println("systemMenus = " + buildMenuTree(systemMenus));
ArrayList<TempVo> tempVos = new ArrayList<>();
/* for (SystemMenu systemMenu : systemMenus) {
TempVo tempVo = new TempVo();
tempVo.setName(systemMenu.getName());
tempVo.setPath(systemMenu.getPath());
RouterMeta routerMeta = new RouterMeta();
routerMeta.setTitle("暂时没有");
ArrayList<String> strings1 = new ArrayList<>();
routerMeta.setAuths(strings1);
ArrayList<String> strings2 = new ArrayList<>();
routerMeta.setRoles(strings1);
tempVo.setMeta(routerMeta);
List<TempVo> list = new ArrayList<>(List.of(tempVo));
//tempVo.setChildren(list);
tempVos.add(tempVo);
}
System.out.println("tempVos = " + tempVos);*/
ArrayList<Integer> integers = new ArrayList<>();
for (SystemMenu systemMenu : systemMenus) {
System.out.println("systemMenu = " + systemMenu);
Integer count = 1;
Long parentId = systemMenu.getParentId();
while (parentId != 0L){
List<Long> temp = systemMenuMapper.selectObjs(new LambdaQueryWrapper<SystemMenu>()
.eq(SystemMenu::getId, parentId).select(SystemMenu::getParentId));
if (temp.isEmpty()) {
// 如果找不到父菜单跳出循环避免死循环
break;
}
parentId = temp.get(0);
count++;
};
integers.add(count);
}
System.out.println("integers = " + integers);
System.out.println("max = " + Collections.max(integers));
System.out.println("integers = " + integers.size());
return "111";
// System.out.println("systemMenus = " + buildMenuTree(systemMenus));
}
// 构建 JSON 数据结构
private static Map<String, Object> buildJson(List<SystemMenu> rootMenus) {
Map<String, Object> rootJson = new HashMap<>();
for (SystemMenu menu : rootMenus) {
Map<String, Object> menuJson = new HashMap<>();
menuJson.put("path", menu.getPath());
// 构建 meta 数据
Map<String, Object> meta = new HashMap<>();
meta.put("title", menu.getName()); // 使用菜单名称作为 title
meta.put("icon", menu.getIcon());
meta.put("rank", menu.getSort());
// 如果有子菜单则继续构建
if (menu.getChildren() != null && !menu.getChildren().isEmpty()) {
menuJson.put("children", buildJson(menu.getChildren()));
}
menuJson.put("meta", meta);
rootJson.put(menu.getPath(), menuJson);
}
return rootJson;
}
// 方法将菜单列表构建为树形结构
public static List<SystemMenu> buildMenuTree(List<SystemMenu> menuList) {
// parentId 分组菜单
@ -118,10 +212,10 @@ public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper, Syste
private static List<SystemMenu> buildTree(Map<Long, List<SystemMenu>> parentIdMap, Long parentId) {
List<SystemMenu> children = parentIdMap.getOrDefault(parentId, new ArrayList<>());
for (SystemMenu menu : children) {
/* for (SystemMenu menu : children) {
// 递归设置子菜单
menu.setChildren(buildTree(parentIdMap, menu.getId()));
}
}*/
return children;
}