package cn.techwolf.boss.crm.utils;
import org.apache.commons.collections4.CollectionUtils;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class TreeUtils {
/**
* 列表转树
* @return
*/
public static <T,R extends Tree<T>> List<R> listToTree (List<R> data) {
if (CollectionUtils.isEmpty(data)) {
return data;
}
Iterator<R> iterator = data.iterator();
Map<Object, R> cache = data.stream().collect(Collectors.toMap(Tree::id, tree -> tree));
while (iterator.hasNext()) {
Tree<T> next = iterator.next();
if (!next.noneParentId()) {
iterator.remove();
Tree<T> parent = cache.get(next.parentId());
Optional.ofNullable(parent).ifPresent(treeParent -> treeParent.addChildren(next));
}
}
return data;
}
/**
* 树转列表
* @param data
* @param <T>
* @return
*/
public static <T,R extends Tree<T>> List<R> treeToList (List<R> data) {
if (CollectionUtils.isEmpty(data)) {
return data;
}
List<R> result = new ArrayList<>();
treeToList(data,result);
return result;
}
private static <T,R extends Tree<T>> List<R> treeToList (List<R> data,List<R> result) {
if (CollectionUtils.isEmpty(data)) {
return data;
}
for (R tree : data) {
result.add(tree);
List<R> children = (List<R>)tree.getChildren();
if (CollectionUtils.isNotEmpty(children)) {
treeToList(children,result);
tree.removeChildren();
}
}
return result;
}
/**
* 移除树的叶子节点
* @param root
* @param <T>
* @return
*/
public static <T,R extends Tree<T>> List<R> removeLeaf(List<R> root) {
if (CollectionUtils.isEmpty(root)) {
return root;
}
Iterator<? extends Tree<T>> iterator = root.iterator();
while (iterator.hasNext()) {
Tree<T> next = iterator.next();
if (next.isLeaf()) {
iterator.remove();
} else {
if (CollectionUtils.isEmpty(removeLeaf(next.getChildren()))) {
next.removeChildren();
}
}
}
return root;
}
/**
* 通过id获取节点
* @param data
* @param id
* @param <T>
* @return
*/
public static <T,R extends Tree<T>> R getById(List<R> data, T id) {
if (CollectionUtils.isEmpty(data)) {
return null;
}
for (R tree : data) {
R result = tree.getById(id);
if (result != null) {
return result;
}
}
return null;
}
/**
* 计算
* @param data
* @param <T>
* @param <R>
*/
public static <T,R extends Tree<T>> void cal(List<R> data, Consumer<R> consumer) {
if (CollectionUtils.isEmpty(data) || consumer == null) {
return;
}
for (R one : data) {
List<R> children = (List<R>)one.getChildren();
if (CollectionUtils.isNotEmpty(children)) {
cal(children,consumer);
}
consumer.accept(one);
}
}
}
举一下对上面例子的应用
1.列表转树的话
TreeUtils.listToTree(deptStatisticsList); // 列表转树
直接传入部门的列表,通过这种方法生成部门树
2.获取树的子树
DepartmentDO departmentDO = TreeUtils.getById(departmentService.getAllDepartmentTree(), Long.valueOf(1));
传入部门列表以及树上的某一个部门id获取到子树。
3.树上的数据的汇总
TreeUtils.cal(deptStatisticsList, MissionTaskVo::calRatio); // 计算占比
/**
* 计算占比
* @return
*/
public MissionTaskVo calRatio() {
// 获取孩子的数据
if (CollectionUtils.isNotEmpty(this.getChildren())) {
List<MissionTaskVo> children = this.children;
for (MissionTaskVo child : children) {
this.todoCount += child.getTodoCount();
this.overTimeCount += child.getOverTimeCount();
this.finishCount += child.getFinishCount();
this.overTimefinishCount += child.getOverTimefinishCount();
this.validCount += child.getValidCount();
this.cancelCount += child.getCancelCount();
}
}
return this;
}
在实体里面定义一个汇总的方法,然后在调用树的汇总的方法的时候,穿进去这个方法,就可以进行汇总数据的功能。

1339

被折叠的 条评论
为什么被折叠?



