| package com.example.g8backend.mapper; |
| import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| import com.example.g8backend.entity.Post; |
| import com.example.g8backend.entity.PostLike; |
| import org.apache.ibatis.annotations.*; |
| import java.util.List; |
| @Mapper |
| public interface PostMapper extends BaseMapper<Post> { |
| // 获取用户的帖子 |
| List<Post> getPostsByUserId(@Param("userId") Long userId); |
| // 搜索帖子 |
| @Select("<script>" + |
| "SELECT DISTINCT p.* " + // 添加 DISTINCT 解决重复问题 |
| "FROM posts p " + |
| "LEFT JOIN users u ON p.user_id = u.user_id " + // 只保留必要的 JOIN |
| "WHERE (p.post_title LIKE CONCAT('%', #{keyword}, '%') " + |
| " OR p.post_content LIKE CONCAT('%', #{keyword}, '%')) " + |
| "<if test='tagIds != null and tagIds.size() > 0'> " + |
| " AND p.post_id IN ( " + // 改为子查询确保正确过滤标签 |
| " SELECT pt.post_id FROM post_tag pt " + |
| " WHERE pt.tag_id IN " + |
| " <foreach item='tagId' collection='tagIds' open='(' separator=',' close=')'> " + |
| " #{tagId} " + |
| " </foreach> " + |
| " ) " + |
| "</if> " + |
| "<if test='author != null and !author.isEmpty()'> " + // 增加空检查 |
| " AND u.user_name LIKE CONCAT('%', #{author}, '%') " + // 改为模糊匹配 |
| "</if> " + |
| "ORDER BY p.created_at DESC " + // 添加默认排序 |
| "LIMIT 1000" + // 添加结果限制防止性能问题 |
| "</script>") |
| List<Post> searchPosts(@Param("keyword") String keyword, |
| @Param("tagIds") List<Long> tagIds, |
| @Param("author") String author); |
| // 检查用户是否已经点赞该帖子 |
| @Select("SELECT EXISTS (SELECT 1 FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId})") |
| boolean existsByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId); |
| // 插入一条点赞记录 |
| @Insert("INSERT INTO post_likes (user_id, post_id) VALUES (#{userId}, #{postId})") |
| // void insert(PostLike postLike); |
| void insertLike(PostLike postLike); |
| // 删除用户对帖子的点赞记录 |
| @Delete("DELETE FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId}") |
| void deleteLikeByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId); |
| // 获取某个帖子点赞数 |
| @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}") |
| Long selectCount(@Param("postId") Long postId); |
| @Update("UPDATE posts SET view_count = view_count + 1 WHERE post_id = #{postId}") |
| void incrementViewCount(Long postId); |
| @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}") |
| Long selectLikeCount(Long postId); |
| @Select("SELECT post_id FROM post_views WHERE user_id = #{userId}") |
| List<Long> findViewedPostIds(Long userId); |
| @Update({ |
| "<script>", |
| "UPDATE posts", |
| "SET hot_score = CASE", |
| " <foreach collection='posts' item='post'>", |
| " WHEN post_id = #{post.postId} THEN #{post.hotScore}", |
| " </foreach>", |
| "END,", |
| "last_calculated = NOW()", |
| "WHERE post_id IN", |
| " <foreach collection='posts' item='post' open='(' separator=',' close=')'>", |
| " #{post.postId}", |
| " </foreach>", |
| "</script>" |
| }) |
| int batchUpdateHotScore(@Param("posts") List<Post> posts); |
| |
| @Update("UPDATE posts SET average_rating = #{averageRating}, rating_count = #{ratingCount} WHERE post_id = #{postId}") |
| void updateRatingStats( |
| @Param("postId") Long postId, |
| @Param("averageRating") Double averageRating, |
| @Param("ratingCount") Integer ratingCount |
| ); |
| } |