BUGFIX:1. 排序优化,添加唯一约束和重试

2.like前先查询是否有被like
This commit is contained in:
2025-10-13 17:35:27 +08:00
parent 6ac9680c64
commit 19c08f4237
4 changed files with 112 additions and 40 deletions

View File

@@ -22,4 +22,22 @@ public interface CollectionSortMapper extends CommonMapper<CollectionSort> {
@Select("SELECT * FROM collection_sort WHERE parent_id IN (SELECT id FROM collection_sort WHERE relation_id = #{relationId})") @Select("SELECT * FROM collection_sort WHERE parent_id IN (SELECT id FROM collection_sort WHERE relation_id = #{relationId})")
List<CollectionSort> queryCollectionSortsIsNotDesignByUserLikeRelationId(@Param("relationId") Long relationId); List<CollectionSort> queryCollectionSortsIsNotDesignByUserLikeRelationId(@Param("relationId") Long relationId);
@Update({
"<script>",
"UPDATE collection_sort",
"SET sort = sort - 1",
"WHERE project_id = #{projectId}",
"AND sort > #{deletedSort}",
"<if test='parentId != null'>",
"AND parent_id = #{parentId}",
"</if>",
"<if test='parentId == null'>",
"AND parent_id IS NULL",
"</if>",
"</script>"
})
void decrementSortAfterDelete(@Param("projectId") Long projectId,
@Param("parentId") Long parentId,
@Param("deletedSort") int deletedSort);
} }

View File

@@ -13,7 +13,7 @@ public interface CollectionSortService {
void deleteCollectionSort(Long relationId, String relationType, Long projectId, Long parentId); void deleteCollectionSort(Long relationId, String relationType, Long projectId, Long parentId);
CollectionSort getUserLikeSortByUserLikeId(Long relationId, String relationType, Long projectId); CollectionSort getUserLikeSortByUserLikeId(Long relationId, String relationType, Long projectId, Long parentId);
Boolean sort(CollectionSortDTO userLikeSortDTO); Boolean sort(CollectionSortDTO userLikeSortDTO);

View File

@@ -26,26 +26,39 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
@Override @Override
public CollectionSort addCollectionSort(Long relationId, String relationType, Long projectId, Long collectionSortParentId) { public CollectionSort addCollectionSort(Long relationId, String relationType, Long projectId, Long collectionSortParentId) {
CollectionSort collectionSort = queryCollectionSortByRelation(relationId, relationType, projectId); CollectionSort collectionSort = queryCollectionSortByRelation(relationId, relationType, projectId);
if (Objects.nonNull(collectionSort)){ if (Objects.nonNull(collectionSort)) {
return collectionSort; return collectionSort;
} }
int sort = getNextSort(projectId, collectionSortParentId);
CollectionSort userLikeSort = new CollectionSort(); CollectionSort userLikeSort = new CollectionSort();
// userLikeSort.setUserLikeGroupId(userGroupId); // userLikeSort.setUserLikeGroupId(userGroupId);
// userLikeSort.setUserLikeId(relationId); // userLikeSort.setUserLikeId(relationId);
userLikeSort.setProjectId(projectId); userLikeSort.setProjectId(projectId);
userLikeSort.setRelationId(relationId); userLikeSort.setRelationId(relationId);
userLikeSort.setRelationType(relationType); userLikeSort.setRelationType(relationType);
userLikeSort.setSort(sort);
if (null != collectionSortParentId) { if (null != collectionSortParentId) {
userLikeSort.setParentId(collectionSortParentId); userLikeSort.setParentId(collectionSortParentId);
} }
userLikeSort.setCreateTime(LocalDateTime.now()); userLikeSort.setCreateTime(LocalDateTime.now());
int retryCount = 3;
while (retryCount-- > 0) {
try {
int sort = getNextSort(projectId, collectionSortParentId);
userLikeSort.setSort(sort);
baseMapper.insert(userLikeSort); baseMapper.insert(userLikeSort);
return userLikeSort; return userLikeSort;
} catch (DuplicateKeyException e) {
// 如果发生唯一约束冲突,重试
if (retryCount == 0) {
throw new BusinessException("获取排序号失败,请重试");
}
}
}
throw new BusinessException("获取排序号失败");
} }
public CollectionSort queryCollectionSortByRelation(Long relationId, String relationType, Long projectId){ public CollectionSort queryCollectionSortByRelation(Long relationId, String relationType, Long projectId) {
QueryWrapper<CollectionSort> qw = new QueryWrapper<>(); QueryWrapper<CollectionSort> qw = new QueryWrapper<>();
qw.lambda().eq(CollectionSort::getProjectId, projectId). qw.lambda().eq(CollectionSort::getProjectId, projectId).
eq(CollectionSort::getRelationId, relationId). eq(CollectionSort::getRelationId, relationId).
@@ -75,7 +88,7 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
int retryCount = 3; int retryCount = 3;
while (retryCount-- > 0) { while (retryCount-- > 0) {
try { try {
Integer maxSort = getMaxSortFromDB(projectId, parentId); int maxSort = Math.toIntExact(getMaxSortFromDB(projectId, parentId));
return maxSort + 1; return maxSort + 1;
} catch (DuplicateKeyException e) { } catch (DuplicateKeyException e) {
// 如果发生唯一约束冲突,重试 // 如果发生唯一约束冲突,重试
@@ -87,7 +100,7 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
throw new BusinessException("获取排序号失败"); throw new BusinessException("获取排序号失败");
} }
private Integer getMaxSortFromDB(Long projectId, Long parentId) { private Long getMaxSortFromDB(Long projectId, Long parentId) {
QueryWrapper<CollectionSort> qw = new QueryWrapper<>(); QueryWrapper<CollectionSort> qw = new QueryWrapper<>();
qw.select("COALESCE(MAX(sort), 0) as maxSort") qw.select("COALESCE(MAX(sort), 0) as maxSort")
.eq("project_id", projectId); .eq("project_id", projectId);
@@ -99,16 +112,16 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
} }
Map<String, Object> result = baseMapper.selectMaps(qw).get(0); Map<String, Object> result = baseMapper.selectMaps(qw).get(0);
return (Integer) result.get("maxSort"); return (Long) result.get("maxSort");
} }
@Override @Override
public void deleteCollectionSort(Long relationId, String relationType, Long projectId, Long parentId) { /*public void deleteCollectionSort(Long relationId, String relationType, Long projectId, Long parentId) {
QueryWrapper<CollectionSort> qw = new QueryWrapper<>(); QueryWrapper<CollectionSort> qw = new QueryWrapper<>();
qw.lambda().eq(CollectionSort::getProjectId, projectId); qw.lambda().eq(CollectionSort::getProjectId, projectId);
if (null != parentId) { if (null != parentId) {
qw.lambda().eq(CollectionSort::getParentId, parentId); qw.lambda().eq(CollectionSort::getParentId, parentId);
}else { } else {
qw.lambda().isNull(CollectionSort::getParentId); qw.lambda().isNull(CollectionSort::getParentId);
} }
qw.lambda().orderByDesc(CollectionSort::getSort); qw.lambda().orderByDesc(CollectionSort::getSort);
@@ -121,12 +134,30 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
baseMapper.deleteById(likeSort); baseMapper.deleteById(likeSort);
deleteByParentId(likeSort.getId()); deleteByParentId(likeSort.getId());
break; break;
}else { } else {
likeSort.setSort(likeSort.getSort() - 1); likeSort.setSort(likeSort.getSort() - 1);
baseMapper.updateById(likeSort); baseMapper.updateById(likeSort);
} }
} }
} }
}*/
@Transactional
public void deleteCollectionSort(Long relationId, String relationType, Long projectId, Long parentId) {
// 1. 找到要删除的记录
CollectionSort userLikeSort = getUserLikeSortByUserLikeId(relationId, relationType, projectId, null);
if (Objects.isNull(userLikeSort)) {
return;
}
Long userLikeSortId = userLikeSort.getId();
int deletedSort = userLikeSort.getSort();
// 2. 先删除记录
baseMapper.deleteById(userLikeSortId);
deleteByParentId(userLikeSortId);
// 3. 批量更新排序号使用自定义Mapper方法
baseMapper.decrementSortAfterDelete(projectId, parentId, deletedSort);
} }
private void deleteByParentId(Long parentId) { private void deleteByParentId(Long parentId) {
@@ -136,11 +167,14 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
} }
@Override @Override
public CollectionSort getUserLikeSortByUserLikeId(Long relationId, String relationType, Long projectId) { public CollectionSort getUserLikeSortByUserLikeId(Long relationId, String relationType, Long projectId, Long parentId) {
QueryWrapper<CollectionSort> qw = new QueryWrapper<>(); QueryWrapper<CollectionSort> qw = new QueryWrapper<>();
qw.lambda().eq(CollectionSort::getRelationId, relationId); qw.lambda().eq(CollectionSort::getRelationId, relationId);
qw.lambda().eq(CollectionSort::getRelationType, relationType); qw.lambda().eq(CollectionSort::getRelationType, relationType);
qw.lambda().eq(CollectionSort::getProjectId, projectId); qw.lambda().eq(CollectionSort::getProjectId, projectId);
if (Objects.nonNull(parentId)){
qw.lambda().eq(CollectionSort::getParentId, parentId);
}
CollectionSort userLikeSort = baseMapper.selectOne(qw); CollectionSort userLikeSort = baseMapper.selectOne(qw);
return userLikeSort; return userLikeSort;
} }
@@ -155,6 +189,7 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
/** /**
* 只有当使用子集中的元素进行生成时,才需要重新排序 * 只有当使用子集中的元素进行生成时,才需要重新排序
*
* @param childId 生成元素的id * @param childId 生成元素的id
* @param parentId 父级id * @param parentId 父级id
* @param relationType 生成功能 * @param relationType 生成功能
@@ -174,12 +209,12 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
.eq("relation_type", relationType).orderByDesc("id")); .eq("relation_type", relationType).orderByDesc("id"));
if (childList.isEmpty()) { if (childList.isEmpty()) {
return null; return null;
}else if (childList.size() > 1){ } else if (childList.size() > 1) {
log.error("CollectionSort表中relation_id为{}relation_type为{}的记录有 {} 条,", childId, relationType, childList.size()); log.error("CollectionSort表中relation_id为{}relation_type为{}的记录有 {} 条,", childId, relationType, childList.size());
} }
CollectionSort child = childList.get(0); CollectionSort child = childList.get(0);
CollectionSort collectionSort = baseMapper.selectById(userLikeSortId); CollectionSort collectionSort = baseMapper.selectById(userLikeSortId);
if (Objects.isNull(collectionSort)){ if (Objects.isNull(collectionSort)) {
return null; return null;
} }
child.setSort(collectionSort.getSort()); child.setSort(collectionSort.getSort());
@@ -213,11 +248,11 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
private String transElementType(String elementType) { private String transElementType(String elementType) {
if (elementType.equals("DesignOutfit")) { if (elementType.equals("DesignOutfit")) {
return CollectionType.DESIGN.getValue(); return CollectionType.DESIGN.getValue();
}else if (elementType.equals("ToProductImage")) { } else if (elementType.equals("ToProductImage")) {
return CollectionType.TO_PRODUCT_IMAGE.getValue(); return CollectionType.TO_PRODUCT_IMAGE.getValue();
}else if (elementType.equals("PoseTransfer")) { } else if (elementType.equals("PoseTransfer")) {
return CollectionType.POSE_TRANSFORM.getValue(); return CollectionType.POSE_TRANSFORM.getValue();
}else { } else {
return ""; return "";
} }
} }

View File

@@ -771,29 +771,48 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
} }
@Override @Override
public CollectionSort productImageLike(ProductImageLikeDTO productImageLikeDTO) { public CollectionSort productImageLike(ProductImageLikeDTO dto) {
List<Long> toProductImageResultId = productImageLikeDTO.getToProductImageResultId(); List<Long> resultIds = dto.getToProductImageResultId();
QueryWrapper<ToProductImageResult> qw = new QueryWrapper<>(); ToProductImageResult result = toProductImageResultMapper.selectById(resultIds.get(0));
qw.lambda().in(ToProductImageResult::getId, toProductImageResultId);
ToProductImageResult toProductImageResult = new ToProductImageResult();
toProductImageResult.setIsLike(1);
toProductImageResultMapper.update(toProductImageResult, qw);
ToProductImageResult toProductImageResult1 = toProductImageResultMapper.selectById(toProductImageResultId.get(0));
CollectionSort collectionSort = null; CollectionSort collectionSort = null;
if (toProductImageResult1.getResultType().equals("Relight")) { boolean updatedFlag = false;
if (null != productImageLikeDTO.getCollectionSortParentId()) {
collectionSort = collectionSortService.addCollectionSort(toProductImageResult1.getId(), CollectionType.RELIGHT.getValue(), productImageLikeDTO.getProjectId(), productImageLikeDTO.getCollectionSortParentId()); Long parentId = dto.getCollectionSortParentId();
if (parentId == null) {
return null;
} }
} else {
if (null != productImageLikeDTO.getCollectionSortParentId()) { // 统一处理两种类型
collectionSort = collectionSortService.addCollectionSort(toProductImageResult1.getId(), CollectionType.TO_PRODUCT_IMAGE.getValue(), productImageLikeDTO.getProjectId(), productImageLikeDTO.getCollectionSortParentId()); CollectionType collectionType = "Relight".equals(result.getResultType())
? CollectionType.RELIGHT
: CollectionType.TO_PRODUCT_IMAGE;
collectionSort = collectionSortService.getUserLikeSortByUserLikeId(
result.getId(), collectionType.getValue(), dto.getProjectId(), parentId);
if (Objects.isNull(collectionSort)) {
collectionSort = collectionSortService.addCollectionSort(
result.getId(), collectionType.getValue(), dto.getProjectId(), parentId);
updatedFlag = true;
} }
if (updatedFlag) {
updateLikeStatus(resultIds);
projectService.modifyProjectUpdateTime(dto.getProjectId());
} }
// 更新项目更新时间
projectService.modifyProjectUpdateTime(productImageLikeDTO.getProjectId());
return collectionSort; return collectionSort;
} }
private void updateLikeStatus(List<Long> resultIds) {
QueryWrapper<ToProductImageResult> qw = new QueryWrapper<>();
qw.lambda().in(ToProductImageResult::getId, resultIds);
ToProductImageResult updateEntity = new ToProductImageResult();
updateEntity.setIsLike(1);
toProductImageResultMapper.update(updateEntity, qw);
}
@Resource @Resource
private RedisUtil redisUtil; private RedisUtil redisUtil;
@Value("${redis.key.toProductImageResultKey}") @Value("${redis.key.toProductImageResultKey}")