feat: 完善后端架构和service层实现
- 创建完整的entity实体类体系,包括所有业务实体 - 实现BaseEntity基类,统一管理公共字段 - 创建雪花算法ID生成器和自动填充处理器 - 简化所有mapper接口,只继承BaseMapper - 重构service层,使用LambdaQueryWrapper进行数据库操作 - 创建BasePageRequest分页查询基类 - 完善用户上下文管理和JWT认证 - 新增WebSocket聊天功能和相关控制器 - 更新前端配置和组件,完善用户认证流程 - 同步数据库建表脚本
This commit is contained in:
@@ -26,6 +26,7 @@ public class AiService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AiService.class);
|
||||
|
||||
|
||||
private String cozeApiToken = "your-coze-api-token";
|
||||
private String cozeBaseUrl = "https://api.coze.cn";
|
||||
private String botId = "7523042446285439016";
|
||||
|
||||
@@ -1,149 +1,88 @@
|
||||
package com.emotion.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.emotion.common.BasePageRequest;
|
||||
import com.emotion.entity.Conversation;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 对话服务
|
||||
*
|
||||
* 会话服务接口
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-22
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Service
|
||||
public class ConversationService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ConversationService.class);
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
public interface ConversationService extends IService<Conversation> {
|
||||
|
||||
/**
|
||||
* 对话行映射器
|
||||
* 分页查询会话
|
||||
*/
|
||||
private static class ConversationRowMapper implements RowMapper<Conversation> {
|
||||
@Override
|
||||
public Conversation mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Conversation conversation = new Conversation();
|
||||
conversation.setId(rs.getString("id"));
|
||||
conversation.setUserId(rs.getString("user_id"));
|
||||
conversation.setTitle(rs.getString("title"));
|
||||
conversation.setType(rs.getString("type"));
|
||||
conversation.setStartTime(rs.getTimestamp("start_time") != null ?
|
||||
rs.getTimestamp("start_time").toLocalDateTime() : null);
|
||||
conversation.setEndTime(rs.getTimestamp("end_time") != null ?
|
||||
rs.getTimestamp("end_time").toLocalDateTime() : null);
|
||||
conversation.setMessageCount(rs.getInt("message_count"));
|
||||
conversation.setStatus(rs.getInt("status"));
|
||||
conversation.setClientIp(rs.getString("client_ip"));
|
||||
conversation.setUserAgent(rs.getString("user_agent"));
|
||||
conversation.setCozeConversationId(rs.getString("coze_conversation_id"));
|
||||
conversation.setCreateTime(rs.getTimestamp("create_time") != null ?
|
||||
rs.getTimestamp("create_time").toLocalDateTime() : null);
|
||||
conversation.setUpdateTime(rs.getTimestamp("update_time") != null ?
|
||||
rs.getTimestamp("update_time").toLocalDateTime() : null);
|
||||
return conversation;
|
||||
}
|
||||
}
|
||||
IPage<Conversation> getPage(BasePageRequest request);
|
||||
|
||||
/**
|
||||
* 创建对话
|
||||
* 根据用户ID分页查询会话
|
||||
*/
|
||||
public Conversation createConversation(String userId, String title, String type, String clientIp) {
|
||||
try {
|
||||
Conversation conversation = new Conversation();
|
||||
conversation.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||
conversation.setUserId(userId);
|
||||
conversation.setTitle(title != null ? title : "新对话");
|
||||
conversation.setType(type != null ? type : "user");
|
||||
conversation.setStartTime(LocalDateTime.now());
|
||||
conversation.setMessageCount(0);
|
||||
conversation.setStatus(1);
|
||||
conversation.setClientIp(clientIp);
|
||||
conversation.setCreateTime(LocalDateTime.now());
|
||||
conversation.setUpdateTime(LocalDateTime.now());
|
||||
conversation.setIsDeleted(0);
|
||||
|
||||
String sql = "INSERT INTO conversation (id, user_id, title, type, start_time, " +
|
||||
"message_count, status, client_ip, user_agent, create_time, update_time, is_deleted) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
jdbcTemplate.update(sql,
|
||||
conversation.getId(), conversation.getUserId(), conversation.getTitle(),
|
||||
conversation.getType(), conversation.getStartTime(), conversation.getMessageCount(),
|
||||
conversation.getStatus(), conversation.getClientIp(), conversation.getUserAgent(),
|
||||
conversation.getCreateTime(), conversation.getUpdateTime(), conversation.getIsDeleted());
|
||||
|
||||
log.info("对话创建成功: {}", conversation.getId());
|
||||
return conversation;
|
||||
} catch (Exception e) {
|
||||
log.error("创建对话失败: {}", e.getMessage());
|
||||
throw new RuntimeException("创建对话失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
IPage<Conversation> getPageByUserId(BasePageRequest request, String userId);
|
||||
|
||||
/**
|
||||
* 根据ID查询对话
|
||||
* 根据用户ID查询会话列表
|
||||
*/
|
||||
public Conversation findById(String id) {
|
||||
try {
|
||||
String sql = "SELECT * FROM conversation WHERE id = ? AND is_deleted = 0";
|
||||
List<Conversation> conversations = jdbcTemplate.query(sql, new ConversationRowMapper(), id);
|
||||
return conversations.isEmpty() ? null : conversations.get(0);
|
||||
} catch (Exception e) {
|
||||
log.error("根据ID查询对话失败: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
List<Conversation> getByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 根据用户ID查询对话列表
|
||||
* 根据用户ID查询活跃会话列表
|
||||
*/
|
||||
public List<Conversation> findByUserId(String userId) {
|
||||
try {
|
||||
String sql = "SELECT * FROM conversation WHERE user_id = ? AND is_deleted = 0 ORDER BY create_time DESC";
|
||||
return jdbcTemplate.query(sql, new ConversationRowMapper(), userId);
|
||||
} catch (Exception e) {
|
||||
log.error("根据用户ID查询对话列表失败: {}", e.getMessage());
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
List<Conversation> getActiveByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 更新消息数量
|
||||
* 根据Coze会话ID查询会话
|
||||
*/
|
||||
public boolean updateMessageCount(String conversationId, int messageCount) {
|
||||
try {
|
||||
String sql = "UPDATE conversation SET message_count = ?, update_time = ? WHERE id = ? AND is_deleted = 0";
|
||||
int rows = jdbcTemplate.update(sql, messageCount, LocalDateTime.now(), conversationId);
|
||||
return rows > 0;
|
||||
} catch (Exception e) {
|
||||
log.error("更新消息数量失败: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Conversation getByCozeConversationId(String cozeConversationId);
|
||||
|
||||
/**
|
||||
* 结束对话
|
||||
* 更新会话消息数量
|
||||
*/
|
||||
public boolean endConversation(String conversationId) {
|
||||
try {
|
||||
String sql = "UPDATE conversation SET status = 0, end_time = ?, update_time = ? WHERE id = ? AND is_deleted = 0";
|
||||
int rows = jdbcTemplate.update(sql, LocalDateTime.now(), LocalDateTime.now(), conversationId);
|
||||
return rows > 0;
|
||||
} catch (Exception e) {
|
||||
log.error("结束对话失败: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean updateMessageCount(String conversationId, Integer messageCount);
|
||||
|
||||
/**
|
||||
* 更新会话状态
|
||||
*/
|
||||
boolean updateStatus(String conversationId, Integer status);
|
||||
|
||||
/**
|
||||
* 更新会话结束时间
|
||||
*/
|
||||
boolean updateEndTime(String conversationId, LocalDateTime endTime);
|
||||
|
||||
/**
|
||||
* 统计用户的会话数量
|
||||
*/
|
||||
Long countByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 统计用户的活跃会话数量
|
||||
*/
|
||||
Long countActiveByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 查询需要归档的会话(超过指定天数未活跃)
|
||||
*/
|
||||
List<Conversation> getForArchive(Integer days);
|
||||
|
||||
/**
|
||||
* 批量归档会话
|
||||
*/
|
||||
boolean batchArchive(List<String> conversationIds);
|
||||
|
||||
/**
|
||||
* 创建会话
|
||||
*/
|
||||
Conversation createConversation(String userId, String title, String cozeConversationId);
|
||||
|
||||
/**
|
||||
* 结束会话
|
||||
*/
|
||||
boolean endConversation(String conversationId);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.emotion.service;
|
||||
|
||||
import com.emotion.entity.Message;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AI服务接口
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
public interface IAiService {
|
||||
|
||||
/**
|
||||
* 发送聊天消息到AI
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @param message 用户消息
|
||||
* @param userId 用户ID
|
||||
* @return AI回复内容
|
||||
*/
|
||||
String sendChatMessage(String conversationId, String message, String userId);
|
||||
|
||||
/**
|
||||
* 根据聊天记录生成对话总结
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @param userId 用户ID
|
||||
* @return 对话总结
|
||||
*/
|
||||
String generateConversationSummary(String conversationId, String userId);
|
||||
|
||||
/**
|
||||
* 根据消息列表生成总结
|
||||
*
|
||||
* @param messages 消息列表
|
||||
* @param userId 用户ID
|
||||
* @return 对话总结
|
||||
*/
|
||||
String generateSummaryFromRecords(List<Message> messages, String userId);
|
||||
|
||||
/**
|
||||
* 检查AI服务是否可用
|
||||
*
|
||||
* @return 是否可用
|
||||
*/
|
||||
boolean isServiceAvailable();
|
||||
|
||||
/**
|
||||
* 获取AI服务状态信息
|
||||
*
|
||||
* @return 状态信息
|
||||
*/
|
||||
String getServiceStatus();
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.emotion.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.emotion.entity.Conversation;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 会话服务接口
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
public interface IConversationService extends IService<Conversation> {
|
||||
|
||||
/**
|
||||
* 创建新会话
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param title 会话标题
|
||||
* @param type 会话类型
|
||||
* @return 会话信息
|
||||
*/
|
||||
Conversation createConversation(String userId, String title, String type);
|
||||
|
||||
/**
|
||||
* 根据用户ID分页查询会话列表
|
||||
*
|
||||
* @param page 分页参数
|
||||
* @param userId 用户ID
|
||||
* @return 会话分页数据
|
||||
*/
|
||||
IPage<Conversation> getByUserId(Page<Conversation> page, String userId);
|
||||
|
||||
/**
|
||||
* 根据用户ID查询活跃会话列表
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 活跃会话列表
|
||||
*/
|
||||
List<Conversation> getActiveByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 根据Coze会话ID查询会话
|
||||
*
|
||||
* @param cozeConversationId Coze会话ID
|
||||
* @return 会话信息
|
||||
*/
|
||||
Conversation getByCozeConversationId(String cozeConversationId);
|
||||
|
||||
/**
|
||||
* 更新会话消息数量
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @param messageCount 消息数量
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean updateMessageCount(String conversationId, Integer messageCount);
|
||||
|
||||
/**
|
||||
* 更新会话状态
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @param status 状态
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean updateStatus(String conversationId, Integer status);
|
||||
|
||||
/**
|
||||
* 结束会话
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean endConversation(String conversationId);
|
||||
|
||||
/**
|
||||
* 统计用户的会话数量
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 会话数量
|
||||
*/
|
||||
Long countByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 统计用户的活跃会话数量
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 活跃会话数量
|
||||
*/
|
||||
Long countActiveByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 归档超时会话
|
||||
*
|
||||
* @param days 超时天数
|
||||
* @return 归档的会话数量
|
||||
*/
|
||||
int archiveTimeoutConversations(Integer days);
|
||||
|
||||
/**
|
||||
* 批量归档会话
|
||||
*
|
||||
* @param conversationIds 会话ID列表
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean batchArchive(List<String> conversationIds);
|
||||
|
||||
/**
|
||||
* 获取或创建会话
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param cozeConversationId Coze会话ID
|
||||
* @param title 会话标题
|
||||
* @return 会话信息
|
||||
*/
|
||||
Conversation getOrCreateConversation(String userId, String cozeConversationId, String title);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package com.emotion.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.emotion.entity.CozeApiCall;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Coze API调用记录服务接口
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
public interface ICozeApiCallService extends IService<CozeApiCall> {
|
||||
|
||||
/**
|
||||
* 根据会话ID分页查询API调用记录
|
||||
*/
|
||||
IPage<CozeApiCall> getByConversationId(Page<CozeApiCall> page, String conversationId);
|
||||
|
||||
/**
|
||||
* 根据用户ID分页查询API调用记录
|
||||
*/
|
||||
IPage<CozeApiCall> getByUserId(Page<CozeApiCall> page, String userId);
|
||||
|
||||
/**
|
||||
* 根据Bot ID查询API调用记录
|
||||
*/
|
||||
List<CozeApiCall> getByBotId(String botId);
|
||||
|
||||
/**
|
||||
* 根据状态查询API调用记录
|
||||
*/
|
||||
List<CozeApiCall> getByStatus(String status);
|
||||
|
||||
/**
|
||||
* 根据请求类型查询API调用记录
|
||||
*/
|
||||
List<CozeApiCall> getByRequestType(String requestType);
|
||||
|
||||
/**
|
||||
* 根据时间范围查询API调用记录
|
||||
*/
|
||||
List<CozeApiCall> getByTimeRange(LocalDateTime startTime, LocalDateTime endTime);
|
||||
|
||||
/**
|
||||
* 统计用户的API调用次数
|
||||
*/
|
||||
Long countByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 统计Bot的API调用次数
|
||||
*/
|
||||
Long countByBotId(String botId);
|
||||
|
||||
/**
|
||||
* 统计指定状态的API调用次数
|
||||
*/
|
||||
Long countByStatus(String status);
|
||||
|
||||
/**
|
||||
* 统计用户的Token使用量
|
||||
*/
|
||||
Long sumTokensByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 统计用户的API调用费用
|
||||
*/
|
||||
BigDecimal sumCostByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 查询失败的API调用记录
|
||||
*/
|
||||
List<CozeApiCall> getFailedCalls();
|
||||
|
||||
/**
|
||||
* 查询超时的API调用记录
|
||||
*/
|
||||
List<CozeApiCall> getTimeoutCalls();
|
||||
|
||||
/**
|
||||
* 根据追踪ID查询API调用记录
|
||||
*/
|
||||
CozeApiCall getByTraceId(String traceId);
|
||||
|
||||
/**
|
||||
* 根据会话ID和请求类型查询API调用记录
|
||||
*/
|
||||
List<CozeApiCall> getByConversationIdAndRequestType(String conversationId, String requestType);
|
||||
|
||||
/**
|
||||
* 更新API调用状态
|
||||
*/
|
||||
boolean updateStatus(String id, String status, String finalStatus, LocalDateTime endTime);
|
||||
|
||||
/**
|
||||
* 更新API调用结果
|
||||
*/
|
||||
boolean updateResult(String id, Integer responseStatus, String responseBody, String aiReply,
|
||||
Integer totalTokens, BigDecimal cost, String status, String finalStatus,
|
||||
LocalDateTime endTime);
|
||||
|
||||
/**
|
||||
* 创建API调用记录
|
||||
*/
|
||||
CozeApiCall createApiCall(String conversationId, String messageId, String userId,
|
||||
String requestType, String requestUrl, String requestBody);
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.emotion.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.emotion.entity.Message;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 消息服务接口
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
public interface IMessageService extends IService<Message> {
|
||||
|
||||
/**
|
||||
* 保存消息
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @param content 消息内容
|
||||
* @param type 消息类型
|
||||
* @param sender 发送者
|
||||
* @return 消息
|
||||
*/
|
||||
Message saveMessage(String conversationId, String content, String type, String sender);
|
||||
|
||||
/**
|
||||
* 根据会话ID分页查询消息
|
||||
*
|
||||
* @param page 分页参数
|
||||
* @param conversationId 会话ID
|
||||
* @return 消息分页数据
|
||||
*/
|
||||
IPage<Message> getByConversationId(Page<Message> page, String conversationId);
|
||||
|
||||
/**
|
||||
* 根据发送者分页查询消息
|
||||
*
|
||||
* @param page 分页参数
|
||||
* @param sender 发送者
|
||||
* @return 消息分页数据
|
||||
*/
|
||||
IPage<Message> getBySender(Page<Message> page, String sender);
|
||||
|
||||
/**
|
||||
* 根据会话ID查询消息列表(用于总结)
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @param limit 限制数量
|
||||
* @return 消息列表
|
||||
*/
|
||||
List<Message> getByConversationIdForSummary(String conversationId, Integer limit);
|
||||
|
||||
/**
|
||||
* 根据时间范围查询消息
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 消息列表
|
||||
*/
|
||||
List<Message> getByTimeRange(String conversationId, LocalDateTime startTime, LocalDateTime endTime);
|
||||
|
||||
/**
|
||||
* 统计会话的消息数量
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @return 消息数量
|
||||
*/
|
||||
Long countByConversationId(String conversationId);
|
||||
|
||||
/**
|
||||
* 统计发送者的消息数量
|
||||
*
|
||||
* @param sender 发送者
|
||||
* @return 消息数量
|
||||
*/
|
||||
Long countBySender(String sender);
|
||||
|
||||
/**
|
||||
* 查询会话的最后一条消息
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @return 最后一条消息
|
||||
*/
|
||||
Message getLastMessageByConversationId(String conversationId);
|
||||
|
||||
/**
|
||||
* 根据发送者查询消息
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @param sender 发送者
|
||||
* @return 消息列表
|
||||
*/
|
||||
List<Message> getByConversationIdAndSender(String conversationId, String sender);
|
||||
|
||||
/**
|
||||
* 更新消息状态
|
||||
*
|
||||
* @param messageId 消息ID
|
||||
* @param status 状态
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean updateStatus(String messageId, String status);
|
||||
|
||||
/**
|
||||
* 更新消息已读状态
|
||||
*
|
||||
* @param messageId 消息ID
|
||||
* @param isRead 是否已读
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean updateReadStatus(String messageId, Integer isRead);
|
||||
|
||||
/**
|
||||
* 批量更新会话消息为已读
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean markConversationMessagesAsRead(String conversationId);
|
||||
|
||||
/**
|
||||
* 批量保存消息
|
||||
*
|
||||
* @param messages 消息列表
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean saveBatch(List<Message> messages);
|
||||
|
||||
/**
|
||||
* 删除会话的所有消息
|
||||
*
|
||||
* @param conversationId 会话ID
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean deleteByConversationId(String conversationId);
|
||||
}
|
||||
@@ -1,165 +1,94 @@
|
||||
package com.emotion.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.emotion.common.BasePageRequest;
|
||||
import com.emotion.entity.Message;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 消息服务
|
||||
* 消息服务接口
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-22
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Service
|
||||
public class MessageService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MessageService.class);
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
private ConversationService conversationService;
|
||||
|
||||
public interface MessageService extends IService<Message> {
|
||||
|
||||
/**
|
||||
* 消息行映射器
|
||||
* 分页查询消息
|
||||
*/
|
||||
private static class MessageRowMapper implements RowMapper<Message> {
|
||||
@Override
|
||||
public Message mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Message message = new Message();
|
||||
message.setId(rs.getString("id"));
|
||||
message.setConversationId(rs.getString("conversation_id"));
|
||||
message.setUserId(rs.getString("user_id"));
|
||||
message.setContent(rs.getString("content"));
|
||||
message.setContentType(rs.getString("content_type"));
|
||||
message.setSenderType(rs.getString("sender_type"));
|
||||
message.setSenderId(rs.getString("sender_id"));
|
||||
message.setStatus(rs.getString("status"));
|
||||
message.setSendTime(rs.getTimestamp("send_time") != null ?
|
||||
rs.getTimestamp("send_time").toLocalDateTime() : null);
|
||||
message.setIsRead(rs.getInt("is_read"));
|
||||
message.setParentMessageId(rs.getString("parent_message_id"));
|
||||
message.setCozeRole(rs.getString("coze_role"));
|
||||
message.setCozeMessageId(rs.getString("coze_message_id"));
|
||||
message.setErrorMessage(rs.getString("error_message"));
|
||||
message.setRetryCount(rs.getInt("retry_count"));
|
||||
message.setCreateTime(rs.getTimestamp("create_time") != null ?
|
||||
rs.getTimestamp("create_time").toLocalDateTime() : null);
|
||||
message.setUpdateTime(rs.getTimestamp("update_time") != null ?
|
||||
rs.getTimestamp("update_time").toLocalDateTime() : null);
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
IPage<Message> getPage(BasePageRequest request);
|
||||
|
||||
/**
|
||||
* 根据会话ID分页查询消息
|
||||
*/
|
||||
IPage<Message> getPageByConversationId(BasePageRequest request, String conversationId);
|
||||
|
||||
/**
|
||||
* 根据会话ID查询消息列表
|
||||
*/
|
||||
List<Message> getByConversationId(String conversationId);
|
||||
|
||||
/**
|
||||
* 根据发送者查询消息列表
|
||||
*/
|
||||
List<Message> getBySender(String sender);
|
||||
|
||||
/**
|
||||
* 根据时间范围查询消息
|
||||
*/
|
||||
List<Message> getByTimeRange(String conversationId, LocalDateTime startTime, LocalDateTime endTime);
|
||||
|
||||
/**
|
||||
* 查询会话的最后一条消息
|
||||
*/
|
||||
Message getLastMessageByConversationId(String conversationId);
|
||||
|
||||
/**
|
||||
* 根据父消息ID查询回复消息
|
||||
*/
|
||||
List<Message> getRepliesByParentId(String parentMessageId);
|
||||
|
||||
/**
|
||||
* 统计会话的消息数量
|
||||
*/
|
||||
Long countByConversationId(String conversationId);
|
||||
|
||||
/**
|
||||
* 统计发送者的消息数量
|
||||
*/
|
||||
Long countBySender(String sender);
|
||||
|
||||
/**
|
||||
* 查询未读消息数量
|
||||
*/
|
||||
Long countUnreadMessages(String conversationId);
|
||||
|
||||
/**
|
||||
* 更新消息状态
|
||||
*/
|
||||
boolean updateStatus(String messageId, String status);
|
||||
|
||||
/**
|
||||
* 更新消息已读状态
|
||||
*/
|
||||
boolean updateReadStatus(String messageId, Integer isRead);
|
||||
|
||||
/**
|
||||
* 批量更新会话消息为已读
|
||||
*/
|
||||
boolean markConversationMessagesAsRead(String conversationId);
|
||||
|
||||
/**
|
||||
* 创建消息
|
||||
*/
|
||||
public Message createMessage(String conversationId, String userId, String content,
|
||||
String senderType, String senderId) {
|
||||
try {
|
||||
Message message = new Message();
|
||||
message.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||
message.setConversationId(conversationId);
|
||||
message.setUserId(userId);
|
||||
message.setContent(content);
|
||||
message.setContentType("text");
|
||||
message.setSenderType(senderType);
|
||||
message.setSenderId(senderId);
|
||||
message.setStatus("sent");
|
||||
message.setSendTime(LocalDateTime.now());
|
||||
message.setIsRead(0);
|
||||
message.setRetryCount(0);
|
||||
message.setCreateTime(LocalDateTime.now());
|
||||
message.setUpdateTime(LocalDateTime.now());
|
||||
message.setIsDeleted(0);
|
||||
|
||||
String sql = "INSERT INTO message (id, conversation_id, user_id, content, content_type, " +
|
||||
"sender_type, sender_id, status, send_time, is_read, retry_count, " +
|
||||
"create_time, update_time, is_deleted) VALUES " +
|
||||
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
jdbcTemplate.update(sql,
|
||||
message.getId(), message.getConversationId(), message.getUserId(),
|
||||
message.getContent(), message.getContentType(), message.getSenderType(),
|
||||
message.getSenderId(), message.getStatus(), message.getSendTime(),
|
||||
message.getIsRead(), message.getRetryCount(), message.getCreateTime(),
|
||||
message.getUpdateTime(), message.getIsDeleted());
|
||||
|
||||
// 更新对话的消息数量
|
||||
updateConversationMessageCount(conversationId);
|
||||
|
||||
log.info("消息创建成功: {}", message.getId());
|
||||
return message;
|
||||
} catch (Exception e) {
|
||||
log.error("创建消息失败: {}", e.getMessage());
|
||||
throw new RuntimeException("创建消息失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据对话ID查询消息列表
|
||||
*/
|
||||
public List<Message> findByConversationId(String conversationId) {
|
||||
try {
|
||||
String sql = "SELECT * FROM message WHERE conversation_id = ? AND is_deleted = 0 ORDER BY send_time ASC";
|
||||
return jdbcTemplate.query(sql, new MessageRowMapper(), conversationId);
|
||||
} catch (Exception e) {
|
||||
log.error("根据对话ID查询消息列表失败: {}", e.getMessage());
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID查询消息
|
||||
*/
|
||||
public Message findById(String id) {
|
||||
try {
|
||||
String sql = "SELECT * FROM message WHERE id = ? AND is_deleted = 0";
|
||||
List<Message> messages = jdbcTemplate.query(sql, new MessageRowMapper(), id);
|
||||
return messages.isEmpty() ? null : messages.get(0);
|
||||
} catch (Exception e) {
|
||||
log.error("根据ID查询消息失败: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Message createMessage(String conversationId, String userId, String content,
|
||||
String contentType, String senderType, String senderId);
|
||||
|
||||
/**
|
||||
* 标记消息为已读
|
||||
*/
|
||||
public boolean markAsRead(String messageId) {
|
||||
try {
|
||||
String sql = "UPDATE message SET is_read = 1, update_time = ? WHERE id = ? AND is_deleted = 0";
|
||||
int rows = jdbcTemplate.update(sql, LocalDateTime.now(), messageId);
|
||||
return rows > 0;
|
||||
} catch (Exception e) {
|
||||
log.error("标记消息为已读失败: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新对话的消息数量
|
||||
*/
|
||||
private void updateConversationMessageCount(String conversationId) {
|
||||
try {
|
||||
String sql = "SELECT COUNT(*) FROM message WHERE conversation_id = ? AND is_deleted = 0";
|
||||
Integer count = jdbcTemplate.queryForObject(sql, Integer.class, conversationId);
|
||||
if (count != null) {
|
||||
conversationService.updateMessageCount(conversationId, count);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("更新对话消息数量失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
boolean markAsRead(String messageId);
|
||||
}
|
||||
|
||||
@@ -1,193 +1,108 @@
|
||||
package com.emotion.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.emotion.common.BasePageRequest;
|
||||
import com.emotion.entity.User;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 用户服务
|
||||
* 用户服务接口
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-22
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(UserService.class);
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
public interface UserService extends IService<User> {
|
||||
|
||||
/**
|
||||
* 用户行映射器
|
||||
* 分页查询用户
|
||||
*/
|
||||
private static class UserRowMapper implements RowMapper<User> {
|
||||
@Override
|
||||
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
User user = new User();
|
||||
user.setId(rs.getString("id"));
|
||||
user.setUsername(rs.getString("username"));
|
||||
user.setAccount(rs.getString("account"));
|
||||
user.setPassword(rs.getString("password"));
|
||||
user.setEmail(rs.getString("email"));
|
||||
user.setPhone(rs.getString("phone"));
|
||||
user.setNickname(rs.getString("nickname"));
|
||||
user.setAvatar(rs.getString("avatar"));
|
||||
user.setGender(rs.getInt("gender"));
|
||||
user.setBio(rs.getString("bio"));
|
||||
user.setMemberLevel(rs.getString("member_level"));
|
||||
user.setTotalDays(rs.getInt("total_days"));
|
||||
user.setStatus(rs.getInt("status"));
|
||||
user.setIsVerified(rs.getInt("is_verified"));
|
||||
user.setCreateTime(rs.getTimestamp("create_time") != null ?
|
||||
rs.getTimestamp("create_time").toLocalDateTime() : null);
|
||||
user.setUpdateTime(rs.getTimestamp("update_time") != null ?
|
||||
rs.getTimestamp("update_time").toLocalDateTime() : null);
|
||||
user.setLastActiveTime(rs.getTimestamp("last_active_time") != null ?
|
||||
rs.getTimestamp("last_active_time").toLocalDateTime() : null);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
IPage<User> getPage(BasePageRequest request);
|
||||
|
||||
/**
|
||||
* 根据账号查询用户
|
||||
*/
|
||||
public User findByAccount(String account) {
|
||||
try {
|
||||
String sql = "SELECT * FROM user WHERE account = ? AND is_deleted = 0";
|
||||
List<User> users = jdbcTemplate.query(sql, new UserRowMapper(), account);
|
||||
return users.isEmpty() ? null : users.get(0);
|
||||
} catch (Exception e) {
|
||||
log.error("根据账号查询用户失败: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
User getByAccount(String account);
|
||||
|
||||
/**
|
||||
* 根据ID查询用户
|
||||
* 根据用户名查询用户
|
||||
*/
|
||||
public User findById(String id) {
|
||||
try {
|
||||
String sql = "SELECT * FROM user WHERE id = ? AND is_deleted = 0";
|
||||
List<User> users = jdbcTemplate.query(sql, new UserRowMapper(), id);
|
||||
return users.isEmpty() ? null : users.get(0);
|
||||
} catch (Exception e) {
|
||||
log.error("根据ID查询用户失败: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
User getByUsername(String username);
|
||||
|
||||
/**
|
||||
* 根据邮箱查询用户
|
||||
*/
|
||||
User getByEmail(String email);
|
||||
|
||||
/**
|
||||
* 根据手机号查询用户
|
||||
*/
|
||||
User getByPhone(String phone);
|
||||
|
||||
/**
|
||||
* 根据第三方平台信息查询用户
|
||||
*/
|
||||
User getByThirdParty(String thirdPartyId, String thirdPartyType);
|
||||
|
||||
/**
|
||||
* 根据状态查询用户列表
|
||||
*/
|
||||
List<User> getByStatus(Integer status);
|
||||
|
||||
/**
|
||||
* 根据会员等级查询用户列表
|
||||
*/
|
||||
List<User> getByMemberLevel(String memberLevel);
|
||||
|
||||
/**
|
||||
* 查询活跃用户(最近N天有活动)
|
||||
*/
|
||||
List<User> getActiveUsers(Integer days);
|
||||
|
||||
/**
|
||||
* 查询新注册用户(最近N天注册)
|
||||
*/
|
||||
List<User> getNewUsers(Integer days);
|
||||
|
||||
/**
|
||||
* 更新用户最后活跃时间
|
||||
*/
|
||||
boolean updateLastActiveTime(String userId, LocalDateTime lastActiveTime);
|
||||
|
||||
/**
|
||||
* 更新用户状态
|
||||
*/
|
||||
boolean updateStatus(String userId, Integer status);
|
||||
|
||||
/**
|
||||
* 更新用户使用天数
|
||||
*/
|
||||
boolean updateTotalDays(String userId, Integer totalDays);
|
||||
|
||||
/**
|
||||
* 统计指定状态的用户数量
|
||||
*/
|
||||
Long countByStatus(Integer status);
|
||||
|
||||
/**
|
||||
* 统计指定会员等级的用户数量
|
||||
*/
|
||||
Long countByMemberLevel(String memberLevel);
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
*/
|
||||
public User createUser(User user) {
|
||||
try {
|
||||
// 生成ID
|
||||
user.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||
|
||||
// 加密密码
|
||||
if (user.getPassword() != null) {
|
||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
user.setCreateTime(LocalDateTime.now());
|
||||
user.setUpdateTime(LocalDateTime.now());
|
||||
user.setStatus(1);
|
||||
user.setIsDeleted(0);
|
||||
user.setIsVerified(0);
|
||||
user.setTotalDays(0);
|
||||
user.setMemberLevel("free");
|
||||
|
||||
String sql = "INSERT INTO user (id, username, account, password, email, phone, nickname, " +
|
||||
"avatar, gender, bio, member_level, total_days, status, is_verified, " +
|
||||
"create_time, update_time, last_active_time, is_deleted) VALUES " +
|
||||
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
jdbcTemplate.update(sql,
|
||||
user.getId(), user.getUsername(), user.getAccount(), user.getPassword(),
|
||||
user.getEmail(), user.getPhone(), user.getNickname(), user.getAvatar(),
|
||||
user.getGender(), user.getBio(), user.getMemberLevel(), user.getTotalDays(),
|
||||
user.getStatus(), user.getIsVerified(), user.getCreateTime(), user.getUpdateTime(),
|
||||
user.getLastActiveTime(), user.getIsDeleted());
|
||||
|
||||
log.info("用户创建成功: {}", user.getAccount());
|
||||
return user;
|
||||
} catch (Exception e) {
|
||||
log.error("创建用户失败: {}", e.getMessage());
|
||||
throw new RuntimeException("创建用户失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
User createUser(String account, String username, String password, String email, String phone);
|
||||
|
||||
/**
|
||||
* 更新用户
|
||||
* 验证用户密码
|
||||
*/
|
||||
public boolean updateUser(User user) {
|
||||
try {
|
||||
user.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
String sql = "UPDATE user SET username = ?, email = ?, phone = ?, nickname = ?, " +
|
||||
"avatar = ?, gender = ?, bio = ?, update_time = ? WHERE id = ? AND is_deleted = 0";
|
||||
|
||||
int rows = jdbcTemplate.update(sql,
|
||||
user.getUsername(), user.getEmail(), user.getPhone(), user.getNickname(),
|
||||
user.getAvatar(), user.getGender(), user.getBio(), user.getUpdateTime(),
|
||||
user.getId());
|
||||
|
||||
return rows > 0;
|
||||
} catch (Exception e) {
|
||||
log.error("更新用户失败: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
boolean validatePassword(String userId, String password);
|
||||
|
||||
/**
|
||||
* 更新最后活跃时间
|
||||
* 更新用户密码
|
||||
*/
|
||||
public boolean updateLastActiveTime(String userId) {
|
||||
try {
|
||||
String sql = "UPDATE user SET last_active_time = ?, update_time = ? WHERE id = ? AND is_deleted = 0";
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
int rows = jdbcTemplate.update(sql, now, now, userId);
|
||||
return rows > 0;
|
||||
} catch (Exception e) {
|
||||
log.error("更新最后活跃时间失败: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证密码
|
||||
*/
|
||||
public boolean validatePassword(String rawPassword, String encodedPassword) {
|
||||
return passwordEncoder.matches(rawPassword, encodedPassword);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查账号是否存在
|
||||
*/
|
||||
public boolean accountExists(String account) {
|
||||
try {
|
||||
String sql = "SELECT COUNT(*) FROM user WHERE account = ? AND is_deleted = 0";
|
||||
Integer count = jdbcTemplate.queryForObject(sql, Integer.class, account);
|
||||
return count != null && count > 0;
|
||||
} catch (Exception e) {
|
||||
log.error("检查账号是否存在失败: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
boolean updatePassword(String userId, String newPassword);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
package com.emotion.service;
|
||||
|
||||
import com.emotion.dto.websocket.ChatRequest;
|
||||
import com.emotion.dto.websocket.ConnectRequest;
|
||||
import com.emotion.dto.websocket.WebSocketMessage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* WebSocket服务
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class WebSocketService {
|
||||
|
||||
@Autowired
|
||||
private SimpMessagingTemplate messagingTemplate;
|
||||
|
||||
@Autowired
|
||||
private IAiService aiService;
|
||||
|
||||
@Autowired
|
||||
private IMessageService messageService;
|
||||
|
||||
@Autowired
|
||||
private IConversationService conversationService;
|
||||
|
||||
// 在线用户管理
|
||||
private final ConcurrentHashMap<String, String> onlineUsers = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 处理聊天消息
|
||||
*/
|
||||
public void handleChatMessage(ChatRequest request, String sessionId, Principal principal) {
|
||||
try {
|
||||
log.info("处理聊天消息: {}", request);
|
||||
|
||||
// 验证请求参数
|
||||
if (request.getContent() == null || request.getContent().trim().isEmpty()) {
|
||||
sendErrorMessage(request.getSenderId(), "消息内容不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
// 构建用户消息
|
||||
WebSocketMessage userMessage = WebSocketMessage.builder()
|
||||
.messageId(UUID.randomUUID().toString())
|
||||
.conversationId(request.getConversationId())
|
||||
.type(WebSocketMessage.MessageType.TEXT)
|
||||
.content(request.getContent())
|
||||
.senderId(request.getSenderId())
|
||||
.senderType(WebSocketMessage.SenderType.valueOf(request.getSenderType().name()))
|
||||
.status(WebSocketMessage.MessageStatus.SENT)
|
||||
.createTime(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
// 发送用户消息到会话
|
||||
if (request.getConversationId() != null) {
|
||||
messagingTemplate.convertAndSend("/topic/conversation/" + request.getConversationId(), userMessage);
|
||||
}
|
||||
|
||||
// 发送给用户私有队列
|
||||
messagingTemplate.convertAndSendToUser(request.getSenderId(), "/queue/messages", userMessage);
|
||||
|
||||
// 发送AI思考状态
|
||||
sendAiThinkingMessage(request.getSenderId(), request.getConversationId());
|
||||
|
||||
// 异步调用AI服务
|
||||
processAiResponse(request);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理聊天消息失败", e);
|
||||
sendErrorMessage(request.getSenderId(), "消息处理失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理用户连接
|
||||
*/
|
||||
public void handleUserConnect(ConnectRequest request, String sessionId, Principal principal) {
|
||||
try {
|
||||
String userId = request.getUserId();
|
||||
if (userId == null && principal != null) {
|
||||
userId = principal.getName();
|
||||
}
|
||||
if (userId == null) {
|
||||
userId = "guest_" + sessionId;
|
||||
}
|
||||
|
||||
log.info("用户连接WebSocket: userId={}, sessionId={}", userId, sessionId);
|
||||
|
||||
// 记录在线用户
|
||||
onlineUsers.put(sessionId, userId);
|
||||
|
||||
// 发送连接成功消息
|
||||
WebSocketMessage connectMessage = WebSocketMessage.builder()
|
||||
.messageId(UUID.randomUUID().toString())
|
||||
.type(WebSocketMessage.MessageType.CONNECTION)
|
||||
.content("连接成功")
|
||||
.senderId("system")
|
||||
.senderType(WebSocketMessage.SenderType.SYSTEM)
|
||||
.status(WebSocketMessage.MessageStatus.SENT)
|
||||
.createTime(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
messagingTemplate.convertAndSendToUser(userId, "/queue/messages", connectMessage);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理用户连接失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理用户断开连接
|
||||
*/
|
||||
public void handleUserDisconnect(String sessionId, Principal principal) {
|
||||
try {
|
||||
String userId = onlineUsers.remove(sessionId);
|
||||
log.info("用户断开WebSocket连接: userId={}, sessionId={}", userId, sessionId);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理用户断开连接失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理心跳消息
|
||||
*/
|
||||
public void handleHeartbeat(String sessionId, Principal principal) {
|
||||
try {
|
||||
String userId = onlineUsers.get(sessionId);
|
||||
if (userId == null && principal != null) {
|
||||
userId = principal.getName();
|
||||
}
|
||||
|
||||
// 发送心跳响应
|
||||
WebSocketMessage heartbeatMessage = WebSocketMessage.builder()
|
||||
.messageId(UUID.randomUUID().toString())
|
||||
.type(WebSocketMessage.MessageType.HEARTBEAT)
|
||||
.content("pong")
|
||||
.senderId("system")
|
||||
.senderType(WebSocketMessage.SenderType.SYSTEM)
|
||||
.status(WebSocketMessage.MessageStatus.SENT)
|
||||
.createTime(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
if (userId != null) {
|
||||
messagingTemplate.convertAndSendToUser(userId, "/queue/messages", heartbeatMessage);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理心跳消息失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送AI思考状态消息
|
||||
*/
|
||||
private void sendAiThinkingMessage(String userId, String conversationId) {
|
||||
WebSocketMessage thinkingMessage = WebSocketMessage.builder()
|
||||
.messageId(UUID.randomUUID().toString())
|
||||
.conversationId(conversationId)
|
||||
.type(WebSocketMessage.MessageType.AI_THINKING)
|
||||
.content("AI正在思考中...")
|
||||
.senderId("ai")
|
||||
.senderType(WebSocketMessage.SenderType.AI)
|
||||
.status(WebSocketMessage.MessageStatus.SENT)
|
||||
.createTime(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
messagingTemplate.convertAndSendToUser(userId, "/queue/messages", thinkingMessage);
|
||||
|
||||
if (conversationId != null) {
|
||||
messagingTemplate.convertAndSend("/topic/conversation/" + conversationId, thinkingMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步处理AI响应
|
||||
*/
|
||||
private void processAiResponse(ChatRequest request) {
|
||||
// 使用线程池异步处理AI响应
|
||||
new Thread(() -> {
|
||||
try {
|
||||
// 保存用户消息到数据库
|
||||
messageService.saveMessage(
|
||||
request.getConversationId(),
|
||||
request.getContent(),
|
||||
request.getMessageType().name(),
|
||||
request.getSenderType().name()
|
||||
);
|
||||
|
||||
// 调用AI服务
|
||||
String aiReply = aiService.sendChatMessage(
|
||||
request.getConversationId(),
|
||||
request.getContent(),
|
||||
request.getSenderId()
|
||||
);
|
||||
|
||||
// 构建AI回复消息
|
||||
WebSocketMessage aiMessage = WebSocketMessage.builder()
|
||||
.messageId(UUID.randomUUID().toString())
|
||||
.conversationId(request.getConversationId())
|
||||
.type(WebSocketMessage.MessageType.TEXT)
|
||||
.content(aiReply)
|
||||
.senderId("ai")
|
||||
.senderType(WebSocketMessage.SenderType.AI)
|
||||
.status(WebSocketMessage.MessageStatus.SENT)
|
||||
.createTime(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
// 保存AI回复到数据库
|
||||
messageService.saveMessage(
|
||||
request.getConversationId(),
|
||||
aiReply,
|
||||
"text",
|
||||
"assistant"
|
||||
);
|
||||
|
||||
// 发送AI回复
|
||||
messagingTemplate.convertAndSendToUser(request.getSenderId(), "/queue/messages", aiMessage);
|
||||
|
||||
if (request.getConversationId() != null) {
|
||||
messagingTemplate.convertAndSend("/topic/conversation/" + request.getConversationId(), aiMessage);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("AI响应处理失败", e);
|
||||
sendErrorMessage(request.getSenderId(), "AI服务暂时不可用,请稍后重试");
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送错误消息
|
||||
*/
|
||||
private void sendErrorMessage(String userId, String errorContent) {
|
||||
WebSocketMessage errorMessage = WebSocketMessage.builder()
|
||||
.messageId(UUID.randomUUID().toString())
|
||||
.type(WebSocketMessage.MessageType.ERROR)
|
||||
.content(errorContent)
|
||||
.senderId("system")
|
||||
.senderType(WebSocketMessage.SenderType.SYSTEM)
|
||||
.status(WebSocketMessage.MessageStatus.SENT)
|
||||
.createTime(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
messagingTemplate.convertAndSendToUser(userId, "/queue/messages", errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线用户数量
|
||||
*/
|
||||
public int getOnlineUserCount() {
|
||||
return onlineUsers.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
package com.emotion.service.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.emotion.entity.Message;
|
||||
import com.emotion.service.IAiService;
|
||||
import com.emotion.service.IMessageService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* AI服务实现类
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class AiServiceImpl implements IAiService {
|
||||
|
||||
@Autowired
|
||||
private IMessageService messageService;
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
// Coze平台配置 - 对话聊天
|
||||
@Value("${coze.api.token}")
|
||||
private String cozeApiToken;
|
||||
|
||||
@Value("${coze.api.base-url:https://api.coze.cn}")
|
||||
private String cozeBaseUrl;
|
||||
|
||||
@Value("${coze.api.chat.bot-id}")
|
||||
private String chatBotId;
|
||||
|
||||
// Coze平台配置 - 聊天记录总结
|
||||
@Value("${coze.api.summary.bot-id}")
|
||||
private String summaryBotId;
|
||||
|
||||
public AiServiceImpl() {
|
||||
this.restTemplate = new RestTemplate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String sendChatMessage(String conversationId, String message, String userId) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
try {
|
||||
log.info("发送聊天消息到AI: conversationId={}, userId={}, message={}",
|
||||
conversationId, userId, message);
|
||||
|
||||
// 构建聊天请求数据
|
||||
Map<String, Object> requestData = buildChatRequestData(conversationId, message, userId);
|
||||
|
||||
// 发送请求到Coze API
|
||||
String response = sendToCozeApi(requestData, chatBotId);
|
||||
|
||||
// 解析响应
|
||||
String aiReply = parseCozeResponse(response);
|
||||
|
||||
log.info("AI聊天回复成功: conversationId={}, 耗时={}ms, 回复长度={}",
|
||||
conversationId, System.currentTimeMillis() - startTime, aiReply.length());
|
||||
|
||||
return aiReply;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("AI聊天服务调用失败: conversationId={}, error={}", conversationId, e.getMessage(), e);
|
||||
return "抱歉,我现在无法回复您的消息,请稍后再试。";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateConversationSummary(String conversationId, String userId) {
|
||||
try {
|
||||
log.info("生成对话总结: conversationId={}, userId={}", conversationId, userId);
|
||||
|
||||
// 获取消息记录(限制数量避免token过多)
|
||||
List<Message> messages = messageService.getByConversationIdForSummary(conversationId, 100);
|
||||
|
||||
if (messages.isEmpty()) {
|
||||
return "暂无对话记录可供总结。";
|
||||
}
|
||||
|
||||
return generateSummaryFromRecords(messages, userId);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("生成对话总结失败: conversationId={}, error={}", conversationId, e.getMessage(), e);
|
||||
return "对话总结生成失败,请稍后再试。";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateSummaryFromRecords(List<Message> messages, String userId) {
|
||||
try {
|
||||
if (messages.isEmpty()) {
|
||||
return "暂无对话记录可供总结。";
|
||||
}
|
||||
|
||||
// 构建对话历史文本
|
||||
String conversationText = buildConversationText(messages);
|
||||
|
||||
// 构建总结请求数据
|
||||
Map<String, Object> requestData = buildSummaryRequestData(conversationText, userId);
|
||||
|
||||
// 发送请求到Coze API
|
||||
String response = sendToCozeApi(requestData, summaryBotId);
|
||||
|
||||
// 解析响应
|
||||
String summary = parseCozeResponse(response);
|
||||
|
||||
log.info("对话总结生成成功: userId={}, 记录数量={}, 总结长度={}",
|
||||
userId, messages.size(), summary.length());
|
||||
|
||||
return summary;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("根据记录生成总结失败: userId={}, error={}", userId, e.getMessage(), e);
|
||||
return "对话总结生成失败,请稍后再试。";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isServiceAvailable() {
|
||||
try {
|
||||
// 简单的健康检查
|
||||
return StringUtils.hasText(cozeApiToken) &&
|
||||
StringUtils.hasText(chatBotId) &&
|
||||
StringUtils.hasText(summaryBotId);
|
||||
} catch (Exception e) {
|
||||
log.error("AI服务可用性检查失败", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceStatus() {
|
||||
try {
|
||||
boolean available = isServiceAvailable();
|
||||
return String.format("AI服务状态: %s, 聊天Bot: %s, 总结Bot: %s",
|
||||
available ? "可用" : "不可用", chatBotId, summaryBotId);
|
||||
} catch (Exception e) {
|
||||
return "AI服务状态检查失败: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建聊天请求数据
|
||||
*/
|
||||
private Map<String, Object> buildChatRequestData(String conversationId, String message, String userId) {
|
||||
Map<String, Object> requestData = new HashMap<>();
|
||||
requestData.put("bot_id", chatBotId);
|
||||
requestData.put("user_id", userId);
|
||||
requestData.put("query", message);
|
||||
requestData.put("stream", false);
|
||||
|
||||
if (StringUtils.hasText(conversationId)) {
|
||||
requestData.put("conversation_id", conversationId);
|
||||
}
|
||||
|
||||
return requestData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建总结请求数据
|
||||
*/
|
||||
private Map<String, Object> buildSummaryRequestData(String conversationText, String userId) {
|
||||
Map<String, Object> requestData = new HashMap<>();
|
||||
requestData.put("bot_id", summaryBotId);
|
||||
requestData.put("user_id", userId);
|
||||
requestData.put("query", "请对以下对话内容进行总结,提取关键信息和主要话题:\n\n" + conversationText);
|
||||
requestData.put("stream", false);
|
||||
|
||||
return requestData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送请求到Coze API
|
||||
*/
|
||||
private String sendToCozeApi(Map<String, Object> requestData, String botId) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.setBearerAuth(cozeApiToken);
|
||||
|
||||
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestData, headers);
|
||||
|
||||
String url = cozeBaseUrl + "/v3/chat";
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
|
||||
|
||||
if (response.getStatusCode() == HttpStatus.OK) {
|
||||
return response.getBody();
|
||||
} else {
|
||||
throw new RuntimeException("Coze API调用失败: " + response.getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析Coze响应
|
||||
*/
|
||||
private String parseCozeResponse(String response) {
|
||||
try {
|
||||
JSONObject jsonResponse = JSON.parseObject(response);
|
||||
|
||||
if (jsonResponse.getInteger("code") == 0) {
|
||||
JSONArray messages = jsonResponse.getJSONArray("messages");
|
||||
if (messages != null && !messages.isEmpty()) {
|
||||
JSONObject lastMessage = messages.getJSONObject(messages.size() - 1);
|
||||
return lastMessage.getString("content");
|
||||
}
|
||||
}
|
||||
|
||||
log.warn("Coze响应解析异常: {}", response);
|
||||
return "AI服务响应异常,请稍后再试。";
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("解析Coze响应失败", e);
|
||||
return "AI服务响应解析失败,请稍后再试。";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建对话历史文本
|
||||
*/
|
||||
private String buildConversationText(List<Message> messages) {
|
||||
return messages.stream()
|
||||
.map(message -> {
|
||||
String senderName = "user".equals(message.getSender()) ? "用户" : "AI助手";
|
||||
return senderName + ": " + message.getContent();
|
||||
})
|
||||
.collect(Collectors.joining("\n"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package com.emotion.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.emotion.entity.CozeApiCall;
|
||||
import com.emotion.mapper.CozeApiCallMapper;
|
||||
import com.emotion.service.ICozeApiCallService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Coze API调用记录服务实现类
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Service
|
||||
public class CozeApiCallServiceImpl extends ServiceImpl<CozeApiCallMapper, CozeApiCall> implements ICozeApiCallService {
|
||||
|
||||
@Override
|
||||
public IPage<CozeApiCall> getByConversationId(Page<CozeApiCall> page, String conversationId) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getConversationId, conversationId)
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.orderByDesc(CozeApiCall::getStartTime);
|
||||
return this.page(page, wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPage<CozeApiCall> getByUserId(Page<CozeApiCall> page, String userId) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getUserId, userId)
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.orderByDesc(CozeApiCall::getStartTime);
|
||||
return this.page(page, wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CozeApiCall> getByBotId(String botId) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getBotId, botId)
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.orderByDesc(CozeApiCall::getStartTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CozeApiCall> getByStatus(String status) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getStatus, status)
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.orderByDesc(CozeApiCall::getStartTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CozeApiCall> getByRequestType(String requestType) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getRequestType, requestType)
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.orderByDesc(CozeApiCall::getStartTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CozeApiCall> getByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.between(CozeApiCall::getStartTime, startTime, endTime)
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.orderByDesc(CozeApiCall::getStartTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByUserId(String userId) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getUserId, userId)
|
||||
.eq(CozeApiCall::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByBotId(String botId) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getBotId, botId)
|
||||
.eq(CozeApiCall::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByStatus(String status) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getStatus, status)
|
||||
.eq(CozeApiCall::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long sumTokensByUserId(String userId) {
|
||||
// 使用原生SQL或者查询后计算
|
||||
List<CozeApiCall> calls = this.list(new LambdaQueryWrapper<CozeApiCall>()
|
||||
.eq(CozeApiCall::getUserId, userId)
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.isNotNull(CozeApiCall::getTotalTokens));
|
||||
return calls.stream().mapToLong(call -> call.getTotalTokens() != null ? call.getTotalTokens() : 0).sum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigDecimal sumCostByUserId(String userId) {
|
||||
List<CozeApiCall> calls = this.list(new LambdaQueryWrapper<CozeApiCall>()
|
||||
.eq(CozeApiCall::getUserId, userId)
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.isNotNull(CozeApiCall::getCost));
|
||||
return calls.stream()
|
||||
.map(call -> call.getCost() != null ? call.getCost() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CozeApiCall> getFailedCalls() {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.and(w -> w.eq(CozeApiCall::getStatus, "failed").or().eq(CozeApiCall::getFinalStatus, "failed"))
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.orderByDesc(CozeApiCall::getStartTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CozeApiCall> getTimeoutCalls() {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.and(w -> w.eq(CozeApiCall::getStatus, "timeout").or().eq(CozeApiCall::getFinalStatus, "timeout"))
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.orderByDesc(CozeApiCall::getStartTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CozeApiCall getByTraceId(String traceId) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getTraceId, traceId)
|
||||
.eq(CozeApiCall::getIsDeleted, 0);
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CozeApiCall> getByConversationIdAndRequestType(String conversationId, String requestType) {
|
||||
LambdaQueryWrapper<CozeApiCall> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getConversationId, conversationId)
|
||||
.eq(CozeApiCall::getRequestType, requestType)
|
||||
.eq(CozeApiCall::getIsDeleted, 0)
|
||||
.orderByDesc(CozeApiCall::getStartTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateStatus(String id, String status, String finalStatus, LocalDateTime endTime) {
|
||||
LambdaUpdateWrapper<CozeApiCall> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getId, id)
|
||||
.set(CozeApiCall::getStatus, status)
|
||||
.set(CozeApiCall::getFinalStatus, finalStatus)
|
||||
.set(CozeApiCall::getEndTime, endTime)
|
||||
.set(CozeApiCall::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateResult(String id, Integer responseStatus, String responseBody, String aiReply,
|
||||
Integer totalTokens, BigDecimal cost, String status, String finalStatus,
|
||||
LocalDateTime endTime) {
|
||||
LambdaUpdateWrapper<CozeApiCall> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(CozeApiCall::getId, id)
|
||||
.set(CozeApiCall::getResponseStatus, responseStatus)
|
||||
.set(CozeApiCall::getResponseBody, responseBody)
|
||||
.set(CozeApiCall::getAiReply, aiReply)
|
||||
.set(CozeApiCall::getTotalTokens, totalTokens)
|
||||
.set(CozeApiCall::getCost, cost)
|
||||
.set(CozeApiCall::getStatus, status)
|
||||
.set(CozeApiCall::getFinalStatus, finalStatus)
|
||||
.set(CozeApiCall::getEndTime, endTime)
|
||||
.set(CozeApiCall::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CozeApiCall createApiCall(String conversationId, String messageId, String userId,
|
||||
String requestType, String requestUrl, String requestBody) {
|
||||
CozeApiCall apiCall = CozeApiCall.builder()
|
||||
.conversationId(conversationId)
|
||||
.messageId(messageId)
|
||||
.userId(userId)
|
||||
.requestType(requestType)
|
||||
.requestUrl(requestUrl)
|
||||
.requestBody(requestBody)
|
||||
.status("pending")
|
||||
.startTime(LocalDateTime.now())
|
||||
.build();
|
||||
this.save(apiCall);
|
||||
return apiCall;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package com.emotion.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.emotion.common.BasePageRequest;
|
||||
import com.emotion.entity.Message;
|
||||
import com.emotion.mapper.MessageMapper;
|
||||
import com.emotion.service.MessageService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 消息服务实现类
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Service
|
||||
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MessageServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public IPage<Message> getPage(BasePageRequest request) {
|
||||
Page<Message> page = new Page<>(request.getCurrent(), request.getSize());
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
// 关键词搜索
|
||||
if (StringUtils.hasText(request.getKeyword())) {
|
||||
wrapper.like(Message::getContent, request.getKeyword());
|
||||
}
|
||||
|
||||
wrapper.eq(Message::getIsDeleted, 0);
|
||||
|
||||
// 排序
|
||||
if (StringUtils.hasText(request.getOrderBy())) {
|
||||
if ("asc".equalsIgnoreCase(request.getOrderDirection())) {
|
||||
wrapper.orderByAsc(Message::getTimestamp);
|
||||
} else {
|
||||
wrapper.orderByDesc(Message::getTimestamp);
|
||||
}
|
||||
} else {
|
||||
wrapper.orderByDesc(Message::getTimestamp);
|
||||
}
|
||||
|
||||
return this.page(page, wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPage<Message> getPageByConversationId(BasePageRequest request, String conversationId) {
|
||||
Page<Message> page = new Page<>(request.getCurrent(), request.getSize());
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Message::getConversationId, conversationId)
|
||||
.eq(Message::getIsDeleted, 0);
|
||||
|
||||
// 关键词搜索
|
||||
if (StringUtils.hasText(request.getKeyword())) {
|
||||
wrapper.like(Message::getContent, request.getKeyword());
|
||||
}
|
||||
|
||||
wrapper.orderByAsc(Message::getTimestamp);
|
||||
return this.page(page, wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Message> getByConversationId(String conversationId) {
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Message::getConversationId, conversationId)
|
||||
.eq(Message::getIsDeleted, 0)
|
||||
.orderByAsc(Message::getTimestamp);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Message> getBySender(String sender) {
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Message::getSender, sender)
|
||||
.eq(Message::getIsDeleted, 0)
|
||||
.orderByDesc(Message::getTimestamp);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Message> getByTimeRange(String conversationId, LocalDateTime startTime, LocalDateTime endTime) {
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Message::getConversationId, conversationId)
|
||||
.between(Message::getTimestamp, startTime, endTime)
|
||||
.eq(Message::getIsDeleted, 0)
|
||||
.orderByAsc(Message::getTimestamp);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message getLastMessageByConversationId(String conversationId) {
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Message::getConversationId, conversationId)
|
||||
.eq(Message::getIsDeleted, 0)
|
||||
.orderByDesc(Message::getTimestamp)
|
||||
.last("LIMIT 1");
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Message> getRepliesByParentId(String parentMessageId) {
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Message::getParentMessageId, parentMessageId)
|
||||
.eq(Message::getIsDeleted, 0)
|
||||
.orderByAsc(Message::getTimestamp);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByConversationId(String conversationId) {
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Message::getConversationId, conversationId)
|
||||
.eq(Message::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countBySender(String sender) {
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Message::getSender, sender)
|
||||
.eq(Message::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countUnreadMessages(String conversationId) {
|
||||
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Message::getConversationId, conversationId)
|
||||
.eq(Message::getIsRead, 0)
|
||||
.eq(Message::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateStatus(String messageId, String status) {
|
||||
LambdaUpdateWrapper<Message> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(Message::getId, messageId)
|
||||
.set(Message::getStatus, status)
|
||||
.set(Message::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateReadStatus(String messageId, Integer isRead) {
|
||||
LambdaUpdateWrapper<Message> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(Message::getId, messageId)
|
||||
.set(Message::getIsRead, isRead)
|
||||
.set(Message::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean markConversationMessagesAsRead(String conversationId) {
|
||||
LambdaUpdateWrapper<Message> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(Message::getConversationId, conversationId)
|
||||
.eq(Message::getIsRead, 0)
|
||||
.set(Message::getIsRead, 1)
|
||||
.set(Message::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message createMessage(String conversationId, String userId, String content,
|
||||
String contentType, String senderType, String senderId) {
|
||||
try {
|
||||
Message message = Message.builder()
|
||||
.id(UUID.randomUUID().toString())
|
||||
.conversationId(conversationId)
|
||||
.userId(userId)
|
||||
.content(content)
|
||||
.contentType(StringUtils.hasText(contentType) ? contentType : "text")
|
||||
.senderType(senderType)
|
||||
.sender(senderId)
|
||||
.timestamp(LocalDateTime.now())
|
||||
.status("sent")
|
||||
.isRead(0)
|
||||
.build();
|
||||
|
||||
boolean saved = this.save(message);
|
||||
if (saved) {
|
||||
log.info("保存消息成功: id={}, conversationId={}, sender={}",
|
||||
message.getId(), conversationId, senderId);
|
||||
return message;
|
||||
} else {
|
||||
log.error("保存消息失败: conversationId={}, sender={}", conversationId, senderId);
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("保存消息异常: conversationId={}, error={}", conversationId, e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean markAsRead(String messageId) {
|
||||
return updateReadStatus(messageId, 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package com.emotion.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.emotion.common.BasePageRequest;
|
||||
import com.emotion.entity.User;
|
||||
import com.emotion.mapper.UserMapper;
|
||||
import com.emotion.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户服务实现类
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
public IPage<User> getPage(BasePageRequest request) {
|
||||
Page<User> page = new Page<>(request.getCurrent(), request.getSize());
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
// 关键词搜索
|
||||
if (StringUtils.hasText(request.getKeyword())) {
|
||||
wrapper.and(w -> w.like(User::getUsername, request.getKeyword())
|
||||
.or().like(User::getAccount, request.getKeyword())
|
||||
.or().like(User::getEmail, request.getKeyword())
|
||||
.or().like(User::getPhone, request.getKeyword()));
|
||||
}
|
||||
|
||||
wrapper.eq(User::getIsDeleted, 0);
|
||||
|
||||
// 排序
|
||||
if (StringUtils.hasText(request.getOrderBy())) {
|
||||
if ("asc".equalsIgnoreCase(request.getOrderDirection())) {
|
||||
wrapper.orderByAsc(getColumnByField(request.getOrderBy()));
|
||||
} else {
|
||||
wrapper.orderByDesc(getColumnByField(request.getOrderBy()));
|
||||
}
|
||||
} else {
|
||||
wrapper.orderByDesc(User::getCreateTime);
|
||||
}
|
||||
|
||||
return this.page(page, wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getByAccount(String account) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getAccount, account)
|
||||
.eq(User::getIsDeleted, 0);
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getByUsername(String username) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getUsername, username)
|
||||
.eq(User::getIsDeleted, 0);
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getByEmail(String email) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getEmail, email)
|
||||
.eq(User::getIsDeleted, 0);
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getByPhone(String phone) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getPhone, phone)
|
||||
.eq(User::getIsDeleted, 0);
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getByThirdParty(String thirdPartyId, String thirdPartyType) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getThirdPartyId, thirdPartyId)
|
||||
.eq(User::getThirdPartyType, thirdPartyType)
|
||||
.eq(User::getIsDeleted, 0);
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getByStatus(Integer status) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getStatus, status)
|
||||
.eq(User::getIsDeleted, 0)
|
||||
.orderByDesc(User::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getByMemberLevel(String memberLevel) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getMemberLevel, memberLevel)
|
||||
.eq(User::getIsDeleted, 0)
|
||||
.orderByDesc(User::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getActiveUsers(Integer days) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.ge(User::getLastActiveTime, LocalDateTime.now().minusDays(days))
|
||||
.eq(User::getIsDeleted, 0)
|
||||
.orderByDesc(User::getLastActiveTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getNewUsers(Integer days) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.ge(User::getCreateTime, LocalDateTime.now().minusDays(days))
|
||||
.eq(User::getIsDeleted, 0)
|
||||
.orderByDesc(User::getCreateTime);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateLastActiveTime(String userId, LocalDateTime lastActiveTime) {
|
||||
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(User::getId, userId)
|
||||
.set(User::getLastActiveTime, lastActiveTime)
|
||||
.set(User::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateStatus(String userId, Integer status) {
|
||||
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(User::getId, userId)
|
||||
.set(User::getStatus, status)
|
||||
.set(User::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateTotalDays(String userId, Integer totalDays) {
|
||||
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(User::getId, userId)
|
||||
.set(User::getTotalDays, totalDays)
|
||||
.set(User::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByStatus(Integer status) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getStatus, status)
|
||||
.eq(User::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countByMemberLevel(String memberLevel) {
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getMemberLevel, memberLevel)
|
||||
.eq(User::getIsDeleted, 0);
|
||||
return this.count(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User createUser(String account, String username, String password, String email, String phone) {
|
||||
User user = User.builder()
|
||||
.account(account)
|
||||
.username(username)
|
||||
.password(passwordEncoder.encode(password))
|
||||
.email(email)
|
||||
.phone(phone)
|
||||
.status(1)
|
||||
.memberLevel("basic")
|
||||
.totalDays(0)
|
||||
.lastActiveTime(LocalDateTime.now())
|
||||
.build();
|
||||
this.save(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validatePassword(String userId, String password) {
|
||||
User user = this.getById(userId);
|
||||
if (user == null) {
|
||||
return false;
|
||||
}
|
||||
return passwordEncoder.matches(password, user.getPassword());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePassword(String userId, String newPassword) {
|
||||
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(User::getId, userId)
|
||||
.set(User::getPassword, passwordEncoder.encode(newPassword))
|
||||
.set(User::getUpdateTime, LocalDateTime.now());
|
||||
return this.update(wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字段名获取对应的数据库列
|
||||
*/
|
||||
private String getColumnByField(String field) {
|
||||
// 这里可以根据需要扩展更多字段映射
|
||||
switch (field) {
|
||||
case "createTime":
|
||||
return "create_time";
|
||||
case "updateTime":
|
||||
return "update_time";
|
||||
case "lastActiveTime":
|
||||
return "last_active_time";
|
||||
default:
|
||||
return field;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user