|
|
@@ -134,6 +134,47 @@ public class FoodRecommendService {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取用户"当前食材"推荐列表(含分类与营养信息,供饮食推荐页展示)。
|
|
|
+ * 数据来自 food_recommend_idx(有新报告时 recalculateForUser 有则更新/无则添加),
|
|
|
+ * join foods 表补充 category/protein 等字段。
|
|
|
+ */
|
|
|
+ public List<Map<String, Object>> getCurrentFoods(Long userId) {
|
|
|
+ List<FoodRecommendIndex> indices = getIndexByUser(userId);
|
|
|
+ if (indices.isEmpty()) return new ArrayList<>();
|
|
|
+
|
|
|
+ Set<Long> foodIds = new HashSet<>();
|
|
|
+ for (FoodRecommendIndex idx : indices) {
|
|
|
+ if (idx.getFoodId() != null) foodIds.add(idx.getFoodId());
|
|
|
+ }
|
|
|
+ Map<Long, Food> foodMap = new HashMap<>();
|
|
|
+ if (!foodIds.isEmpty()) {
|
|
|
+ List<Food> foods = foodService.listByIds(foodIds);
|
|
|
+ for (Food f : foods) {
|
|
|
+ if (f != null) foodMap.put(f.getId(), f);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
+ for (FoodRecommendIndex idx : indices) {
|
|
|
+ Food food = foodMap.get(idx.getFoodId());
|
|
|
+ Map<String, Object> m = new LinkedHashMap<>();
|
|
|
+ m.put("name", food != null && food.getName() != null ? food.getName() : idx.getFoodName());
|
|
|
+ m.put("foodName", m.get("name"));
|
|
|
+ m.put("score", idx.getIndexScore());
|
|
|
+ m.put("category", food != null ? food.getCategory() : "");
|
|
|
+ if (food != null) {
|
|
|
+ m.put("protein", food.getProtein());
|
|
|
+ m.put("fat", food.getFat());
|
|
|
+ m.put("carbs", food.getCarbs());
|
|
|
+ m.put("fiber", food.getFiber());
|
|
|
+ m.put("energyKj", food.getEnergyKj());
|
|
|
+ }
|
|
|
+ result.add(m);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
public List<FoodRecommendIdxLog> getIndexHistory(Long userId, Long foodId) {
|
|
|
return foodRecommendIdxLogMapper.selectList(
|
|
|
new LambdaQueryWrapper<FoodRecommendIdxLog>()
|