修改菜单树的构建
parent
8414fcb010
commit
7f8a37803a
|
@ -1,7 +1,6 @@
|
|||
package xyz.zhouxy.plusone.system.application.service;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
@ -13,8 +12,8 @@ import org.springframework.transaction.annotation.Propagation;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.MoreCollections;
|
||||
import xyz.zhouxy.plusone.constant.EntityStatus;
|
||||
import xyz.zhouxy.plusone.domain.IWithOrderNumber;
|
||||
import xyz.zhouxy.plusone.exception.DataNotExistException;
|
||||
import xyz.zhouxy.plusone.system.application.common.exception.UnsupportedMenuTypeException;
|
||||
import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
|
||||
|
@ -139,38 +138,33 @@ public class MenuManagementService {
|
|||
|
||||
@Transactional(propagation = Propagation.SUPPORTS)
|
||||
public List<MenuViewObject> buildMenuTree(List<MenuViewObject> menus) {
|
||||
List<MenuViewObject> rootMenus = menus
|
||||
.stream()
|
||||
.filter(menu -> Objects.equals(menu.getParentId(), 0L))
|
||||
.toList();
|
||||
|
||||
Map<Long, MenuViewObject> allMenus = new HashMap<>();
|
||||
for (var item : menus) {
|
||||
allMenus.put(item.getId(), item);
|
||||
}
|
||||
|
||||
// 创建并填充 Map
|
||||
final Map<Long, MenuViewObject> menuMap = MoreCollections.toHashMap(menus, MenuViewObject::getId);
|
||||
for (MenuViewObject menu : menus) {
|
||||
long parentId = menu.getParentId();
|
||||
while (parentId != 0 && !allMenus.containsKey(parentId)) {
|
||||
if (parentId != 0L) {
|
||||
while (!menuMap.containsKey(parentId)) {
|
||||
MenuViewObject parent = findById(parentId);
|
||||
if (parent == null) {
|
||||
break;
|
||||
}
|
||||
allMenus.put(parent.getId(), parent);
|
||||
menuMap.put(parent.getId(), parent);
|
||||
parentId = parent.getParentId();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var menu : allMenus.values()) {
|
||||
var parent = allMenus.getOrDefault(menu.getParentId(), null);
|
||||
Collection<MenuViewObject> allMenus = menuMap.values();
|
||||
for (var menu : allMenus) {
|
||||
var parent = menuMap.get(menu.getParentId());
|
||||
if (parent != null) {
|
||||
parent.addChild(menu);
|
||||
}
|
||||
}
|
||||
|
||||
return rootMenus
|
||||
.stream()
|
||||
.sorted(Comparator.comparing(IWithOrderNumber::getOrderNumber))
|
||||
return allMenus.stream()
|
||||
.filter(menu -> (menu != null) && Objects.equals(menu.getParentId(), 0L))
|
||||
.sorted()
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue