Skip to content

Commit

Permalink
Merge pull request #459 from YeungHoiChiu/stepSort-optimate-backend
Browse files Browse the repository at this point in the history
feat: 测试步骤分组自由拖拽
  • Loading branch information
ZhouYixun authored Aug 8, 2024
2 parents 20cc6f6 + c6844f1 commit 9568960
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "被移动步骤的主键id", required = false, example = "1")
private Integer stepsId;

public Integer getStepsId() {
return stepsId;
}

public void setStepsId(Integer stepsId) {
this.stepsId = stepsId;
}

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;
Expand Down Expand Up @@ -60,6 +90,9 @@ public String toString() {
", direction='" + direction + '\'' +
", startId=" + startId +
", endId=" + endId +
", newParentId=" + newParentId +
", newIndex=" + newIndex +
", stepsId=" + stepsId +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -252,15 +251,20 @@ public StepsDTO findById(int id) {
@Override
@Transactional(rollbackFor = Exception.class)
public void sortSteps(StepSort stepSort) {

List<Steps> stepsList = lambdaQuery().eq(Steps::getCaseId, stepSort.getCaseId())
// <=
.le(Steps::getSort, stepSort.getStartId())
// >=
.ge(Steps::getSort, stepSort.getEndId())
.orderByAsc(Steps::getSort)
.list();

List<Steps> 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();
Expand All @@ -277,6 +281,48 @@ public void sortSteps(StepSort stepSort) {
saveOrUpdateBatch(stepsList);
}

/**
* 拖拽步骤顺序,步骤所在分组发生变化时,仅对新分组以及移动步骤的sort进行重新排序
* @param stepSort
* @return
*/
private List<Steps> exchangeAddedStepSort(StepSort stepSort) {
// 获取新分组的case步骤
List<Steps> stepsList = lambdaQuery().eq(Steps::getCaseId, stepSort.getCaseId()).eq(Steps::getParentId, stepSort.getNewParentId()).list();
// 被移动的步骤实例
Steps movedStep = lambdaQuery().eq(Steps::getId, stepSort.getStepsId()).eq(Steps::getCaseId, stepSort.getCaseId()).one();
if(movedStep == null){
throw new SonicException("case中未能获取到该id的数据: %s", stepSort.getStepsId());
}
movedStep.setParentId(stepSort.getNewParentId()); // 更新父步骤id
stepsList.add(movedStep); // 添加到组内列表
if (stepsList.size() == 1){
// 原本没有子步骤,直接更改父id就好了,没必要重新排序
stepSort.setEndId(movedStep.getSort()) ;
stepSort.setStartId(movedStep.getSort());
stepSort.setDirection("down");
return stepsList;
}else {
// 将所有子步骤包含新加入的步骤重新排序,这样就相当于在同一个分组内拖拽排序
List<Steps> 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(movedStep.getSort());
}else {
stepSort.setDirection("up");
stepSort.setStartId(movedStep.getSort());
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<StepsDTO> findByProjectIdAndPlatform(int projectId, int platform, Page<Steps> pageable) {

Expand Down

0 comments on commit 9568960

Please sign in to comment.