wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 1 | package com.example.g8backend.mapper; |
| 2 | |
| 3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame^] | 4 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 5 | import com.example.g8backend.entity.Post; |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame^] | 6 | import com.example.g8backend.entity.PostLike; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 7 | import org.apache.ibatis.annotations.Mapper; |
| 8 | import org.apache.ibatis.annotations.Param; |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 9 | import org.apache.ibatis.annotations.Select; |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame^] | 10 | import org.apache.ibatis.annotations.Delete; |
| 11 | import org.apache.ibatis.annotations.Insert; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 12 | |
| 13 | import java.util.List; |
| 14 | |
| 15 | @Mapper |
| 16 | public interface PostMapper extends BaseMapper<Post> { |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame^] | 17 | |
| 18 | // 获取用户的帖子 |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 19 | List<Post> getPostsByUserId(@Param("userId") Long userId); |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 20 | |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame^] | 21 | // 搜索帖子 |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 22 | @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, |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame^] | 40 | @Param("tagIds") List<Long> tagIds, |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 41 | @Param("author") String author); |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame^] | 42 | |
| 43 | // 检查用户是否已经点赞该帖子 |
| 44 | @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); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 58 | } |