package com.emotion.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.emotion.entity.Message; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.time.LocalDateTime; import java.util.List; /** * 消息Mapper接口 * * @author emotion-museum * @date 2025-07-23 */ @Mapper public interface MessageMapper extends BaseMapper { /** * 根据用户ID和时间范围查询消息 * 通过conversation表关联查询 */ @Select("SELECT m.* FROM message m " + "INNER JOIN conversation c ON m.conversation_id = c.id " + "WHERE c.user_id = #{userId} " + "AND m.create_time BETWEEN #{startTime} AND #{endTime} " + "AND m.is_deleted = 0 " + "ORDER BY m.create_time ASC") List getByUserIdAndTimeRange(@Param("userId") String userId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime); /** * 根据用户ID分页查询消息 * 通过conversation表关联查询 */ @Select("SELECT m.* FROM message m " + "INNER JOIN conversation c ON m.conversation_id = c.id " + "WHERE c.user_id = #{userId} " + "AND m.is_deleted = 0 " + "ORDER BY m.create_time DESC " + "LIMIT #{offset}, #{size}") List getByUserIdWithPageList(@Param("userId") String userId, @Param("offset") Integer offset, @Param("size") Integer size); /** * 统计用户消息总数 */ @Select("SELECT COUNT(*) FROM message m " + "INNER JOIN conversation c ON m.conversation_id = c.id " + "WHERE c.user_id = #{userId} " + "AND m.is_deleted = 0") Long countByUserId(@Param("userId") String userId); /** * 根据用户ID和关键词搜索消息 */ @Select("SELECT m.* FROM message m " + "INNER JOIN conversation c ON m.conversation_id = c.id " + "WHERE c.user_id = #{userId} " + "AND m.content LIKE CONCAT('%', #{keyword}, '%') " + "AND m.is_deleted = 0 " + "ORDER BY m.create_time DESC " + "LIMIT #{limit}") List searchByUserIdAndKeyword(@Param("userId") String userId, @Param("keyword") String keyword, @Param("limit") Integer limit); }