feat: 完善后端架构和service层实现
- 创建完整的entity实体类体系,包括所有业务实体 - 实现BaseEntity基类,统一管理公共字段 - 创建雪花算法ID生成器和自动填充处理器 - 简化所有mapper接口,只继承BaseMapper - 重构service层,使用LambdaQueryWrapper进行数据库操作 - 创建BasePageRequest分页查询基类 - 完善用户上下文管理和JWT认证 - 新增WebSocket聊天功能和相关控制器 - 更新前端配置和组件,完善用户认证流程 - 同步数据库建表脚本
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.service.IAiService;
|
||||
import com.emotion.service.IMessageService;
|
||||
import com.emotion.service.IConversationService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* AI聊天控制器
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/ai")
|
||||
public class AiChatController {
|
||||
|
||||
@Autowired
|
||||
private IAiService aiService;
|
||||
|
||||
@Autowired
|
||||
private IMessageService messageService;
|
||||
|
||||
@Autowired
|
||||
private IConversationService conversationService;
|
||||
|
||||
/**
|
||||
* 发送聊天消息
|
||||
*/
|
||||
@PostMapping("/chat")
|
||||
public Result<Map<String, Object>> sendChatMessage(@RequestBody Map<String, String> request) {
|
||||
try {
|
||||
String conversationId = request.get("conversationId");
|
||||
String message = request.get("message");
|
||||
String userId = request.get("userId");
|
||||
|
||||
if (message == null || message.trim().isEmpty()) {
|
||||
return Result.error("消息内容不能为空");
|
||||
}
|
||||
|
||||
if (userId == null || userId.trim().isEmpty()) {
|
||||
userId = "guest_" + System.currentTimeMillis();
|
||||
}
|
||||
|
||||
log.info("收到AI聊天请求: conversationId={}, userId={}, message={}",
|
||||
conversationId, userId, message);
|
||||
|
||||
// 调用AI服务
|
||||
String aiReply = aiService.sendChatMessage(conversationId, message, userId);
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("conversationId", conversationId);
|
||||
response.put("userMessage", message);
|
||||
response.put("aiReply", aiReply);
|
||||
response.put("userId", userId);
|
||||
response.put("timestamp", System.currentTimeMillis());
|
||||
|
||||
return Result.success(response);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("AI聊天请求处理失败", e);
|
||||
return Result.error("AI聊天服务暂时不可用,请稍后再试");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成对话总结
|
||||
*/
|
||||
@PostMapping("/summary")
|
||||
public Result<Map<String, Object>> generateSummary(@RequestBody Map<String, String> request) {
|
||||
try {
|
||||
String conversationId = request.get("conversationId");
|
||||
String userId = request.get("userId");
|
||||
|
||||
if (conversationId == null || conversationId.trim().isEmpty()) {
|
||||
return Result.error("会话ID不能为空");
|
||||
}
|
||||
|
||||
if (userId == null || userId.trim().isEmpty()) {
|
||||
userId = "guest_" + System.currentTimeMillis();
|
||||
}
|
||||
|
||||
log.info("收到对话总结请求: conversationId={}, userId={}", conversationId, userId);
|
||||
|
||||
// 调用AI总结服务
|
||||
String summary = aiService.generateConversationSummary(conversationId, userId);
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("conversationId", conversationId);
|
||||
response.put("summary", summary);
|
||||
response.put("userId", userId);
|
||||
response.put("timestamp", System.currentTimeMillis());
|
||||
|
||||
return Result.success(response);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("对话总结请求处理失败", e);
|
||||
return Result.error("对话总结服务暂时不可用,请稍后再试");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取AI服务状态
|
||||
*/
|
||||
@GetMapping("/status")
|
||||
public Result<Map<String, Object>> getServiceStatus() {
|
||||
try {
|
||||
boolean available = aiService.isServiceAvailable();
|
||||
String status = aiService.getServiceStatus();
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("available", available);
|
||||
response.put("status", status);
|
||||
response.put("timestamp", System.currentTimeMillis());
|
||||
|
||||
return Result.success(response);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取AI服务状态失败", e);
|
||||
return Result.error("无法获取AI服务状态");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取聊天记录统计
|
||||
*/
|
||||
@GetMapping("/stats")
|
||||
public Result<Map<String, Object>> getChatStats(@RequestParam(required = false) String userId,
|
||||
@RequestParam(required = false) String conversationId) {
|
||||
try {
|
||||
Map<String, Object> stats = new HashMap<>();
|
||||
|
||||
if (userId != null && !userId.trim().isEmpty()) {
|
||||
Long userConversationCount = conversationService.countByUserId(userId);
|
||||
Long activeConversationCount = conversationService.countActiveByUserId(userId);
|
||||
|
||||
stats.put("userConversationCount", userConversationCount);
|
||||
stats.put("activeConversationCount", activeConversationCount);
|
||||
}
|
||||
|
||||
if (conversationId != null && !conversationId.trim().isEmpty()) {
|
||||
Long conversationMessageCount = messageService.countByConversationId(conversationId);
|
||||
stats.put("conversationMessageCount", conversationMessageCount);
|
||||
}
|
||||
|
||||
stats.put("timestamp", System.currentTimeMillis());
|
||||
|
||||
return Result.success(stats);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取聊天统计失败", e);
|
||||
return Result.error("无法获取聊天统计信息");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user