|
@@ -1,6 +1,8 @@
|
|
|
package com.etotem.cfc.service;
|
|
package com.etotem.cfc.service;
|
|
|
|
|
|
|
|
import com.etotem.cfc.dto.IngredientRecommendation;
|
|
import com.etotem.cfc.dto.IngredientRecommendation;
|
|
|
|
|
+import com.etotem.cfc.entity.Food;
|
|
|
|
|
+import com.etotem.cfc.mapper.FoodMapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
@@ -15,6 +17,9 @@ public class DietIngredientService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private BeijingNutritionService beijingNutritionService;
|
|
private BeijingNutritionService beijingNutritionService;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FoodMapper foodMapper;
|
|
|
|
|
+
|
|
|
// 会话内食材选择(按 familyId 隔离)
|
|
// 会话内食材选择(按 familyId 隔离)
|
|
|
private final Map<Long, List<IngredientRecommendation>> selectedIngredients = new HashMap<>();
|
|
private final Map<Long, List<IngredientRecommendation>> selectedIngredients = new HashMap<>();
|
|
|
private final Map<Long, Integer> refreshSeed = new HashMap<>();
|
|
private final Map<Long, Integer> refreshSeed = new HashMap<>();
|
|
@@ -33,11 +38,11 @@ public class DietIngredientService {
|
|
|
public Map<String, Object> refreshIngredients(Long familyId) {
|
|
public Map<String, Object> refreshIngredients(Long familyId) {
|
|
|
Integer seed = refreshSeed.getOrDefault(familyId, 0) + 1;
|
|
Integer seed = refreshSeed.getOrDefault(familyId, 0) + 1;
|
|
|
refreshSeed.put(familyId, seed);
|
|
refreshSeed.put(familyId, seed);
|
|
|
-
|
|
|
|
|
- // 重新生成(通过随机种子变化)
|
|
|
|
|
- List<IngredientRecommendation> suggestions = beijingNutritionService.generateIngredientList(familyId, LocalDate.now());
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 重新生成(通过 seed 偏移截取,真正换一批)
|
|
|
|
|
+ List<IngredientRecommendation> suggestions = beijingNutritionService.generateIngredientList(familyId, LocalDate.now(), seed);
|
|
|
selectedIngredients.put(familyId, suggestions);
|
|
selectedIngredients.put(familyId, suggestions);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
result.put("ingredients", suggestions);
|
|
result.put("ingredients", suggestions);
|
|
|
result.put("selectedCount", 0);
|
|
result.put("selectedCount", 0);
|
|
@@ -88,4 +93,34 @@ public class DietIngredientService {
|
|
|
|
|
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 搜索食材(供"添加食材"弹窗使用)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param keyword 关键字(空=返回全部,最多50条)
|
|
|
|
|
+ * @return {foods:[{id,name,category}]}
|
|
|
|
|
+ */
|
|
|
|
|
+ public Map<String, Object> searchFoods(String keyword) {
|
|
|
|
|
+ List<Map<String, Object>> foods = new ArrayList<>();
|
|
|
|
|
+ List<Food> all = foodMapper.selectList(
|
|
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Food>()
|
|
|
|
|
+ .eq(Food::getStatus, "active")
|
|
|
|
|
+ .orderByAsc(Food::getSortOrder)
|
|
|
|
|
+ );
|
|
|
|
|
+ String kw = keyword == null ? "" : keyword.trim();
|
|
|
|
|
+ int count = 0;
|
|
|
|
|
+ for (Food f : all) {
|
|
|
|
|
+ if (kw.isEmpty() || (f.getName() != null && f.getName().contains(kw))) {
|
|
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
|
|
+ item.put("id", f.getId());
|
|
|
|
|
+ item.put("name", f.getName());
|
|
|
|
|
+ item.put("category", f.getCategory());
|
|
|
|
|
+ foods.add(item);
|
|
|
|
|
+ if (++count >= 50) break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+ result.put("foods", foods);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|