|
@@ -0,0 +1,169 @@
|
|
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.dto.HealthAnalysisResult;
|
|
|
|
|
+import com.etotem.cfc.entity.HealthGutFlora;
|
|
|
|
|
+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.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class HealthAnalysisService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private HealthReportService healthReportService;
|
|
|
|
|
+
|
|
|
|
|
+ public HealthAnalysisResult analyze(Long reportId) {
|
|
|
|
|
+ Map<String, Object> detail = healthReportService.getReportDetail(reportId);
|
|
|
|
|
+ HealthReport report = (HealthReport) detail.get("report");
|
|
|
|
|
+ if (report == null) {
|
|
|
|
|
+ log.warn("报告不存在: reportId={}", reportId);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ List<HealthGutFlora> gutFloraList = (List<HealthGutFlora>) detail.getOrDefault("gutFlora", Collections.emptyList());
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ List<HealthIndicator> indicators = (List<HealthIndicator>) detail.getOrDefault("indicators", Collections.emptyList());
|
|
|
|
|
+
|
|
|
|
|
+ List<String> findings = new ArrayList<>();
|
|
|
|
|
+ List<String> suggestions = new ArrayList<>();
|
|
|
|
|
+ Set<String> nutritionTags = new LinkedHashSet<>();
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 总分判断
|
|
|
|
|
+ if (report.getNutritionScore() != null && report.getNutritionScore() < 60) {
|
|
|
|
|
+ findings.add("营养均衡评分偏低(" + report.getNutritionScore() + "分),建议补充多种维生素和矿物质");
|
|
|
|
|
+ suggestions.add("增加富含维生素和矿物质的食物摄入");
|
|
|
|
|
+ nutritionTags.add("维生素矿物质补充");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (report.getGutHealthScore() != null && report.getGutHealthScore() < 60) {
|
|
|
|
|
+ findings.add("菌群健康评分偏低(" + report.getGutHealthScore() + "分),肠道菌群需要调理");
|
|
|
|
|
+ suggestions.add("调节肠道菌群平衡,增加益生元和膳食纤维摄入");
|
|
|
|
|
+ nutritionTags.add("肠道菌群调理");
|
|
|
|
|
+ nutritionTags.add("益生元补充");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 肠型分析
|
|
|
|
|
+ String gutType = report.getGutType();
|
|
|
|
|
+ if (gutType != null && !gutType.isEmpty()) {
|
|
|
|
|
+ if (gutType.contains("拟杆菌") || gutType.contains("Bacteroid")) {
|
|
|
|
|
+ suggestions.add("增加膳食纤维摄入,促进肠道蠕动");
|
|
|
|
|
+ nutritionTags.add("膳食纤维");
|
|
|
|
|
+ } else if (gutType.contains("普氏菌") || gutType.contains("Prevotella")) {
|
|
|
|
|
+ suggestions.add("注意碳水化合物与蛋白质的均衡摄入");
|
|
|
|
|
+ nutritionTags.add("碳水均衡");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 菌种分析
|
|
|
|
|
+ for (HealthGutFlora flora : gutFloraList) {
|
|
|
|
|
+ String name = flora.getBacteriaName();
|
|
|
|
|
+ String status = flora.getStatus();
|
|
|
|
|
+ if (name == null) continue;
|
|
|
|
|
+
|
|
|
|
|
+ if ("偏低".equals(status) || "low".equalsIgnoreCase(status)) {
|
|
|
|
|
+ if (name.contains("双歧杆菌") || name.contains("Bifidobacterium")) {
|
|
|
|
|
+ findings.add("双歧杆菌偏低,肠道保护屏障减弱,建议补充益生元");
|
|
|
|
|
+ suggestions.add("补充低聚糖类益生元(如低聚果糖、低聚半乳糖)");
|
|
|
|
|
+ nutritionTags.add("益生元");
|
|
|
|
|
+ } else if (name.contains("乳酸杆菌") || name.contains("Lactobacillus")) {
|
|
|
|
|
+ findings.add("乳酸杆菌偏低,建议补充益生菌");
|
|
|
|
|
+ suggestions.add("补充含乳酸杆菌的益生菌制剂或发酵食品");
|
|
|
|
|
+ nutritionTags.add("益生菌");
|
|
|
|
|
+ } else if (name.contains("瘤胃球菌") || name.contains("Ruminococcus")) {
|
|
|
|
|
+ nutritionTags.add("膳食纤维");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if ("偏高".equals(status) || "high".equalsIgnoreCase(status)) {
|
|
|
|
|
+ if (name.contains("拟杆菌") || name.contains("Bacteroides")) {
|
|
|
|
|
+ findings.add("拟杆菌属偏高,建议增加膳食纤维摄入以改善菌群结构");
|
|
|
|
|
+ suggestions.add("增加全谷物、蔬菜、豆类等膳食纤维丰富的食物");
|
|
|
|
|
+ nutritionTags.add("膳食纤维");
|
|
|
|
|
+ } else if (name.contains("梭菌") || name.contains("Clostridium")) {
|
|
|
|
|
+ findings.add("条件致病菌偏高,建议关注肠道炎症风险");
|
|
|
|
|
+ suggestions.add("增加抗炎食物摄入(Omega-3、多酚类)");
|
|
|
|
|
+ nutritionTags.add("抗炎营养");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 指标分析
|
|
|
|
|
+ for (HealthIndicator ind : indicators) {
|
|
|
|
|
+ String indName = ind.getIndicatorName();
|
|
|
|
|
+ String indStatus = ind.getStatus();
|
|
|
|
|
+ if (indName == null) continue;
|
|
|
|
|
+
|
|
|
|
|
+ if ("异常".equals(indStatus) || "偏高".equals(indStatus) || "偏低".equals(indStatus)) {
|
|
|
|
|
+ if (indName.contains("维生素D") || indName.contains("25-羟基")) {
|
|
|
|
|
+ findings.add("维生素D水平" + indStatus + ",建议补充维生素D");
|
|
|
|
|
+ nutritionTags.add("维生素D");
|
|
|
|
|
+ } else if (indName.contains("铁") || indName.contains("Ferritin")) {
|
|
|
|
|
+ findings.add(indName + indStatus + ",建议关注铁摄入");
|
|
|
|
|
+ nutritionTags.add("铁元素补充");
|
|
|
|
|
+ } else if (indName.contains("钙")) {
|
|
|
|
|
+ nutritionTags.add("钙补充");
|
|
|
|
|
+ } else if (indName.contains("锌")) {
|
|
|
|
|
+ nutritionTags.add("锌补充");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 菌群多样性判断
|
|
|
|
|
+ if (gutFloraList.size() > 5) {
|
|
|
|
|
+ long abnormalCount = gutFloraList.stream()
|
|
|
|
|
+ .filter(f -> "偏低".equals(f.getStatus()) || "偏高".equals(f.getStatus()))
|
|
|
|
|
+ .count();
|
|
|
|
|
+ if (abnormalCount > gutFloraList.size() / 3) {
|
|
|
|
|
+ findings.add("菌群多样性偏低,多项菌群指标异常,建议全面改善饮食结构");
|
|
|
|
|
+ suggestions.add("增加食物多样性,摄入多种类型的发酵食品和膳食纤维");
|
|
|
|
|
+ nutritionTags.add("全面营养调理");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 6. 组装菌群概要
|
|
|
|
|
+ Map<String, List<HealthGutFlora>> grouped = gutFloraList.stream()
|
|
|
|
|
+ .filter(f -> f.getCategory() != null)
|
|
|
|
|
+ .collect(Collectors.groupingBy(HealthGutFlora::getCategory));
|
|
|
|
|
+
|
|
|
|
|
+ List<HealthAnalysisResult.FloraSummary> summaries = new ArrayList<>();
|
|
|
|
|
+ for (Map.Entry<String, List<HealthGutFlora>> entry : grouped.entrySet()) {
|
|
|
|
|
+ HealthAnalysisResult.FloraSummary summary = new HealthAnalysisResult.FloraSummary();
|
|
|
|
|
+ summary.setCategory(entry.getKey());
|
|
|
|
|
+ long normal = entry.getValue().stream()
|
|
|
|
|
+ .filter(f -> "正常".equals(f.getStatus()) || "normal".equalsIgnoreCase(f.getStatus()))
|
|
|
|
|
+ .count();
|
|
|
|
|
+ summary.setNormalCount((int) normal);
|
|
|
|
|
+ summary.setAbnormalCount(entry.getValue().size() - (int) normal);
|
|
|
|
|
+ summary.setKeyAbnormal(entry.getValue().stream()
|
|
|
|
|
+ .filter(f -> !"正常".equals(f.getStatus()) && !"normal".equalsIgnoreCase(f.getStatus()))
|
|
|
|
|
+ .map(HealthGutFlora::getBacteriaName)
|
|
|
|
|
+ .filter(Objects::nonNull)
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ summaries.add(summary);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ HealthAnalysisResult result = new HealthAnalysisResult();
|
|
|
|
|
+ result.setReportId(reportId);
|
|
|
|
|
+ result.setNutritionScore(report.getNutritionScore());
|
|
|
|
|
+ result.setGutHealthScore(report.getGutHealthScore());
|
|
|
|
|
+ result.setGutType(report.getGutType());
|
|
|
|
|
+ result.setFloraSummary(summaries);
|
|
|
|
|
+ result.setFindings(findings);
|
|
|
|
|
+ result.setSuggestions(suggestions);
|
|
|
|
|
+ result.setNutritionTags(new ArrayList<>(nutritionTags));
|
|
|
|
|
+
|
|
|
|
|
+ log.info("健康分析完成: reportId={}, findings={}, tags={}",
|
|
|
|
|
+ reportId, findings.size(), nutritionTags);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public HealthAnalysisResult analyzeLatestByUser(Long userId) {
|
|
|
|
|
+ HealthReport report = healthReportService.getLatestReport(userId);
|
|
|
|
|
+ if (report == null) return null;
|
|
|
|
|
+ return analyze(report.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|