Skip to content

Commit

Permalink
Merge pull request #43 from Yiuman/dev
Browse files Browse the repository at this point in the history
refactor: 优化列表转树
  • Loading branch information
Yiuman authored Jun 9, 2022
2 parents 3cefe42 + a222e9c commit 1bcbb49
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
1 change: 1 addition & 0 deletions citrus-main/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ spring:
activiti:
db-history-used: true
history-level: audit
database-schema-update: true

citrus:
verify:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -142,7 +139,7 @@ public E treeQuery(Query query) {
list.addAll(getTreeMapper().treeLink(table.getTableName(), Wrappers.<E>query().apply(parentSql).in("t1." + table.getKeyColumn(), ids)));
final E root = getRoot();
//传list进去前需要去重,并排除根节点
initTreeFromList(root, list.parallelStream().distinct().filter(item -> !Objects.equals(item.getId(), root.getId())).collect(Collectors.toList()));
listToTree(root, list.parallelStream().distinct().filter(item -> !Objects.equals(item.getId(), root.getId())).collect(Collectors.toList()));
return root;
}

Expand All @@ -152,13 +149,11 @@ public E treeQuery(Query query) {
* @param start 挂载的节点
* @param list 节点列表
*/
protected void initTreeFromList(E start, List<E> list) {
List<E> childrenOfStart = list.parallelStream()
.filter(current -> Objects.equals(current.getParentId(), start.getId())).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(childrenOfStart)) {
start.setChildren(childrenOfStart);
childrenOfStart.parallelStream().forEach(next -> initTreeFromList(next, list));
}
protected void listToTree(E start, List<E> list) {
Map<K, List<E>> parentIdChildrenMap = list.stream().collect(Collectors.groupingBy(Tree::getParentId));
list.forEach(entity -> entity.setChildren(parentIdChildrenMap.get(entity.getId())));
start.setChildren(list.parallelStream()
.filter(current -> Objects.equals(current.getParentId(), start.getId())).collect(Collectors.toList()));
}

@Override
Expand All @@ -177,7 +172,7 @@ public void load(E current, boolean isLazy) {
});
current.setChildren(children);
} else {
initTreeFromList(current, list());
listToTree(current, list());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yiuman.citrus.support.crud.query.Query;
import com.github.yiuman.citrus.support.model.BaseTree;
import com.github.yiuman.citrus.support.model.Tree;
import com.github.yiuman.citrus.support.utils.LambdaUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -43,27 +47,20 @@ public E load(boolean isLazy) {

@Override
public E treeQuery(Query query) {
return initSimpleTreeByList(list(query));
return listToTree(list(query));
}

protected E initSimpleTreeByList(List<E> list) {
protected E listToTree(List<E> list) {
E root = getRoot();
final Set<E> existsEntity = Collections.synchronizedSet(new LinkedHashSet<>(list));
list.parallelStream().forEach(item -> existsEntity.addAll(parents(item)));
this.mountByList(root, existsEntity);
listToTree(root, list);
return root;

}

private void mountByList(E current, final Set<E> sets) {
//这里的根节点肯定是虚拟节点,ID为空,第二级挂的父节点ID为空,所以需要这么判断
List<E> children = sets.stream().filter(item -> (
!Objects.equals(item.getId(), current.getId())
&& (Objects.equals(current.getId(), item.getParentId())
|| (Objects.nonNull(item.getParentId()) && Objects.equals(item.getParentId(), current.getId()))
))).collect(Collectors.toList());
current.setChildren(children);
children.parallelStream().forEach(item -> mountByList(item, sets));
private void listToTree(E current, final List<E> list) {
Map<K, List<E>> parentIdChildrenMap = list.stream().collect(Collectors.groupingBy(Tree::getParentId));
list.forEach(entity -> entity.setChildren(parentIdChildrenMap.get(entity.getId()).stream().distinct().collect(Collectors.toList())));
current.setChildren(list.stream().filter(entity -> Objects.equals(entity.getParentId(), current.getParentId())).collect(Collectors.toList()));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>17</source>
<target>17</target>
<source>16</source>
<target>16</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
Expand Down

0 comments on commit 1bcbb49

Please sign in to comment.