完成会话列表查询
This commit is contained in:
@@ -7,6 +7,7 @@ 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.pojo.vo.ChatSessionsVO;
|
||||
import com.bao.dating.service.ChatService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -46,5 +47,14 @@ public class ChatController {
|
||||
return Result.success(ResultCode.SUCCESS, "获取聊天记录成功", history);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取会话列表
|
||||
* @return 会话列表
|
||||
*/
|
||||
@GetMapping("/sessions")
|
||||
public Result<List<ChatSessionsVO>> getSessionList() {
|
||||
Long currentUserId = UserContext.getUserId();
|
||||
List<ChatSessionsVO> list = chatService.getSessionList(currentUserId);
|
||||
return Result.success(ResultCode.SUCCESS, "获取会话列表成功", list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,11 @@ import com.bao.dating.anno.Log;
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserInfoDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import com.bao.dating.service.UserService;
|
||||
import io.jsonwebtoken.Jwt;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -91,7 +90,7 @@ public class UserController {
|
||||
*/
|
||||
@Log
|
||||
@PostMapping("/info/update")
|
||||
public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||
public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoDTO userInfoUpdateDTO) {
|
||||
Long userId = UserContext.getUserId();
|
||||
userInfoUpdateDTO.setUserId(userId);
|
||||
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
||||
|
||||
@@ -6,35 +6,24 @@ import com.bao.dating.pojo.entity.ChatSessions;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ChatSessionsMapper {
|
||||
|
||||
/**
|
||||
* 如果发送方不存在会话则创建
|
||||
* 如果发送方存在会话则更新,不存在则创建
|
||||
* @param chatSessions 会话
|
||||
* @return 影响行数
|
||||
*/
|
||||
int insertIfNotExistsForSender(ChatSessions chatSessions);
|
||||
int upsertSessionForSender(ChatSessions chatSessions);
|
||||
|
||||
/**
|
||||
* 如果接收方不存在会话则创建
|
||||
* 如果接收方存在会话则更新,不存在则创建
|
||||
* @param chatSessions 会话
|
||||
* @return 影响行数
|
||||
*/
|
||||
int insertIfNotExistsForReceiver(ChatSessions chatSessions);
|
||||
|
||||
/**
|
||||
* 更新发送方的会话信息
|
||||
* @param chatSessions 会话
|
||||
* @return 影响行数
|
||||
*/
|
||||
int updateSessionForSender(ChatSessions chatSessions);
|
||||
|
||||
/**
|
||||
* 更新接收方的会话信息
|
||||
* @param chatSessions 会话
|
||||
* @return 影响行数
|
||||
*/
|
||||
int updateSessionForReceiver(ChatSessions chatSessions);
|
||||
int upsertSessionForReceiver(ChatSessions chatSessions);
|
||||
|
||||
/**
|
||||
* 清空会话的未读数
|
||||
@@ -46,4 +35,11 @@ public interface ChatSessionsMapper {
|
||||
@Param("userId") Long userId,
|
||||
@Param("targetUserId") Long targetUserId
|
||||
);
|
||||
|
||||
/**
|
||||
* 查询当前用户的会话列表
|
||||
* @param userId 当前用户ID
|
||||
* @return 会话列表
|
||||
*/
|
||||
List<ChatSessions> selectSessionsByUserId(@Param("userId") Long userId);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserInfoDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@@ -31,6 +31,6 @@ public interface UserMapper {
|
||||
* 更新用户信息
|
||||
* @param userInfoUpdateDTO 用户信息更新参数
|
||||
*/
|
||||
void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||
void updateUserInfoByUserId(UserInfoDTO userInfoUpdateDTO);
|
||||
|
||||
}
|
||||
|
||||
@@ -8,11 +8,11 @@ import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户信息更新数据传输对象
|
||||
* 用户信息数据传输对象
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class UserInfoUpdateDTO implements Serializable {
|
||||
public class UserInfoDTO implements Serializable {
|
||||
private Long userId;
|
||||
private String userName;
|
||||
private String nickname;
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.bao.dating.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用户昵称数据传输对象
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class UserNicknameDTO {
|
||||
private Long userId;
|
||||
private String nickname;
|
||||
}
|
||||
@@ -13,20 +13,18 @@ public class ChatSessionsVO {
|
||||
|
||||
/** 会话ID */
|
||||
private Long sessionId;
|
||||
/** 当前用户ID */
|
||||
private Long userId;
|
||||
/** 对方用户ID */
|
||||
private Long targetUserId;
|
||||
/** 会话名称 */
|
||||
private String sessionName;
|
||||
/** 对方用户头像 */
|
||||
private String avatarUrl;
|
||||
/** 最后一条消息内容 */
|
||||
private String lastMessageContent;
|
||||
/** 最后一条消息时间 */
|
||||
private LocalDateTime lastMessageTime;
|
||||
/** 未读消息数量 */
|
||||
private Integer unreadCount;
|
||||
/** 会话状态 */
|
||||
private Integer sessionStatus;
|
||||
/** 置顶状态 */
|
||||
private Integer topStatus;
|
||||
/** 免打扰状态 */
|
||||
|
||||
@@ -4,6 +4,7 @@ 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;
|
||||
import com.bao.dating.pojo.vo.ChatSessionsVO;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@@ -35,4 +36,11 @@ public interface ChatService {
|
||||
* @param targetUserId 目标用户ID
|
||||
*/
|
||||
void markChatMessagesAsRead(Long currentUserId, Long targetUserId);
|
||||
|
||||
/**
|
||||
* 获取会话列表
|
||||
* @param currentUserId 当前用户ID
|
||||
* @return 会话列表
|
||||
*/
|
||||
List<ChatSessionsVO> getSessionList(Long currentUserId);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.bao.dating.service;
|
||||
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserInfoDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.dto.UserNicknameDTO;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -52,12 +51,12 @@ public interface UserService {
|
||||
* @param userInfoUpdateDTO 用户信息
|
||||
* @return 更新后的用户信息
|
||||
*/
|
||||
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||
UserInfoVO updateUserInfo(UserInfoDTO userInfoUpdateDTO);
|
||||
|
||||
/**
|
||||
* 根据用户ID查询用户昵称
|
||||
* 根据用户ID查询用户信息
|
||||
* @param userId 用户ID
|
||||
* @return 用户
|
||||
*/
|
||||
UserNicknameDTO getUserNicknameById(Long userId);
|
||||
UserInfoDTO getUserInfoById(Long userId);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
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.dto.UserInfoDTO;
|
||||
import com.bao.dating.pojo.entity.ChatRecords;
|
||||
import com.bao.dating.pojo.entity.ChatSessions;
|
||||
import com.bao.dating.pojo.vo.ChatRecordsVO;
|
||||
import com.bao.dating.pojo.vo.ChatSessionsVO;
|
||||
import com.bao.dating.service.ChatService;
|
||||
import com.bao.dating.service.UserService;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -32,7 +30,7 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ChatServiceImpl implements ChatService {
|
||||
public class ChatServiceImpl implements ChatService {
|
||||
@Autowired
|
||||
private ChatRecordsMapper chatRecordsMapper;
|
||||
|
||||
@@ -63,13 +61,13 @@ public class ChatServiceImpl implements ChatService {
|
||||
// 插入消息记录
|
||||
chatRecordsMapper.insert(record);
|
||||
|
||||
// 创建接收方会话
|
||||
// 创建发送方会话
|
||||
ChatSessions sessions = new ChatSessions();
|
||||
sessions.setUserId(senderUserId);
|
||||
sessions.setTargetUserId(dto.getReceiverUserId());
|
||||
// 获取接收方昵称作为发送方会话名称
|
||||
try {
|
||||
UserNicknameDTO receiverNicknameInfo = userService.getUserNicknameById(dto.getReceiverUserId());
|
||||
UserInfoDTO receiverNicknameInfo = userService.getUserInfoById(dto.getReceiverUserId());
|
||||
if (receiverNicknameInfo != null && receiverNicknameInfo.getNickname() != null) {
|
||||
sessions.setSessionName(receiverNicknameInfo.getNickname());
|
||||
} else {
|
||||
@@ -84,14 +82,13 @@ public class ChatServiceImpl implements ChatService {
|
||||
sessions.setLastMessageContent(record.getMessageContent());
|
||||
sessions.setLastMessageTime(record.getSendTime());
|
||||
|
||||
chatSessionsMapper.insertIfNotExistsForSender(sessions);
|
||||
chatSessionsMapper.updateSessionForSender(sessions);
|
||||
chatSessionsMapper.upsertSessionForSender(sessions);
|
||||
|
||||
// 创建接收方会话
|
||||
sessions.setUserId(dto.getReceiverUserId());
|
||||
sessions.setTargetUserId(senderUserId);
|
||||
try {
|
||||
UserNicknameDTO senderNicknameInfo = userService.getUserNicknameById(senderUserId);
|
||||
UserInfoDTO senderNicknameInfo = userService.getUserInfoById(senderUserId);
|
||||
if (senderNicknameInfo != null && senderNicknameInfo.getNickname() != null) {
|
||||
sessions.setSessionName(senderNicknameInfo.getNickname());
|
||||
} else {
|
||||
@@ -103,8 +100,8 @@ public class ChatServiceImpl implements ChatService {
|
||||
sessions.setSessionName("用户" + senderUserId);
|
||||
}
|
||||
|
||||
chatSessionsMapper.insertIfNotExistsForReceiver(sessions);
|
||||
chatSessionsMapper.updateSessionForReceiver(sessions);
|
||||
chatSessionsMapper.upsertSessionForReceiver(sessions);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -158,4 +155,28 @@ public class ChatServiceImpl implements ChatService {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话列表
|
||||
* @return 会话列表
|
||||
*/
|
||||
@Override
|
||||
public List<ChatSessionsVO> getSessionList(Long currentUserId) {
|
||||
List<ChatSessions> sessions = chatSessionsMapper.selectSessionsByUserId(currentUserId);
|
||||
|
||||
return sessions.stream().map(session -> {
|
||||
ChatSessionsVO vo = new ChatSessionsVO();
|
||||
BeanUtils.copyProperties(session, vo);
|
||||
|
||||
UserInfoDTO targetUser = userService.getUserInfoById(session.getTargetUserId());
|
||||
if (targetUser != null){
|
||||
vo.setSessionName(targetUser.getNickname());
|
||||
vo.setAvatarUrl(targetUser.getAvatarUrl());
|
||||
}else {
|
||||
vo.setSessionName("用户" + session.getTargetUserId());
|
||||
vo.setAvatarUrl(null);
|
||||
}
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,12 +5,10 @@ import com.bao.dating.common.aliyun.GreenImageScan;
|
||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
||||
import com.bao.dating.common.result.AliOssResult;
|
||||
import com.bao.dating.common.result.GreenAuditResult;
|
||||
import com.bao.dating.config.RedisConfig;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.mapper.UserMapper;
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserInfoDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.dto.UserNicknameDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
@@ -232,7 +230,7 @@ public class UserServiceImpl implements UserService {
|
||||
* @param userInfoUpdateDTO 用户信息更新参数
|
||||
*/
|
||||
@Override
|
||||
public UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||
public UserInfoVO updateUserInfo(UserInfoDTO userInfoUpdateDTO) {
|
||||
Long userId = userInfoUpdateDTO.getUserId();
|
||||
User user = userMapper.selectByUserId(userId);
|
||||
if (user == null) {
|
||||
@@ -314,19 +312,19 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID获取用户昵称
|
||||
* 根据用户ID获取用户昵称和头像
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 用户昵称
|
||||
*/
|
||||
@Override
|
||||
public UserNicknameDTO getUserNicknameById(Long userId) {
|
||||
public UserInfoDTO getUserInfoById(Long userId) {
|
||||
// 查询数据库获取昵称
|
||||
User user = userMapper.selectByUserId(userId);
|
||||
if (user == null) {
|
||||
throw new RuntimeException("没有此用户");
|
||||
}
|
||||
UserNicknameDTO dto = new UserNicknameDTO();
|
||||
UserInfoDTO dto = new UserInfoDTO();
|
||||
dto.setUserId(user.getUserId());
|
||||
dto.setNickname(user.getNickname());
|
||||
return dto;
|
||||
|
||||
@@ -3,61 +3,41 @@
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.bao.dating.mapper.ChatSessionsMapper">
|
||||
|
||||
<!-- 如果发送方不存在会话则创建 -->
|
||||
<insert id="insertIfNotExistsForSender" parameterType="com.bao.dating.pojo.entity.ChatSessions">
|
||||
<!-- 插入或更新发送方会话 -->
|
||||
<insert id="upsertSessionForSender" parameterType="com.bao.dating.pojo.entity.ChatSessions">
|
||||
INSERT INTO chat_sessions
|
||||
(user_id, target_user_id, session_name, last_message_id, last_message_content, last_message_time,
|
||||
unread_count, session_status, top_status, mute_status, created_at, updated_at)
|
||||
SELECT
|
||||
unread_count, session_status, top_status, mute_status, created_at, updated_at)
|
||||
VALUES
|
||||
(
|
||||
#{userId}, #{targetUserId}, #{sessionName}, #{lastMessageId}, #{lastMessageContent}, #{lastMessageTime},
|
||||
0, 1, 0, 0, NOW(), NOW()
|
||||
FROM DUAL
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM chat_sessions
|
||||
WHERE user_id = #{userId} AND target_user_id = #{targetUserId}
|
||||
);
|
||||
</insert>
|
||||
|
||||
<!-- 如果接收方不存在会话则创建 -->
|
||||
<insert id="insertIfNotExistsForReceiver" parameterType="com.bao.dating.pojo.entity.ChatSessions">
|
||||
INSERT INTO chat_sessions
|
||||
(user_id, target_user_id, session_name, last_message_id, last_message_content, last_message_time,
|
||||
unread_count, session_status, top_status, mute_status, created_at, updated_at)
|
||||
SELECT
|
||||
#{userId}, #{targetUserId}, #{sessionName}, #{lastMessageId}, #{lastMessageContent}, #{lastMessageTime},
|
||||
0, 1, 0, 0, NOW(), NOW()
|
||||
FROM DUAL
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM chat_sessions
|
||||
WHERE user_id = #{userId} AND target_user_id = #{targetUserId}
|
||||
)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
last_message_id = VALUES(last_message_id),
|
||||
last_message_content = VALUES(last_message_content),
|
||||
last_message_time = VALUES(last_message_time),
|
||||
unread_count = 0,
|
||||
updated_at = NOW();
|
||||
</insert>
|
||||
|
||||
<!-- 更新发送方的会话信息 -->
|
||||
<update id="updateSessionForSender" parameterType="com.bao.dating.pojo.entity.ChatSessions">
|
||||
UPDATE chat_sessions
|
||||
SET
|
||||
last_message_id = #{lastMessageId},
|
||||
last_message_content = #{lastMessageContent},
|
||||
last_message_time = #{lastMessageTime},
|
||||
unread_count = 0,
|
||||
updated_at = NOW()
|
||||
WHERE user_id = #{userId}
|
||||
AND target_user_id = #{targetUserId};
|
||||
</update>
|
||||
|
||||
<!-- 更新接收方的会话信息 -->
|
||||
<update id="updateSessionForReceiver" parameterType="com.bao.dating.pojo.entity.ChatSessions">
|
||||
UPDATE chat_sessions
|
||||
SET
|
||||
last_message_id = #{lastMessageId},
|
||||
last_message_content = #{lastMessageContent},
|
||||
last_message_time = #{lastMessageTime},
|
||||
unread_count = unread_count + 1,
|
||||
updated_at = NOW()
|
||||
WHERE user_id = #{userId}
|
||||
AND target_user_id = #{targetUserId}
|
||||
</update>
|
||||
<!-- 插入或更新接收方会话 -->
|
||||
<insert id="upsertSessionForReceiver" parameterType="com.bao.dating.pojo.entity.ChatSessions">
|
||||
INSERT INTO chat_sessions
|
||||
(user_id, target_user_id, session_name, last_message_id, last_message_content, last_message_time,
|
||||
unread_count, session_status, top_status, mute_status, created_at, updated_at)
|
||||
VALUES
|
||||
(
|
||||
#{userId}, #{targetUserId}, #{sessionName}, #{lastMessageId}, #{lastMessageContent}, #{lastMessageTime},
|
||||
1, 1, 0, 0, NOW(), NOW()
|
||||
)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
last_message_id = VALUES(last_message_id),
|
||||
last_message_content = VALUES(last_message_content),
|
||||
last_message_time = VALUES(last_message_time),
|
||||
unread_count = unread_count + 1,
|
||||
updated_at = NOW();
|
||||
</insert>
|
||||
|
||||
<!-- 清空未读消息数 -->
|
||||
<update id="clearUnreadCount">
|
||||
@@ -70,4 +50,14 @@
|
||||
AND target_user_id = #{targetUserId}
|
||||
AND session_status = 1
|
||||
</update>
|
||||
|
||||
<!-- 查询会话列表 -->
|
||||
<select id="selectSessionsByUserId" resultType="com.bao.dating.pojo.entity.ChatSessions">
|
||||
SELECT session_id, target_user_id, session_name, last_message_content,
|
||||
last_message_time, unread_count, top_status, mute_status
|
||||
FROM chat_sessions
|
||||
WHERE user_id = #{userId}
|
||||
AND session_status = 1
|
||||
ORDER BY top_status DESC, last_message_time DESC
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user