blob: 8f7e02961a378b11263330b8fcbe958af768e0fc [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;
wuchimedese5722e32025-04-13 17:38:50 +08007import org.apache.ibatis.annotations.Mapper;
8import org.apache.ibatis.annotations.Param;
夜雨声烦4527a722025-04-23 17:04:25 +08009import org.apache.ibatis.annotations.Select;
夜雨声烦02063592025-04-23 18:10:00 +080010import org.apache.ibatis.annotations.Delete;
11import org.apache.ibatis.annotations.Insert;
wuchimedese5722e32025-04-13 17:38:50 +080012
13import java.util.List;
14
15@Mapper
16public interface PostMapper extends BaseMapper<Post> {
夜雨声烦02063592025-04-23 18:10:00 +080017
18 // 获取用户的帖子
wuchimedese5722e32025-04-13 17:38:50 +080019 List<Post> getPostsByUserId(@Param("userId") Long userId);
夜雨声烦4527a722025-04-23 17:04:25 +080020
夜雨声烦02063592025-04-23 18:10:00 +080021 // 搜索帖子
夜雨声烦4527a722025-04-23 17:04:25 +080022 @Select("<script>" +
23 "SELECT p.* " +
24 "FROM posts p " +
25 "LEFT JOIN post_tag pt ON p.post_id = pt.post_id " +
26 "LEFT JOIN tags t ON pt.tag_id = t.tag_id " +
27 "LEFT JOIN users u ON p.user_id = u.user_id " +
28 "WHERE (p.post_title LIKE CONCAT('%', #{keyword}, '%') OR p.post_content LIKE CONCAT('%', #{keyword}, '%')) " +
29 "<if test='tagIds != null and tagIds.size() > 0'> " +
30 "AND pt.tag_id IN " +
31 "<foreach item='tagId' collection='tagIds' open='(' separator=',' close=')'> " +
32 "#{tagId} " +
33 "</foreach> " +
34 "</if>" +
35 "<if test='author != null'> " +
36 "AND u.user_name = #{author} " +
37 "</if>" +
38 "</script>")
39 List<Post> searchPosts(@Param("keyword") String keyword,
夜雨声烦02063592025-04-23 18:10:00 +080040 @Param("tagIds") List<Long> tagIds,
夜雨声烦4527a722025-04-23 17:04:25 +080041 @Param("author") String author);
夜雨声烦02063592025-04-23 18:10:00 +080042
夜雨声烦73fcb382025-04-23 18:27:43 +080043 // 检查用户是否已经点赞该帖子
夜雨声烦02063592025-04-23 18:10:00 +080044 @Select("SELECT EXISTS (SELECT 1 FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId})")
45 boolean existsByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
46
47 // 插入一条点赞记录
48 @Insert("INSERT INTO post_likes (user_id, post_id) VALUES (#{userId}, #{postId})")
49 void insert(PostLike postLike);
50
51 // 删除用户对帖子的点赞记录
52 @Delete("DELETE FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId}")
53 void deleteLikeByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
54
55 // 获取某个帖子点赞数
56 @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}")
57 Long selectCount(@Param("postId") Long postId);
wuchimedese5722e32025-04-13 17:38:50 +080058}