You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.9 KiB
60 lines
1.9 KiB
package cn.teammodel.service.impl;
|
|
|
|
import cn.teammodel.model.entity.EvaluationTreeNode;
|
|
import cn.teammodel.service.EvaluationTreeService;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @author winter
|
|
* @create 2023-11-20 17:47
|
|
*/
|
|
public class EvaluationTreeServiceImpl implements EvaluationTreeService {
|
|
|
|
@Override
|
|
public List<EvaluationTreeNode> buildTree(List<EvaluationTreeNode> nodes) {
|
|
List<EvaluationTreeNode> parents = new ArrayList<>();
|
|
// pid 为 null 或者 "" 则为 parents
|
|
for (EvaluationTreeNode node : nodes) {
|
|
if (StringUtils.isBlank(node.getPid())) {
|
|
parents.add(node);
|
|
}
|
|
}
|
|
|
|
// 迭代构建孩子节点
|
|
for (EvaluationTreeNode parent : parents) {
|
|
buildChildren(parent, nodes);
|
|
}
|
|
return parents;
|
|
}
|
|
|
|
/**
|
|
* 递归的构建父亲节点的孩子,以及孩子的孩子 (理论无极树,但应该考虑是否增加递归深度)
|
|
*/
|
|
private void buildChildren(EvaluationTreeNode parent, List<EvaluationTreeNode> nodes) {
|
|
List<EvaluationTreeNode> children = getChildren(parent, nodes);
|
|
if (children.size() > 0) return;
|
|
|
|
parent.setChildren(children);
|
|
for (EvaluationTreeNode child : children) {
|
|
// 如果子节点还有孩子的话,就继续递归构建
|
|
if (getChildren(child, nodes).size() > 0) {
|
|
buildChildren(child, nodes);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private List<EvaluationTreeNode> getChildren(EvaluationTreeNode parent, List<EvaluationTreeNode> nodes) {
|
|
List<EvaluationTreeNode> children = new ArrayList<>();
|
|
for (EvaluationTreeNode node : nodes) {
|
|
if (node.getPid().equals(parent.getId())) {
|
|
children.add(node);
|
|
}
|
|
}
|
|
return children;
|
|
}
|
|
}
|