From 988b593fb8095b2be166321dba278d3a1d470f30 Mon Sep 17 00:00:00 2001
From: liu <liujiatong112@163.com>
Date: Fri, 7 Jun 2024 10:46:30 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../QaKnowledgeManageController.java          | 12 ------
 .../manage/feign/QaKnowledgeManageClient.java | 18 --------
 .../impl/QaKnowledgeManageServiceImpl.java    | 41 +++++++++++++++----
 .../src/main/resources/application.yml        |  2 +
 4 files changed, 34 insertions(+), 39 deletions(-)
 delete mode 100644 virtual-patient-manage/src/main/java/com/supervision/manage/feign/QaKnowledgeManageClient.java

diff --git a/virtual-patient-manage/src/main/java/com/supervision/manage/controller/qaKnowledge/QaKnowledgeManageController.java b/virtual-patient-manage/src/main/java/com/supervision/manage/controller/qaKnowledge/QaKnowledgeManageController.java
index 5c302741..06568266 100644
--- a/virtual-patient-manage/src/main/java/com/supervision/manage/controller/qaKnowledge/QaKnowledgeManageController.java
+++ b/virtual-patient-manage/src/main/java/com/supervision/manage/controller/qaKnowledge/QaKnowledgeManageController.java
@@ -1,31 +1,19 @@
 package com.supervision.manage.controller.qaKnowledge;
 
-import com.supervision.dto.QaKnowledgeDTO;
 import com.supervision.manage.service.QaKnowledgeManageService;
-import com.supervision.service.AskTemplateQuestionLibraryService;
 import io.swagger.v3.oas.annotations.Operation;
 import lombok.RequiredArgsConstructor;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.List;
-
 @RestController
 @RequestMapping("qaKnowledge")
 @RequiredArgsConstructor
 public class QaKnowledgeManageController {
 
-    private final AskTemplateQuestionLibraryService askTemplateQuestionLibraryService;
-
     private final QaKnowledgeManageService qaKnowledgeManageService;
 
-    @Operation(summary = "获取问答知识库")
-    @GetMapping("queryQaKnowledge")
-    public List<QaKnowledgeDTO> queryQaKnowledge() {
-        return askTemplateQuestionLibraryService.queryQaKnowledge();
-    }
-
     @Operation(summary = "手动刷新知识库")
     @GetMapping("refreshQaKnowledge")
     public void refreshQaKnowledge() {
diff --git a/virtual-patient-manage/src/main/java/com/supervision/manage/feign/QaKnowledgeManageClient.java b/virtual-patient-manage/src/main/java/com/supervision/manage/feign/QaKnowledgeManageClient.java
deleted file mode 100644
index 3927c6ec..00000000
--- a/virtual-patient-manage/src/main/java/com/supervision/manage/feign/QaKnowledgeManageClient.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.supervision.manage.feign;
-
-import com.supervision.dto.QaKnowledgeDTO;
-import org.springframework.cloud.openfeign.FeignClient;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-
-import java.util.List;
-
-@FeignClient(name = "qaKnowledgeManageClient", url = "${qaSimilarity}")
-public interface QaKnowledgeManageClient {
-
-
-    @PostMapping("updateDatabase")
-    void refreshQuestionList(@RequestBody List<QaKnowledgeDTO> qaKnowledgeVOList);
-
-
-}
diff --git a/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/QaKnowledgeManageServiceImpl.java b/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/QaKnowledgeManageServiceImpl.java
index 4eaf6e17..48ad5599 100644
--- a/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/QaKnowledgeManageServiceImpl.java
+++ b/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/QaKnowledgeManageServiceImpl.java
@@ -1,16 +1,19 @@
 package com.supervision.manage.service.impl;
 
-import cn.hutool.core.date.DateUtil;
-import cn.hutool.core.date.TimeInterval;
-import com.supervision.dto.QaKnowledgeDTO;
-import com.supervision.manage.feign.QaKnowledgeManageClient;
+import cn.hutool.core.collection.CollUtil;
 import com.supervision.manage.service.QaKnowledgeManageService;
+import com.supervision.model.AskTemplateQuestionLibrary;
+import com.supervision.model.AskTemplateQuestionSimilarity;
 import com.supervision.service.AskTemplateQuestionLibraryService;
+import com.supervision.service.AskTemplateQuestionSimilarityService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.ai.document.Document;
+import org.springframework.ai.vectorstore.RedisVectorStore;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.Map;
 
 @Service
 @RequiredArgsConstructor
@@ -19,12 +22,32 @@ public class QaKnowledgeManageServiceImpl implements QaKnowledgeManageService {
 
     private final AskTemplateQuestionLibraryService askTemplateQuestionLibraryService;
 
-    private final QaKnowledgeManageClient qaKnowledgeManageClient;
+    private final AskTemplateQuestionSimilarityService askTemplateQuestionSimilarityService;
+
+    private final RedisVectorStore redisVectorStore;
+
 
     public void refreshQaKnowledge() {
-        TimeInterval timer = DateUtil.timer();
-        List<QaKnowledgeDTO> queryQaKnowledge = askTemplateQuestionLibraryService.queryQaKnowledge();
-        qaKnowledgeManageClient.refreshQuestionList(queryQaKnowledge);
-        log.info("QaKnowledgeManageServiceImpl : refreshQaKnowledge耗时:{} s" , timer.intervalSecond());
+        // 修改为手动刷新向量库
+        List<AskTemplateQuestionLibrary> list = askTemplateQuestionLibraryService.list();
+        if (CollUtil.isEmpty(list)) {
+            return;
+        }
+        for (AskTemplateQuestionLibrary askTemplateQuestionLibrary : list) {
+            String description = askTemplateQuestionLibrary.getStandardQuestion();
+            redisVectorStore.add(List.of(new Document(description,
+                    Map.of("type", "1",
+                            "libraryQuestionId", askTemplateQuestionLibrary.getId(),
+                            "matchQuestionId", askTemplateQuestionLibrary.getId(),
+                            "dictId", String.valueOf(askTemplateQuestionLibrary.getDictId())))));
+            List<AskTemplateQuestionSimilarity> similarityList = askTemplateQuestionSimilarityService.lambdaQuery().eq(AskTemplateQuestionSimilarity::getLibraryId, askTemplateQuestionLibrary.getId()).list();
+            for (AskTemplateQuestionSimilarity askTemplateQuestionSimilarity : similarityList) {
+                redisVectorStore.add(List.of(new Document(askTemplateQuestionSimilarity.getSimilarityQuestion(),
+                        Map.of("type", "2",
+                                "libraryQuestionId", askTemplateQuestionSimilarity.getLibraryId(),
+                                "matchQuestionId", askTemplateQuestionSimilarity.getId(),
+                                "dictId", String.valueOf(askTemplateQuestionLibrary.getDictId())))));
+            }
+        }
     }
 }
diff --git a/virtual-patient-manage/src/main/resources/application.yml b/virtual-patient-manage/src/main/resources/application.yml
index 8d8f4111..0818d4b3 100644
--- a/virtual-patient-manage/src/main/resources/application.yml
+++ b/virtual-patient-manage/src/main/resources/application.yml
@@ -13,6 +13,8 @@ server:
     direct-buffers: true
 
 spring:
+  main:
+    allow-bean-definition-overriding: true
   servlet:
     multipart:
       max-file-size: 100MB