diff --git a/snowy-plugin/snowy-plugin-sys/src/main/java/vip/xiaonuo/sys/modular/resource/service/impl/SysMenuServiceImpl.java b/snowy-plugin/snowy-plugin-sys/src/main/java/vip/xiaonuo/sys/modular/resource/service/impl/SysMenuServiceImpl.java index d4e3fb27..ece25ab6 100644 --- a/snowy-plugin/snowy-plugin-sys/src/main/java/vip/xiaonuo/sys/modular/resource/service/impl/SysMenuServiceImpl.java +++ b/snowy-plugin/snowy-plugin-sys/src/main/java/vip/xiaonuo/sys/modular/resource/service/impl/SysMenuServiceImpl.java @@ -14,6 +14,7 @@ package vip.xiaonuo.sys.modular.resource.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollStreamUtil; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.lang.tree.Tree; import cn.hutool.core.lang.tree.TreeNode; @@ -48,6 +49,10 @@ import vip.xiaonuo.sys.modular.resource.service.SysModuleService; import javax.annotation.Resource; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Collectors; /** @@ -97,6 +102,10 @@ public class SysMenuServiceImpl extends ServiceImpl impl lambdaQueryWrapper.like(SysMenu::getTitle, sysMenuTreeParam.getSearchKey()); } List resourceList = this.list(lambdaQueryWrapper); + + // 填充上层的父级菜单 + this.fillParentSysMenuInfo(resourceList); + List> treeNodeList = resourceList.stream().map(sysMenu -> new TreeNode<>(sysMenu.getId(), sysMenu.getParentId(), sysMenu.getTitle(), sysMenu.getSortCode()).setExtra(JSONUtil.parseObj(sysMenu))) @@ -104,6 +113,36 @@ public class SysMenuServiceImpl extends ServiceImpl impl return TreeUtil.build(treeNodeList, "0"); } + private void fillParentSysMenuInfo(List resourceList) { + if(CollUtil.isNotEmpty(resourceList)){ + List parentRelationSysMenus = resourceList.stream().filter(distinctByKey(SysMenu::getParentId)).collect(Collectors.toList()); + + List parentIds = null; + if(CollUtil.isNotEmpty(parentRelationSysMenus)){ + parentIds = CollUtil.newArrayList(); + for(SysMenu parentRelationSysMenu : parentRelationSysMenus){ + if(!StrUtil.equals(parentRelationSysMenu.getParentId(),"0")){ + parentIds.add(parentRelationSysMenu.getParentId()); + } + } + } + if(CollUtil.isNotEmpty(parentIds)){ + LambdaQueryWrapper parentSysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>(); + parentSysMenuLambdaQueryWrapper.in(SysMenu::getId,parentIds); + List parentSysMenus = this.list(parentSysMenuLambdaQueryWrapper); + if(CollUtil.isNotEmpty(parentSysMenus)){ + this.fillParentSysMenuInfo(parentSysMenus); + resourceList.addAll(parentSysMenus); + } + } + } + } + + private static Predicate distinctByKey(Function keyExtractor) { + Set seen = ConcurrentHashMap.newKeySet(); + return t -> seen.add(keyExtractor.apply(t)); + } + @Transactional(rollbackFor = Exception.class) @Override public void add(SysMenuAddParam sysMenuAddParam) {