将查询历史消息修改为只查询消息,不进行多余操作

This commit is contained in:
KilLze
2026-01-06 23:38:11 +08:00
parent c0969406b3
commit 1d179da910
8 changed files with 28 additions and 139 deletions

View File

@@ -4,15 +4,11 @@ import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode; import com.bao.dating.common.ResultCode;
import com.bao.dating.common.result.PageResult; import com.bao.dating.common.result.PageResult;
import com.bao.dating.context.UserContext; import com.bao.dating.context.UserContext;
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
import com.bao.dating.pojo.vo.ChatRecordsVO; import com.bao.dating.pojo.vo.ChatRecordsVO;
import com.bao.dating.service.ChatService; import com.bao.dating.service.ChatService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** /**
* 聊天控制器 * 聊天控制器
@@ -28,15 +24,18 @@ public class ChatController {
/** /**
* 获取聊天记录 * 获取聊天记录
* @param targetUserId 目标用户ID * @param targetUserId 目标用户ID
* @param queryDTO 查询参数 * @param page 页码
* @return 聊天记录列表 * @param size 页大小
*/ */
@GetMapping("/history/{targetUserId}") @GetMapping("/history/{targetUserId}")
public Result<PageResult<ChatRecordsVO>> getChatHistory( public Result<PageResult<ChatRecordsVO>> getChatHistory(
@PathVariable Long targetUserId, @PathVariable Long targetUserId,
ChatHistoryQueryDTO queryDTO){ @RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "50") Integer size){
Long currentUserId = UserContext.getUserId(); Long currentUserId = UserContext.getUserId();
PageResult<ChatRecordsVO> history = chatService.getChatHistory(currentUserId, targetUserId, queryDTO); PageResult<ChatRecordsVO> history = chatService.getChatHistory(currentUserId, targetUserId, page, size);
return Result.success(ResultCode.SUCCESS, "获取聊天记录成功", history); return Result.success(ResultCode.SUCCESS, "获取聊天记录成功", history);
} }
} }

View File

@@ -1,12 +1,10 @@
package com.bao.dating.mapper; package com.bao.dating.mapper;
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
import com.bao.dating.pojo.entity.ChatRecords; import com.bao.dating.pojo.entity.ChatRecords;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
@Mapper @Mapper
@@ -19,12 +17,8 @@ public interface ChatRecordsMapper {
/** /**
* 根据用户ID和接收方用户ID查询聊天记录 * 根据用户ID和接收方用户ID查询聊天记录
*/ */
List<ChatRecords> selectByUsersAndConditions( List<ChatRecords> selectChatWindowHistory(
@Param("currentUserId") Long currentUserId, @Param("currentUserId") Long currentUserId,
@Param("targetUserId") Long targetUserId, @Param("targetUserId") Long targetUserId
@Param("messageContent") String messageContent,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime,
@Param("messageType") Integer messageType
); );
} }

View File

@@ -1,23 +0,0 @@
package com.bao.dating.pojo.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
/**
* 聊天记录查询参数
* @author KilLze
*/
@Data
public class ChatHistoryQueryDTO {
private Integer page;
private Integer size;
private Long targetUserId;
private String messageContent;
@JsonFormat(pattern = "yyyy-MM-dd")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate date;
private Integer messageType;
}

View File

@@ -1,32 +0,0 @@
package com.bao.dating.pojo.dto;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 聊天记录数据传输对象
* @author KilLze
*/
@Data
public class ChatRecordsDTO {
/** 聊天记录ID */
private Long chatId;
/** 发送者用户ID */
private Long senderUserId;
/** 接收者用户ID */
private Long receiverUserId;
/** 消息内容 */
private String messageContent;
/** 消息类型 1-文本消息2-文件消息) */
private Integer messageType;
/** 阅读状态 0-未读1-已读) */
private Integer readStatus;
/** 阅读时间 */
private LocalDateTime readTime;
/** 发送时间 */
private LocalDateTime sendTime;
/** 消息状态 1-正常2-已撤回3-已删除) */
private Integer messageStatus;
}

View File

@@ -1,19 +0,0 @@
package com.bao.dating.pojo.vo;
import lombok.Data;
import java.util.List;
/**
* 聊天会话详情
* @author KilLze
*/
@Data
public class ChatSessionDetailVO {
/** 会话信息 */
private ChatSessionsVO session;
/** 聊天记录列表 */
private List<ChatRecordsVO> records;
}

View File

@@ -1,7 +1,6 @@
package com.bao.dating.service; package com.bao.dating.service;
import com.bao.dating.common.result.PageResult; import com.bao.dating.common.result.PageResult;
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
import com.bao.dating.pojo.dto.ChatRecordSendDTO; import com.bao.dating.pojo.dto.ChatRecordSendDTO;
import com.bao.dating.pojo.vo.ChatRecordsVO; import com.bao.dating.pojo.vo.ChatRecordsVO;
@@ -20,10 +19,11 @@ public interface ChatService {
/** /**
* 获取聊天记录 * 获取聊天记录
* @param currentUserId 发送方用户ID * @param currentUserId 当前用户ID
* @param targetUserId 接收方用户ID * @param targetUserId 目标用户ID
* @param queryDTO 查询参数 * @param page 页码
* @return 聊天记录VO列表 * @param size 页大小
* @return 聊天记录列表
*/ */
PageResult<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, ChatHistoryQueryDTO queryDTO); PageResult<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, Integer page, Integer size);
} }

View File

@@ -3,7 +3,6 @@ package com.bao.dating.service.impl;
import com.bao.dating.common.result.PageResult; import com.bao.dating.common.result.PageResult;
import com.bao.dating.mapper.ChatRecordsMapper; import com.bao.dating.mapper.ChatRecordsMapper;
import com.bao.dating.mapper.ChatSessionsMapper; import com.bao.dating.mapper.ChatSessionsMapper;
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
import com.bao.dating.pojo.dto.ChatRecordSendDTO; import com.bao.dating.pojo.dto.ChatRecordSendDTO;
import com.bao.dating.pojo.dto.UserNicknameDTO; import com.bao.dating.pojo.dto.UserNicknameDTO;
import com.bao.dating.pojo.entity.ChatRecords; import com.bao.dating.pojo.entity.ChatRecords;
@@ -20,7 +19,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -115,41 +113,24 @@ public class ChatServiceImpl implements ChatService {
/** /**
* 获取聊天记录 * 获取聊天记录
* @param currentUserId 当前用户ID
* @param targetUserId 目标用户ID
* @param queryDTO 查询参数
* @return 聊天记录列表 * @return 聊天记录列表
*/ */
@Override @Override
public PageResult<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, ChatHistoryQueryDTO queryDTO) { public PageResult<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, Integer page, Integer size) {
Integer page = queryDTO.getPage();
if (page == null || page < 1) { if (page == null || page < 1) {
page = 1; page = 1;
} }
Integer size = queryDTO.getSize();
if (size == null || size < 1 || size > 100) { if (size == null || size < 1 || size > 100) {
size = 50; size = 50;
} }
LocalDateTime startTime = null;
LocalDateTime endTime = null;
if (queryDTO.getDate() != null) {
startTime = queryDTO.getDate().atStartOfDay();
endTime = queryDTO.getDate().atTime(LocalTime.MAX);
}
// 分页 // 分页
PageHelper.startPage(page, size); PageHelper.startPage(page, size);
// 查询 // 查询
List<ChatRecords> recordsList = chatRecordsMapper.selectByUsersAndConditions( List<ChatRecords> recordsList =
currentUserId, chatRecordsMapper.selectChatWindowHistory(currentUserId, targetUserId);
targetUserId,
queryDTO.getMessageContent(),
startTime,
endTime,
queryDTO.getMessageType()
);
// 使用 PageInfo 封装查询结果 // 使用 PageInfo 封装查询结果
PageInfo<ChatRecords> pageInfo = new PageInfo<>(recordsList); PageInfo<ChatRecords> pageInfo = new PageInfo<>(recordsList);

View File

@@ -31,30 +31,19 @@
) )
</insert> </insert>
<!-- 根据两个用户ID及可选条件查询聊天记录 (按发送时间倒序) - PageHelper 会自动处理分页 --> <!-- 根据两个用户ID查询聊天记录 (按发送时间倒序) - PageHelper 会自动处理分页 -->
<select id="selectByUsersAndConditions" resultType="com.bao.dating.pojo.entity.ChatRecords"> <select id="selectChatWindowHistory" resultType="com.bao.dating.pojo.entity.ChatRecords">
SELECT SELECT
chat_id, sender_user_id, receiver_user_id, message_content, message_type, chat_id, sender_user_id, receiver_user_id, message_content, message_type,
read_status, read_time, send_time, message_status, created_at, updated_at read_status, read_time, send_time, message_status, created_at, updated_at
FROM chat_records FROM chat_records
WHERE ( WHERE
message_status = 1
AND (
(sender_user_id = #{currentUserId} AND receiver_user_id = #{targetUserId}) (sender_user_id = #{currentUserId} AND receiver_user_id = #{targetUserId})
OR (sender_user_id = #{targetUserId} AND receiver_user_id = #{currentUserId}) OR
) (sender_user_id = #{targetUserId} AND receiver_user_id = #{currentUserId})
AND message_status = 1 )
<!-- 动态条件 -->
<if test="messageContent != null and messageContent != ''">
AND message_content LIKE CONCAT('%', #{messageContent}, '%')
</if>
<if test="startTime != null">
AND send_time >= #{startTime}
</if>
<if test="endTime != null">
AND send_time &lt;= #{endTime}
</if>
<if test="messageType != null">
AND message_type = #{messageType}
</if>
ORDER BY send_time DESC ORDER BY send_time DESC
</select> </select>
</mapper> </mapper>