fix: 收藏 toggle 支持重新收藏(修复逻辑删除与 unique key 冲突)
This commit is contained in:
@@ -56,15 +56,28 @@ public class EventFavoriteServiceImpl extends ServiceImpl<EventFavoriteMapper, E
|
|||||||
throw new BusinessException("事件不存在或无权限访问");
|
throw new BusinessException("事件不存在或无权限访问");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询时忽略逻辑删除标记,避免 unique key 冲突导致无法重新收藏
|
||||||
LambdaQueryWrapper<EventFavorite> w = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<EventFavorite> w = new LambdaQueryWrapper<>();
|
||||||
w.eq(EventFavorite::getUserId, userId)
|
w.eq(EventFavorite::getUserId, userId)
|
||||||
.eq(EventFavorite::getEventId, eventId)
|
.eq(EventFavorite::getEventId, eventId);
|
||||||
.eq(EventFavorite::getIsDeleted, 0);
|
|
||||||
|
|
||||||
EventFavorite existing = this.getOne(w);
|
EventFavorite existing = this.getOne(w);
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
|
if (existing.getIsDeleted() != null && existing.getIsDeleted() == 0) {
|
||||||
|
// 已收藏 -> 物理删除(取消收藏)
|
||||||
this.removeById(existing.getId());
|
this.removeById(existing.getId());
|
||||||
r.setIsFavorited(false);
|
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 {
|
} else {
|
||||||
EventFavorite fav = new EventFavorite();
|
EventFavorite fav = new EventFavorite();
|
||||||
fav.setUserId(userId);
|
fav.setUserId(userId);
|
||||||
|
|||||||
@@ -56,15 +56,28 @@ public class ScriptFavoriteServiceImpl extends ServiceImpl<ScriptFavoriteMapper,
|
|||||||
throw new BusinessException("剧本不存在或无权限访问");
|
throw new BusinessException("剧本不存在或无权限访问");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询时忽略逻辑删除标记,避免 unique key 冲突导致无法重新收藏
|
||||||
LambdaQueryWrapper<ScriptFavorite> w = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<ScriptFavorite> w = new LambdaQueryWrapper<>();
|
||||||
w.eq(ScriptFavorite::getUserId, userId)
|
w.eq(ScriptFavorite::getUserId, userId)
|
||||||
.eq(ScriptFavorite::getScriptId, scriptId)
|
.eq(ScriptFavorite::getScriptId, scriptId);
|
||||||
.eq(ScriptFavorite::getIsDeleted, 0);
|
|
||||||
|
|
||||||
ScriptFavorite existing = this.getOne(w);
|
ScriptFavorite existing = this.getOne(w);
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
|
if (existing.getIsDeleted() != null && existing.getIsDeleted() == 0) {
|
||||||
|
// 已收藏 -> 物理删除(取消收藏)
|
||||||
this.removeById(existing.getId());
|
this.removeById(existing.getId());
|
||||||
r.setIsFavorited(false);
|
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 {
|
} else {
|
||||||
ScriptFavorite fav = new ScriptFavorite();
|
ScriptFavorite fav = new ScriptFavorite();
|
||||||
fav.setUserId(userId);
|
fav.setUserId(userId);
|
||||||
|
|||||||
Reference in New Issue
Block a user