refactor: simplify history and mileage runtime

This commit is contained in:
lingniu
2026-07-01 07:18:06 +08:00
parent cf4d369335
commit e47e3eebb4
4 changed files with 70 additions and 26 deletions

View File

@@ -57,8 +57,7 @@ public final class JdbcVehicleStatMetricRepository implements VehicleStatReposit
String normalizedVin = clean(vin);
Date date = Date.valueOf(statDate);
String strategy = DailyMileageStrategy.JT808_TOTAL_MILEAGE_DIFF.name();
DailyMileageState existingState = findDailyMileageState(normalizedVin, date)
.orElseGet(() -> legacyDailyMileageState(normalizedVin, date).orElse(null));
DailyMileageState existingState = findDailyMileageState(normalizedVin, date).orElse(null);
double firstTotalMileage = existingState == null || !Double.isFinite(existingState.firstTotalMileageKm())
? totalMileageKm
: existingState.firstTotalMileageKm();
@@ -90,29 +89,6 @@ public final class JdbcVehicleStatMetricRepository implements VehicleStatReposit
.findFirst();
}
private Optional<DailyMileageState> legacyDailyMileageState(String vin, Date statDate) {
OptionalDouble start = findMetric(vin, statDate, DAILY_MILEAGE_START_TOTAL_KEY);
OptionalDouble latest = findMetric(vin, statDate, DAILY_MILEAGE_LATEST_TOTAL_KEY);
if (start.isEmpty() || latest.isEmpty()) {
return Optional.empty();
}
return Optional.of(new DailyMileageState(start.getAsDouble(), latest.getAsDouble()));
}
private OptionalDouble findMetric(String vin, Date statDate, String metricKey) {
List<Double> rows = jdbcTemplate.query("""
SELECT metric_value
FROM vehicle_stat_metric
WHERE vin = ? AND stat_date = ? AND metric_key = ?
""", (rs, rowNum) -> rs.getBigDecimal("metric_value") == null
? Double.NaN
: rs.getBigDecimal("metric_value").doubleValue(), vin, statDate, metricKey);
if (rows.isEmpty() || !Double.isFinite(rows.getFirst())) {
return OptionalDouble.empty();
}
return OptionalDouble.of(rows.getFirst());
}
private void upsertDailyMileageMetric(String vin, Date statDate, double value, String unit, String strategy,
double firstTotalMileageKm, double latestTotalMileageKm) {
try {