QuYueTing 1 maand geleden
bovenliggende
commit
b1dbb10642

+ 30 - 3
fhKeeper/formulahousekeeper/customerBuler-crm/src/pages/analysis/api.ts

@@ -102,10 +102,26 @@ export type AIQuestionParams = {
   url?: string;
 };
 
-export type AIQuestionResponse = {
+export interface ChatContent {
+  type: 0 | 1;
+  content: string;
+  questionId?: number;
+}
+
+export interface LatestQuestionResponse {
   code: string;
-  data: string;
-};
+  data: {
+    contents: ChatContent[];
+  };
+}
+
+export interface AIQuestionResponse {
+  code: string;
+  data: {
+    queryRes: string;
+    questionId: number;
+  };
+}
 
 export async function askAIQuestion(
   payload: AIQuestionParams
@@ -124,3 +140,14 @@ export async function uploadFileApi(file: File): Promise<UploadFileResponse> {
   
   return await requestUploadFile('/common/uploadFile', formData);
 }
+
+export interface ChatContent {
+  type: 0 | 1;
+  content: string;
+  createTime: string;
+  questionId?: number;
+}
+
+export async function getLatestQuestionList(): Promise<LatestQuestionResponse> {
+  return await post('/aiQuestion/getLatestQuestionList');
+}

+ 59 - 7
fhKeeper/formulahousekeeper/customerBuler-crm/src/pages/analysis/components/AIChat.vue

@@ -111,12 +111,22 @@
 </template>
 
 <script lang="ts" setup>
-import { ref, reactive, nextTick, computed } from 'vue';
+import { ref, reactive, nextTick, computed, onMounted } from 'vue';
 import { marked } from 'marked';
 import { ChatLineRound, User, Download } from '@element-plus/icons-vue';
 import { Document, Paragraph, TextRun, Packer } from 'docx';
-import { askAIQuestion, uploadFileApi, type AIQuestionParams, type UploadFileResponse } from '../api';
+import { 
+  askAIQuestion,
+  uploadFileApi, 
+  getLatestQuestionList,
+  type AIQuestionParams,
+  type UploadFileResponse,
+  type ChatContent,
+  type LatestQuestionResponse,
+  type AIQuestionResponse
+} from '../api';
 import { ElMessage } from 'element-plus/es'
+import * as internal from 'stream';
 const renderMarkdown = (content: string): string => {
   // Configure marked with options
   marked.setOptions({
@@ -300,9 +310,45 @@ const dateRange = ref([getFirstDayOfMonth(), new Date()]);
 // Chat functionality
 const inputMessage = ref('请进行数据分析,给一个总结报告,不超过300字');
 const loading = ref(false);
-const messages = reactive<ChatMessage[]>([
-  { role: 'assistant', content: '你好,需要分析查询哪些数据,请交给我' },
-]);
+const messages = reactive<ChatMessage[]>([]);
+const questionId = ref<number | null>(null);
+
+const isSameDay = (dateString: string, compareDate: Date) => {
+  // Parse yyyy-MM-dd hh:mm:ss format
+  const [datePart] = dateString.split(' ');
+  const [year, month, day] = datePart.split('-').map(Number);
+  
+  return year === compareDate.getFullYear() &&
+         month - 1 === compareDate.getMonth() && // Months are 0-indexed in JS
+         day === compareDate.getDate();
+};
+
+onMounted(async () => {
+  try {
+    const result = await getLatestQuestionList();
+    if (result.code === 'ok' && result.data.contents) {
+      result.data.contents.forEach(content => {
+        messages.push({
+          role: content.type === 0 ? 'assistant' : 'user',
+          content: content.content
+        });
+
+        // Check if createTime is today and set questionId
+        if (content.createTime && content.questionId) {
+          if (isSameDay(content.createTime, new Date())) {
+            questionId.value = content.questionId;
+          }
+        }
+      });
+    } else {
+      // Default message if no history
+      messages.push({ role: 'assistant', content: '你好,需要分析查询哪些数据,请交给我' });
+    }
+  } catch (error) {
+    console.error('Failed to load chat history:', error);
+    messages.push({ role: 'assistant', content: '你好,需要分析查询哪些数据,请交给我' });
+  }
+});
 
 const sendMessage = async () => {
   if (!inputMessage.value.trim() || loading.value) return;
@@ -322,7 +368,7 @@ const sendMessage = async () => {
       'free': 4
     };
 
-    const params: AIQuestionParams = {
+    const params: AIQuestionParams & { questionId?: number } = {
       questionDataSource: dataSourceMap[dataSource.value],
       sourceContent: dataSource.value === 'system' ? systemTable.value : '',
       content: inputMessage.value,
@@ -331,12 +377,18 @@ const sendMessage = async () => {
       url: dataSource.value === 'upload' ? uploadedFilePath.value : ''
     };
 
+    // Only include questionId if we have one from today's conversation
+    if (questionId.value) {
+      params.questionId = questionId.value;
+    }
+
     const result = await askAIQuestion(params);
     messages[thinkingIndex] = {
       role: 'assistant',
-      content: result.data || '根据您的请求,我已分析了相关数据。分析结果显示...',
+      content: result.data.queryRes || '根据您的请求,我已分析了相关数据。分析结果显示...',
       loading: false
     };
+    questionId.value = result.data.questionId;
   } catch (error) {
     console.error('API error:', error);
     messages[thinkingIndex] = {