完成会话列表查询并校验会话状态
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
package com.bao.dating.common.result;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.vo.ChatRecordsVO;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 聊天发送结果
|
||||||
|
* @author KilLze
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class ChatSendResult {
|
||||||
|
private boolean success;
|
||||||
|
private String message;
|
||||||
|
private ChatRecordsVO record;
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import com.bao.dating.pojo.entity.ChatRecords;
|
|||||||
import com.bao.dating.pojo.entity.ChatSessions;
|
import com.bao.dating.pojo.entity.ChatSessions;
|
||||||
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 org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -18,6 +19,15 @@ public interface ChatSessionsMapper {
|
|||||||
*/
|
*/
|
||||||
int upsertSessionForSender(ChatSessions chatSessions);
|
int upsertSessionForSender(ChatSessions chatSessions);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取会话
|
||||||
|
* @param userId 用户ID
|
||||||
|
* @param targetUserId 目标用户ID
|
||||||
|
* @return 会话
|
||||||
|
*/
|
||||||
|
@Select("SELECT * FROM chat_sessions WHERE user_id = #{userId} AND target_user_id = #{targetUserId} LIMIT 1")
|
||||||
|
ChatSessions getSession(@Param("userId") Long userId, @Param("targetUserId") Long targetUserId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果接收方存在会话则更新,不存在则创建
|
* 如果接收方存在会话则更新,不存在则创建
|
||||||
* @param chatSessions 会话
|
* @param chatSessions 会话
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import java.util.stream.Collectors;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class ChatServiceImpl implements ChatService {
|
public class ChatServiceImpl implements ChatService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ChatRecordsMapper chatRecordsMapper;
|
private ChatRecordsMapper chatRecordsMapper;
|
||||||
|
|
||||||
@@ -49,6 +49,12 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public ChatRecordsVO createSession(Long senderUserId, ChatRecordSendDTO dto) {
|
public ChatRecordsVO createSession(Long senderUserId, ChatRecordSendDTO dto) {
|
||||||
|
|
||||||
|
ChatSessions session = chatSessionsMapper.getSession(senderUserId, dto.getReceiverUserId());
|
||||||
|
if (session != null && session.getSessionStatus() == 3) {
|
||||||
|
throw new RuntimeException("会话已删除,无法发送消息");
|
||||||
|
}
|
||||||
|
|
||||||
ChatRecords record = new ChatRecords();
|
ChatRecords record = new ChatRecords();
|
||||||
record.setSenderUserId(senderUserId);
|
record.setSenderUserId(senderUserId);
|
||||||
record.setReceiverUserId(dto.getReceiverUserId());
|
record.setReceiverUserId(dto.getReceiverUserId());
|
||||||
@@ -102,9 +108,6 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
|
|
||||||
chatSessionsMapper.upsertSessionForReceiver(sessions);
|
chatSessionsMapper.upsertSessionForReceiver(sessions);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 3. 返回 VO
|
// 3. 返回 VO
|
||||||
ChatRecordsVO vo = new ChatRecordsVO();
|
ChatRecordsVO vo = new ChatRecordsVO();
|
||||||
BeanUtils.copyProperties(record, vo);
|
BeanUtils.copyProperties(record, vo);
|
||||||
|
|||||||
@@ -58,6 +58,7 @@
|
|||||||
FROM chat_sessions
|
FROM chat_sessions
|
||||||
WHERE user_id = #{userId}
|
WHERE user_id = #{userId}
|
||||||
AND session_status = 1
|
AND session_status = 1
|
||||||
|
AND session_status in (1,2)
|
||||||
ORDER BY top_status DESC, last_message_time DESC
|
ORDER BY top_status DESC, last_message_time DESC
|
||||||
</select>
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user