优化代码
This commit is contained in:
@@ -5,6 +5,7 @@ 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.ChatCursorPageDTO;
|
||||
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;
|
||||
@@ -35,12 +36,13 @@ public class ChatController {
|
||||
@PathVariable Long targetUserId,
|
||||
ChatCursorPageDTO pageDTO){
|
||||
Long currentUserId = UserContext.getUserId();
|
||||
ChatHistoryQueryDTO queryDTO = new ChatHistoryQueryDTO();
|
||||
queryDTO.setCurrentUserId(currentUserId);
|
||||
queryDTO.setTargetUserId(targetUserId);
|
||||
queryDTO.setCursor(pageDTO.getCursor());
|
||||
queryDTO.setSize(pageDTO.getSize());
|
||||
chatService.markChatMessagesAsRead(currentUserId, targetUserId);
|
||||
List<ChatRecordsVO> history = chatService.getChatHistory(
|
||||
currentUserId,
|
||||
targetUserId,
|
||||
pageDTO.getCursor(),
|
||||
pageDTO.getSize());
|
||||
List<ChatRecordsVO> history = chatService.getChatHistory(queryDTO);
|
||||
return Result.success(ResultCode.SUCCESS, "获取聊天记录成功", history);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
|
||||
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
|
||||
import com.bao.dating.pojo.dto.ChatMarkReadDTO;
|
||||
import com.bao.dating.pojo.entity.ChatRecords;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@@ -19,29 +21,15 @@ public interface ChatRecordsMapper {
|
||||
|
||||
/**
|
||||
* 根据时间游标查询聊天记录
|
||||
* @param currentUserId 当前用户ID
|
||||
* @param targetUserId 目标用户ID
|
||||
* @param cursor 时间游标
|
||||
* @param size 页大小
|
||||
* @param queryDTO 查询参数
|
||||
* @return 聊天记录列表
|
||||
*/
|
||||
List<ChatRecords> selectChatHistoryByCursor(
|
||||
@Param("currentUserId") Long currentUserId,
|
||||
@Param("targetUserId") Long targetUserId,
|
||||
@Param("cursor") LocalDateTime cursor,
|
||||
@Param("size") Integer size
|
||||
);
|
||||
List<ChatRecords> selectChatHistoryByCursor(ChatHistoryQueryDTO queryDTO);
|
||||
|
||||
/**
|
||||
* 标记聊天记录为已读
|
||||
* @param senderUserId 发送方用户ID
|
||||
* @param receiverUserId 接收方用户ID
|
||||
* @param readTime 已读时间
|
||||
* @param markReadDTO 标记参数
|
||||
* @return 影响行数
|
||||
*/
|
||||
int markMessagesAsRead(
|
||||
@Param("senderUserId") Long senderUserId,
|
||||
@Param("receiverUserId") Long receiverUserId,
|
||||
@Param("readTime") LocalDateTime readTime
|
||||
);
|
||||
int markMessagesAsRead(ChatMarkReadDTO markReadDTO);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@ import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 聊天记录分页参数
|
||||
* @author lenovo
|
||||
*/
|
||||
@Data
|
||||
public class ChatCursorPageDTO {
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.bao.dating.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 聊天记录查询参数
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class ChatHistoryQueryDTO {
|
||||
private Long currentUserId;
|
||||
private Long targetUserId;
|
||||
private LocalDateTime cursor;
|
||||
private Integer size;
|
||||
}
|
||||
16
src/main/java/com/bao/dating/pojo/dto/ChatMarkReadDTO.java
Normal file
16
src/main/java/com/bao/dating/pojo/dto/ChatMarkReadDTO.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.bao.dating.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 聊天记录已读参数
|
||||
* @author lenovo
|
||||
*/
|
||||
@Data
|
||||
public class ChatMarkReadDTO {
|
||||
private Long senderUserId;
|
||||
private Long receiverUserId;
|
||||
private LocalDateTime readTime;
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.bao.dating.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 创建会话数据传输对象
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class ChatSessionCreateDTO {
|
||||
|
||||
/** 所属用户ID */
|
||||
private Long userId;
|
||||
|
||||
/** 目标用户ID */
|
||||
private Long targetUserId;
|
||||
|
||||
/** 会话名称 */
|
||||
private String sessionName;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.bao.dating.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 会话更新数据传输对象
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class ChatSessionUpdateDTO {
|
||||
/** 会话ID */
|
||||
private Long sessionId;
|
||||
/** 最后一条消息ID (关联chat_records.chat_id) */
|
||||
private Long lastMessageId;
|
||||
/** 最后一条消息内容 */
|
||||
private String lastMessageContent;
|
||||
/** 最后一条消息时间 */
|
||||
private LocalDateTime lastMessageTime;
|
||||
/** 未读消息数量 */
|
||||
private Integer unreadCount;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
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;
|
||||
|
||||
@@ -22,13 +23,10 @@ public interface ChatService {
|
||||
|
||||
/**
|
||||
* 获取聊天记录
|
||||
* @param currentUserId 当前用户ID
|
||||
* @param targetUserId 目标用户ID
|
||||
* @param cursor 时间游标
|
||||
* @param size 页大小
|
||||
* @param dto 查询参数
|
||||
* @return 聊天记录列表
|
||||
*/
|
||||
List<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, LocalDateTime cursor, Integer size);
|
||||
List<ChatRecordsVO> getChatHistory(ChatHistoryQueryDTO dto);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,8 @@ 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.ChatMarkReadDTO;
|
||||
import com.bao.dating.pojo.dto.ChatRecordSendDTO;
|
||||
import com.bao.dating.pojo.dto.UserNicknameDTO;
|
||||
import com.bao.dating.pojo.entity.ChatRecords;
|
||||
@@ -117,21 +119,17 @@ public class ChatServiceImpl implements ChatService {
|
||||
* @return 聊天记录列表
|
||||
*/
|
||||
@Override
|
||||
public List<ChatRecordsVO> getChatHistory(Long currentUserId, Long targetUserId, LocalDateTime cursor, Integer size) {
|
||||
public List<ChatRecordsVO> getChatHistory(ChatHistoryQueryDTO dto) {
|
||||
|
||||
Integer size = dto.getSize();
|
||||
if (size == null || size < 1 || size > 100) {
|
||||
size = 50;
|
||||
dto.setSize(50);
|
||||
}
|
||||
|
||||
// 查询
|
||||
List<ChatRecords> records =
|
||||
chatRecordsMapper.selectChatHistoryByCursor(
|
||||
currentUserId,
|
||||
targetUserId,
|
||||
cursor,
|
||||
size
|
||||
);
|
||||
// 查询聊天记录
|
||||
List<ChatRecords> records = chatRecordsMapper.selectChatHistoryByCursor(dto);
|
||||
|
||||
// 倒序
|
||||
Collections.reverse(records);
|
||||
|
||||
return records.stream().map(record -> {
|
||||
@@ -144,15 +142,16 @@ public class ChatServiceImpl implements ChatService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void markChatMessagesAsRead(Long currentUserId, Long targetUserId) {
|
||||
// 更新 chat_records:把对方发给我的未读消息设为已读
|
||||
ChatMarkReadDTO markReadDTO = new ChatMarkReadDTO();
|
||||
// 因为读的是对方发给我的消息所以要把填入的参数反过来
|
||||
markReadDTO.setSenderUserId(targetUserId);
|
||||
markReadDTO.setReceiverUserId(currentUserId);
|
||||
markReadDTO.setReadTime(LocalDateTime.now());
|
||||
|
||||
// 1. 更新 chat_records:把对方发给我的未读消息设为已读
|
||||
chatRecordsMapper.markMessagesAsRead(
|
||||
targetUserId,
|
||||
currentUserId,
|
||||
LocalDateTime.now()
|
||||
);
|
||||
chatRecordsMapper.markMessagesAsRead(markReadDTO);
|
||||
|
||||
// 2. 更新 chat_sessions:清空当前会话的未读数
|
||||
// 更新 chat_sessions:清空当前会话的未读数
|
||||
chatSessionsMapper.clearUnreadCount(
|
||||
currentUserId,
|
||||
targetUserId
|
||||
|
||||
Reference in New Issue
Block a user