优化代码

This commit is contained in:
smallchill 2018-12-28 21:52:54 +08:00
parent af946ea319
commit 61f53ef39f
3 changed files with 27 additions and 5 deletions

View File

@ -18,8 +18,8 @@ package org.springblade.core.mp.support;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.BeanUtil;
import org.springblade.core.tool.utils.Func;
import java.util.Map; import java.util.Map;
@ -63,6 +63,8 @@ public class Condition {
* @return * @return
*/ */
public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Class<T> clazz) { public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Class<T> clazz) {
query.remove("current");
query.remove("size");
QueryWrapper<T> qw = new QueryWrapper<>(); QueryWrapper<T> qw = new QueryWrapper<>();
qw.setEntity(BeanUtil.newInstance(clazz)); qw.setEntity(BeanUtil.newInstance(clazz));
if (Func.isNotEmpty(query)) { if (Func.isNotEmpty(query)) {

View File

@ -30,6 +30,11 @@ public class ForestNodeManager<T extends INode> {
*/ */
private List<T> list; private List<T> list;
/**
* 森林的父节点ID
*/
private List<Integer> parentIds = new ArrayList<>();
public ForestNodeManager(List<T> items) { public ForestNodeManager(List<T> items) {
list = items; list = items;
} }
@ -49,6 +54,15 @@ public class ForestNodeManager<T extends INode> {
return null; return null;
} }
/**
* 增加父节点ID
*
* @param parentId
*/
public void addParentId(Integer parentId) {
parentIds.add(parentId);
}
/** /**
* 获取树的根节点(一个森林对应多颗树) * 获取树的根节点(一个森林对应多颗树)
* *
@ -57,7 +71,7 @@ public class ForestNodeManager<T extends INode> {
public List<T> getRoot() { public List<T> getRoot() {
List<T> roots = new ArrayList<>(); List<T> roots = new ArrayList<>();
for (T forestNode : list) { for (T forestNode : list) {
if (forestNode.getParentId() == 0) { if (forestNode.getParentId() == 0 || parentIds.contains(forestNode.getId())) {
roots.add(forestNode); roots.add(forestNode);
} }
} }

View File

@ -15,6 +15,7 @@
*/ */
package org.springblade.core.tool.node; package org.springblade.core.tool.node;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -32,13 +33,18 @@ public class ForestNodeMerger {
* @return 多棵树的根节点集合 * @return 多棵树的根节点集合
*/ */
public static <T extends INode> List<T> merge(List<T> items) { public static <T extends INode> List<T> merge(List<T> items) {
List<Integer> parentIds = new ArrayList<>();
ForestNodeManager<T> forestNodeManager = new ForestNodeManager<>(items); ForestNodeManager<T> forestNodeManager = new ForestNodeManager<>(items);
for (T forestNode : items) { items.forEach(forestNode -> {
if (forestNode.getParentId() != 0) { if (forestNode.getParentId() != 0) {
INode node = forestNodeManager.getTreeNodeAT(forestNode.getParentId()); INode node = forestNodeManager.getTreeNodeAT(forestNode.getParentId());
if (node != null) {
node.getChildren().add(forestNode); node.getChildren().add(forestNode);
} else {
forestNodeManager.addParentId(forestNode.getId());
} }
} }
});
return forestNodeManager.getRoot(); return forestNodeManager.getRoot();
} }