|
|
@@ -20,8 +20,10 @@ import shop.alien.store.service.StorePlatformMenuService;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -53,17 +55,61 @@ public class StorePlatformMenuServiceImpl extends ServiceImpl<StorePlatformMenuM
|
|
|
queryWrapper.orderByDesc(StorePlatformMenu::getCreatedTime);
|
|
|
|
|
|
// 查询所有符合条件的菜单
|
|
|
- List<StorePlatformMenu> allMenus = storePlatformMenuMapper.selectList(queryWrapper);
|
|
|
+ List<StorePlatformMenu> matchedMenus = storePlatformMenuMapper.selectList(queryWrapper);
|
|
|
|
|
|
- // 转换为VO对象
|
|
|
+ // 2. 如果提供了菜单名称查询条件,需要向上查找所有父菜单
|
|
|
+ Set<Long> menuIdSet = new HashSet<>();
|
|
|
+ List<StorePlatformMenu> allMenus = new ArrayList<>(matchedMenus);
|
|
|
+
|
|
|
+ if (StringUtils.hasText(menuName) && !CollectionUtils.isEmpty(matchedMenus)) {
|
|
|
+ // 收集所有匹配菜单的ID
|
|
|
+ for (StorePlatformMenu menu : matchedMenus) {
|
|
|
+ menuIdSet.add(menu.getMenuId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 递归向上查找所有父菜单
|
|
|
+ Set<Long> parentIds = new HashSet<>();
|
|
|
+ for (StorePlatformMenu menu : matchedMenus) {
|
|
|
+ Long parentId = menu.getParentId();
|
|
|
+ if (parentId != null && parentId != 0) {
|
|
|
+ parentIds.add(parentId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 递归向上查找所有父菜单,直到根节点
|
|
|
+ while (!parentIds.isEmpty()) {
|
|
|
+ Set<Long> nextLevelParentIds = new HashSet<>();
|
|
|
+ for (Long parentId : parentIds) {
|
|
|
+ if (!menuIdSet.contains(parentId)) {
|
|
|
+ StorePlatformMenu parentMenu = storePlatformMenuMapper.selectById(parentId);
|
|
|
+ if (parentMenu != null && "0".equals(parentMenu.getDelFlag())) {
|
|
|
+ allMenus.add(parentMenu);
|
|
|
+ menuIdSet.add(parentId);
|
|
|
+
|
|
|
+ // 继续向上查找
|
|
|
+ Long grandParentId = parentMenu.getParentId();
|
|
|
+ if (grandParentId != null && grandParentId != 0) {
|
|
|
+ nextLevelParentIds.add(grandParentId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ parentIds = nextLevelParentIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("查询到匹配菜单{}条,向上查找到父菜单{}条,总计{}条",
|
|
|
+ matchedMenus.size(), allMenus.size() - matchedMenus.size(), allMenus.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 转换为VO对象
|
|
|
List<StorePlatformMenuVo> allMenuVos = allMenus.stream()
|
|
|
.map(this::convertToVo)
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
- // 2. 构建树形结构(根据parent_id构建)
|
|
|
+ // 4. 构建树形结构(根据parent_id构建)
|
|
|
List<StorePlatformMenuVo> treeMenus = buildMenuTreeByParentId(allMenuVos);
|
|
|
|
|
|
- // 3. 对一级菜单(parent_id = 0 或 null)进行分页
|
|
|
+ // 5. 对一级菜单(parent_id = 0 或 null)进行分页
|
|
|
List<StorePlatformMenuVo> level1Menus = treeMenus.stream()
|
|
|
.filter(menu -> menu.getParentId() == null || menu.getParentId() == 0)
|
|
|
.collect(Collectors.toList());
|
|
|
@@ -75,7 +121,7 @@ public class StorePlatformMenuServiceImpl extends ServiceImpl<StorePlatformMenuM
|
|
|
|
|
|
List<StorePlatformMenuVo> pageMenus = start < total ? level1Menus.subList(start, end) : new ArrayList<>();
|
|
|
|
|
|
- // 4. 构建分页结果
|
|
|
+ // 6. 构建分页结果
|
|
|
IPage<StorePlatformMenuVo> pageResult = new Page<>(page, size, total);
|
|
|
pageResult.setRecords(pageMenus);
|
|
|
|