BUGFIX:排序优化
This commit is contained in:
@@ -6,6 +6,7 @@ import com.ai.da.mapper.primary.entity.CollectionSort;
|
|||||||
import com.ai.da.model.dto.CollectionSortDTO;
|
import com.ai.da.model.dto.CollectionSortDTO;
|
||||||
import com.ai.da.model.enums.CollectionType;
|
import com.ai.da.model.enums.CollectionType;
|
||||||
import com.ai.da.service.CollectionSortService;
|
import com.ai.da.service.CollectionSortService;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -163,7 +164,8 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
|
|||||||
deleteByParentId(userLikeSortId);
|
deleteByParentId(userLikeSortId);
|
||||||
|
|
||||||
// 3. 批量更新排序号(使用自定义Mapper方法)
|
// 3. 批量更新排序号(使用自定义Mapper方法)
|
||||||
baseMapper.decrementSortAfterDelete(projectId, parentId, deletedSort);
|
// baseMapper.decrementSortAfterDelete(projectId, parentId, deletedSort);
|
||||||
|
resort(projectId, parentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteByParentId(Long parentId) {
|
private void deleteByParentId(Long parentId) {
|
||||||
@@ -225,7 +227,8 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
|
|||||||
}
|
}
|
||||||
child.setSort(collectionSort.getSort());
|
child.setSort(collectionSort.getSort());
|
||||||
// 原来排序的大于等于userLikeSortId的排序的,都要+1
|
// 原来排序的大于等于userLikeSortId的排序的,都要+1
|
||||||
baseMapper.increaseGenerateSortAbove(parentId, /*relationType,*/ collectionSort.getSort() - 1);
|
// baseMapper.increaseGenerateSortAbove(parentId, /*relationType,*/ collectionSort.getSort() - 1);
|
||||||
|
resort(collectionSort.getProjectId(), parentId);
|
||||||
// 当前的生成结果则填入userLikeSortId的排序位置
|
// 当前的生成结果则填入userLikeSortId的排序位置
|
||||||
child.setUpdateTime(LocalDateTime.now());
|
child.setUpdateTime(LocalDateTime.now());
|
||||||
baseMapper.updateById(child);
|
baseMapper.updateById(child);
|
||||||
@@ -262,4 +265,36 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void resort(Long projectId, Long parentId) {
|
||||||
|
|
||||||
|
// 2. 查询同组数据,按原排序升序
|
||||||
|
List<CollectionSort> list = baseMapper.selectList(
|
||||||
|
new LambdaQueryWrapper<CollectionSort>()
|
||||||
|
.eq(CollectionSort::getProjectId, projectId)
|
||||||
|
.eq(CollectionSort::getParentId, parentId)
|
||||||
|
.orderByAsc(CollectionSort::getSort)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 3. 重新编号 sort,从 1 开始
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
list.get(i).setSort(i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 批量更新
|
||||||
|
if (!list.isEmpty()) {
|
||||||
|
updateBatchById(list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新(如果你使用 MyBatis-Plus,可以继承 ServiceImpl 来用内置的 updateBatchById)
|
||||||
|
*/
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void updateBatchById(List<CollectionSort> list) {
|
||||||
|
for (CollectionSort item : list) {
|
||||||
|
baseMapper.updateById(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user