blob: d49125e290f59f5790249b05e941b8d601fd1d0b [file] [log] [blame]
wuchimedese5722e32025-04-13 17:38:50 +08001package com.example.g8backend.mapper;
2
3import com.baomidou.mybatisplus.core.mapper.BaseMapper;
夜雨声烦02063592025-04-23 18:10:00 +08004import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
wuchimedese5722e32025-04-13 17:38:50 +08005import com.example.g8backend.entity.Post;
夜雨声烦02063592025-04-23 18:10:00 +08006import com.example.g8backend.entity.PostLike;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08007import org.apache.ibatis.annotations.*;
wuchimedese5722e32025-04-13 17:38:50 +08008
9import java.util.List;
10
11@Mapper
12public interface PostMapper extends BaseMapper<Post> {
夜雨声烦02063592025-04-23 18:10:00 +080013
14 // 获取用户的帖子
wuchimedese5722e32025-04-13 17:38:50 +080015 List<Post> getPostsByUserId(@Param("userId") Long userId);
夜雨声烦4527a722025-04-23 17:04:25 +080016
夜雨声烦02063592025-04-23 18:10:00 +080017 // 搜索帖子
夜雨声烦4527a722025-04-23 17:04:25 +080018 @Select("<script>" +
19 "SELECT p.* " +
20 "FROM posts p " +
21 "LEFT JOIN post_tag pt ON p.post_id = pt.post_id " +
22 "LEFT JOIN tags t ON pt.tag_id = t.tag_id " +
23 "LEFT JOIN users u ON p.user_id = u.user_id " +
24 "WHERE (p.post_title LIKE CONCAT('%', #{keyword}, '%') OR p.post_content LIKE CONCAT('%', #{keyword}, '%')) " +
25 "<if test='tagIds != null and tagIds.size() > 0'> " +
26 "AND pt.tag_id IN " +
27 "<foreach item='tagId' collection='tagIds' open='(' separator=',' close=')'> " +
28 "#{tagId} " +
29 "</foreach> " +
30 "</if>" +
31 "<if test='author != null'> " +
32 "AND u.user_name = #{author} " +
33 "</if>" +
34 "</script>")
35 List<Post> searchPosts(@Param("keyword") String keyword,
夜雨声烦02063592025-04-23 18:10:00 +080036 @Param("tagIds") List<Long> tagIds,
夜雨声烦4527a722025-04-23 17:04:25 +080037 @Param("author") String author);
夜雨声烦02063592025-04-23 18:10:00 +080038
夜雨声烦73fcb382025-04-23 18:27:43 +080039 // 检查用户是否已经点赞该帖子
夜雨声烦02063592025-04-23 18:10:00 +080040 @Select("SELECT EXISTS (SELECT 1 FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId})")
41 boolean existsByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
42
43 // 插入一条点赞记录
44 @Insert("INSERT INTO post_likes (user_id, post_id) VALUES (#{userId}, #{postId})")
45 void insert(PostLike postLike);
46
47 // 删除用户对帖子的点赞记录
48 @Delete("DELETE FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId}")
49 void deleteLikeByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
50
51 // 获取某个帖子点赞数
52 @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}")
53 Long selectCount(@Param("postId") Long postId);
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080054
55 @Update("UPDATE posts SET view_count = view_count + 1 WHERE post_id = #{postId}")
56 void incrementViewCount(Long postId);
wuchimedese5722e32025-04-13 17:38:50 +080057}