BUGFIX:1. 排序优化,添加唯一约束和重试
2.like前先查询是否有被like
This commit is contained in:
@@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
|||||||
@@ -29,20 +29,33 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
|
|||||||
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) {
|
||||||
@@ -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,11 +112,11 @@ 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) {
|
||||||
@@ -127,6 +140,24 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}*/
|
||||||
|
@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 生成功能
|
||||||
|
|||||||
@@ -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}")
|
||||||
|
|||||||
Reference in New Issue
Block a user