TASK: 删除toProduct Relight Pose后重新排序

This commit is contained in:
2025-07-17 10:51:43 +08:00
parent 432149dce2
commit d264c29557
3 changed files with 92 additions and 43 deletions

View File

@@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
@@ -148,7 +148,8 @@ public class GenerateController {
@ApiOperation(value = "删除pose transfer的结果")
@GetMapping("/deleteResult")
public Response<String> deleteToProductRelightResult(@RequestParam("projectId") Long projectId, @RequestParam("id") Long id){
public Response<String> deleteToProductRelightResult(@RequestParam("projectId") @NotNull Long projectId,
@RequestParam("id") @NotNull Long id){
try{
generateService.deleteGeneratedPose(projectId, id);
return Response.success();

View File

@@ -1831,30 +1831,56 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
@Transactional(rollbackFor = Exception.class)
public void deleteGeneratedPose(Long projectId, Long id) {
// 1. 权限校验
Long accountId = UserContext.getUserHolder().getId();
// 1、删除generate中的结果
UpdateWrapper<PoseTransformation> wrapper = new UpdateWrapper<>();
wrapper.eq("id", id)
.set("is_deleted", 1) // 手动设置
.set("update_time", LocalDateTime.now());
int update = poseTransformationMapper.update(null, wrapper);
log.info("删除PoseTransfer 结果, id为{}, 影响行数={}", id, update);
if (update != 0) {
// 2、如果有排序删除排序,排序后是否需要重置排序计数器以及后续排序数字
// 先校验type的值是不是ToProductImage、Relight、PoseTransfer
if (id == null || projectId == null) {
throw new IllegalArgumentException("参数不能为null");
}
Project project = projectService.getById(projectId);
if (!project.getAccountId().equals(accountId)) {
throw new IllegalArgumentException("项目id不属于当前账号,请仅对自己的账号数据进行处理");
throw new IllegalArgumentException("项目不属于当前账号");
}
int deletedRows = collectionSortMapper.delete(new QueryWrapper<CollectionSort>()
.eq("project_id", projectId)
.eq("relation_id", id).eq("relation_type", "PoseTransfer"));
log.info("deleteGeneratedPose 删除记录id={}, type={}, 影响行数={}", id, "PoseTransfer", deletedRows);
// 2. 软删除主表数据
int update = poseTransformationMapper.update(null,
new UpdateWrapper<PoseTransformation>()
.eq("id", id)
.set("is_deleted", 1)
.set("update_time", LocalDateTime.now()));
log.info("删除PoseTransfer 结果, id为{}, 影响行数={}", id, update);
if (update == 0) return;
// 3. 查询可能删除的多个排序项(存在脏数据可能,所有会查出多个)
List<CollectionSort> deletedItems = collectionSortMapper.selectList(
new QueryWrapper<CollectionSort>()
.eq("project_id", projectId)
.eq("relation_id", id)
.eq("relation_type", "PoseTransfer")
.orderByAsc("sort")
);
if (deletedItems.isEmpty()) return;
// 4. 删除这些排序记录
int deletedCount = collectionSortMapper.delete(
new QueryWrapper<CollectionSort>()
.eq("project_id", projectId)
.eq("relation_id", id)
.eq("relation_type", "PoseTransfer")
);
// 5. 重新调整剩余记录的排序(两种方案可选)
// 方案一:精确调整(每条被删除记录单独处理)
for (CollectionSort deletedItem : deletedItems) {
collectionSortMapper.update(
null,
new UpdateWrapper<CollectionSort>()
.eq("project_id", projectId)
.gt("sort", deletedItem.getSort())
.setSql("sort = sort - 1")
);
}
log.info("删除PoseTransfer排序记录id={}, 删除{}条,已重新排序后续记录", id, deletedCount);
}

View File

@@ -592,7 +592,8 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
*/
@Transactional
public Integer rearrangeChildSort(Long childId, String relationType, Long parentId, Long userLikeSortId) {
if (Objects.isNull(userLikeSortId)) {
// 对父级元素进行生成不需要重新排序
if (Objects.isNull(userLikeSortId) || parentId.equals(userLikeSortId)) {
return null;
}
@@ -1334,33 +1335,54 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
@Transactional(rollbackFor = Exception.class)
public void deleteToProductRelightResult(Long id, Long projectId, String type) {
// 1. 权限校验和删除主数据
Long accountId = UserContext.getUserHolder().getId();
// 1、删除toProductResult中的结果
Project project = projectService.getById(projectId);
if (!project.getAccountId().equals(accountId)) {
throw new IllegalArgumentException("项目id不属于当前账号,请仅对自己的账号数据进行处理");
throw new IllegalArgumentException("项目不属于当前账号");
}
// 2. 删除主表数据
UpdateWrapper<ToProductImageResult> wrapper = new UpdateWrapper<>();
wrapper.eq("id", id)
.eq("project_id", projectId)
.set("is_deleted", 1); // 手动设置
.set("is_deleted", 1);
int update = toProductImageResultMapper.update(null, wrapper);
log.info("删除{} 结果, id{}, 影响行数={}", type, id, update);
log.info("删除 {} 结果, id={}, 影响行数={}", type, id, update);
if (update != 0) {
// 2、如果有排序删除排序,排序后是否需要重置排序计数器以及后续排序数字
// 先校验type的值是不是ToProductImage、Relight、PoseTransfer
if (id == null || type == null || projectId == null) {
throw new IllegalArgumentException("参数不能为null");
if (update == 0) {
return; // 未删除任何数据,直接返回
}
int deletedRows = collectionSortMapper.delete(new QueryWrapper<CollectionSort>()
// 3. 查询所有被删除的排序项按sort升序
List<CollectionSort> deletedItems = collectionSortMapper.selectList(
new QueryWrapper<CollectionSort>()
.eq("project_id", projectId)
.eq("relation_id", id).eq("relation_type", type));
.eq("relation_id", id)
.eq("relation_type", type)
.orderByAsc("sort")
);
log.info("deleteToProductRelightResult 删除记录id={}, type={}, 影响行数={}", id, type, deletedRows);
if (deletedItems.isEmpty()) return;
// 4. 删除这些记录
int deletedCount = collectionSortMapper.delete(
new QueryWrapper<CollectionSort>()
.eq("project_id", projectId)
.eq("relation_id", id)
.eq("relation_type", type)
);
log.info("删除排序记录id={}, type={}, 影响行数={}", id, type, deletedCount);
// 5. 对每个被删除的sort值单独调整后续记录
for (CollectionSort deletedItem : deletedItems) {
collectionSortMapper.update(
null,
new UpdateWrapper<CollectionSort>()
.eq("project_id", projectId)
.gt("sort", deletedItem.getSort())
.setSql("sort = sort - 1") // 每条SQL只减1
);
}
}