feat(platform): paginate daily mileage

This commit is contained in:
lingniu
2026-07-03 22:23:10 +08:00
parent 72aa20b347
commit 39e2788e26
4 changed files with 85 additions and 17 deletions

View File

@@ -138,8 +138,13 @@ func buildDailyMileageSQL(query url.Values) SQLQuery {
args := []any{}
where := []string{"1 = 1"}
if vin := strings.TrimSpace(query.Get("vin")); vin != "" {
where = append(where, "m.vin = ?")
args = append(args, vin)
where = append(where, "(m.vin LIKE ? OR b.plate LIKE ?)")
like := "%" + vin + "%"
args = append(args, like, like)
}
if protocol := strings.TrimSpace(query.Get("protocol")); protocol != "" {
where = append(where, "m.protocol = ?")
args = append(args, protocol)
}
if dateFrom := strings.TrimSpace(query.Get("dateFrom")); dateFrom != "" {
where = append(where, "m.stat_date >= ?")
@@ -149,13 +154,16 @@ func buildDailyMileageSQL(query url.Values) SQLQuery {
where = append(where, "m.stat_date <= ?")
args = append(args, dateTo)
}
countArgs := append([]any(nil), args...)
args = append(args, limit, offset)
fromSQL := `FROM vehicle_daily_mileage m LEFT JOIN vehicle_identity_binding b ON b.vin = m.vin WHERE ` + strings.Join(where, " AND ")
return SQLQuery{
Text: `SELECT m.vin, COALESCE(b.plate, '') AS plate, DATE_FORMAT(m.stat_date, '%Y-%m-%d') AS stat_date, ` +
`m.first_total_mileage_km, m.latest_total_mileage_km, m.daily_mileage_km, m.protocol ` +
`FROM vehicle_daily_mileage m LEFT JOIN vehicle_identity_binding b ON b.vin = m.vin ` +
`WHERE ` + strings.Join(where, " AND ") + ` ORDER BY m.stat_date DESC LIMIT ? OFFSET ?`,
Args: args,
fromSQL + ` ORDER BY m.stat_date DESC, m.vin ASC, m.protocol ASC LIMIT ? OFFSET ?`,
Args: args,
CountText: `SELECT COUNT(*) ` + fromSQL,
CountArgs: countArgs,
}
}

View File

@@ -267,6 +267,12 @@ func (s *ProductionStore) RawFrames(ctx context.Context, query RawFrameQuery) (P
func (s *ProductionStore) DailyMileage(ctx context.Context, query url.Values) (Page[DailyMileageRow], error) {
built := buildDailyMileageSQL(query)
total := 0
if built.CountText != "" {
if err := s.db.QueryRowContext(ctx, built.CountText, built.CountArgs...).Scan(&total); err != nil {
return Page[DailyMileageRow]{}, err
}
}
rows, err := s.db.QueryContext(ctx, built.Text, built.Args...)
if err != nil {
return Page[DailyMileageRow]{}, err
@@ -284,7 +290,10 @@ func (s *ProductionStore) DailyMileage(ctx context.Context, query url.Values) (P
return Page[DailyMileageRow]{}, err
}
limit, offset := buildLimitOffset(query)
return Page[DailyMileageRow]{Items: items, Total: len(items), Limit: limit, Offset: offset}, nil
if built.CountText == "" {
total = len(items)
}
return Page[DailyMileageRow]{Items: items, Total: total, Limit: limit, Offset: offset}, nil
}
func (s *ProductionStore) QualityIssues(ctx context.Context, query url.Values) (Page[QualityIssueRow], error) {

View File

@@ -61,14 +61,23 @@ func TestBuildRealtimeLocationSQL(t *testing.T) {
}
func TestBuildDailyMileageSQL(t *testing.T) {
query := url.Values{"vin": {"VIN001"}, "dateFrom": {"2026-07-01"}, "dateTo": {"2026-07-03"}}
query := url.Values{"vin": {"VIN001"}, "protocol": {"JT808"}, "dateFrom": {"2026-07-01"}, "dateTo": {"2026-07-03"}}
built := buildDailyMileageSQL(query)
if !strings.Contains(built.Text, "vehicle_daily_mileage") || !strings.Contains(built.Text, "vehicle_identity_binding") {
t.Fatalf("SQL = %s", built.Text)
}
if len(built.Args) != 5 || built.Args[0] != "VIN001" || built.Args[3] != 20 || built.Args[4] != 0 {
if !strings.Contains(built.Text, "ORDER BY m.stat_date DESC, m.vin ASC, m.protocol ASC") {
t.Fatalf("SQL should keep stable pagination order: %s", built.Text)
}
if !strings.Contains(built.CountText, "COUNT(*)") || strings.Contains(built.CountText, "LIMIT") {
t.Fatalf("count SQL = %s", built.CountText)
}
if len(built.Args) != 7 || built.Args[0] != "%VIN001%" || built.Args[2] != "JT808" || built.Args[5] != 20 || built.Args[6] != 0 {
t.Fatalf("args = %#v", built.Args)
}
if len(built.CountArgs) != 5 || built.CountArgs[0] != "%VIN001%" || built.CountArgs[2] != "JT808" {
t.Fatalf("count args = %#v", built.CountArgs)
}
}
func TestBuildRawFrameSQL(t *testing.T) {