|
@@ -0,0 +1,131 @@
|
|
|
+package com.qizhen.healsphere.web;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.qizhen.healsphere.ExplainAssistant;
|
|
|
+import com.qizhen.healsphere.service.impl.DepartmentServiceImpl;
|
|
|
+import com.qizhen.healsphere.service.impl.ZYApiServiceImpl;
|
|
|
+import com.qizhen.healsphere.web.param.BacteriaGenus;
|
|
|
+import com.qizhen.healsphere.web.param.DetailInfo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.xml.soap.Text;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@Slf4j
|
|
|
+public class ZYController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ZYApiServiceImpl zyApiService;
|
|
|
+
|
|
|
+ @PostMapping("/info")
|
|
|
+ @ResponseBody
|
|
|
+ public DetailInfo getBacteriaInfo(@RequestParam String input) {
|
|
|
+ // 获取细菌信息
|
|
|
+ List<BacteriaGenus> bacteriaGenusList = zyApiService.getBacteriaInfo(input);
|
|
|
+
|
|
|
+ // 获取解释信息并清洗
|
|
|
+ List<String> explainList = getExplainAssistant(input);
|
|
|
+
|
|
|
+ // 封装返回值到 DetailInfo 对象
|
|
|
+ DetailInfo detailInfo = new DetailInfo();
|
|
|
+ detailInfo.setBacteriaGenusList(bacteriaGenusList);
|
|
|
+ detailInfo.setExplainList(explainList);
|
|
|
+
|
|
|
+ return detailInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取解释信息并清洗
|
|
|
+ *
|
|
|
+ * @param input 用户输入
|
|
|
+ * @return 清洗后的解释列表
|
|
|
+ */
|
|
|
+ public List<String> getExplainAssistant(String input) {
|
|
|
+ String appId = "03a62954-9bb0-4abe-9c7e-b7555cd3c2a0";
|
|
|
+ String response = ExplainAssistant.call(appId, ExplainAssistant.getConversationId(appId), input);
|
|
|
+
|
|
|
+ List<String> resultList = new ArrayList<>();
|
|
|
+ if (response != null && !response.isEmpty()) {
|
|
|
+ try {
|
|
|
+ // 解析 JSON 响应
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ JsonNode rootNode = mapper.readTree(response);
|
|
|
+
|
|
|
+ // 提取 "answer" 或 "text" 字段
|
|
|
+ if (rootNode.has("answer")) {
|
|
|
+ resultList.add(rootNode.get("answer").asText());
|
|
|
+ } else if (rootNode.has("text")) {
|
|
|
+ resultList.add(rootNode.get("text").asText());
|
|
|
+ } else {
|
|
|
+ // 如果 JSON 中没有 "answer" 或 "text",直接按原始逻辑处理
|
|
|
+ processRawResponse(response, resultList);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Failed to parse JSON response: {}", response, e);
|
|
|
+ // 如果解析失败,按原始逻辑处理
|
|
|
+ processRawResponse(response, resultList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清洗和格式化解释文本
|
|
|
+ return cleanExplainList(resultList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理原始响应(按 ### 分割并清洗)
|
|
|
+ *
|
|
|
+ * @param response 原始响应
|
|
|
+ * @param resultList 结果列表
|
|
|
+ */
|
|
|
+ private void processRawResponse(String response, List<String> resultList) {
|
|
|
+ // 按 ### 分割字符串
|
|
|
+ String[] parts = response.split("###");
|
|
|
+ for (String part : parts) {
|
|
|
+ // 替换 \n\n 为 <br/>
|
|
|
+ String cleanedPart = part.trim().replace("\n\n", "<br/>");
|
|
|
+ if (!cleanedPart.isEmpty()) {
|
|
|
+ resultList.add(cleanedPart);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清洗解释列表
|
|
|
+ *
|
|
|
+ * @param rawExplains 原始解释列表
|
|
|
+ * @return 清洗后的解释列表
|
|
|
+ */
|
|
|
+ private List<String> cleanExplainList(List<String> rawExplains) {
|
|
|
+ List<String> cleanedList = new ArrayList<>();
|
|
|
+ for (String rawExplain : rawExplains) {
|
|
|
+ // 移除 JSON 元数据(如 "usage": {...})
|
|
|
+ String cleanedText = rawExplain
|
|
|
+ .replaceAll("\"usage\":\\s*\\{[^}]*\\}", "")
|
|
|
+ .replaceAll("\"event_code\":\\s*\\d+", "")
|
|
|
+ .replaceAll("\\{\\s*.*?\\s*\\}", ""); // 移除残留的 JSON 结构
|
|
|
+
|
|
|
+ // 替换转义字符
|
|
|
+ cleanedText = cleanedText
|
|
|
+ .replace("\n\n", "<br/>") // 替换单个换行符
|
|
|
+ .replace("\"", "")
|
|
|
+ .replace("\\\\", "");
|
|
|
+
|
|
|
+ // 按 ### 分割并添加到 cleanedList
|
|
|
+ String[] parts = cleanedText.split("###");
|
|
|
+ for (String part : parts) {
|
|
|
+ if (!part.trim().isEmpty()) {
|
|
|
+ cleanedList.add(part.trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return cleanedList;
|
|
|
+ }
|
|
|
+}
|