fix: 收藏 toggle 支持重新收藏(修复逻辑删除与 unique key 冲突)

This commit is contained in:
2026-07-19 23:56:41 +08:00
parent 3666c3d530
commit c0245d37fb
2 changed files with 34 additions and 8 deletions
@@ -56,15 +56,28 @@ public class EventFavoriteServiceImpl extends ServiceImpl<EventFavoriteMapper, E
throw new BusinessException("事件不存在或无权限访问");
}
// 查询时忽略逻辑删除标记,避免 unique key 冲突导致无法重新收藏
LambdaQueryWrapper<EventFavorite> w = new LambdaQueryWrapper<>();
w.eq(EventFavorite::getUserId, userId)
.eq(EventFavorite::getEventId, eventId)
.eq(EventFavorite::getIsDeleted, 0);
.eq(EventFavorite::getEventId, eventId);
EventFavorite existing = this.getOne(w);
if (existing != null) {
this.removeById(existing.getId());
r.setIsFavorited(false);
if (existing.getIsDeleted() != null && existing.getIsDeleted() == 0) {
// 已收藏 -> 物理删除(取消收藏)
this.removeById(existing.getId());
r.setIsFavorited(false);
} else {
// 历史逻辑删除记录 -> 恢复为已收藏
this.lambdaUpdate()
.set(EventFavorite::getIsDeleted, 0)
.eq(EventFavorite::getId, existing.getId())
.update();
r.setIsFavorited(true);
if (existing.getCreateTime() != null) {
r.setFavoriteTime(existing.getCreateTime().format(FMT));
}
}
} else {
EventFavorite fav = new EventFavorite();
fav.setUserId(userId);
@@ -56,15 +56,28 @@ public class ScriptFavoriteServiceImpl extends ServiceImpl<ScriptFavoriteMapper,
throw new BusinessException("剧本不存在或无权限访问");
}
// 查询时忽略逻辑删除标记,避免 unique key 冲突导致无法重新收藏
LambdaQueryWrapper<ScriptFavorite> w = new LambdaQueryWrapper<>();
w.eq(ScriptFavorite::getUserId, userId)
.eq(ScriptFavorite::getScriptId, scriptId)
.eq(ScriptFavorite::getIsDeleted, 0);
.eq(ScriptFavorite::getScriptId, scriptId);
ScriptFavorite existing = this.getOne(w);
if (existing != null) {
this.removeById(existing.getId());
r.setIsFavorited(false);
if (existing.getIsDeleted() != null && existing.getIsDeleted() == 0) {
// 已收藏 -> 物理删除(取消收藏)
this.removeById(existing.getId());
r.setIsFavorited(false);
} else {
// 历史逻辑删除记录 -> 恢复为已收藏
this.lambdaUpdate()
.set(ScriptFavorite::getIsDeleted, 0)
.eq(ScriptFavorite::getId, existing.getId())
.update();
r.setIsFavorited(true);
if (existing.getCreateTime() != null) {
r.setFavoriteTime(existing.getCreateTime().format(FMT));
}
}
} else {
ScriptFavorite fav = new ScriptFavorite();
fav.setUserId(userId);