|
@@ -173,6 +173,33 @@ public class AiChatController {
|
|
|
// 如果没有传入 sessionId,则创建一个新的会话ID
|
|
|
if (sessionId == null || sessionId.isEmpty()) {
|
|
|
sessionId = java.util.UUID.randomUUID().toString();
|
|
|
+
|
|
|
+ // 解析 JSON 并提取 "content" 字段的值
|
|
|
+ String questionText;
|
|
|
+ try {
|
|
|
+ JsonNode jsonNode = objectMapper.readTree(content);
|
|
|
+ questionText = jsonNode.get("content").asText(); // 提取 "content" 字段的值
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("Error parsing JSON content", e);
|
|
|
+ return ResponseEntity.badRequest().body(outputStream -> {
|
|
|
+ outputStream.write("Invalid JSON format".getBytes(StandardCharsets.UTF_8));
|
|
|
+ outputStream.flush();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否已经存在相同的 sessionId
|
|
|
+ boolean exists = aiSessionMapper.existsBySessionId(sessionId);
|
|
|
+
|
|
|
+ if (!exists) {
|
|
|
+ // 创建新的 AiSession 实体并存入数据库
|
|
|
+ AiSession aiSession = new AiSession();
|
|
|
+ aiSession.setSessionId(sessionId);
|
|
|
+ aiSession.setUserId(userId);
|
|
|
+ aiSession.setUserName(userName);
|
|
|
+ aiSession.setQuestion(questionText);
|
|
|
+ aiSession.setAskTime(LocalDateTime.now());
|
|
|
+ aiSessionMapper.save(aiSession);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 获取当前用户的对话历史
|