From 61f53ef39f784ee2e23ceccf1abe71994df7c3c0 Mon Sep 17 00:00:00 2001 From: smallchill Date: Fri, 28 Dec 2018 21:52:54 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springblade/core/mp/support/Condition.java | 4 +++- .../core/tool/node/ForestNodeManager.java | 16 +++++++++++++++- .../core/tool/node/ForestNodeMerger.java | 12 +++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/blade-core-mybatis/src/main/java/org/springblade/core/mp/support/Condition.java b/blade-core-mybatis/src/main/java/org/springblade/core/mp/support/Condition.java index e5392a4..5612408 100644 --- a/blade-core-mybatis/src/main/java/org/springblade/core/mp/support/Condition.java +++ b/blade-core-mybatis/src/main/java/org/springblade/core/mp/support/Condition.java @@ -18,8 +18,8 @@ package org.springblade.core.mp.support; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.BeanUtil; +import org.springblade.core.tool.utils.Func; import java.util.Map; @@ -63,6 +63,8 @@ public class Condition { * @return */ public static QueryWrapper getQueryWrapper(Map query, Class clazz) { + query.remove("current"); + query.remove("size"); QueryWrapper qw = new QueryWrapper<>(); qw.setEntity(BeanUtil.newInstance(clazz)); if (Func.isNotEmpty(query)) { diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeManager.java b/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeManager.java index ce69401..bba8e75 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeManager.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeManager.java @@ -30,6 +30,11 @@ public class ForestNodeManager { */ private List list; + /** + * 森林的父节点ID + */ + private List parentIds = new ArrayList<>(); + public ForestNodeManager(List items) { list = items; } @@ -49,6 +54,15 @@ public class ForestNodeManager { return null; } + /** + * 增加父节点ID + * + * @param parentId + */ + public void addParentId(Integer parentId) { + parentIds.add(parentId); + } + /** * 获取树的根节点(一个森林对应多颗树) * @@ -57,7 +71,7 @@ public class ForestNodeManager { public List getRoot() { List roots = new ArrayList<>(); for (T forestNode : list) { - if (forestNode.getParentId() == 0) { + if (forestNode.getParentId() == 0 || parentIds.contains(forestNode.getId())) { roots.add(forestNode); } } diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeMerger.java b/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeMerger.java index dbe096a..4e35d64 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeMerger.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeMerger.java @@ -15,6 +15,7 @@ */ package org.springblade.core.tool.node; +import java.util.ArrayList; import java.util.List; /** @@ -32,13 +33,18 @@ public class ForestNodeMerger { * @return 多棵树的根节点集合 */ public static List merge(List items) { + List parentIds = new ArrayList<>(); ForestNodeManager forestNodeManager = new ForestNodeManager<>(items); - for (T forestNode : items) { + items.forEach(forestNode -> { if (forestNode.getParentId() != 0) { INode node = forestNodeManager.getTreeNodeAT(forestNode.getParentId()); - node.getChildren().add(forestNode); + if (node != null) { + node.getChildren().add(forestNode); + } else { + forestNodeManager.addParentId(forestNode.getId()); + } } - } + }); return forestNodeManager.getRoot(); }