blob: 2de0429cad0951ba3e95935089da3e61982d8dfe [file] [log] [blame]
wuchimedese5722e32025-04-13 17:38:50 +08001package com.example.g8backend.mapper;
wuchimedese5722e32025-04-13 17:38:50 +08002import com.baomidou.mybatisplus.core.mapper.BaseMapper;
夜雨声烦02063592025-04-23 18:10:00 +08003import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
wuchimedese5722e32025-04-13 17:38:50 +08004import com.example.g8backend.entity.Post;
夜雨声烦02063592025-04-23 18:10:00 +08005import com.example.g8backend.entity.PostLike;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08006import org.apache.ibatis.annotations.*;
wuchimedese5722e32025-04-13 17:38:50 +08007import java.util.List;
wuchimedese5722e32025-04-13 17:38:50 +08008@Mapper
9public interface PostMapper extends BaseMapper<Post> {
夜雨声烦02063592025-04-23 18:10:00 +080010 // 获取用户的帖子
wuchimedese5722e32025-04-13 17:38:50 +080011 List<Post> getPostsByUserId(@Param("userId") Long userId);
夜雨声烦02063592025-04-23 18:10:00 +080012 // 搜索帖子
夜雨声烦4527a722025-04-23 17:04:25 +080013 @Select("<script>" +
夜雨声烦39b8cfe2025-06-08 14:10:08 +080014 "SELECT DISTINCT p.* " + // 添加 DISTINCT 解决重复问题
夜雨声烦4527a722025-04-23 17:04:25 +080015 "FROM posts p " +
夜雨声烦39b8cfe2025-06-08 14:10:08 +080016 "LEFT JOIN users u ON p.user_id = u.user_id " + // 只保留必要的 JOIN
17 "WHERE (p.post_title LIKE CONCAT('%', #{keyword}, '%') " +
18 " OR p.post_content LIKE CONCAT('%', #{keyword}, '%')) " +
夜雨声烦4527a722025-04-23 17:04:25 +080019 "<if test='tagIds != null and tagIds.size() > 0'> " +
夜雨声烦39b8cfe2025-06-08 14:10:08 +080020 " AND p.post_id IN ( " + // 改为子查询确保正确过滤标签
21 " SELECT pt.post_id FROM post_tag pt " +
22 " WHERE pt.tag_id IN " +
23 " <foreach item='tagId' collection='tagIds' open='(' separator=',' close=')'> " +
24 " #{tagId} " +
25 " </foreach> " +
26 " ) " +
27 "</if> " +
28 "<if test='author != null and !author.isEmpty()'> " + // 增加空检查
29 " AND u.user_name LIKE CONCAT('%', #{author}, '%') " + // 改为模糊匹配
30 "</if> " +
31 "ORDER BY p.created_at DESC " + // 添加默认排序
32 "LIMIT 1000" + // 添加结果限制防止性能问题
夜雨声烦4527a722025-04-23 17:04:25 +080033 "</script>")
34 List<Post> searchPosts(@Param("keyword") String keyword,
夜雨声烦02063592025-04-23 18:10:00 +080035 @Param("tagIds") List<Long> tagIds,
夜雨声烦4527a722025-04-23 17:04:25 +080036 @Param("author") String author);
夜雨声烦73fcb382025-04-23 18:27:43 +080037 // 检查用户是否已经点赞该帖子
夜雨声烦02063592025-04-23 18:10:00 +080038 @Select("SELECT EXISTS (SELECT 1 FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId})")
39 boolean existsByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
夜雨声烦02063592025-04-23 18:10:00 +080040 // 插入一条点赞记录
41 @Insert("INSERT INTO post_likes (user_id, post_id) VALUES (#{userId}, #{postId})")
wuchimedes191df942025-06-08 11:03:37 +080042// void insert(PostLike postLike);
43 void insertLike(PostLike postLike);
夜雨声烦02063592025-04-23 18:10:00 +080044 // 删除用户对帖子的点赞记录
45 @Delete("DELETE FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId}")
46 void deleteLikeByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
夜雨声烦02063592025-04-23 18:10:00 +080047 // 获取某个帖子点赞数
48 @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}")
49 Long selectCount(@Param("postId") Long postId);
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080050 @Update("UPDATE posts SET view_count = view_count + 1 WHERE post_id = #{postId}")
51 void incrementViewCount(Long postId);
夜雨声烦368e3562025-04-24 01:49:46 +080052 @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}")
53 Long selectLikeCount(Long postId);
夜雨声烦368e3562025-04-24 01:49:46 +080054 @Select("SELECT post_id FROM post_views WHERE user_id = #{userId}")
55 List<Long> findViewedPostIds(Long userId);
夜雨声烦368e3562025-04-24 01:49:46 +080056 @Update({
57 "<script>",
58 "UPDATE posts",
59 "SET hot_score = CASE",
60 " <foreach collection='posts' item='post'>",
61 " WHEN post_id = #{post.postId} THEN #{post.hotScore}",
62 " </foreach>",
63 "END,",
64 "last_calculated = NOW()",
65 "WHERE post_id IN",
66 " <foreach collection='posts' item='post' open='(' separator=',' close=')'>",
67 " #{post.postId}",
68 " </foreach>",
69 "</script>"
70 })
71 int batchUpdateHotScore(@Param("posts") List<Post> posts);
夜雨声烦070c05a2025-05-13 20:33:50 +080072
73 @Update("UPDATE posts SET average_rating = #{averageRating}, rating_count = #{ratingCount} WHERE post_id = #{postId}")
74 void updateRatingStats(
75 @Param("postId") Long postId,
76 @Param("averageRating") Double averageRating,
77 @Param("ratingCount") Integer ratingCount
78 );
夜雨声烦f77d8132025-04-24 19:31:18 +080079}