From 46b88742d7af5478458db9bb811ac9769b22316a Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Tue, 21 Nov 2023 09:00:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E7=A9=BA=E6=8C=87=E9=92=88bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/EvaluationTreeServiceImpl.java | 6 +-- .../TeamModelExtensionApplicationTests.java | 16 +++---- .../java/cn/teammodel/TestWithoutSpring.java | 48 +++++++++++++++---- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationTreeServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationTreeServiceImpl.java index ce46cd5..0da4f17 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationTreeServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationTreeServiceImpl.java @@ -31,11 +31,11 @@ public class EvaluationTreeServiceImpl implements EvaluationTreeService { } /** - * 递归的构建父亲节点的孩子,以及孩子的孩子 (理论无极树,但应该考虑是否增加递归深度) + * 递归的构建父亲节点的孩子,以及孩子的孩子 (理论支持无极树,但应该考虑是否增加递归深度) */ private void buildChildren(EvaluationTreeNode parent, List nodes) { List children = getChildren(parent, nodes); - if (children.size() > 0) return; + if (children.size() == 0) return; parent.setChildren(children); for (EvaluationTreeNode child : children) { @@ -50,7 +50,7 @@ public class EvaluationTreeServiceImpl implements EvaluationTreeService { private List getChildren(EvaluationTreeNode parent, List nodes) { List children = new ArrayList<>(); for (EvaluationTreeNode node : nodes) { - if (node.getPid().equals(parent.getId())) { + if (StringUtils.isNotBlank(node.getPid()) && node.getPid().equals(parent.getId())) { children.add(node); } } diff --git a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java index 8a3ad41..5d58733 100644 --- a/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java +++ b/src/test/java/cn/teammodel/TeamModelExtensionApplicationTests.java @@ -49,16 +49,16 @@ class TeamModelExtensionApplicationTests { // 2 EvaluationTreeNode node1 = new EvaluationTreeNode(); String subId = UUID.randomUUID().toString(); - node.setId(subId); - node.setPid(rootId); - node.setName("child-1"); - node.setScore(0); + node1.setId(subId); + node1.setPid(rootId); + node1.setName("child-1"); + node1.setScore(0); // 3 EvaluationTreeNode node2 = new EvaluationTreeNode(); - node.setId(UUID.randomUUID().toString()); - node.setPid(subId); - node.setName("child-2"); - node.setScore(0); + node2.setId(UUID.randomUUID().toString()); + node2.setPid(subId); + node2.setName("child-2"); + node2.setScore(0); nodes.add(node); nodes.add(node1); nodes.add(node2); diff --git a/src/test/java/cn/teammodel/TestWithoutSpring.java b/src/test/java/cn/teammodel/TestWithoutSpring.java index 54605ec..bfb67f3 100644 --- a/src/test/java/cn/teammodel/TestWithoutSpring.java +++ b/src/test/java/cn/teammodel/TestWithoutSpring.java @@ -1,7 +1,10 @@ package cn.teammodel; 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.service.impl.EvaluationTreeServiceImpl; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiRobotSendRequest; @@ -10,7 +13,10 @@ import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; +import java.util.UUID; /** * @author winter @@ -82,14 +88,38 @@ public class TestWithoutSpring { } @Test public void testResult() { - User user = new User(); - user.setId("ds"); - user.setName("ds"); - user.setPicture("s"); - user.setStandard("s"); - user.setScope("ad"); - user.setWebsite("dsa"); - user.setArea(""); - System.out.println(R.success(user)); + EvaluationTreeServiceImpl service = new EvaluationTreeServiceImpl(); + EvaluationTree evaluationTree = new EvaluationTree(); + evaluationTree.setCode("evaluation"); + evaluationTree.setCampusId("default"); + List nodes = new ArrayList<>(); + + // 1 + String rootId = UUID.randomUUID().toString(); + 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 tree = service.buildTree(nodes); + System.out.println(tree); } }