diff --git a/server/src/main/java/com/emotion/mapper/EventFavoriteMapper.java b/server/src/main/java/com/emotion/mapper/EventFavoriteMapper.java index f57eb1c..d4dd0ee 100644 --- a/server/src/main/java/com/emotion/mapper/EventFavoriteMapper.java +++ b/server/src/main/java/com/emotion/mapper/EventFavoriteMapper.java @@ -3,13 +3,35 @@ package com.emotion.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.emotion.entity.EventFavorite; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; /** - * 人生事件收藏Mapper接口 + * 人生事件收藏 Mapper * * @author huazhongmin * @date 2026-07-19 */ @Mapper public interface EventFavoriteMapper extends BaseMapper { + + /** + * 查询用户对某事件的收藏记录(包含已逻辑删除的记录,绕过 @TableLogic 自动过滤) + * 用于 toggle 时判断是否存在历史记录,避免重新收藏时产生重复记录 + */ + @Select("SELECT * FROM t_event_favorite WHERE user_id = #{userId} AND event_id = #{eventId} LIMIT 1") + EventFavorite selectAnyByUserAndEvent(@Param("userId") String userId, @Param("eventId") String eventId); + + /** + * 物理删除记录(绕过 @TableLogic 的逻辑删除) + */ + @Update("DELETE FROM t_event_favorite WHERE id = #{id}") + int physicalDeleteById(@Param("id") String id); + + /** + * 恢复逻辑删除的记录为已收藏状态 + */ + @Update("UPDATE t_event_favorite SET is_deleted = 0, update_time = NOW() WHERE id = #{id}") + int restoreById(@Param("id") String id); } diff --git a/server/src/main/java/com/emotion/mapper/ScriptFavoriteMapper.java b/server/src/main/java/com/emotion/mapper/ScriptFavoriteMapper.java index e05a277..9dcd3de 100644 --- a/server/src/main/java/com/emotion/mapper/ScriptFavoriteMapper.java +++ b/server/src/main/java/com/emotion/mapper/ScriptFavoriteMapper.java @@ -3,6 +3,9 @@ package com.emotion.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.emotion.entity.ScriptFavorite; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; /** * 剧本收藏Mapper接口 @@ -12,4 +15,23 @@ import org.apache.ibatis.annotations.Mapper; */ @Mapper public interface ScriptFavoriteMapper extends BaseMapper { + + /** + * 查询用户对某剧本的收藏记录(包含已逻辑删除的记录,绕过 @TableLogic 自动过滤) + * 用于 toggle 时判断是否存在历史记录,避免 unique key 冲突 + */ + @Select("SELECT * FROM t_script_favorite WHERE user_id = #{userId} AND script_id = #{scriptId} LIMIT 1") + ScriptFavorite selectAnyByUserAndScript(@Param("userId") String userId, @Param("scriptId") String scriptId); + + /** + * 物理删除记录(绕过 @TableLogic 的逻辑删除) + */ + @Update("DELETE FROM t_script_favorite WHERE id = #{id}") + int physicalDeleteById(@Param("id") String id); + + /** + * 恢复逻辑删除的记录为已收藏状态 + */ + @Update("UPDATE t_script_favorite SET is_deleted = 0, update_time = NOW() WHERE id = #{id}") + int restoreById(@Param("id") String id); } diff --git a/server/src/main/java/com/emotion/service/impl/EventFavoriteServiceImpl.java b/server/src/main/java/com/emotion/service/impl/EventFavoriteServiceImpl.java index 5aa03d3..20863d9 100644 --- a/server/src/main/java/com/emotion/service/impl/EventFavoriteServiceImpl.java +++ b/server/src/main/java/com/emotion/service/impl/EventFavoriteServiceImpl.java @@ -56,23 +56,17 @@ public class EventFavoriteServiceImpl extends ServiceImpl w = new LambdaQueryWrapper<>(); - w.eq(EventFavorite::getUserId, userId) - .eq(EventFavorite::getEventId, eventId); - - EventFavorite existing = this.getOne(w); + // 查询时绕过 @TableLogic 自动过滤,包含已逻辑删除的记录 + // 避免重新收藏时产生重复记录 + EventFavorite existing = baseMapper.selectAnyByUserAndEvent(userId, eventId); if (existing != null) { if (existing.getIsDeleted() != null && existing.getIsDeleted() == 0) { // 已收藏 -> 物理删除(取消收藏) - this.removeById(existing.getId()); + baseMapper.physicalDeleteById(existing.getId()); r.setIsFavorited(false); } else { // 历史逻辑删除记录 -> 恢复为已收藏 - this.lambdaUpdate() - .set(EventFavorite::getIsDeleted, 0) - .eq(EventFavorite::getId, existing.getId()) - .update(); + baseMapper.restoreById(existing.getId()); r.setIsFavorited(true); if (existing.getCreateTime() != null) { r.setFavoriteTime(existing.getCreateTime().format(FMT)); diff --git a/server/src/main/java/com/emotion/service/impl/ScriptFavoriteServiceImpl.java b/server/src/main/java/com/emotion/service/impl/ScriptFavoriteServiceImpl.java index e0885ad..9205a62 100644 --- a/server/src/main/java/com/emotion/service/impl/ScriptFavoriteServiceImpl.java +++ b/server/src/main/java/com/emotion/service/impl/ScriptFavoriteServiceImpl.java @@ -56,23 +56,17 @@ public class ScriptFavoriteServiceImpl extends ServiceImpl w = new LambdaQueryWrapper<>(); - w.eq(ScriptFavorite::getUserId, userId) - .eq(ScriptFavorite::getScriptId, scriptId); - - ScriptFavorite existing = this.getOne(w); + // 查询时绕过 @TableLogic 自动过滤,包含已逻辑删除的记录 + // 避免 unique key 冲突导致无法重新收藏 + ScriptFavorite existing = baseMapper.selectAnyByUserAndScript(userId, scriptId); if (existing != null) { if (existing.getIsDeleted() != null && existing.getIsDeleted() == 0) { // 已收藏 -> 物理删除(取消收藏) - this.removeById(existing.getId()); + baseMapper.physicalDeleteById(existing.getId()); r.setIsFavorited(false); } else { // 历史逻辑删除记录 -> 恢复为已收藏 - this.lambdaUpdate() - .set(ScriptFavorite::getIsDeleted, 0) - .eq(ScriptFavorite::getId, existing.getId()) - .update(); + baseMapper.restoreById(existing.getId()); r.setIsFavorited(true); if (existing.getCreateTime() != null) { r.setFavoriteTime(existing.getCreateTime().format(FMT));