From 021d3caf4f749d5a45bac05fe34792b6e038a028 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Fri, 30 Oct 2020 15:20:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E5=90=8C=E7=BA=A7=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../track/service/TestCaseNodeService.java | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java index 5610abfe9f..16dfadf496 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java @@ -1,6 +1,7 @@ package io.metersphere.track.service; +import com.google.common.util.concurrent.AtomicDouble; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.ext.ExtTestCaseMapper; @@ -53,12 +54,14 @@ public class TestCaseNodeService { @Resource TestCaseReviewMapper testCaseReviewMapper; + private static final double LIMIT_POS = 64; + public String addNode(TestCaseNode node) { validateNode(node); node.setCreateTime(System.currentTimeMillis()); node.setUpdateTime(System.currentTimeMillis()); node.setId(UUID.randomUUID().toString()); - double pos = getNextLevelPos(node.getProjectId(), node.getLevel()); + double pos = getNextLevelPos(node.getProjectId(), node.getLevel(), node.getParentId()); node.setPos(pos); testCaseNodeMapper.insertSelective(node); return node.getId(); @@ -488,7 +491,7 @@ public class TestCaseNodeService { testCaseNode.setUpdateTime(System.currentTimeMillis()); testCaseNode.setLevel(level); testCaseNode.setId(UUID.randomUUID().toString()); - double pos = getNextLevelPos(projectId, level); + double pos = getNextLevelPos(projectId, level, pId); testCaseNode.setPos(pos); testCaseNodeMapper.insert(testCaseNode); return testCaseNode.getId(); @@ -616,17 +619,42 @@ public class TestCaseNodeService { pos = afterCase != null ? (beforeCase.getPos() + afterCase.getPos()) / 2.0 : beforeCase.getPos() + 65536; } - // todo pos 低于阈值时,触发更新方法,重新计算此目录的所有同级目录的 pos 值 - caseNode.setPos(pos); testCaseNodeMapper.updateByPrimaryKeySelective(caseNode); + + // pos 低于阈值时,触发更新方法,重新计算此目录的所有同级目录的 pos 值 + if (pos < LIMIT_POS) { + refreshPos(caseNode.getProjectId(), caseNode.getLevel(), caseNode.getParentId()); + } } - public double getNextLevelPos(String projectId, int level) { + private List getPos(String projectId, int level, String parentId, String order) { TestCaseNodeExample example = new TestCaseNodeExample(); - example.createCriteria().andProjectIdEqualTo(projectId).andLevelEqualTo(level); - example.setOrderByClause("pos desc"); - List list = testCaseNodeMapper.selectByExample(example); + TestCaseNodeExample.Criteria criteria = example.createCriteria(); + criteria.andProjectIdEqualTo(projectId).andLevelEqualTo(level); + if (level != 1 && StringUtils.isNotBlank(parentId)) { + criteria.andParentIdEqualTo(parentId); + } + example.setOrderByClause(order); + return testCaseNodeMapper.selectByExample(example); + } + + private void refreshPos(String projectId, int level, String parentId) { + List nodes = getPos(projectId, level, parentId, "pos asc"); + if (!CollectionUtils.isEmpty(nodes)) { + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + TestCaseNodeMapper testCaseNodeMapper = sqlSession.getMapper(TestCaseNodeMapper.class); + AtomicDouble pos = new AtomicDouble(65536); + nodes.forEach((node) -> { + node.setPos(pos.getAndAdd(65536)); + testCaseNodeMapper.updateByPrimaryKey(node); + }); + sqlSession.flushStatements(); + } + } + + public double getNextLevelPos(String projectId, int level, String parentId) { + List list = getPos(projectId, level, parentId, "pos desc"); if (!CollectionUtils.isEmpty(list)) { return list.get(0).getPos() + 65536; } else {