|
|
@@ -36,6 +36,9 @@ public class FoodRecommendService {
|
|
|
@Resource
|
|
|
private HealthReportService healthReportService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private FoodService foodService;
|
|
|
+
|
|
|
@Transactional
|
|
|
public void recalculateForUser(Long userId) {
|
|
|
HealthReport report = healthReportService.getLatestReport(userId);
|
|
|
@@ -46,15 +49,13 @@ public class FoodRecommendService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- List<ReportFoodSuitability> suitList = reportFoodSuitabilityMapper.selectList(
|
|
|
- new LambdaQueryWrapper<ReportFoodSuitability>()
|
|
|
- .eq(ReportFoodSuitability::getReportId, report.getId())
|
|
|
- );
|
|
|
+ // 从 foods 表读取最新报告关联的食材推荐(正常确认流程写入 foods 表)
|
|
|
+ List<Food> reportFoods = foodService.listByReportId(report.getId());
|
|
|
|
|
|
- if (suitList.isEmpty()) {
|
|
|
+ if (reportFoods.isEmpty()) {
|
|
|
foodRecommendIndexMapper.delete(new LambdaQueryWrapper<FoodRecommendIndex>()
|
|
|
.eq(FoodRecommendIndex::getUserId, userId));
|
|
|
- log.info("报告{}无食材适宜度数据,已清除用户{}的推荐指数", report.getId(), userId);
|
|
|
+ log.info("报告{}无食材推荐数据,已清除用户{}的推荐指数", report.getId(), userId);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -69,15 +70,17 @@ public class FoodRecommendService {
|
|
|
Date now = new Date();
|
|
|
Long reportId = report.getId();
|
|
|
|
|
|
- for (ReportFoodSuitability suit : suitList) {
|
|
|
- Long foodId = suit.getFoodId();
|
|
|
- Integer newIndex = suit.getScore();
|
|
|
+ for (Food food : reportFoods) {
|
|
|
+ Long foodId = food.getId();
|
|
|
+ Integer newIndex = food.getScore();
|
|
|
+ String foodName = food.getName();
|
|
|
FoodRecommendIndex existing = existingMap.get(foodId);
|
|
|
|
|
|
if (existing != null) {
|
|
|
if (!Objects.equals(existing.getIndexScore(), newIndex)) {
|
|
|
Integer oldIndex = existing.getIndexScore();
|
|
|
existing.setIndexScore(newIndex);
|
|
|
+ existing.setFoodName(foodName);
|
|
|
existing.setHealthReportId(reportId);
|
|
|
existing.setCalculatedAt(now);
|
|
|
foodRecommendIndexMapper.updateById(existing);
|
|
|
@@ -85,7 +88,7 @@ public class FoodRecommendService {
|
|
|
FoodRecommendIdxLog logEntry = new FoodRecommendIdxLog();
|
|
|
logEntry.setUserId(userId);
|
|
|
logEntry.setFoodId(foodId);
|
|
|
- logEntry.setFoodName(suit.getFoodName());
|
|
|
+ logEntry.setFoodName(foodName);
|
|
|
logEntry.setPreviousIndex(oldIndex);
|
|
|
logEntry.setNewIndex(newIndex);
|
|
|
logEntry.setHealthReportId(reportId);
|
|
|
@@ -99,7 +102,7 @@ public class FoodRecommendService {
|
|
|
FoodRecommendIndex index = new FoodRecommendIndex();
|
|
|
index.setUserId(userId);
|
|
|
index.setFoodId(foodId);
|
|
|
- index.setFoodName(suit.getFoodName());
|
|
|
+ index.setFoodName(foodName);
|
|
|
index.setIndexScore(newIndex);
|
|
|
index.setHealthReportId(reportId);
|
|
|
index.setCalculatedAt(now);
|