From ea5a55deffa6d33048a1f7e03b71424c8add5e31 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期五, 08 五月 2026 14:51:52 +0800
Subject: [PATCH] feat(ai): 实现采购多文件分析附件存储与历史回显功能

---
 src/main/java/com/ruoyi/ai/store/MongoChatMemoryStore.java |   73 +++++++++++++++++++++++++++++++++++-
 1 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/ruoyi/ai/store/MongoChatMemoryStore.java b/src/main/java/com/ruoyi/ai/store/MongoChatMemoryStore.java
index e88f0e9..e6bcadb 100644
--- a/src/main/java/com/ruoyi/ai/store/MongoChatMemoryStore.java
+++ b/src/main/java/com/ruoyi/ai/store/MongoChatMemoryStore.java
@@ -11,6 +11,8 @@
 import org.springframework.data.mongodb.core.query.Query;
 import org.springframework.data.mongodb.core.query.Update;
 import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
 
 import java.util.Date;
 import java.util.LinkedList;
@@ -24,8 +26,7 @@
 
     @Override
     public List<ChatMessage> getMessages(Object memoryId) {
-        Query query = Query.query(Criteria.where("memoryId").is(memoryIdString(memoryId)));
-        ChatMessages chatMessages = mongoTemplate.findOne(query, ChatMessages.class);
+        ChatMessages chatMessages = findChatMessages(memoryId);
         if (chatMessages == null || chatMessages.getContent() == null) {
             return new LinkedList<>();
         }
@@ -56,7 +57,75 @@
         updateMessages(memoryId, messages);
     }
 
+    public void appendAnalyzeFileContext(Object memoryId, String userQuestion, List<String> filePaths) {
+        String memoryIdValue = memoryIdString(memoryId);
+        if (!StringUtils.hasText(memoryIdValue)) {
+            return;
+        }
+        List<String> validFilePaths = new LinkedList<>();
+        if (!CollectionUtils.isEmpty(filePaths)) {
+            for (String filePath : filePaths) {
+                if (StringUtils.hasText(filePath)) {
+                    validFilePaths.add(filePath);
+                }
+            }
+        }
+        if (!StringUtils.hasText(userQuestion) && validFilePaths.isEmpty()) {
+            return;
+        }
+        Query query = Query.query(Criteria.where("memoryId").is(memoryIdValue));
+        Update update = new Update();
+        update.set("memoryId", memoryIdValue);
+        update.set("updateTime", new Date());
+        update.setOnInsert("createTime", new Date());
+        if (StringUtils.hasText(userQuestion)) {
+            update.push("analyzeUserQuestions", userQuestion);
+        }
+        if (!validFilePaths.isEmpty()) {
+            update.push("analyzeFilePaths").each(validFilePaths.toArray());
+            update.push("analyzeFilePathGroups", validFilePaths);
+        }
+        mongoTemplate.upsert(query, update, ChatMessages.class);
+    }
+
+    public List<String> getAnalyzeUserQuestions(Object memoryId) {
+        ChatMessages chatMessages = findChatMessages(memoryId);
+        if (chatMessages == null || CollectionUtils.isEmpty(chatMessages.getAnalyzeUserQuestions())) {
+            return new LinkedList<>();
+        }
+        return new LinkedList<>(chatMessages.getAnalyzeUserQuestions());
+    }
+
+    public List<List<String>> getAnalyzeFilePathGroups(Object memoryId) {
+        ChatMessages chatMessages = findChatMessages(memoryId);
+        if (chatMessages == null) {
+            return new LinkedList<>();
+        }
+        if (CollectionUtils.isEmpty(chatMessages.getAnalyzeFilePathGroups())) {
+            if (CollectionUtils.isEmpty(chatMessages.getAnalyzeFilePaths())) {
+                return new LinkedList<>();
+            }
+            List<List<String>> fallback = new LinkedList<>();
+            fallback.add(new LinkedList<>(chatMessages.getAnalyzeFilePaths()));
+            return fallback;
+        }
+        List<List<String>> groups = new LinkedList<>();
+        for (List<String> group : chatMessages.getAnalyzeFilePathGroups()) {
+            if (CollectionUtils.isEmpty(group)) {
+                groups.add(new LinkedList<>());
+            } else {
+                groups.add(new LinkedList<>(group));
+            }
+        }
+        return groups;
+    }
+
     private String memoryIdString(Object memoryId) {
         return memoryId == null ? "" : memoryId.toString();
     }
+
+    private ChatMessages findChatMessages(Object memoryId) {
+        Query query = Query.query(Criteria.where("memoryId").is(memoryIdString(memoryId)));
+        return mongoTemplate.findOne(query, ChatMessages.class);
+    }
 }

--
Gitblit v1.9.3