Browse Source

Merge branch 'task/S-cfc-463' into cfclub

E2E Test Bot 2 hours ago
parent
commit
a9824c4ee9

+ 68 - 0
cfc-backend/src/main/java/com/etotem/cfc/config/DatabaseInitializer.java

@@ -3678,6 +3678,10 @@ insertSysConfigSeed("gift_membership_pay_price", "198000", "送会员商品达
         // 迁移502: 一对一陪跑/多方协调工单 butler_one_on_one_coach_records(能力键 butler.oneOnOne.coach,SC-H-02/H-03)
         // 迁移502: 一对一陪跑/多方协调工单 butler_one_on_one_coach_records(能力键 butler.oneOnOne.coach,SC-H-02/H-03)
         // 承载档案+服务计划(可配 SLA/回访节奏)、回访/咨询留痕、协调全过程留痕;CREATE TABLE IF NOT EXISTS 幂等
         // 承载档案+服务计划(可配 SLA/回访节奏)、回访/咨询留痕、协调全过程留痕;CREATE TABLE IF NOT EXISTS 幂等
         createButlerOneOnOneCoachRecordsTable();
         createButlerOneOnOneCoachRecordsTable();
+
+        // 迁移503: 谷禾新版物种功能库 health_species_library + 异常菌派生视图 health_abnormal_species
+        // (能力键 health.report.abnormalSpecies,P0-6 异常菌清单接口 ▲ 直出;设计 §三 P0-4 + §五 DDL④⑤)
+        createHealthSpeciesLibraryTable();
     }
     }
 
 
     /**
     /**
@@ -16981,4 +16985,68 @@ log.info("迁移298: 已为无 openid 的 child 账号补齐 family_members 记
                 "INT NULL COMMENT '草稿版本号(并发存两版草稿:后者覆盖且提示冲突)'");
                 "INT NULL COMMENT '草稿版本号(并发存两版草稿:后者覆盖且提示冲突)'");
         log.info("迁移496: growth_plans.draft_expires_at / draft_version 列已就绪(plan.draft.save / SC-P-81)");
         log.info("迁移496: growth_plans.draft_expires_at / draft_version 列已就绪(plan.draft.save / SC-P-81)");
     }
     }
+
+    /**
+     * 迁移503: 谷禾新版物种功能库 {@code health_species_library} + 异常菌派生视图 {@code health_abnormal_species}
+     * (能力键 {@code health.report.abnormalSpecies},P0-6 异常菌清单接口 ▲ 直出)。
+     *
+     * <p>76 条/份(知识层 + 个人层)落 {@code health_species_library};异常菌清单从该表派生,
+     * 仅收录 {@code direction IS NOT NULL} 的 ▲▼ 条目(视图口径见 schema.sql,
+     * 对象名为 {@code health_abnormal_species},已存在同名表时降级建表,保持幂等)。</p>
+     *
+     * <p>幂等:CREATE TABLE IF NOT EXISTS + try/catch 忽略「表/视图已存在」异常,可重复执行。</p>
+     */
+    void createHealthSpeciesLibraryTable() {
+        try {
+            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS health_species_library ("
+                    + "id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID', "
+                    + "report_id BIGINT NOT NULL COMMENT '关联报告ID(health_reports.id)', "
+                    + "species_no INT NOT NULL COMMENT '菌种编号(报告原始序号)', "
+                    + "cn_name VARCHAR(128) COMMENT '中文名', "
+                    + "latin_name VARCHAR(128) COMMENT '拉丁名', "
+                    + "function_tag VARCHAR(64) COMMENT '功能标签', "
+                    + "detection_rate VARCHAR(32) COMMENT '%正常人有检出', "
+                    + "function_desc TEXT COMMENT '功能说明', "
+                    + "improve_method TEXT COMMENT '改善方法', "
+                    + "abundance VARCHAR(32) COMMENT '丰度', "
+                    + "ref_range VARCHAR(64) COMMENT '参考范围原文', "
+                    + "population_level VARCHAR(64) COMMENT '处于人群%水平', "
+                    + "status VARCHAR(16) COMMENT '状态(正常/偏高/偏低)', "
+                    + "direction VARCHAR(2) COMMENT '▲超标/▼偏低, NULL=正常', "
+                    + "created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', "
+                    + "INDEX idx_report (report_id), "
+                    + "INDEX idx_report_no (report_id, species_no)"
+                    + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='谷禾新版物种功能库(76条/份,知识层+个人层)'");
+            log.info("迁移503: 已创建/确认 health_species_library 表");
+        } catch (Exception e) {
+            log.warn("迁移503: health_species_library 表创建失败(忽略): {}", e.getMessage());
+        }
+        // 异常菌派生视图:仅收录 direction IS NOT NULL 的 ▲▼ 条目;已存在同名对象(表/视图)则跳过
+        try {
+            jdbcTemplate.execute("DROP VIEW IF EXISTS health_abnormal_species");
+            jdbcTemplate.execute("CREATE VIEW health_abnormal_species AS "
+                    + "SELECT sl.id, sl.report_id, sl.species_no, sl.cn_name, sl.latin_name, "
+                    + "sl.abundance, sl.ref_range, sl.population_level, sl.direction "
+                    + "FROM health_species_library sl WHERE sl.direction IS NOT NULL");
+        } catch (Exception e) {
+            log.warn("迁移503: health_abnormal_species 视图创建失败,降级建表(忽略): {}", e.getMessage());
+            try {
+                jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS health_abnormal_species ("
+                        + "id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID', "
+                        + "report_id BIGINT NOT NULL COMMENT '关联报告ID(health_reports.id)', "
+                        + "species_no INT NOT NULL COMMENT '菌种编号', "
+                        + "cn_name VARCHAR(128) COMMENT '中文名', "
+                        + "latin_name VARCHAR(128) COMMENT '拉丁名', "
+                        + "abundance VARCHAR(32) COMMENT '丰度', "
+                        + "ref_range VARCHAR(64) COMMENT '参考范围原文', "
+                        + "population_level VARCHAR(64) COMMENT '处于人群%水平', "
+                        + "direction VARCHAR(2) COMMENT '▲超标/▼偏低', "
+                        + "INDEX idx_report (report_id)"
+                        + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='异常菌清单(仅收录▲▼条目)'");
+            } catch (Exception e2) {
+                log.warn("迁移503: health_abnormal_species 降级建表失败(忽略): {}", e2.getMessage());
+            }
+        }
+        log.info("迁移503: 异常菌清单数据源已就绪(health_species_library + health_abnormal_species 视图/表)");
+    }
 }
 }

+ 30 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/HealthReportController.java

@@ -624,6 +624,36 @@ public class HealthReportController {
             return Result.success(java.util.Collections.emptyList());
             return Result.success(java.util.Collections.emptyList());
         }
         }
     }
     }
+
+    /**
+     * 异常菌清单接口(能力键 {@code health.report.abnormalSpecies},P0-6 ▲ 直出,§六 6.3)。
+     *
+     * <p>让前端一键展示「你的报告有 N 条 ▲ 超标菌」,无需遍历 payload:
+     * 仅返回 {@code health_species_library} 中 {@code direction IS NOT NULL} 的 ▲▼ 条目,
+     * 按 {@code direction} 排序(▲ 超标优先),响应统一附 {@code disclaimer} 口径。
+     * 入参 {@code reportId} 必填;不传则返回空清单。</p>
+     */
+    @Operation(summary = "获取报告异常菌清单(▲ 直出)")
+    @PostMapping("/report/abnormal-species")
+    public Result<Map<String, Object>> getAbnormalSpecies(
+            @RequestBody Map<String, Object> params,
+            @RequestAttribute("userId") Long userId,
+            @RequestAttribute(value = "currentMemberId", required = false) Long currentMemberId) {
+        Long reportId = ParamUtils.getLong(params.get("reportId"), null);
+        Map<String, Object> result = new java.util.HashMap<>();
+        result.put("abnormalSpecies", java.util.Collections.emptyList());
+        result.put("disclaimer", healthReportService.ABNORMAL_SPECIES_DISCLAIMER);
+        if (reportId == null) {
+            return Result.success(result);
+        }
+        try {
+            result.put("abnormalSpecies", healthReportService.listAbnormalSpecies(reportId));
+            return Result.success(result);
+        } catch (Exception e) {
+            log.warn("获取异常菌清单失败: {}", e.getMessage());
+            return Result.success(result);
+        }
+    }
     /**
     /**
      * [admin] 回填成员指标当前值 diff(对存量未走新确认流程的报告手动触发)
      * [admin] 回填成员指标当前值 diff(对存量未走新确认流程的报告手动触发)
      * 入参: memberId(必填);调用 healthReportService.rebuildForMember
      * 入参: memberId(必填);调用 healthReportService.rebuildForMember

+ 99 - 0
cfc-backend/src/main/java/com/etotem/cfc/dto/ParsedAbnormalSpecies.java

@@ -0,0 +1,99 @@
+package com.etotem.cfc.dto;
+
+/**
+ * 异常菌清单条目(能力键 {@code health.report.abnormalSpecies},P0-6 接口响应字段)。
+ *
+ * <p>对应「异常菌清单接口(▲ 直出)」契约(设计 §三 P0-6 / §六 6.3):
+ * 前端一键展示「你的报告有 N 条 ▲ 超标菌」,无需前端遍历 payload。
+ * 数据来源 {@code health_species_library}(仅收录 {@code direction IS NOT NULL} 的 ▲▼ 条目)。</p>
+ */
+public class ParsedAbnormalSpecies {
+
+    /** 菌种编号(健康报告原始序号) */
+    private Integer speciesNo;
+
+    /** 中文名 */
+    private String cnName;
+
+    /** 拉丁名 */
+    private String latinName;
+
+    /** 丰度(如 8.3%) */
+    private String abundance;
+
+    /** 参考范围原文(如 1%~3%) */
+    private String refRange;
+
+    /** 方向:▲(超标)/▼(偏低) */
+    private String direction;
+
+    /** 分类(core/probiotic/harmful/other 等) */
+    private String category;
+
+    /** 处于人群%水平(如 35%) */
+    private String populationLevel;
+
+    public Integer getSpeciesNo() {
+        return speciesNo;
+    }
+
+    public void setSpeciesNo(Integer speciesNo) {
+        this.speciesNo = speciesNo;
+    }
+
+    public String getCnName() {
+        return cnName;
+    }
+
+    public void setCnName(String cnName) {
+        this.cnName = cnName;
+    }
+
+    public String getLatinName() {
+        return latinName;
+    }
+
+    public void setLatinName(String latinName) {
+        this.latinName = latinName;
+    }
+
+    public String getAbundance() {
+        return abundance;
+    }
+
+    public void setAbundance(String abundance) {
+        this.abundance = abundance;
+    }
+
+    public String getRefRange() {
+        return refRange;
+    }
+
+    public void setRefRange(String refRange) {
+        this.refRange = refRange;
+    }
+
+    public String getDirection() {
+        return direction;
+    }
+
+    public void setDirection(String direction) {
+        this.direction = direction;
+    }
+
+    public String getCategory() {
+        return category;
+    }
+
+    public void setCategory(String category) {
+        this.category = category;
+    }
+
+    public String getPopulationLevel() {
+        return populationLevel;
+    }
+
+    public void setPopulationLevel(String populationLevel) {
+        this.populationLevel = populationLevel;
+    }
+}

+ 28 - 0
cfc-backend/src/main/java/com/etotem/cfc/dto/ParsedReportResult.java

@@ -97,6 +97,18 @@ public class ParsedReportResult {
     /** 益生菌种 (level=SPECIES) */
     /** 益生菌种 (level=SPECIES) */
     private List<ParsedGutFlora> probioticSpecies;
     private List<ParsedGutFlora> probioticSpecies;
 
 
+    /**
+     * 物种功能库明细(P0-4 DTO 扩展,能力键 {@code health.report.abnormalSpecies}):
+     * 谷禾新版「肠道菌群重要菌种[]」76 条/份全量,落 {@code health_species_library} 表。
+     */
+    private List<ParsedSpeciesLibrary> speciesLibrary;
+
+    /**
+     * 异常菌清单(P0-4 DTO 扩展):仅收录 {@code direction IS NOT NULL} 的 ▲▼ 条目,
+     * 供 P0-6 异常菌清单接口直出。
+     */
+    private List<ParsedAbnormalSpecies> abnormalSpecies;
+
     /** 健康指标 — 营养、屏障、短链脂肪酸、神经递质、抗生素耐药等 */
     /** 健康指标 — 营养、屏障、短链脂肪酸、神经递质、抗生素耐药等 */
     private List<ParsedIndicator> indicators;
     private List<ParsedIndicator> indicators;
 
 
@@ -291,4 +303,20 @@ public class ParsedReportResult {
     public void setReportFormat(String reportFormat) {
     public void setReportFormat(String reportFormat) {
         this.reportFormat = reportFormat;
         this.reportFormat = reportFormat;
     }
     }
+
+    public List<ParsedSpeciesLibrary> getSpeciesLibrary() {
+        return speciesLibrary;
+    }
+
+    public void setSpeciesLibrary(List<ParsedSpeciesLibrary> speciesLibrary) {
+        this.speciesLibrary = speciesLibrary;
+    }
+
+    public List<ParsedAbnormalSpecies> getAbnormalSpecies() {
+        return abnormalSpecies;
+    }
+
+    public void setAbnormalSpecies(List<ParsedAbnormalSpecies> abnormalSpecies) {
+        this.abnormalSpecies = abnormalSpecies;
+    }
 }
 }

+ 131 - 0
cfc-backend/src/main/java/com/etotem/cfc/dto/ParsedSpeciesLibrary.java

@@ -0,0 +1,131 @@
+package com.etotem.cfc.dto;
+
+/**
+ * 物种功能库条目(能力键 {@code health.report.abnormalSpecies} 依赖,P0-4 DTO 扩展)。
+ *
+ * <p>对应「肠道菌群重要菌种[]」76 条/份全量(知识层 + 个人层),承载于
+ * {@code health_species_library} 表;由 Python 解析结果反序列化后落库。</p>
+ */
+public class ParsedSpeciesLibrary {
+
+    /** 菌种编号 */
+    private Integer speciesNo;
+
+    /** 中文名 */
+    private String cnName;
+
+    /** 拉丁名 */
+    private String latinName;
+
+    /** 功能标签(如 产丁酸/屏障维护) */
+    private String functionTag;
+
+    /** 检出率原文(如 98.56%) */
+    private String detectionRate;
+
+    /** 功能说明 */
+    private String functionDesc;
+
+    /** 改善方法 */
+    private String improveMethod;
+
+    /** 丰度(如 8.3%) */
+    private String abundance;
+
+    /** 参考范围原文(如 1%~3%) */
+    private String refRange;
+
+    /** 状态(如 正常/偏高/偏低) */
+    private String status;
+
+    /** 方向:▲(超标)/▼(偏低)/空(正常) */
+    private String direction;
+
+    public Integer getSpeciesNo() {
+        return speciesNo;
+    }
+
+    public void setSpeciesNo(Integer speciesNo) {
+        this.speciesNo = speciesNo;
+    }
+
+    public String getCnName() {
+        return cnName;
+    }
+
+    public void setCnName(String cnName) {
+        this.cnName = cnName;
+    }
+
+    public String getLatinName() {
+        return latinName;
+    }
+
+    public void setLatinName(String latinName) {
+        this.latinName = latinName;
+    }
+
+    public String getFunctionTag() {
+        return functionTag;
+    }
+
+    public void setFunctionTag(String functionTag) {
+        this.functionTag = functionTag;
+    }
+
+    public String getDetectionRate() {
+        return detectionRate;
+    }
+
+    public void setDetectionRate(String detectionRate) {
+        this.detectionRate = detectionRate;
+    }
+
+    public String getFunctionDesc() {
+        return functionDesc;
+    }
+
+    public void setFunctionDesc(String functionDesc) {
+        this.functionDesc = functionDesc;
+    }
+
+    public String getImproveMethod() {
+        return improveMethod;
+    }
+
+    public void setImproveMethod(String improveMethod) {
+        this.improveMethod = improveMethod;
+    }
+
+    public String getAbundance() {
+        return abundance;
+    }
+
+    public void setAbundance(String abundance) {
+        this.abundance = abundance;
+    }
+
+    public String getRefRange() {
+        return refRange;
+    }
+
+    public void setRefRange(String refRange) {
+        this.refRange = refRange;
+    }
+
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+
+    public String getDirection() {
+        return direction;
+    }
+
+    public void setDirection(String direction) {
+        this.direction = direction;
+    }
+}

+ 62 - 0
cfc-backend/src/main/java/com/etotem/cfc/entity/HealthSpeciesLibrary.java

@@ -0,0 +1,62 @@
+package com.etotem.cfc.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * 谷禾新版物种功能库 — 每份报告 76 条/份(知识层 + 个人层)
+ * 异常菌清单(direction IS NOT NULL)从本表派生,见 {@code health_abnormal_species} 视图
+ */
+@Data
+@TableName("health_species_library")
+public class HealthSpeciesLibrary implements Serializable {
+
+    @TableId(type = IdType.AUTO)
+    private Long id;
+
+    /** 关联报告ID */
+    private Long reportId;
+
+    /** 菌种编号(报告原始序号) */
+    private Integer speciesNo;
+
+    /** 中文名 */
+    private String cnName;
+
+    /** 拉丁名 */
+    private String latinName;
+
+    /** 功能标签 */
+    private String functionTag;
+
+    /** %正常人有检出 */
+    private String detectionRate;
+
+    /** 功能说明 */
+    private String functionDesc;
+
+    /** 改善方法 */
+    private String improveMethod;
+
+    /** 丰度 */
+    private String abundance;
+
+    /** 参考范围原文 */
+    private String refRange;
+
+    /** 处于人群%水平 */
+    private String populationLevel;
+
+    /** 状态(正常/偏高/偏低) */
+    private String status;
+
+    /** 方向:▲超标/▼偏低,NULL=正常 */
+    private String direction;
+
+    private Date createdAt;
+}

+ 9 - 0
cfc-backend/src/main/java/com/etotem/cfc/mapper/HealthSpeciesLibraryMapper.java

@@ -0,0 +1,9 @@
+package com.etotem.cfc.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.etotem.cfc.entity.HealthSpeciesLibrary;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface HealthSpeciesLibraryMapper extends BaseMapper<HealthSpeciesLibrary> {
+}

+ 93 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/HealthReportService.java

@@ -3,12 +3,15 @@ package com.etotem.cfc.service;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.etotem.cfc.dto.MemberMatchResult;
 import com.etotem.cfc.dto.MemberMatchResult;
+import com.etotem.cfc.dto.ParsedAbnormalSpecies;
 import com.etotem.cfc.dto.ParsedReportPayload;
 import com.etotem.cfc.dto.ParsedReportPayload;
+import com.etotem.cfc.dto.ParsedSpeciesLibrary;
 import com.etotem.cfc.entity.*;
 import com.etotem.cfc.entity.*;
 import com.etotem.cfc.mapper.FamilyMemberMapper;
 import com.etotem.cfc.mapper.FamilyMemberMapper;
 import com.etotem.cfc.mapper.GrowthPlanMapper;
 import com.etotem.cfc.mapper.GrowthPlanMapper;
 import com.etotem.cfc.mapper.HealthDiseaseRiskMapper;
 import com.etotem.cfc.mapper.HealthDiseaseRiskMapper;
 import com.etotem.cfc.mapper.HealthGutFloraMapper;
 import com.etotem.cfc.mapper.HealthGutFloraMapper;
+import com.etotem.cfc.mapper.HealthSpeciesLibraryMapper;
 import com.etotem.cfc.mapper.HealthIndicatorMapper;
 import com.etotem.cfc.mapper.HealthIndicatorMapper;
 import com.etotem.cfc.mapper.IndicatorDefinitionMapper;
 import com.etotem.cfc.mapper.IndicatorDefinitionMapper;
 import com.etotem.cfc.mapper.IndicatorValueMapper;
 import com.etotem.cfc.mapper.IndicatorValueMapper;
@@ -42,6 +45,7 @@ import java.util.Objects;
 import java.math.BigDecimal;
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Map;
 import java.util.Map;
+import java.util.stream.Collectors;
 import com.etotem.cfc.entity.MemberIndicatorCurrent;
 import com.etotem.cfc.entity.MemberIndicatorCurrent;
 import java.util.Set;
 import java.util.Set;
 import com.etotem.cfc.util.SortUtil;
 import com.etotem.cfc.util.SortUtil;
@@ -66,6 +70,9 @@ public class HealthReportService {
     @Resource
     @Resource
     private HealthGutFloraMapper healthGutFloraMapper;
     private HealthGutFloraMapper healthGutFloraMapper;
 
 
+    @Resource
+    private HealthSpeciesLibraryMapper healthSpeciesLibraryMapper;
+
     @Resource
     @Resource
     private HealthDiseaseRiskMapper healthDiseaseRiskMapper;
     private HealthDiseaseRiskMapper healthDiseaseRiskMapper;
 
 
@@ -874,6 +881,92 @@ public class HealthReportService {
         }
         }
     }
     }
 
 
+    // ======================== 物种功能库 + 异常菌清单(health.report.abnormalSpecies) ========================
+
+    /**
+     * 异常菌清单统一口径(P0-6 口径约束):关联非因果、测评 ≠ 诊断,供前端统一展示。
+     */
+    public static final String ABNORMAL_SPECIES_DISCLAIMER =
+            "本清单为菌群检测结果的关联性提示,不代表医学因果关系,也不构成医疗诊断,请以专业医生意见为准。";
+
+    /**
+     * 异常菌清单(能力键 {@code health.report.abnormalSpecies},P0-6 ▲ 直出)。
+     *
+     * <p>口径:仅收录 {@code health_species_library} 中 {@code direction IS NOT NULL} 的 ▲▼ 条目,
+     * 按 {@code direction} 排序(▲ 超标优先,其次 ▼ 偏低),同级按菌种编号升序,
+     * 让前端可一键展示「你的报告有 N 条 ▲ 超标菌」,无需遍历 payload。</p>
+     */
+    public List<ParsedAbnormalSpecies> listAbnormalSpecies(Long reportId) {
+        if (reportId == null) return Collections.emptyList();
+        List<HealthSpeciesLibrary> rows = healthSpeciesLibraryMapper.selectList(
+                new LambdaQueryWrapper<HealthSpeciesLibrary>()
+                        .eq(HealthSpeciesLibrary::getReportId, reportId)
+                        .isNotNull(HealthSpeciesLibrary::getDirection)
+        );
+        if (rows.isEmpty()) return Collections.emptyList();
+
+        Comparator<HealthSpeciesLibrary> byDirection =
+                Comparator.comparingInt(HealthReportService::directionRank)
+                        .thenComparing(HealthReportService::speciesNoOf);
+        List<ParsedAbnormalSpecies> result = new ArrayList<>();
+        for (HealthSpeciesLibrary row : rows.stream().sorted(byDirection).collect(Collectors.toList())) {
+            ParsedAbnormalSpecies item = new ParsedAbnormalSpecies();
+            item.setSpeciesNo(row.getSpeciesNo());
+            item.setCnName(row.getCnName());
+            item.setLatinName(row.getLatinName());
+            item.setAbundance(row.getAbundance());
+            item.setRefRange(row.getRefRange());
+            item.setDirection(row.getDirection());
+            item.setCategory(row.getFunctionTag());
+            item.setPopulationLevel(row.getPopulationLevel());
+            result.add(item);
+        }
+        return result;
+    }
+
+    /** 按 direction 排序权重:▲ 超标优先(0),▼ 偏低次之(1),其余兜底(2)。 */
+    private static int directionRank(HealthSpeciesLibrary row) {
+        if (row == null) return 2;
+        String direction = row.getDirection();
+        if (direction == null) return 2;
+        if (direction.contains("▲")) return 0;
+        if (direction.contains("▼")) return 1;
+        return 2;
+    }
+
+    /** 同级 direction 内按菌种编号升序,保证清单顺序稳定。 */
+    private static int speciesNoOf(HealthSpeciesLibrary row) {
+        Integer speciesNo = row.getSpeciesNo();
+        return speciesNo == null ? Integer.MAX_VALUE : speciesNo;
+    }
+
+    /**
+     * 保存物种功能库明细(解析入库时调用,同一报告先清后写,保持幂等)
+     */
+    @Transactional
+    public void saveSpeciesLibrary(Long reportId, List<ParsedSpeciesLibrary> speciesList) {
+        if (reportId == null || speciesList == null || speciesList.isEmpty()) return;
+        healthSpeciesLibraryMapper.delete(new LambdaQueryWrapper<HealthSpeciesLibrary>()
+                .eq(HealthSpeciesLibrary::getReportId, reportId));
+        for (ParsedSpeciesLibrary parsed : speciesList) {
+            HealthSpeciesLibrary item = new HealthSpeciesLibrary();
+            item.setReportId(reportId);
+            item.setSpeciesNo(parsed.getSpeciesNo());
+            item.setCnName(parsed.getCnName());
+            item.setLatinName(parsed.getLatinName());
+            item.setFunctionTag(parsed.getFunctionTag());
+            item.setDetectionRate(parsed.getDetectionRate());
+            item.setFunctionDesc(parsed.getFunctionDesc());
+            item.setImproveMethod(parsed.getImproveMethod());
+            item.setAbundance(parsed.getAbundance());
+            item.setRefRange(parsed.getRefRange());
+            item.setStatus(parsed.getStatus());
+            item.setDirection(parsed.getDirection());
+            item.setCreatedAt(new Date());
+            healthSpeciesLibraryMapper.insert(item);
+        }
+    }
+
     /**
     /**
      * 获取身体维度异常指标(身体页面"指标告警"专供)
      * 获取身体维度异常指标(身体页面"指标告警"专供)
      * 返回:疾病风险(非低风险) + 主要营养评估异常 + 氨基酸评估异常
      * 返回:疾病风险(非低风险) + 主要营养评估异常 + 氨基酸评估异常

+ 29 - 0
cfc-backend/src/main/resources/schema.sql

@@ -2058,6 +2058,35 @@ CREATE TABLE IF NOT EXISTS health_gut_flora (
     INDEX idx_report (report_id)
     INDEX idx_report (report_id)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菌群检测专项数据(每种菌种的丰度值和评估状态)';
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菌群检测专项数据(每种菌种的丰度值和评估状态)';
 
 
+-- 谷禾新版物种功能库(76 条/份,知识层 + 个人层;P0-4 / P0-6 异常菌清单数据来源)
+CREATE TABLE IF NOT EXISTS health_species_library (
+    id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键 ID',
+    report_id BIGINT NOT NULL COMMENT '关联报告 ID',
+    species_no INT NOT NULL COMMENT '菌种编号(报告原始序号)',
+    cn_name VARCHAR(128) COMMENT '中文名',
+    latin_name VARCHAR(128) COMMENT '拉丁名',
+    function_tag VARCHAR(64) COMMENT '功能标签',
+    detection_rate VARCHAR(32) COMMENT '%正常人有检出',
+    function_desc TEXT COMMENT '功能说明',
+    improve_method TEXT COMMENT '改善方法',
+    abundance VARCHAR(32) COMMENT '丰度',
+    ref_range VARCHAR(64) COMMENT '参考范围原文',
+    population_level VARCHAR(64) COMMENT '处于人群%水平',
+    status VARCHAR(16) COMMENT '状态(正常/偏高/偏低)',
+    direction VARCHAR(2) COMMENT '▲超标/▼偏低,NULL=正常',
+    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
+    INDEX idx_report (report_id),
+    INDEX idx_report_no (report_id, species_no)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='谷禾新版物种功能库(76 条/份,知识层 + 个人层)';
+
+-- 异常菌清单派生视图:仅收录 direction IS NOT NULL 的 ▲▼ 条目
+-- (能力键 health.report.abnormalSpecies,P0-6 异常菌清单接口 ▲ 直出)
+CREATE VIEW health_abnormal_species AS
+SELECT id, report_id, species_no, cn_name, latin_name,
+       abundance, ref_range, population_level, direction
+FROM health_species_library
+WHERE direction IS NOT NULL;
+
 -- 教育体系配置表
 -- 教育体系配置表
 CREATE TABLE IF NOT EXISTS education_systems (
 CREATE TABLE IF NOT EXISTS education_systems (
     id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID',
     id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID',

+ 1 - 0
docs/superpowers/api/API_REFERENCE.md

@@ -2852,6 +2852,7 @@ W0.2 双配置中心合并:`product_config` 已并入 `service_config`(唯
 | `POST /api/health/report/list` | `{memberId?, page?, size?}` | `page` 未传 → `List<HealthReport>`(兼容小程序);`page` 传 → 分页结构 `{total, records[]}` | 报告列表。未传 memberId 返回当前家庭全部报告(按 `visibleIdentityIds` 权限过滤;管理员全部可见);传 memberId 且无查看权限 → 空列表 |
 | `POST /api/health/report/list` | `{memberId?, page?, size?}` | `page` 未传 → `List<HealthReport>`(兼容小程序);`page` 传 → 分页结构 `{total, records[]}` | 报告列表。未传 memberId 返回当前家庭全部报告(按 `visibleIdentityIds` 权限过滤;管理员全部可见);传 memberId 且无查看权限 → 空列表 |
 | `POST /api/health/report/detail` | `{reportId}` | `Map`:报告详情(含指标明细 + 菌群数据 + 疾病风险评估) | 完整详情;权限校验(本人/管理员/被授权成员),无权返回「无权查看该报告」 |
 | `POST /api/health/report/detail` | `{reportId}` | `Map`:报告详情(含指标明细 + 菌群数据 + 疾病风险评估) | 完整详情;权限校验(本人/管理员/被授权成员),无权返回「无权查看该报告」 |
 | `POST /api/health/report/payload` | `{reportId}` | `Map`:报告 payload(编辑回填用,按需加载) | 同 detail 权限校验 |
 | `POST /api/health/report/payload` | `{reportId}` | `Map`:报告 payload(编辑回填用,按需加载) | 同 detail 权限校验 |
+| `POST /api/health/report/abnormal-species` | `{reportId}` | `{abnormalSpecies[], disclaimer}`:异常菌清单(仅 ▲▼ 条目,按 direction 排序 ▲ 优先)+ 统一口径免责文案 | 能力键 `health.report.abnormalSpecies`(P0-6 ▲ 直出):前端一键展示「N 条 ▲ 超标菌」,无需遍历 payload;reportId 缺失或无数据时返回空清单(`abnormalSpecies: []`)并附 disclaimer |
 | `POST /api/health/report/edit` | `{reportId, subjectId?, payload}` | `"已更新"` | 编辑已发布报告(修正 AI 解析错误值);subjectId 可 null 表示解绑,变更时重算旧/新成员维度分 |
 | `POST /api/health/report/edit` | `{reportId, subjectId?, payload}` | `"已更新"` | 编辑已发布报告(修正 AI 解析错误值);subjectId 可 null 表示解绑,变更时重算旧/新成员维度分 |
 | `POST /api/health/report/delete` | `{reportId}` | `"已删除"` | 删除报告 |
 | `POST /api/health/report/delete` | `{reportId}` | `"已删除"` | 删除报告 |
 | `POST /api/health/report/download` | `{reportId}` | `{fileUrl, fileName}`(本地相对路径 `/uploads/**` 自动拼当前域名) | 报告文件下载信息(供 uni.downloadFile) |
 | `POST /api/health/report/download` | `{reportId}` | `{fileUrl, fileName}`(本地相对路径 `/uploads/**` 自动拼当前域名) | 报告文件下载信息(供 uni.downloadFile) |