blob: 178df396df4685a08075203b2dc2c7e0e516bf06 [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>" +
14 "SELECT p.* " +
15 "FROM posts p " +
16 "LEFT JOIN post_tag pt ON p.post_id = pt.post_id " +
17 "LEFT JOIN tags t ON pt.tag_id = t.tag_id " +
18 "LEFT JOIN users u ON p.user_id = u.user_id " +
19 "WHERE (p.post_title LIKE CONCAT('%', #{keyword}, '%') OR p.post_content LIKE CONCAT('%', #{keyword}, '%')) " +
20 "<if test='tagIds != null and tagIds.size() > 0'> " +
21 "AND pt.tag_id IN " +
22 "<foreach item='tagId' collection='tagIds' open='(' separator=',' close=')'> " +
23 "#{tagId} " +
24 "</foreach> " +
25 "</if>" +
26 "<if test='author != null'> " +
27 "AND u.user_name = #{author} " +
28 "</if>" +
29 "</script>")
30 List<Post> searchPosts(@Param("keyword") String keyword,
夜雨声烦02063592025-04-23 18:10:00 +080031 @Param("tagIds") List<Long> tagIds,
夜雨声烦4527a722025-04-23 17:04:25 +080032 @Param("author") String author);
夜雨声烦73fcb382025-04-23 18:27:43 +080033 // 检查用户是否已经点赞该帖子
夜雨声烦02063592025-04-23 18:10:00 +080034 @Select("SELECT EXISTS (SELECT 1 FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId})")
35 boolean existsByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
夜雨声烦02063592025-04-23 18:10:00 +080036 // 插入一条点赞记录
37 @Insert("INSERT INTO post_likes (user_id, post_id) VALUES (#{userId}, #{postId})")
wuchimedes191df942025-06-08 11:03:37 +080038// void insert(PostLike postLike);
39 void insertLike(PostLike postLike);
夜雨声烦02063592025-04-23 18:10:00 +080040 // 删除用户对帖子的点赞记录
41 @Delete("DELETE FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId}")
42 void deleteLikeByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
夜雨声烦02063592025-04-23 18:10:00 +080043 // 获取某个帖子点赞数
44 @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}")
45 Long selectCount(@Param("postId") Long postId);
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080046 @Update("UPDATE posts SET view_count = view_count + 1 WHERE post_id = #{postId}")
47 void incrementViewCount(Long postId);
夜雨声烦368e3562025-04-24 01:49:46 +080048 @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}")
49 Long selectLikeCount(Long postId);
夜雨声烦368e3562025-04-24 01:49:46 +080050 @Select("SELECT post_id FROM post_views WHERE user_id = #{userId}")
51 List<Long> findViewedPostIds(Long userId);
夜雨声烦368e3562025-04-24 01:49:46 +080052 @Update({
53 "<script>",
54 "UPDATE posts",
55 "SET hot_score = CASE",
56 " <foreach collection='posts' item='post'>",
57 " WHEN post_id = #{post.postId} THEN #{post.hotScore}",
58 " </foreach>",
59 "END,",
60 "last_calculated = NOW()",
61 "WHERE post_id IN",
62 " <foreach collection='posts' item='post' open='(' separator=',' close=')'>",
63 " #{post.postId}",
64 " </foreach>",
65 "</script>"
66 })
67 int batchUpdateHotScore(@Param("posts") List<Post> posts);
夜雨声烦070c05a2025-05-13 20:33:50 +080068
69 @Update("UPDATE posts SET average_rating = #{averageRating}, rating_count = #{ratingCount} WHERE post_id = #{postId}")
70 void updateRatingStats(
71 @Param("postId") Long postId,
72 @Param("averageRating") Double averageRating,
73 @Param("ratingCount") Integer ratingCount
74 );
夜雨声烦f77d8132025-04-24 19:31:18 +080075}