From 75ce71bc37f8683566f1f7d174df53a4c7fa8fc3 Mon Sep 17 00:00:00 2001 From: xueqingkun Date: Thu, 23 May 2024 09:14:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/com/supervision/VecTest.java | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 virtual-patient-web/src/test/java/com/supervision/VecTest.java diff --git a/virtual-patient-web/src/test/java/com/supervision/VecTest.java b/virtual-patient-web/src/test/java/com/supervision/VecTest.java new file mode 100644 index 00000000..6a5ca53e --- /dev/null +++ b/virtual-patient-web/src/test/java/com/supervision/VecTest.java @@ -0,0 +1,130 @@ +package com.supervision; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.lang.Assert; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.NumberUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.json.JSONArray; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import cn.hutool.poi.excel.ExcelReader; +import cn.hutool.poi.excel.ExcelUtil; +import com.supervision.model.AskTemplateQuestionLibrary; +import com.supervision.model.CommonDic; +import com.supervision.service.AskTemplateQuestionLibraryService; +import com.supervision.service.CommonDicService; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Slf4j +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@RunWith(SpringJUnit4ClassRunner.class) +public class VecTest { + + + @Autowired + private AskTemplateQuestionLibraryService askTemplateQuestionLibraryService; + + @Autowired + private CommonDicService commonDicService; + + + private static final String BASE_URL = "http://192.168.10.137:8711"; + @Test + public void mainTest() throws Exception { + String filePath = "F:\\tmp\\1\\副本医生临床问诊问题收集v1.xlsx"; + + ExcelReader reader = ExcelUtil.getReader(filePath,1); + List> dataList = reader.readAll(); + + // 加载数据库中的问题数据 + List questionLibraries = askTemplateQuestionLibraryService.list(); + Map idMapQuestion = questionLibraries.stream().collect(Collectors.toMap(AskTemplateQuestionLibrary::getId, v -> v)); + List commonDics = commonDicService.lambdaQuery().eq(CommonDic::getGroupCode, "AQT").list(); + Map pathMapDic = commonDics.stream().collect(Collectors.toMap(CommonDic::getId, v -> v)); + + + for (Map map : dataList) { + String question = MapUtil.getStr(map, "部分问诊问题(示例)"); + String questionCode = askMatch(StrUtil.trim(question)); + if (StrUtil.isEmpty(questionCode)){ + continue; + } + map.put("匹配到的问题Code",questionCode); + if (!NumberUtil.isNumber(questionCode)){ + log.warn("question:{}的匹配Code不是数字:{}",question,questionCode); + continue; + } + AskTemplateQuestionLibrary library = idMapQuestion.get(questionCode); + if (ObjectUtil.isNotEmpty(library)){ + map.put("匹配到的问题",library.getDescription()); + CommonDic commonDic = pathMapDic.get(library.getDictId()); + if (ObjectUtil.isNotEmpty(commonDic)){ + String nameZhPath = commonDic.getNameZhPath(); + List split = StrUtil.split(nameZhPath, "/"); + String first = split.get(0); + String second = null; + if (split.size()>1){ + second = split.get(1); + } + if (StrUtil.equals(MapUtil.getStr(map,"一级问诊类目"),first) + && StrUtil.equals(MapUtil.getStr(map,"二级问诊类目"),second)){ + map.put("匹配结果","正确"); + }else { + map.put("匹配结果","错误"); + } + map.put("匹配到的问题路径",commonDic.getNameZhPath()); + map.put("匹配到的字典id",commonDic.getId()); + } + } + + } + + log.info("开始把结果数据写入表格"); + ExcelUtil.getWriter(filePath,"结果对比").write(dataList).flush() ; + log.info("结束把结果数据写入表格"); + } + + + private String askMatch(String question){ + + Assert.notEmpty(question, "问题不能为空"); + Map params = new HashMap<>(); + params.put("question",question); + HttpRequest request = HttpRequest.post(BASE_URL + "/matchQuestion") + .body(JSONUtil.toJsonStr(params)); + log.info("askMatch:question:{}", question); + try (HttpResponse response = request.execute()){ + String body = response.body(); + log.info("askMatch:响应结果:{}", body); + JSONObject entries = JSONUtil.parseObj(body); + if (entries.get("code",Integer.class) != 200){ + log.warn("askMatch:匹配失败"); + return null; + } + JSONArray jsonArray = JSONUtil.parseArray(entries.get("data")); + if (CollUtil.isNotEmpty(jsonArray)){ + return JSONUtil.parseObj(jsonArray.get(0)).get("matchQuestionCode",String.class); + } + }catch (Exception e){ + log.error("chat:请求失败", e); + } + + log.warn("askMatch:匹配失败"); + return null; + } + +}