feat: 完善后端架构和service层实现
- 创建完整的entity实体类体系,包括所有业务实体 - 实现BaseEntity基类,统一管理公共字段 - 创建雪花算法ID生成器和自动填充处理器 - 简化所有mapper接口,只继承BaseMapper - 重构service层,使用LambdaQueryWrapper进行数据库操作 - 创建BasePageRequest分页查询基类 - 完善用户上下文管理和JWT认证 - 新增WebSocket聊天功能和相关控制器 - 更新前端配置和组件,完善用户认证流程 - 同步数据库建表脚本
This commit is contained in:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user