From 7733410aa97707099a801121b3b90b8cb19e8a88 Mon Sep 17 00:00:00 2001 From: YeungHoiChiu <1005935991@qq.com> Date: Mon, 24 Jun 2024 14:51:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=B5=8B=E8=AF=95=E6=AD=A5?= =?UTF-8?q?=E9=AA=A4=E5=88=86=E7=BB=84=E8=87=AA=E7=94=B1=E6=8B=96=E6=8B=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/models/http/StepSort.java | 33 ++++++++++ .../services/impl/StepsServiceImpl.java | 65 +++++++++++++++---- 2 files changed, 86 insertions(+), 12 deletions(-) diff --git a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/models/http/StepSort.java b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/models/http/StepSort.java index c4e4c748..fbe1ca93 100644 --- a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/models/http/StepSort.java +++ b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/models/http/StepSort.java @@ -20,6 +20,36 @@ public class StepSort implements Serializable { @Positive @Schema(description = "移动后被影响的最后一个步骤sort序号", required = true, example = "9") private int endId; + @Schema(description = "移动步骤发生分组更改的新parentId", required = false, example = "1") + private Integer newParentId; + @Schema(description = "更换分组后在新分组中新的index", required = false, example = "1") + private Integer newIndex; + @Schema(description = "移动步骤的sort序号", required = false, example = "1") + private Integer thisSort; + + public Integer getThisSort() { + return thisSort; + } + + public void setThisSort(Integer thisSort) { + this.thisSort = thisSort; + } + + public Integer getNewIndex() { + return newIndex; + } + + public void setNewIndex(Integer newIndex) { + this.newIndex = newIndex; + } + + public Integer getNewParentId() { + return newParentId; + } + + public void setNewParentId(Integer newParentId) { + this.newParentId = newParentId; + } public int getCaseId() { return caseId; @@ -60,6 +90,9 @@ public String toString() { ", direction='" + direction + '\'' + ", startId=" + startId + ", endId=" + endId + + ", newParentId=" + newParentId + + ", newIndex=" + newIndex + + ", thisSort=" + thisSort + '}'; } } diff --git a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/services/impl/StepsServiceImpl.java b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/services/impl/StepsServiceImpl.java index 03d2f5d7..0c2b3009 100644 --- a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/services/impl/StepsServiceImpl.java +++ b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/services/impl/StepsServiceImpl.java @@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.cloud.sonic.common.exception.SonicException; import org.cloud.sonic.controller.mapper.*; import org.cloud.sonic.controller.models.base.CommentPage; import org.cloud.sonic.controller.models.base.TypeConverter; @@ -40,9 +41,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; /** @@ -252,15 +251,20 @@ public StepsDTO findById(int id) { @Override @Transactional(rollbackFor = Exception.class) public void sortSteps(StepSort stepSort) { - - List stepsList = lambdaQuery().eq(Steps::getCaseId, stepSort.getCaseId()) - // <= - .le(Steps::getSort, stepSort.getStartId()) - // >= - .ge(Steps::getSort, stepSort.getEndId()) - .orderByAsc(Steps::getSort) - .list(); - + List stepsList; + if (stepSort.getNewParentId() != null && stepSort.getNewIndex() != null) { + // 分组拖拽 + stepsList = exchangeAddedStepSort(stepSort); + } else { + // 同组内拖拽排序 + stepsList = lambdaQuery().eq(Steps::getCaseId, stepSort.getCaseId()) + // <= + .le(Steps::getSort, stepSort.getStartId()) + // >= + .ge(Steps::getSort, stepSort.getEndId()) + .orderByAsc(Steps::getSort) + .list(); + } if (stepSort.getDirection().equals("down")) { for (int i = 0; i < stepsList.size() - 1; i++) { int temp = stepsList.get(stepsList.size() - 1).getSort(); @@ -277,6 +281,43 @@ public void sortSteps(StepSort stepSort) { saveOrUpdateBatch(stepsList); } + private List exchangeAddedStepSort(StepSort stepSort) { + // 获取case全部步骤 + List stepsList = lambdaQuery().eq(Steps::getCaseId, stepSort.getCaseId()).orderByAsc(Steps::getSort).list(); + // 移动新分组内的原本存在的子步骤 + List groupStepList = stepsList.stream().filter(steps -> Objects.equals(steps.getParentId(), stepSort.getNewParentId())).collect(Collectors.toList()); + // 被移动的步骤实例 + Steps movedStep = stepsList.stream().filter(steps -> Objects.equals(steps.getSort(), stepSort.getThisSort())).findFirst().orElse(null); + if(movedStep == null){ + throw new SonicException("case中未能获取到该Sort: %s", stepSort.getThisSort()); + } + movedStep.setParentId(stepSort.getNewParentId()); // 更新父步骤id + groupStepList.add(movedStep); // 添加到组内列表 + if (groupStepList.size() == 1){ + // 原本没有子步骤,直接更改父id就好了,不用重新排序 + stepSort.setEndId(stepSort.getThisSort()) ; + stepSort.setStartId(stepSort.getThisSort()); + stepSort.setDirection("down"); + }else { + groupStepList = groupStepList.stream().sorted(Comparator.comparingInt(Steps::getSort)).collect(Collectors.toList()); // 将所有子步骤包含新加入的步骤重新排序 + if (groupStepList.get(stepSort.getNewIndex()).getSort() >= stepSort.getThisSort()){ + stepSort.setDirection("down"); + stepSort.setStartId(groupStepList.get(stepSort.getNewIndex()).getSort()); + stepSort.setEndId(stepSort.getThisSort()); + }else { + stepSort.setDirection("up"); + stepSort.setStartId(stepSort.getThisSort()); + stepSort.setEndId(groupStepList.get(stepSort.getNewIndex()).getSort()); + } + // 取出需要重新排序的步骤 + groupStepList = groupStepList.stream().filter( + steps -> steps.getSort() >= stepSort.getEndId() + && steps.getSort() <= stepSort.getStartId()) + .collect(Collectors.toList()); + } + return groupStepList; + } + @Override public CommentPage findByProjectIdAndPlatform(int projectId, int platform, Page pageable) { From c6844f116a525b37b17787e32475f103e394885b Mon Sep 17 00:00:00 2001 From: YeungHoiChiu <1005935991@qq.com> Date: Wed, 26 Jun 2024 10:35:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A0=B9=E6=8D=AEsort?= =?UTF-8?q?=E6=9F=A5=E8=A2=AB=E7=A7=BB=E5=8A=A8=E6=AD=A5=E9=AA=A4=E4=B8=BA?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E4=B8=BB=E9=94=AEid=E6=9D=A5=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/models/http/StepSort.java | 14 +++---- .../services/impl/StepsServiceImpl.java | 37 +++++++++++-------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/models/http/StepSort.java b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/models/http/StepSort.java index fbe1ca93..45f03324 100644 --- a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/models/http/StepSort.java +++ b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/models/http/StepSort.java @@ -24,15 +24,15 @@ public class StepSort implements Serializable { private Integer newParentId; @Schema(description = "更换分组后在新分组中新的index", required = false, example = "1") private Integer newIndex; - @Schema(description = "移动步骤的sort序号", required = false, example = "1") - private Integer thisSort; + @Schema(description = "被移动步骤的主键id", required = false, example = "1") + private Integer stepsId; - public Integer getThisSort() { - return thisSort; + public Integer getStepsId() { + return stepsId; } - public void setThisSort(Integer thisSort) { - this.thisSort = thisSort; + public void setStepsId(Integer stepsId) { + this.stepsId = stepsId; } public Integer getNewIndex() { @@ -92,7 +92,7 @@ public String toString() { ", endId=" + endId + ", newParentId=" + newParentId + ", newIndex=" + newIndex + - ", thisSort=" + thisSort + + ", stepsId=" + stepsId + '}'; } } diff --git a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/services/impl/StepsServiceImpl.java b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/services/impl/StepsServiceImpl.java index 0c2b3009..29f6a5ab 100644 --- a/sonic-server-controller/src/main/java/org/cloud/sonic/controller/services/impl/StepsServiceImpl.java +++ b/sonic-server-controller/src/main/java/org/cloud/sonic/controller/services/impl/StepsServiceImpl.java @@ -281,32 +281,37 @@ public void sortSteps(StepSort stepSort) { saveOrUpdateBatch(stepsList); } + /** + * 拖拽步骤顺序,步骤所在分组发生变化时,仅对新分组以及移动步骤的sort进行重新排序 + * @param stepSort + * @return + */ private List exchangeAddedStepSort(StepSort stepSort) { - // 获取case全部步骤 - List stepsList = lambdaQuery().eq(Steps::getCaseId, stepSort.getCaseId()).orderByAsc(Steps::getSort).list(); - // 移动新分组内的原本存在的子步骤 - List groupStepList = stepsList.stream().filter(steps -> Objects.equals(steps.getParentId(), stepSort.getNewParentId())).collect(Collectors.toList()); + // 获取新分组的case步骤 + List stepsList = lambdaQuery().eq(Steps::getCaseId, stepSort.getCaseId()).eq(Steps::getParentId, stepSort.getNewParentId()).list(); // 被移动的步骤实例 - Steps movedStep = stepsList.stream().filter(steps -> Objects.equals(steps.getSort(), stepSort.getThisSort())).findFirst().orElse(null); + Steps movedStep = lambdaQuery().eq(Steps::getId, stepSort.getStepsId()).eq(Steps::getCaseId, stepSort.getCaseId()).one(); if(movedStep == null){ - throw new SonicException("case中未能获取到该Sort: %s", stepSort.getThisSort()); + throw new SonicException("case中未能获取到该id的数据: %s", stepSort.getStepsId()); } movedStep.setParentId(stepSort.getNewParentId()); // 更新父步骤id - groupStepList.add(movedStep); // 添加到组内列表 - if (groupStepList.size() == 1){ - // 原本没有子步骤,直接更改父id就好了,不用重新排序 - stepSort.setEndId(stepSort.getThisSort()) ; - stepSort.setStartId(stepSort.getThisSort()); + stepsList.add(movedStep); // 添加到组内列表 + if (stepsList.size() == 1){ + // 原本没有子步骤,直接更改父id就好了,没必要重新排序 + stepSort.setEndId(movedStep.getSort()) ; + stepSort.setStartId(movedStep.getSort()); stepSort.setDirection("down"); + return stepsList; }else { - groupStepList = groupStepList.stream().sorted(Comparator.comparingInt(Steps::getSort)).collect(Collectors.toList()); // 将所有子步骤包含新加入的步骤重新排序 - if (groupStepList.get(stepSort.getNewIndex()).getSort() >= stepSort.getThisSort()){ + // 将所有子步骤包含新加入的步骤重新排序,这样就相当于在同一个分组内拖拽排序 + List groupStepList = stepsList.stream().sorted(Comparator.comparingInt(Steps::getSort)).collect(Collectors.toList()); + if (groupStepList.get(stepSort.getNewIndex()).getSort() >= movedStep.getSort()){ stepSort.setDirection("down"); stepSort.setStartId(groupStepList.get(stepSort.getNewIndex()).getSort()); - stepSort.setEndId(stepSort.getThisSort()); + stepSort.setEndId(movedStep.getSort()); }else { stepSort.setDirection("up"); - stepSort.setStartId(stepSort.getThisSort()); + stepSort.setStartId(movedStep.getSort()); stepSort.setEndId(groupStepList.get(stepSort.getNewIndex()).getSort()); } // 取出需要重新排序的步骤 @@ -314,8 +319,8 @@ private List exchangeAddedStepSort(StepSort stepSort) { steps -> steps.getSort() >= stepSort.getEndId() && steps.getSort() <= stepSort.getStartId()) .collect(Collectors.toList()); + return groupStepList; } - return groupStepList; } @Override