Browse Source

feat(diet): S-cfc-484 diet.recommendation.byReport 落地 recommendByReport(Long) 使红测转绿

zxyj-langgraph 1 hour ago
parent
commit
a2787c49a8

+ 84 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/FoodRecommendService.java

@@ -583,6 +583,90 @@ public class FoodRecommendService {
         return result;
         return result;
     }
     }
 
 
+    /**
+     * SC-P-26 / diet.recommendation.byReport:按成员最新菌群报告生成可吃/慎吃清单。
+     *
+     * <p>契约:返回 {@code hasReport/reportId/canEat/caution}。无报告({@code getLatestReport==null})
+     * 时无推荐,两清单为空;以传入的 {@code memberId} 作为报告主体查询(成员维度隔离,不串他人报告);
+     * 每次调用按最新报告重算,不缓存上一期结果(随报告刷新)。食材优先取 foods 表(正常确认流程写入),
+     * 并合并 report_food_suitability(审核路径写入)中未重复的食材,按 foodId 去重;推荐指数为 null 时
+     * 按 0 分兜底,非负进 canEat、负分进 caution。</p>
+     *
+     * @param memberId 家庭成员 id(health_reports.subject_id 口径)
+     * @return 含 hasReport/reportId/canEat/caution 的 Map
+     */
+    public Map<String, Object> recommendByReport(Long memberId) {
+        Map<String, Object> result = new LinkedHashMap<>();
+        List<Map<String, Object>> canEat = new ArrayList<>();
+        List<Map<String, Object>> caution = new ArrayList<>();
+        result.put("canEat", canEat);
+        result.put("caution", caution);
+        result.put("hasReport", Boolean.FALSE);
+        result.put("reportId", null);
+
+        if (memberId == null) {
+            return result;
+        }
+        HealthReport report = healthReportService.getLatestReport(memberId);
+        if (report == null) {
+            return result;
+        }
+        result.put("hasReport", Boolean.TRUE);
+        result.put("reportId", report.getId());
+
+        Long reportId = report.getId();
+        Set<String> seenFoodKeys = new HashSet<>();
+
+        // 主路径:正常确认流程写入 foods 表
+        List<Food> foods = reportId == null ? null : foodService.listByReportId(reportId);
+        if (foods != null) {
+            for (Food food : foods) {
+                if (food == null) {
+                    continue;
+                }
+                if (!seenFoodKeys.add(byReportFoodKey(food.getId(), food.getName()))) {
+                    continue;
+                }
+                addByReportFood(canEat, caution, food.getName(), food.getId(), food.getScore());
+            }
+        }
+
+        // 回退/补充路径:审核流程只写 report_food_suitability;与主表按 foodId 去重后合并
+        if (reportId != null) {
+            List<ReportFoodSuitability> fallback = reportFoodSuitabilityMapper.selectList(
+                    new LambdaQueryWrapper<ReportFoodSuitability>()
+                            .eq(ReportFoodSuitability::getReportId, reportId));
+            if (fallback != null) {
+                for (ReportFoodSuitability rfs : fallback) {
+                    if (rfs == null) {
+                        continue;
+                    }
+                    if (!seenFoodKeys.add(byReportFoodKey(rfs.getFoodId(), rfs.getFoodName()))) {
+                        continue;
+                    }
+                    addByReportFood(canEat, caution, rfs.getFoodName(), rfs.getFoodId(), rfs.getScore());
+                }
+            }
+        }
+        return result;
+    }
+
+    private static String byReportFoodKey(Long foodId, String name) {
+        return foodId != null ? "id:" + foodId : "name:" + name;
+    }
+
+    private static void addByReportFood(List<Map<String, Object>> canEat,
+                                        List<Map<String, Object>> caution,
+                                        String name, Long foodId, Integer rawScore) {
+        int score = rawScore == null ? 0 : rawScore.intValue();
+        Map<String, Object> item = new LinkedHashMap<>();
+        item.put("name", name);
+        item.put("foodName", name);
+        item.put("score", score);
+        item.put("foodId", foodId);
+        (score >= 0 ? canEat : caution).add(item);
+    }
+
     /**
     /**
      * 获取戒备食材趋势数据:返回每个食材的 indexScore 历史序列(最近N条变更记录)
      * 获取戒备食材趋势数据:返回每个食材的 indexScore 历史序列(最近N条变更记录)
      * 每个元素: { foodId, foodName, currentScore, trend: [reportDate, score], changeDir: "up"|"down"|"flat" }
      * 每个元素: { foodId, foodName, currentScore, trend: [reportDate, score], changeDir: "up"|"down"|"flat" }