|
|
@@ -97,7 +97,27 @@ public class DietRecommendationService {
|
|
|
String normalizedMenuJson = safeWrite(normalized);
|
|
|
Map<String, Object> nutritionSummary = aggregateNutrition(normalized);
|
|
|
|
|
|
- // 6. 保存推荐
|
|
|
+ // 6. 幂等保存:同日同 mealType 已存在则更新,否则新建
|
|
|
+ LambdaQueryWrapper<DietRecommendation> existWrapper = new LambdaQueryWrapper<>();
|
|
|
+ existWrapper.eq(DietRecommendation::getFamilyId, familyId)
|
|
|
+ .eq(DietRecommendation::getRecommendationDate, java.util.Date.from(date.atStartOfDay(java.time.ZoneId.systemDefault()).toInstant()))
|
|
|
+ .eq(DietRecommendation::getMealType, mealType != null ? mealType : "all");
|
|
|
+ DietRecommendation existRecord = dietRecommendationMapper.selectOne(existWrapper);
|
|
|
+ if (existRecord != null) {
|
|
|
+ existRecord.setMenuJson(normalizedMenuJson != null ? normalizedMenuJson : "{}");
|
|
|
+ existRecord.setNutritionSummary(safeWrite(nutritionSummary));
|
|
|
+ existRecord.setParticipantMemberIds(participantIds.toString());
|
|
|
+ existRecord.setStatus("pending");
|
|
|
+ existRecord.setUpdatedAt(new Date());
|
|
|
+ dietRecommendationMapper.updateById(existRecord);
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("id", existRecord.getId());
|
|
|
+ result.put("menu", existRecord.getMenuJson());
|
|
|
+ result.put("nutritionSummary", existRecord.getNutritionSummary());
|
|
|
+ result.put("message", "食谱推荐已更新");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
DietRecommendation record = new DietRecommendation();
|
|
|
record.setFamilyId(familyId);
|
|
|
record.setRecommendationDate(java.util.Date.from(date.atStartOfDay(java.time.ZoneId.systemDefault()).toInstant()));
|