feat(platform): paginate realtime locations

This commit is contained in:
lingniu
2026-07-03 22:15:16 +08:00
parent c34996dcfb
commit ea96b6909a
4 changed files with 99 additions and 24 deletions

View File

@@ -104,6 +104,34 @@ func buildVehicleCoverageSQL(query url.Values) SQLQuery {
}
}
func buildRealtimeLocationSQL(query url.Values) SQLQuery {
limit := parsePositive(query.Get("limit"), 20)
offset := parsePositive(query.Get("offset"), 0)
args := []any{}
where := []string{"1 = 1"}
if protocol := strings.TrimSpace(query.Get("protocol")); protocol != "" {
where = append(where, "l.protocol = ?")
args = append(args, protocol)
}
if vin := strings.TrimSpace(query.Get("vin")); vin != "" {
where = append(where, "(l.vin LIKE ? OR l.plate LIKE ? OR b.plate LIKE ?)")
like := "%" + vin + "%"
args = append(args, like, like, like)
}
countArgs := append([]any(nil), args...)
args = append(args, limit, offset)
fromSQL := `FROM vehicle_realtime_location l LEFT JOIN vehicle_identity_binding b ON b.vin = l.vin WHERE ` + strings.Join(where, " AND ")
return SQLQuery{
Text: `SELECT l.vin, COALESCE(NULLIF(l.plate, ''), b.plate, '') AS plate, l.protocol, l.longitude, l.latitude, ` +
`COALESCE(l.speed_kmh, 0), COALESCE(l.soc_percent, 0), COALESCE(l.total_mileage_km, 0), ` +
`COALESCE(DATE_FORMAT(l.updated_at, '%Y-%m-%d %H:%i:%s'), '') ` +
fromSQL + ` ORDER BY l.updated_at DESC, l.vin ASC, l.protocol ASC LIMIT ? OFFSET ?`,
Args: args,
CountText: `SELECT COUNT(*) ` + fromSQL,
CountArgs: countArgs,
}
}
func buildDailyMileageSQL(query url.Values) SQLQuery {
limit := parsePositive(query.Get("limit"), 20)
offset := parsePositive(query.Get("offset"), 0)

View File

@@ -125,24 +125,12 @@ func (s *ProductionStore) VehicleCoverage(ctx context.Context, query url.Values)
}
func (s *ProductionStore) RealtimeLocations(ctx context.Context, query url.Values) (Page[RealtimeLocationRow], error) {
limit, offset := buildLimitOffset(query)
where := []string{"1 = 1"}
args := []any{}
if protocol := strings.TrimSpace(query.Get("protocol")); protocol != "" {
where = append(where, "l.protocol = ?")
args = append(args, protocol)
built := buildRealtimeLocationSQL(query)
total := 0
if err := s.db.QueryRowContext(ctx, built.CountText, built.CountArgs...).Scan(&total); err != nil {
return Page[RealtimeLocationRow]{}, err
}
if vin := strings.TrimSpace(query.Get("vin")); vin != "" {
where = append(where, "l.vin = ?")
args = append(args, vin)
}
args = append(args, limit, offset)
sqlText := `SELECT l.vin, COALESCE(NULLIF(l.plate, ''), b.plate, '') AS plate, l.protocol, l.longitude, l.latitude, ` +
`COALESCE(l.speed_kmh, 0), COALESCE(l.soc_percent, 0), COALESCE(l.total_mileage_km, 0), ` +
`COALESCE(DATE_FORMAT(l.updated_at, '%Y-%m-%d %H:%i:%s'), '') ` +
`FROM vehicle_realtime_location l LEFT JOIN vehicle_identity_binding b ON b.vin = l.vin ` +
`WHERE ` + strings.Join(where, " AND ") + ` ORDER BY l.updated_at DESC LIMIT ? OFFSET ?`
rows, err := s.db.QueryContext(ctx, sqlText, args...)
rows, err := s.db.QueryContext(ctx, built.Text, built.Args...)
if err != nil {
return Page[RealtimeLocationRow]{}, err
}
@@ -158,7 +146,8 @@ func (s *ProductionStore) RealtimeLocations(ctx context.Context, query url.Value
if err := rows.Err(); err != nil {
return Page[RealtimeLocationRow]{}, err
}
return Page[RealtimeLocationRow]{Items: items, Total: len(items), Limit: limit, Offset: offset}, nil
limit, offset := buildLimitOffset(query)
return Page[RealtimeLocationRow]{Items: items, Total: total, Limit: limit, Offset: offset}, nil
}
func (s *ProductionStore) HistoryLocations(ctx context.Context, query url.Values) (Page[HistoryLocationRow], error) {

View File

@@ -41,6 +41,25 @@ func TestBuildVehicleCoverageSQL(t *testing.T) {
}
}
func TestBuildRealtimeLocationSQL(t *testing.T) {
query := url.Values{"vin": {"粤A"}, "protocol": {"GB32960"}, "limit": {"10"}, "offset": {"20"}}
built := buildRealtimeLocationSQL(query)
for _, want := range []string{"vehicle_realtime_location", "vehicle_identity_binding", "ORDER BY l.updated_at DESC, l.vin ASC, l.protocol ASC"} {
if !strings.Contains(built.Text, want) {
t.Fatalf("SQL missing %q: %s", want, built.Text)
}
}
if !strings.Contains(built.CountText, "COUNT(*)") || strings.Contains(built.CountText, "LIMIT") {
t.Fatalf("count SQL = %s", built.CountText)
}
if len(built.Args) != 6 || built.Args[0] != "GB32960" || built.Args[4] != 10 || built.Args[5] != 20 {
t.Fatalf("args = %#v", built.Args)
}
if len(built.CountArgs) != 4 || built.CountArgs[0] != "GB32960" {
t.Fatalf("count args = %#v", built.CountArgs)
}
}
func TestBuildDailyMileageSQL(t *testing.T) {
query := url.Values{"vin": {"VIN001"}, "dateFrom": {"2026-07-01"}, "dateTo": {"2026-07-03"}}
built := buildDailyMileageSQL(query)