Răsfoiți Sursa

feat: 在 HealthReportController 添加自动保存解析结果到 JSON 文件

- 新增 saveResultToJsonFile() 方法,将解析结果保存到本地文件
- 保存路径:uploads/json-results/{timestamp}_result.json
- 每次调用 POST /api/health/report/parse-preview 时自动保存
- 使用 Jackson ObjectMapper 格式化输出 JSON
- 日志中记录保存的文件路径和关键信息摘要
- 不影响主要流程,保存失败仅记录警告
李宇 3 luni în urmă
părinte
comite
8af3520589

+ 48 - 3
cfc-backend/src/main/java/com/etotem/cfc/controller/HealthReportController.java

@@ -459,6 +459,15 @@ public class HealthReportController {
             result.put("extractedGender", parsed.getGender());
             result.put("extractedAge", parsed.getAge());
             result.put("needBind", matchResult.getMatchedMemberId() == null && familyId != null);
+            
+            // 保存结果到本地 JSON 文件
+            try {
+                saveResultToJsonFile(result);
+            } catch (Exception e) {
+                log.warn("保存结果到 JSON 文件失败:{}", e.getMessage());
+                // 不影响主要流程,只是记录警告
+            }
+            
             return Result.success(result);
 
         } catch (IOException e) {
@@ -1267,6 +1276,42 @@ public class HealthReportController {
         return Result.success("感谢您的反馈");
     }
 
+    /**
+     * 将解析结果保存到本地 JSON 文件(用于调试)
+     */
+    private void saveResultToJsonFile(Map<String, Object> result) throws IOException {
+        // 创建输出目录
+        String outputDir = "uploads" + File.separator + "json-results";
+        File dir = new File(outputDir);
+        if (!dir.exists()) {
+            dir.mkdirs();
+        }
+        
+        // 生成文件名:timestamp_result.json
+        String timestamp = java.time.LocalDateTime.now().format(
+            java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")
+        );
+        String filename = timestamp + "_result.json";
+        String filePath = outputDir + File.separator + filename;
+        
+        // 写入 JSON 文件
+        try (java.io.PrintWriter writer = new java.io.PrintWriter(filePath, "UTF-8")) {
+            writer.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result));
+        }
+        
+        log.info("✅ 解析结果已保存到 JSON 文件:{}", filePath);
+        
+        // 同时打印部分关键信息到日志(限制长度避免过大)
+        String summary = String.format(
+            "[JSON 保存] draftId=%s, name=%s, matched=%s, confidence=%.2f",
+            result.get("draftId"),
+            result.get("extractedName"),
+            result.get("matchedMemberId") != null ? "找到匹配" : "未匹配",
+            result.get("confidence")
+        );
+        log.info(summary);
+    }
+
     /**
      * 保存上传文件到本地存储
      */
@@ -1276,13 +1321,13 @@ public class HealthReportController {
         if (!dir.exists()) {
             dir.mkdirs();
         }
-
+    
         String originalFilename = file.getOriginalFilename();
         if (originalFilename == null) {
             originalFilename = "report.pdf";
         }
-
-        // 文件名: timestamp_uuid.extension
+    
+        // 文件名:timestamp_uuid.extension
         String extension = "";
         int dotIdx = originalFilename.lastIndexOf('.');
         if (dotIdx > 0) {