fix: 修复生成树的空指针bug

11111
winter 1 year ago
parent 676f6fc46b
commit 46b88742d7

@ -31,11 +31,11 @@ public class EvaluationTreeServiceImpl implements EvaluationTreeService {
} }
/** /**
* , (,) * , (,)
*/ */
private void buildChildren(EvaluationTreeNode parent, List<EvaluationTreeNode> nodes) { private void buildChildren(EvaluationTreeNode parent, List<EvaluationTreeNode> nodes) {
List<EvaluationTreeNode> children = getChildren(parent, nodes); List<EvaluationTreeNode> children = getChildren(parent, nodes);
if (children.size() > 0) return; if (children.size() == 0) return;
parent.setChildren(children); parent.setChildren(children);
for (EvaluationTreeNode child : children) { for (EvaluationTreeNode child : children) {
@ -50,7 +50,7 @@ public class EvaluationTreeServiceImpl implements EvaluationTreeService {
private List<EvaluationTreeNode> getChildren(EvaluationTreeNode parent, List<EvaluationTreeNode> nodes) { private List<EvaluationTreeNode> getChildren(EvaluationTreeNode parent, List<EvaluationTreeNode> nodes) {
List<EvaluationTreeNode> children = new ArrayList<>(); List<EvaluationTreeNode> children = new ArrayList<>();
for (EvaluationTreeNode node : nodes) { for (EvaluationTreeNode node : nodes) {
if (node.getPid().equals(parent.getId())) { if (StringUtils.isNotBlank(node.getPid()) && node.getPid().equals(parent.getId())) {
children.add(node); children.add(node);
} }
} }

@ -49,16 +49,16 @@ class TeamModelExtensionApplicationTests {
// 2 // 2
EvaluationTreeNode node1 = new EvaluationTreeNode(); EvaluationTreeNode node1 = new EvaluationTreeNode();
String subId = UUID.randomUUID().toString(); String subId = UUID.randomUUID().toString();
node.setId(subId); node1.setId(subId);
node.setPid(rootId); node1.setPid(rootId);
node.setName("child-1"); node1.setName("child-1");
node.setScore(0); node1.setScore(0);
// 3 // 3
EvaluationTreeNode node2 = new EvaluationTreeNode(); EvaluationTreeNode node2 = new EvaluationTreeNode();
node.setId(UUID.randomUUID().toString()); node2.setId(UUID.randomUUID().toString());
node.setPid(subId); node2.setPid(subId);
node.setName("child-2"); node2.setName("child-2");
node.setScore(0); node2.setScore(0);
nodes.add(node); nodes.add(node);
nodes.add(node1); nodes.add(node1);
nodes.add(node2); nodes.add(node2);

@ -1,7 +1,10 @@
package cn.teammodel; package cn.teammodel;
import cn.teammodel.common.R; import cn.teammodel.common.R;
import cn.teammodel.model.entity.EvaluationTree;
import cn.teammodel.model.entity.EvaluationTreeNode;
import cn.teammodel.model.entity.User; import cn.teammodel.model.entity.User;
import cn.teammodel.service.impl.EvaluationTreeServiceImpl;
import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.request.OapiRobotSendRequest;
@ -10,7 +13,10 @@ import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.UUID;
/** /**
* @author winter * @author winter
@ -82,14 +88,38 @@ public class TestWithoutSpring {
} }
@Test @Test
public void testResult() { public void testResult() {
User user = new User(); EvaluationTreeServiceImpl service = new EvaluationTreeServiceImpl();
user.setId("ds"); EvaluationTree evaluationTree = new EvaluationTree();
user.setName("ds"); evaluationTree.setCode("evaluation");
user.setPicture("s"); evaluationTree.setCampusId("default");
user.setStandard("s"); List<EvaluationTreeNode> nodes = new ArrayList<>();
user.setScope("ad");
user.setWebsite("dsa"); // 1
user.setArea(""); String rootId = UUID.randomUUID().toString();
System.out.println(R.success(user)); EvaluationTreeNode node = new EvaluationTreeNode();
node.setId(rootId);
node.setPid(null);
node.setName("root");
node.setScore(0);
// 2
EvaluationTreeNode node1 = new EvaluationTreeNode();
String subId = UUID.randomUUID().toString();
node1.setId(subId);
node1.setPid(rootId);
node1.setName("child-1");
node1.setScore(0);
// 3
EvaluationTreeNode node2 = new EvaluationTreeNode();
node2.setId(UUID.randomUUID().toString());
node2.setPid(subId);
node2.setName("child-2");
node2.setScore(0);
nodes.add(node);
nodes.add(node1);
nodes.add(node2);
evaluationTree.setNodes(nodes);
List<EvaluationTreeNode> tree = service.buildTree(nodes);
System.out.println(tree);
} }
} }

Loading…
Cancel
Save