|
|
@@ -125,6 +125,17 @@ public class FoodRecommendService {
|
|
|
index.setCalculatedAt(now);
|
|
|
index.setCreatedAt(now);
|
|
|
foodRecommendIndexMapper.insert(index);
|
|
|
+
|
|
|
+ FoodRecommendIdxLog logEntry = new FoodRecommendIdxLog();
|
|
|
+ logEntry.setUserId(userId);
|
|
|
+ logEntry.setFoodId(foodId);
|
|
|
+ logEntry.setFoodName(foodName);
|
|
|
+ logEntry.setPreviousIndex(null);
|
|
|
+ logEntry.setNewIndex(newIndex);
|
|
|
+ logEntry.setHealthReportId(reportId);
|
|
|
+ logEntry.setChangeReason("首次建立基线");
|
|
|
+ logEntry.setChangedAt(now);
|
|
|
+ foodRecommendIdxLogMapper.insert(logEntry);
|
|
|
}
|
|
|
existingMap.remove(foodId);
|
|
|
}
|
|
|
@@ -303,10 +314,10 @@ public class FoodRecommendService {
|
|
|
FoodRecommendIdxLog latest = foodLogs.get(0);
|
|
|
Integer currentScore = latest.getNewIndex() != null ? latest.getNewIndex() : latest.getPreviousIndex();
|
|
|
|
|
|
- // 取最近4条变更构建趋势(如果不足4条则全部)
|
|
|
+ // 取最近4条变更构建趋势(如果不足4条则全部),时间升序(最旧→最新)供前端折线图展示
|
|
|
int count = Math.min(4, foodLogs.size());
|
|
|
List<Map<String, Object>> points = new ArrayList<>();
|
|
|
- for (int i = 0; i < count; i++) {
|
|
|
+ for (int i = count - 1; i >= 0; i--) {
|
|
|
FoodRecommendIdxLog l = foodLogs.get(i);
|
|
|
Map<String, Object> p = new LinkedHashMap<>();
|
|
|
p.put("reportDate", l.getChangedAt());
|
|
|
@@ -315,15 +326,13 @@ public class FoodRecommendService {
|
|
|
points.add(p);
|
|
|
}
|
|
|
|
|
|
- // 计算变化方向:最新 score vs 上条 previousIndex
|
|
|
+ // 计算变化方向:最新 score vs 最新条的 previousIndex
|
|
|
String changeDir = "flat";
|
|
|
- if (foodLogs.size() >= 2) {
|
|
|
- Integer prev = foodLogs.get(1).getPreviousIndex();
|
|
|
+ if (latest.getPreviousIndex() != null && latest.getNewIndex() != null) {
|
|
|
+ Integer prev = latest.getPreviousIndex();
|
|
|
Integer cur = latest.getNewIndex();
|
|
|
- if (prev != null && cur != null) {
|
|
|
- if (cur > prev) changeDir = "up";
|
|
|
- else if (cur < prev) changeDir = "down";
|
|
|
- }
|
|
|
+ if (cur > prev) changeDir = "up";
|
|
|
+ else if (cur < prev) changeDir = "down";
|
|
|
}
|
|
|
|
|
|
Map<String, Object> item = new LinkedHashMap<>();
|