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