|
|
@@ -0,0 +1,199 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.HealthIndicator;
|
|
|
+import com.etotem.cfc.entity.HealthReport;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 报告档案对比服务(能力键 {@code report.archive.manage},SC-P-24 / TC-P-24「对比展示差异」)。
|
|
|
+ *
|
|
|
+ * <p>契约宿主:{@code §4.59.10} 未实现端点 {@code POST /api/health/report/compare} 的服务层载体。
|
|
|
+ * 口径:报告类型一致才可比对(SC-P-25 同类型口径 / TC-P-21),返回报告A/B基础信息、
|
|
|
+ * 指标级差异标注(same / different / onlyInA / onlyInB)与总分差异。</p>
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ReportArchiveCompareService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private HealthReportService healthReportService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报告对比入口。
|
|
|
+ *
|
|
|
+ * @param reportAId 报告 A 的 ID
|
|
|
+ * @param reportBId 报告 B 的 ID
|
|
|
+ * @return 比较结果 Map,含 comparable(是否可比对)、message、reportA/reportB 摘要、
|
|
|
+ * differences(指标级差异标注)、scoreComparison(总分差异)
|
|
|
+ */
|
|
|
+ public Map<String, Object> compareReports(Long reportAId, Long reportBId) {
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
+
|
|
|
+ if (reportAId == null || reportBId == null) {
|
|
|
+ result.put("comparable", false);
|
|
|
+ result.put("message", "reportAId 与 reportBId 不能为空");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ HealthReport reportA = healthReportService.getReportById(reportAId);
|
|
|
+ HealthReport reportB = healthReportService.getReportById(reportBId);
|
|
|
+
|
|
|
+ if (reportA == null || reportB == null) {
|
|
|
+ result.put("comparable", false);
|
|
|
+ result.put("message", "报告不存在");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 类型一致性检查:仅同类型报告可比对(SC-P-25 / TC-P-21)
|
|
|
+ if (!Objects.equals(reportA.getReportType(), reportB.getReportType())) {
|
|
|
+ result.put("comparable", false);
|
|
|
+ result.put("message", "报告类型不一致,无法对比");
|
|
|
+ result.put("reportAType", reportA.getReportType());
|
|
|
+ result.put("reportBType", reportB.getReportType());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ result.put("comparable", true);
|
|
|
+ result.put("message", "报告类型一致,可进行差异对比");
|
|
|
+ result.put("reportAId", reportAId);
|
|
|
+ result.put("reportBId", reportBId);
|
|
|
+ result.put("reportA", buildReportSummary(reportA));
|
|
|
+ result.put("reportB", buildReportSummary(reportB));
|
|
|
+
|
|
|
+ // 指标级差异标注
|
|
|
+ result.put("differences", compareIndicators(reportA, reportB));
|
|
|
+
|
|
|
+ // 总分差异
|
|
|
+ result.put("scoreComparison", buildScoreComparison(reportA, reportB));
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> buildReportSummary(HealthReport report) {
|
|
|
+ Map<String, Object> summary = new LinkedHashMap<>();
|
|
|
+ summary.put("id", report.getId());
|
|
|
+ summary.put("reportType", report.getReportType());
|
|
|
+ summary.put("reportDate", report.getReportDate() != null
|
|
|
+ ? new java.text.SimpleDateFormat("yyyy-MM-dd").format(report.getReportDate()) : null);
|
|
|
+ summary.put("overallScore", report.getOverallScore());
|
|
|
+ summary.put("gutHealthScore", report.getGutHealthScore());
|
|
|
+ summary.put("nutritionScore", report.getNutritionScore());
|
|
|
+ summary.put("fileUrl", report.getFileUrl());
|
|
|
+ summary.put("status", report.getStatus());
|
|
|
+ return summary;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> buildScoreComparison(HealthReport reportA, HealthReport reportB) {
|
|
|
+ Map<String, Object> scoreDiff = new LinkedHashMap<>();
|
|
|
+ Integer scoreA = reportA.getOverallScore();
|
|
|
+ Integer scoreB = reportB.getOverallScore();
|
|
|
+ scoreDiff.put("scoreA", scoreA);
|
|
|
+ scoreDiff.put("scoreB", scoreB);
|
|
|
+ if (scoreA != null && scoreB != null) {
|
|
|
+ scoreDiff.put("difference", scoreB - scoreA);
|
|
|
+ scoreDiff.put("percentageDiff", scoreA != 0 ? Double.valueOf((scoreB - scoreA) * 100.0 / scoreA) : null);
|
|
|
+ } else {
|
|
|
+ scoreDiff.put("difference", null);
|
|
|
+ scoreDiff.put("percentageDiff", null);
|
|
|
+ }
|
|
|
+ return scoreDiff;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 比较两份报告的指标明细,返回差异标注数组(SC-P-24「差异清晰标注」)。
|
|
|
+ *
|
|
|
+ * @param reportA 报告 A
|
|
|
+ * @param reportB 报告 B
|
|
|
+ * @return 差异标注数组,每项含 indicatorName、diffStatus(same/different/onlyInA/onlyInB)等
|
|
|
+ */
|
|
|
+ private List<Map<String, Object>> compareIndicators(HealthReport reportA, HealthReport reportB) {
|
|
|
+ List<Map<String, Object>> diffs = new ArrayList<>();
|
|
|
+
|
|
|
+ List<HealthIndicator> indicatorsA = healthReportService.getReportIndicators(reportA.getId());
|
|
|
+ List<HealthIndicator> indicatorsB = healthReportService.getReportIndicators(reportB.getId());
|
|
|
+
|
|
|
+ // 按指标名建映射,便于差异检测
|
|
|
+ Map<String, HealthIndicator> mapA = indexByName(indicatorsA);
|
|
|
+ Map<String, HealthIndicator> mapB = indexByName(indicatorsB);
|
|
|
+
|
|
|
+ Set<String> allNames = new LinkedHashSet<>();
|
|
|
+ allNames.addAll(mapA.keySet());
|
|
|
+ allNames.addAll(mapB.keySet());
|
|
|
+
|
|
|
+ for (String name : allNames) {
|
|
|
+ HealthIndicator aInd = mapA.get(name);
|
|
|
+ HealthIndicator bInd = mapB.get(name);
|
|
|
+
|
|
|
+ Map<String, Object> diff = new LinkedHashMap<>();
|
|
|
+ diff.put("indicatorName", name);
|
|
|
+
|
|
|
+ if (aInd != null && bInd != null) {
|
|
|
+ // 双方都有:比较 value/status/refRange
|
|
|
+ diff.put("inBoth", true);
|
|
|
+ diff.put("statusA", aInd.getStatus());
|
|
|
+ diff.put("statusB", bInd.getStatus());
|
|
|
+ diff.put("valueA", aInd.getIndicatorValue());
|
|
|
+ diff.put("valueB", bInd.getIndicatorValue());
|
|
|
+ diff.put("refRangeA", aInd.getRefRange());
|
|
|
+ diff.put("refRangeB", bInd.getRefRange());
|
|
|
+
|
|
|
+ boolean sameValue = Objects.equals(aInd.getIndicatorValue(), bInd.getIndicatorValue());
|
|
|
+ boolean sameStatus = Objects.equals(aInd.getStatus(), bInd.getStatus());
|
|
|
+ boolean sameRefRange = Objects.equals(aInd.getRefRange(), bInd.getRefRange());
|
|
|
+
|
|
|
+ if (sameValue && sameStatus && sameRefRange) {
|
|
|
+ diff.put("diffStatus", "same");
|
|
|
+ } else {
|
|
|
+ diff.put("diffStatus", "different");
|
|
|
+ Map<String, Object> fieldDiffs = new LinkedHashMap<>();
|
|
|
+ if (!sameValue) fieldDiffs.put("valueDifferent", true);
|
|
|
+ if (!sameStatus) fieldDiffs.put("statusDifferent", true);
|
|
|
+ if (!sameRefRange) fieldDiffs.put("refRangeDifferent", true);
|
|
|
+ diff.put("fieldDiffs", fieldDiffs);
|
|
|
+ }
|
|
|
+ } else if (aInd != null) {
|
|
|
+ // 仅 A 有
|
|
|
+ diff.put("inBoth", false);
|
|
|
+ diff.put("diffStatus", "onlyInA");
|
|
|
+ diff.put("valueA", aInd.getIndicatorValue());
|
|
|
+ diff.put("statusA", aInd.getStatus());
|
|
|
+ diff.put("refRangeA", aInd.getRefRange());
|
|
|
+ } else if (bInd != null) {
|
|
|
+ // 仅 B 有
|
|
|
+ diff.put("inBoth", false);
|
|
|
+ diff.put("diffStatus", "onlyInB");
|
|
|
+ diff.put("valueB", bInd.getIndicatorValue());
|
|
|
+ diff.put("statusB", bInd.getStatus());
|
|
|
+ diff.put("refRangeB", bInd.getRefRange());
|
|
|
+ } else {
|
|
|
+ diff.put("diffStatus", "none");
|
|
|
+ }
|
|
|
+ diffs.add(diff);
|
|
|
+ }
|
|
|
+
|
|
|
+ return diffs;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, HealthIndicator> indexByName(List<HealthIndicator> indicators) {
|
|
|
+ Map<String, HealthIndicator> map = new LinkedHashMap<>();
|
|
|
+ if (indicators == null) {
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ for (HealthIndicator ind : indicators) {
|
|
|
+ if (ind.getIndicatorName() != null) {
|
|
|
+ map.put(ind.getIndicatorName(), ind);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+}
|