From 19c08f4237e3f96af536b36045027f1403c39cf6 Mon Sep 17 00:00:00 2001 From: xupei Date: Mon, 13 Oct 2025 17:35:27 +0800 Subject: [PATCH] =?UTF-8?q?BUGFIX:1.=20=E6=8E=92=E5=BA=8F=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=94=AF=E4=B8=80=E7=BA=A6?= =?UTF-8?q?=E6=9D=9F=E5=92=8C=E9=87=8D=E8=AF=95=202.like=E5=89=8D=E5=85=88?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=98=AF=E5=90=A6=E6=9C=89=E8=A2=ABlike?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/primary/CollectionSortMapper.java | 18 +++++ .../ai/da/service/CollectionSortService.java | 2 +- .../impl/CollectionSortServiceImpl.java | 77 ++++++++++++++----- .../impl/UserLikeGroupServiceImpl.java | 55 ++++++++----- 4 files changed, 112 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/ai/da/mapper/primary/CollectionSortMapper.java b/src/main/java/com/ai/da/mapper/primary/CollectionSortMapper.java index 1173a2ab..3d715baf 100644 --- a/src/main/java/com/ai/da/mapper/primary/CollectionSortMapper.java +++ b/src/main/java/com/ai/da/mapper/primary/CollectionSortMapper.java @@ -22,4 +22,22 @@ public interface CollectionSortMapper extends CommonMapper { @Select("SELECT * FROM collection_sort WHERE parent_id IN (SELECT id FROM collection_sort WHERE relation_id = #{relationId})") List queryCollectionSortsIsNotDesignByUserLikeRelationId(@Param("relationId") Long relationId); + @Update({ + "" + }) + void decrementSortAfterDelete(@Param("projectId") Long projectId, + @Param("parentId") Long parentId, + @Param("deletedSort") int deletedSort); + } diff --git a/src/main/java/com/ai/da/service/CollectionSortService.java b/src/main/java/com/ai/da/service/CollectionSortService.java index 3dd20d9f..d77ebe59 100644 --- a/src/main/java/com/ai/da/service/CollectionSortService.java +++ b/src/main/java/com/ai/da/service/CollectionSortService.java @@ -13,7 +13,7 @@ public interface CollectionSortService { 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); diff --git a/src/main/java/com/ai/da/service/impl/CollectionSortServiceImpl.java b/src/main/java/com/ai/da/service/impl/CollectionSortServiceImpl.java index d922754d..fe87c23f 100644 --- a/src/main/java/com/ai/da/service/impl/CollectionSortServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/CollectionSortServiceImpl.java @@ -26,26 +26,39 @@ public class CollectionSortServiceImpl extends ServiceImpl 0) { + try { + int sort = getNextSort(projectId, collectionSortParentId); + userLikeSort.setSort(sort); + baseMapper.insert(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 qw = new QueryWrapper<>(); qw.lambda().eq(CollectionSort::getProjectId, projectId). eq(CollectionSort::getRelationId, relationId). @@ -75,7 +88,7 @@ public class CollectionSortServiceImpl extends ServiceImpl 0) { try { - Integer maxSort = getMaxSortFromDB(projectId, parentId); + int maxSort = Math.toIntExact(getMaxSortFromDB(projectId, parentId)); return maxSort + 1; } catch (DuplicateKeyException e) { // 如果发生唯一约束冲突,重试 @@ -87,7 +100,7 @@ public class CollectionSortServiceImpl extends ServiceImpl qw = new QueryWrapper<>(); qw.select("COALESCE(MAX(sort), 0) as maxSort") .eq("project_id", projectId); @@ -99,16 +112,16 @@ public class CollectionSortServiceImpl extends ServiceImpl result = baseMapper.selectMaps(qw).get(0); - return (Integer) result.get("maxSort"); + return (Long) result.get("maxSort"); } @Override - public void deleteCollectionSort(Long relationId, String relationType, Long projectId, Long parentId) { + /*public void deleteCollectionSort(Long relationId, String relationType, Long projectId, Long parentId) { QueryWrapper qw = new QueryWrapper<>(); qw.lambda().eq(CollectionSort::getProjectId, projectId); if (null != parentId) { qw.lambda().eq(CollectionSort::getParentId, parentId); - }else { + } else { qw.lambda().isNull(CollectionSort::getParentId); } qw.lambda().orderByDesc(CollectionSort::getSort); @@ -121,12 +134,30 @@ public class CollectionSortServiceImpl extends ServiceImpl qw = new QueryWrapper<>(); qw.lambda().eq(CollectionSort::getRelationId, relationId); qw.lambda().eq(CollectionSort::getRelationType, relationType); qw.lambda().eq(CollectionSort::getProjectId, projectId); + if (Objects.nonNull(parentId)){ + qw.lambda().eq(CollectionSort::getParentId, parentId); + } CollectionSort userLikeSort = baseMapper.selectOne(qw); return userLikeSort; } @@ -155,9 +189,10 @@ public class CollectionSortServiceImpl extends ServiceImpl 1){ + } else if (childList.size() > 1) { log.error("CollectionSort表中,relation_id为{},relation_type为{}的记录有 {} 条,", childId, relationType, childList.size()); } CollectionSort child = childList.get(0); CollectionSort collectionSort = baseMapper.selectById(userLikeSortId); - if (Objects.isNull(collectionSort)){ + if (Objects.isNull(collectionSort)) { return null; } child.setSort(collectionSort.getSort()); @@ -213,11 +248,11 @@ public class CollectionSortServiceImpl extends ServiceImpl toProductImageResultId = productImageLikeDTO.getToProductImageResultId(); - QueryWrapper qw = new QueryWrapper<>(); - qw.lambda().in(ToProductImageResult::getId, toProductImageResultId); - ToProductImageResult toProductImageResult = new ToProductImageResult(); - toProductImageResult.setIsLike(1); - toProductImageResultMapper.update(toProductImageResult, qw); - ToProductImageResult toProductImageResult1 = toProductImageResultMapper.selectById(toProductImageResultId.get(0)); + public CollectionSort productImageLike(ProductImageLikeDTO dto) { + List resultIds = dto.getToProductImageResultId(); + ToProductImageResult result = toProductImageResultMapper.selectById(resultIds.get(0)); CollectionSort collectionSort = null; - if (toProductImageResult1.getResultType().equals("Relight")) { - if (null != productImageLikeDTO.getCollectionSortParentId()) { - collectionSort = collectionSortService.addCollectionSort(toProductImageResult1.getId(), CollectionType.RELIGHT.getValue(), productImageLikeDTO.getProjectId(), productImageLikeDTO.getCollectionSortParentId()); - } - } else { - if (null != productImageLikeDTO.getCollectionSortParentId()) { - collectionSort = collectionSortService.addCollectionSort(toProductImageResult1.getId(), CollectionType.TO_PRODUCT_IMAGE.getValue(), productImageLikeDTO.getProjectId(), productImageLikeDTO.getCollectionSortParentId()); - } + boolean updatedFlag = false; + + Long parentId = dto.getCollectionSortParentId(); + if (parentId == null) { + return null; } - // 更新项目更新时间 - projectService.modifyProjectUpdateTime(productImageLikeDTO.getProjectId()); + + // 统一处理两种类型 + 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()); + } + return collectionSort; } + private void updateLikeStatus(List resultIds) { + QueryWrapper qw = new QueryWrapper<>(); + qw.lambda().in(ToProductImageResult::getId, resultIds); + + ToProductImageResult updateEntity = new ToProductImageResult(); + updateEntity.setIsLike(1); + toProductImageResultMapper.update(updateEntity, qw); + } + @Resource private RedisUtil redisUtil; @Value("${redis.key.toProductImageResultKey}")