🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` /** * @author 张跃帅 * @Description: 系统菜单-工具 * @date 2020/08/12 */ public class SystemMenuUtil { private static final SystemMenuService systemMenuService = SpringUtil.getBean(SystemMenuService.class); /** * 获取-系统应用tree */ public static List<Tree<String>> getTree(String name) { // 获取参数 String appCode = SystemContext.me().getAppCode(); // 变量创建ArrayList List<Tree<String>> treeNodeList = CollectionUtil.newArrayList(); // 创建查询包装器 LambdaQueryWrapper<SystemMenuEntity> queryWrapper = new LambdaQueryWrapper<>(); // 设置条件 queryWrapper.eq(SystemMenuEntity::getAppCode, appCode) .eq(SystemMenuEntity::getDataFlag, CommonDataFlagEnum.NORMAL.getCode()) .eq(SystemMenuEntity::getDelFlag, CommonDelFlagEnum.NOT_DELETE.getCode()); // 设置排序 queryWrapper.orderByAsc(SystemMenuEntity::getSort); // 查询 List<SystemMenuEntity> systemMenuList = systemMenuService.list(queryWrapper); // 判断 if (systemMenuList != null && systemMenuList.size() > 0) { // 获取参数 String parentId = CommonTreeParentIdEnum.PARENTID_0.getCode(); // tree转换器 treeNodeList = TreeUtil.build(systemMenuList, parentId, (systemMenu, tree) -> { // 赋值 tree.setId(Convert.toStr(systemMenu.getId())); tree.setParentId(Convert.toStr(systemMenu.getPid())); tree.setName(systemMenu.getName()); }); } // 判断-条件搜索 if (treeNodeList != null && treeNodeList.size() > 0 && StrUtil.isNotBlank(name)) { // 变量创建ArrayList List<Tree<String>> searchTreeNodeList = CollectionUtil.newArrayList(); // 遍历 for (Tree<String> treeNode : treeNodeList) { // 搜索查询 searchTreeNodeByName(treeNode, name, searchTreeNodeList); } // 赋值 treeNodeList = searchTreeNodeList; } // 返回 return treeNodeList; } /** * 递归遍历树节点-搜索匹配节点 */ private static List<Tree<String>> searchTreeNodeByName(Tree<String> treeNode, String name, List<Tree<String>> searchTreeNodeList) { // 获取参数 String nodeName = treeNode.getName().toString(); List<Tree<String>> childNodeList = treeNode.getChildren(); // 判断 if (nodeName.contains(name)) { // 添加 searchTreeNodeList.add(treeNode); } // 判断 if (childNodeList != null && childNodeList.size() > 0) { // 遍历 for (Tree<String> childNode : treeNode.getChildren()) { // 匹配子节点 searchTreeNodeByName(childNode, name, searchTreeNodeList); } } // 返回 return searchTreeNodeList; } }