将查询历史消息修改为只查询消息,不进行多余操作
This commit is contained in:
@@ -4,15 +4,11 @@ import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.common.result.PageResult;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
|
||||
import com.bao.dating.pojo.vo.ChatRecordsVO;
|
||||
import com.bao.dating.service.ChatService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 聊天控制器
|
||||
@@ -28,15 +24,18 @@ public class ChatController {
|
||||
/**
|
||||
* 获取聊天记录
|
||||
* @param targetUserId 目标用户ID
|
||||
* @param queryDTO 查询参数
|
||||
* @return 聊天记录列表
|
||||
* @param page 页码
|
||||
* @param size 页大小
|
||||
*/
|
||||
@GetMapping("/history/{targetUserId}")
|
||||
public Result<PageResult<ChatRecordsVO>> getChatHistory(
|
||||
@PathVariable Long targetUserId,
|
||||
ChatHistoryQueryDTO queryDTO){
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "50") Integer size){
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
|
||||
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
|
||||
import com.bao.dating.pojo.entity.ChatRecords;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
@@ -19,12 +17,8 @@ public interface ChatRecordsMapper {
|
||||
/**
|
||||
* 根据用户ID和接收方用户ID查询聊天记录
|
||||
*/
|
||||
List<ChatRecords> selectByUsersAndConditions(
|
||||
List<ChatRecords> selectChatWindowHistory(
|
||||
@Param("currentUserId") Long currentUserId,
|
||||
@Param("targetUserId") Long targetUserId,
|
||||
@Param("messageContent") String messageContent,
|
||||
@Param("startTime") LocalDateTime startTime,
|
||||
@Param("endTime") LocalDateTime endTime,
|
||||
@Param("messageType") Integer messageType
|
||||
@Param("targetUserId") Long targetUserId
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.bao.dating.service;
|
||||
|
||||
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.vo.ChatRecordsVO;
|
||||
|
||||
@@ -20,10 +19,11 @@ public interface ChatService {
|
||||
|
||||
/**
|
||||
* 获取聊天记录
|
||||
* @param currentUserId 发送方用户ID
|
||||
* @param targetUserId 接收方用户ID
|
||||
* @param queryDTO 查询参数
|
||||
* @return 聊天记录VO列表
|
||||
* @param currentUserId 当前用户ID
|
||||
* @param targetUserId 目标用户ID
|
||||
* @param page 页码
|
||||
* @param size 页大小
|
||||
* @return 聊天记录列表
|
||||
*/
|
||||
PageResult<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, ChatHistoryQueryDTO queryDTO);
|
||||
PageResult<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, Integer page, Integer size);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.bao.dating.service.impl;
|
||||
import com.bao.dating.common.result.PageResult;
|
||||
import com.bao.dating.mapper.ChatRecordsMapper;
|
||||
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.UserNicknameDTO;
|
||||
import com.bao.dating.pojo.entity.ChatRecords;
|
||||
@@ -20,7 +19,6 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -115,41 +113,24 @@ public class ChatServiceImpl implements ChatService {
|
||||
|
||||
/**
|
||||
* 获取聊天记录
|
||||
* @param currentUserId 当前用户ID
|
||||
* @param targetUserId 目标用户ID
|
||||
* @param queryDTO 查询参数
|
||||
* @return 聊天记录列表
|
||||
*/
|
||||
@Override
|
||||
public PageResult<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, ChatHistoryQueryDTO queryDTO) {
|
||||
Integer page = queryDTO.getPage();
|
||||
public PageResult<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, Integer page, Integer size) {
|
||||
|
||||
if (page == null || page < 1) {
|
||||
page = 1;
|
||||
}
|
||||
Integer size = queryDTO.getSize();
|
||||
if (size == null || size < 1 || size > 100) {
|
||||
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);
|
||||
|
||||
// 查询
|
||||
List<ChatRecords> recordsList = chatRecordsMapper.selectByUsersAndConditions(
|
||||
currentUserId,
|
||||
targetUserId,
|
||||
queryDTO.getMessageContent(),
|
||||
startTime,
|
||||
endTime,
|
||||
queryDTO.getMessageType()
|
||||
);
|
||||
List<ChatRecords> recordsList =
|
||||
chatRecordsMapper.selectChatWindowHistory(currentUserId, targetUserId);
|
||||
|
||||
// 使用 PageInfo 封装查询结果
|
||||
PageInfo<ChatRecords> pageInfo = new PageInfo<>(recordsList);
|
||||
|
||||
@@ -31,30 +31,19 @@
|
||||
)
|
||||
</insert>
|
||||
|
||||
<!-- 根据两个用户ID及可选条件查询聊天记录 (按发送时间倒序) - PageHelper 会自动处理分页 -->
|
||||
<select id="selectByUsersAndConditions" resultType="com.bao.dating.pojo.entity.ChatRecords">
|
||||
<!-- 根据两个用户ID查询聊天记录 (按发送时间倒序) - PageHelper 会自动处理分页 -->
|
||||
<select id="selectChatWindowHistory" resultType="com.bao.dating.pojo.entity.ChatRecords">
|
||||
SELECT
|
||||
chat_id, sender_user_id, receiver_user_id, message_content, message_type,
|
||||
read_status, read_time, send_time, message_status, created_at, updated_at
|
||||
FROM chat_records
|
||||
WHERE (
|
||||
WHERE
|
||||
message_status = 1
|
||||
AND (
|
||||
(sender_user_id = #{currentUserId} AND receiver_user_id = #{targetUserId})
|
||||
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 <= #{endTime}
|
||||
</if>
|
||||
<if test="messageType != null">
|
||||
AND message_type = #{messageType}
|
||||
</if>
|
||||
OR
|
||||
(sender_user_id = #{targetUserId} AND receiver_user_id = #{currentUserId})
|
||||
)
|
||||
ORDER BY send_time DESC
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user