wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 1 | package com.example.g8backend.mapper; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 2 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 4 | import com.example.g8backend.entity.Post; |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 5 | import com.example.g8backend.entity.PostLike; |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 6 | import org.apache.ibatis.annotations.*; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 7 | import java.util.List; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 8 | @Mapper |
| 9 | public interface PostMapper extends BaseMapper<Post> { |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 10 | // 获取用户的帖子 |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 11 | List<Post> getPostsByUserId(@Param("userId") Long userId); |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 12 | // 搜索帖子 |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 13 | @Select("<script>" + |
夜雨声烦 | 39b8cfe | 2025-06-08 14:10:08 +0800 | [diff] [blame] | 14 | "SELECT DISTINCT p.* " + // 添加 DISTINCT 解决重复问题 |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 15 | "FROM posts p " + |
夜雨声烦 | 39b8cfe | 2025-06-08 14:10:08 +0800 | [diff] [blame] | 16 | "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}, '%')) " + |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 19 | "<if test='tagIds != null and tagIds.size() > 0'> " + |
夜雨声烦 | 39b8cfe | 2025-06-08 14:10:08 +0800 | [diff] [blame] | 20 | " 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" + // 添加结果限制防止性能问题 |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 33 | "</script>") |
| 34 | List<Post> searchPosts(@Param("keyword") String keyword, |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 35 | @Param("tagIds") List<Long> tagIds, |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 36 | @Param("author") String author); |
夜雨声烦 | 73fcb38 | 2025-04-23 18:27:43 +0800 | [diff] [blame] | 37 | // 检查用户是否已经点赞该帖子 |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 38 | @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); |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 40 | // 插入一条点赞记录 |
| 41 | @Insert("INSERT INTO post_likes (user_id, post_id) VALUES (#{userId}, #{postId})") |
wuchimedes | 191df94 | 2025-06-08 11:03:37 +0800 | [diff] [blame] | 42 | // void insert(PostLike postLike); |
| 43 | void insertLike(PostLike postLike); |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 44 | // 删除用户对帖子的点赞记录 |
| 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); |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 47 | // 获取某个帖子点赞数 |
| 48 | @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}") |
| 49 | Long selectCount(@Param("postId") Long postId); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 50 | @Update("UPDATE posts SET view_count = view_count + 1 WHERE post_id = #{postId}") |
| 51 | void incrementViewCount(Long postId); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 52 | @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}") |
| 53 | Long selectLikeCount(Long postId); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 54 | @Select("SELECT post_id FROM post_views WHERE user_id = #{userId}") |
| 55 | List<Long> findViewedPostIds(Long userId); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 56 | @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); |
夜雨声烦 | 070c05a | 2025-05-13 20:33:50 +0800 | [diff] [blame] | 72 | |
| 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 | ); |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 79 | } |