fix(interaction): 收藏计数同步维护并新增评论数接口

This commit is contained in:
Zhou Weidong committed 2026-10-21 11:33:59 +08:00
1 parent 88794b816e
commit 65e8fbfc70
11 files changed
+382 -47

No files matched your search

@@ -15,7 +15,7 @@ import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/** 收藏:切换、列表、批量删除。计数走异步聚合,不在事务里更新统计列。 */
/** 收藏:切换、列表、批量删除;收藏数在同一事务内同步维护。 */
@Service
@RequiredArgsConstructor
public class FavoriteService {
@@ -33,7 +33,10 @@ public class FavoriteService {
.last("limit 1"));
if (existing != null) {
// 必须物理删除:唯一索引 (user_id, drama_id) 会挡住"取消后再收藏"
favoriteMapper.physicalDelete(userId, dramaId);
int removed = favoriteMapper.physicalDelete(userId, dramaId);
if (removed > 0) {
dramaService.adjustFavoriteCount(dramaId, -1);
}
return false;
}
UserFavorite favorite = new UserFavorite();
@@ -41,6 +44,7 @@ public class FavoriteService {
favorite.setDramaId(dramaId);
favorite.setSort(0);
favoriteMapper.insert(favorite);
dramaService.adjustFavoriteCount(dramaId, 1);
return true;
}
@@ -79,7 +83,10 @@ public class FavoriteService {
@Transactional(rollbackFor = Exception.class)
public void remove(Long userId, Long dramaId) {
favoriteMapper.physicalDelete(userId, dramaId);
int removed = favoriteMapper.physicalDelete(userId, dramaId);
if (removed > 0) {
dramaService.adjustFavoriteCount(dramaId, -1);
}
}
@Transactional(rollbackFor = Exception.class)
@@ -87,12 +94,18 @@ public class FavoriteService {
if (dramaIds == null || dramaIds.isEmpty()) {
return;
}
favoriteMapper.delete(Wrappers.<UserFavorite>lambdaQuery()
.eq(UserFavorite::getUserId, userId)
.in(UserFavorite::getDramaId, dramaIds));
for (Long dramaId : dramaIds) {
if (dramaId == null) {
continue;
}
int removed = favoriteMapper.physicalDelete(userId, dramaId);
if (removed > 0) {
dramaService.adjustFavoriteCount(dramaId, -1);
}
}
}
/** 供收藏数聚合任务调用,标记最近变更时间。 */
/** 查询用户对某部剧最近一次收藏时间。 */
public LocalDateTime lastChangedAt(Long userId, Long dramaId) {
UserFavorite favorite = favoriteMapper.selectOne(Wrappers.<UserFavorite>lambdaQuery()
.eq(UserFavorite::getUserId, userId)