Просмотр исходного кода

fix(health): /api/health/foods 未传 reportId 时返回当前用户最新报告食材

- 指定 reportId 保持返回该报告食材(兼容既有调用)
- 未指定 reportId 时按当前登录用户查最新报告食材,返回当前最适合的推荐
- 修复食材查询页空数据问题
liaoxg 3 недель назад
Родитель
Сommit
63a3719b85

+ 16 - 8
cfc-backend/src/main/java/com/etotem/cfc/controller/HealthReportController.java

@@ -1396,15 +1396,23 @@ public class HealthReportController {
 
     @Operation(summary = "获取食材推荐列表")
     @PostMapping("/foods")
-    public Result<List<Food>> listFoods(@RequestBody(required = false) Map<String, Object> params) {
-        if (params == null) {
-            return Result.success(java.util.Collections.emptyList());
-        }
-        Long reportId = ParamUtils.getLong(params.get("reportId"));
-        if (reportId == null) {
-            return Result.success(java.util.Collections.emptyList());
+    public Result<List<Food>> listFoods(@RequestBody(required = false) Map<String, Object> params,
+                                        @RequestAttribute(value = "userId", required = false) Long userId) {
+        Long reportId = params != null ? ParamUtils.getLong(params.get("reportId")) : null;
+        List<Food> foods;
+        if (reportId != null) {
+            // 指定报告:返回该报告的食材推荐
+            foods = foodService.listByReportId(reportId);
+        } else {
+            // 未指定报告:返回当前用户最新报告的食材推荐(当前最适合)
+            Long uid = userId != null ? userId : -1L;
+            HealthReport latest = healthReportService.getLatestReport(uid);
+            if (latest == null) {
+                foods = java.util.Collections.emptyList();
+            } else {
+                foods = foodService.listByReportId(latest.getId());
+            }
         }
-        List<Food> foods = foodService.listByReportId(reportId);
         return Result.success(foods);
     }