|
|
@@ -100,6 +100,26 @@ public class HealthReportController {
|
|
|
private static final String LANGGRAPH_BASE_URL = "http://localhost:9000";
|
|
|
private static final String REPORT_PARSE_URL = LANGGRAPH_BASE_URL + "/api/v1/report/parse";
|
|
|
|
|
|
+ // ==================== guhe_v2 异步路由契约锚点(P0-2 / §六 6.1,能力键 health.report.guheV2AsyncRoute) ====================
|
|
|
+
|
|
|
+ /** §6.1 上传响应新增键:报告格式(guhe / guhe_v2 / beijing / aidefansi) */
|
|
|
+ public static final String RESPONSE_KEY_REPORT_FORMAT = "reportFormat";
|
|
|
+
|
|
|
+ /** §6.1 上传响应新增键:解析方式(local / langgraph) */
|
|
|
+ public static final String RESPONSE_KEY_PARSE_METHOD = "parseMethod";
|
|
|
+
|
|
|
+ /** §6.1 collect/status 响应新增键:_解析自检(JSON) */
|
|
|
+ public static final String RESPONSE_KEY_PARSE_SELF_CHECK = "parseSelfCheck";
|
|
|
+
|
|
|
+ /** 解析方式值:LangGraph 异步解析(guhe_v2 一律走此通道) */
|
|
|
+ public static final String PARSE_METHOD_LANGGRAPH = "langgraph";
|
|
|
+
|
|
|
+ /** 解析方式值:Java 本地同步解析(旧版 guhe / beijing 既有路径) */
|
|
|
+ public static final String PARSE_METHOD_LOCAL = "local";
|
|
|
+
|
|
|
+ /** 报告格式值:谷禾 2026-11 新版 32 键报告(P0-2 自动切 upload-only + submitAsyncCollect 的识别标记) */
|
|
|
+ public static final String FORMAT_GUHE_V2 = ParsedReportResult.FORMAT_GUHE_V2;
|
|
|
+
|
|
|
/**
|
|
|
* LangGraph 服务地址(生产环境通过 application.yml 覆盖,避免 Docker 容器跨主机调用失败)
|
|
|
* TODO(部署): 容器化部署时需确保 Java 上传目录(uploads/health-reports)
|
|
|
@@ -930,6 +950,14 @@ public class HealthReportController {
|
|
|
|
|
|
try {
|
|
|
ParsedReportResult parsed = pdfParseService.parse(file.getInputStream());
|
|
|
+
|
|
|
+ // P0-2(能力键 health.report.guheV2AsyncRoute):识别到 guhe_v2(谷禾 32 键新版)
|
|
|
+ // 自动切换为「仅上传 + 异步采集」(复用现有异步链路),Java 不做本地解析,
|
|
|
+ // 响应返回 draftId + parseMethod="langgraph",前端跳「解析中」等待页轮询 collect/status。
|
|
|
+ if (ParsedReportResult.FORMAT_GUHE_V2.equals(parsed.getReportFormat())) {
|
|
|
+ return routeGuheV2UploadOnly(file, userId, familyId, originalFilename);
|
|
|
+ }
|
|
|
+
|
|
|
if (parsed.getOverallScore() == null && parsed.getGutHealthScore() == null) {
|
|
|
return Result.error("无法解析PDF文件,请确认是募极生物肠道菌群报告");
|
|
|
}
|
|
|
@@ -992,6 +1020,10 @@ public class HealthReportController {
|
|
|
detail.put("extractedReportNumber", parsed.getReportNumber());
|
|
|
detail.put("extractedReportDate", parsed.getReportDate());
|
|
|
detail.put("needBind", matchedMemberId == null);
|
|
|
+ // §6.1(能力键 health.report.guheV2AsyncRoute):上传响应新增 reportFormat / parseMethod 键。
|
|
|
+ // 非 guhe_v2 走 Java 本地同步解析(parseMethod="local");guhe_v2 分支在 routeGuheV2UploadOnly 返回。
|
|
|
+ detail.put(RESPONSE_KEY_REPORT_FORMAT, parsed.getReportFormat());
|
|
|
+ detail.put(RESPONSE_KEY_PARSE_METHOD, PARSE_METHOD_LOCAL);
|
|
|
return Result.success(detail);
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
@@ -1000,6 +1032,68 @@ public class HealthReportController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * P0-2(能力键 {@code health.report.guheV2AsyncRoute}):guhe_v2 报告自动切「仅上传 + 异步采集」分支。
|
|
|
+ *
|
|
|
+ * <p>{@code uploadReport()} 识别到 guhe_v2(谷禾 32 键新版)后调用本方法:复用现有异步链路
|
|
|
+ * ({@code submitAsyncCollect} → LangGraph 携带 {@code reportFormatHint="guhe_v2"} 解析),
|
|
|
+ * <b>不</b>做 Java 本地解析。响应返回 draftId + parseMethod="langgraph",前端跳「解析中」等待页轮询。</p>
|
|
|
+ */
|
|
|
+ private Result<Map<String, Object>> routeGuheV2UploadOnly(MultipartFile file, Long userId, Long familyId,
|
|
|
+ String originalFilename) {
|
|
|
+ try {
|
|
|
+ byte[] fileBytes = file.getBytes();
|
|
|
+ String fileHash = computeMd5(fileBytes);
|
|
|
+
|
|
|
+ // 去重:同一 MD5 已入库 → 返回既有报告;正在采集 → 返回既有草稿(与 upload-only 分支一致)
|
|
|
+ Long existingReportId = healthReportService.findReportIdByFileHash(fileHash, "gut_flora");
|
|
|
+ if (existingReportId != null) {
|
|
|
+ Map<String, Object> dup = new LinkedHashMap<>();
|
|
|
+ dup.put("duplicate", true);
|
|
|
+ dup.put("reportId", existingReportId);
|
|
|
+ dup.put("message", "该报告已上传过,无需重复解析");
|
|
|
+ dup.put(RESPONSE_KEY_REPORT_FORMAT, FORMAT_GUHE_V2);
|
|
|
+ dup.put(RESPONSE_KEY_PARSE_METHOD, PARSE_METHOD_LANGGRAPH);
|
|
|
+ return Result.success(dup);
|
|
|
+ }
|
|
|
+ Long collectingDraftId = healthReportService.findCollectingDraftIdByFileHash(fileHash);
|
|
|
+ if (collectingDraftId != null) {
|
|
|
+ Map<String, Object> dup = new LinkedHashMap<>();
|
|
|
+ dup.put("duplicate", true);
|
|
|
+ dup.put("draftId", collectingDraftId);
|
|
|
+ dup.put("collecting", true);
|
|
|
+ dup.put("message", "该报告正在解析中,请稍候");
|
|
|
+ dup.put(RESPONSE_KEY_REPORT_FORMAT, FORMAT_GUHE_V2);
|
|
|
+ dup.put(RESPONSE_KEY_PARSE_METHOD, PARSE_METHOD_LANGGRAPH);
|
|
|
+ return Result.success(dup);
|
|
|
+ }
|
|
|
+
|
|
|
+ String fileUrl = storageService.storeFile(file, "health-reports");
|
|
|
+ ParsedReportPayload.Payload emptyPayload = new ParsedReportPayload.Payload();
|
|
|
+ String payloadJson = objectMapper.writeValueAsString(emptyPayload);
|
|
|
+
|
|
|
+ HealthReportDraft draft = healthReportDraftService.createDraft(
|
|
|
+ userId, familyId, "gut_flora", fileUrl, originalFilename, payloadJson, fileHash, null);
|
|
|
+
|
|
|
+ // 提交异步采集:LangGraph 携带 reportFormatHint="guhe_v2" 走 guhe_v2 专用提取器
|
|
|
+ reportCollectService.submitAsyncCollect(draft.getId());
|
|
|
+
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
+ result.put("draftId", draft.getId());
|
|
|
+ result.put("fileUrl", fileUrl);
|
|
|
+ result.put("originalFilename", originalFilename);
|
|
|
+ result.put(RESPONSE_KEY_REPORT_FORMAT, FORMAT_GUHE_V2);
|
|
|
+ result.put(RESPONSE_KEY_PARSE_METHOD, PARSE_METHOD_LANGGRAPH);
|
|
|
+ result.put("parseStatus", ReportCollectService.STATUS_COLLECTING);
|
|
|
+ result.put("collecting", true);
|
|
|
+ log.info("guhe_v2 报告已切换为异步 LangGraph 解析 draftId={}, userId={}", draft.getId(), userId);
|
|
|
+ return Result.success(result);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("guhe_v2 报告异步路由失败", e);
|
|
|
+ return Result.error("报告上传失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 两阶段入库 Phase 0: 仅上传文件,不解析。返回 draftId 和 fileUrl,
|
|
|
* 供前端展示「开始解析」按钮并导航至确认页触发异步解析。
|
|
|
@@ -1174,9 +1268,32 @@ public class HealthReportController {
|
|
|
result.put("parseError", draft.getParseError());
|
|
|
result.put("reportId", draft.getConfirmedReportId());
|
|
|
result.put("reportType", draft.getReportType());
|
|
|
+ // §6.1(能力键 health.report.guheV2AsyncRoute):collect/status 响应新增 parseSelfCheck
|
|
|
+ // (JSON,_解析自检)——透传草稿 collectValidation(guhe_v2 解析链路上游回填的自检信息)
|
|
|
+ result.put(RESPONSE_KEY_PARSE_SELF_CHECK, parseSelfCheckOf(draft));
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * §6.1(能力键 {@code health.report.guheV2AsyncRoute}):解析自检(_解析自检)透传。
|
|
|
+ * <p>从草稿 {@code collectValidation}(JSON,含 typeId/expected/matched/pass/missing 及
|
|
|
+ * guhe_v2 解析链路回填的 通过/P0告警数/P1提示数/告警)反序列化返回;无自检数据时返回 null。</p>
|
|
|
+ */
|
|
|
+ private Map<String, Object> parseSelfCheckOf(HealthReportDraft draft) {
|
|
|
+ String validationJson = draft.getCollectValidation();
|
|
|
+ if (validationJson == null || validationJson.trim().isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> selfCheck = objectMapper.readValue(validationJson, Map.class);
|
|
|
+ return selfCheck;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("parseSelfCheck 反序列化失败 draftId={}: {}", draft.getId(), e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 查询当前用户所有"采集中"的草稿(供报告列表顶部展示采集进度)
|
|
|
*/
|