feat(platform): paginate vehicle coverage

This commit is contained in:
lingniu
2026-07-03 22:12:04 +08:00
parent 9359254d28
commit c34996dcfb
4 changed files with 63 additions and 17 deletions

View File

@@ -7,8 +7,10 @@ import (
)
type SQLQuery struct {
Text string
Args []any
Text string
Args []any
CountText string
CountArgs []any
}
func buildVehicleListSQL(query url.Values) SQLQuery {
@@ -73,11 +75,17 @@ func buildVehicleCoverageSQL(query url.Values) SQLQuery {
case "unbound":
having = append(having, "MAX(CASE WHEN b.vin IS NOT NULL AND b.vin <> '' THEN 1 ELSE 0 END) = 0")
}
countArgs := append([]any(nil), args...)
args = append(args, limit, offset)
havingSQL := ""
if len(having) > 0 {
havingSQL = ` HAVING ` + strings.Join(having, " AND ") + ` `
}
groupSQL := `FROM vehicle_realtime_snapshot s ` +
`LEFT JOIN vehicle_identity_binding b ON b.vin = s.vin ` +
`WHERE ` + strings.Join(where, " AND ") + ` ` +
`GROUP BY s.vin, b.plate, b.phone, b.oem, b.vin ` +
havingSQL
return SQLQuery{
Text: `SELECT s.vin, ` +
`COALESCE(NULLIF(MAX(NULLIF(s.plate, '')), ''), b.plate, '') AS plate, ` +
@@ -88,13 +96,11 @@ func buildVehicleCoverageSQL(query url.Values) SQLQuery {
`CASE WHEN COUNT(DISTINCT CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN s.protocol END) > 0 THEN 1 ELSE 0 END AS online, ` +
`COALESCE(DATE_FORMAT(MAX(s.updated_at), '%Y-%m-%d %H:%i:%s'), '') AS last_seen, ` +
`CASE WHEN b.vin IS NOT NULL AND b.vin <> '' THEN 'bound' ELSE 'unbound' END AS binding_status ` +
`FROM vehicle_realtime_snapshot s ` +
`LEFT JOIN vehicle_identity_binding b ON b.vin = s.vin ` +
`WHERE ` + strings.Join(where, " AND ") + ` ` +
`GROUP BY s.vin, b.plate, b.phone, b.oem, b.vin ` +
havingSQL +
`ORDER BY MAX(s.updated_at) DESC LIMIT ? OFFSET ?`,
Args: args,
groupSQL +
`ORDER BY MAX(s.updated_at) DESC, s.vin ASC LIMIT ? OFFSET ?`,
Args: args,
CountText: `SELECT COUNT(*) FROM (SELECT s.vin ` + groupSQL + `) vehicle_coverage_count`,
CountArgs: countArgs,
}
}

View File

@@ -91,6 +91,12 @@ func (s *ProductionStore) Vehicles(ctx context.Context, query url.Values) (Page[
func (s *ProductionStore) VehicleCoverage(ctx context.Context, query url.Values) (Page[VehicleCoverageRow], error) {
built := buildVehicleCoverageSQL(query)
total := 0
if built.CountText != "" {
if err := s.db.QueryRowContext(ctx, built.CountText, built.CountArgs...).Scan(&total); err != nil {
return Page[VehicleCoverageRow]{}, err
}
}
rows, err := s.db.QueryContext(ctx, built.Text, built.Args...)
if err != nil {
return Page[VehicleCoverageRow]{}, err
@@ -112,7 +118,10 @@ func (s *ProductionStore) VehicleCoverage(ctx context.Context, query url.Values)
return Page[VehicleCoverageRow]{}, err
}
limit, offset := buildLimitOffset(query)
return Page[VehicleCoverageRow]{Items: items, Total: len(items), Limit: limit, Offset: offset}, nil
if built.CountText == "" {
total = len(items)
}
return Page[VehicleCoverageRow]{Items: items, Total: total, Limit: limit, Offset: offset}, nil
}
func (s *ProductionStore) RealtimeLocations(ctx context.Context, query url.Values) (Page[RealtimeLocationRow], error) {

View File

@@ -22,7 +22,7 @@ func TestBuildVehicleListSQL(t *testing.T) {
func TestBuildVehicleCoverageSQL(t *testing.T) {
query := url.Values{"keyword": {"粤A"}, "protocol": {"GB32960"}, "coverage": {"multi"}, "online": {"online"}, "bindingStatus": {"bound"}, "limit": {"8"}, "offset": {"16"}}
built := buildVehicleCoverageSQL(query)
for _, want := range []string{"GROUP BY s.vin", "HAVING", "GROUP_CONCAT(DISTINCT s.protocol", "source_count", "online_source_count"} {
for _, want := range []string{"GROUP BY s.vin", "HAVING", "GROUP_CONCAT(DISTINCT s.protocol", "source_count", "online_source_count", "ORDER BY MAX(s.updated_at) DESC, s.vin ASC"} {
if !strings.Contains(built.Text, want) {
t.Fatalf("SQL missing %q: %s", want, built.Text)
}
@@ -30,9 +30,15 @@ func TestBuildVehicleCoverageSQL(t *testing.T) {
if strings.Contains(built.Text, "1ORDER BY") || strings.Contains(built.Text, "0ORDER BY") {
t.Fatalf("SQL should keep whitespace before ORDER BY: %s", built.Text)
}
if !strings.Contains(built.CountText, "vehicle_coverage_count") || !strings.Contains(built.CountText, "HAVING") {
t.Fatalf("count SQL = %s", built.CountText)
}
if len(built.Args) != 9 || built.Args[0] != "%粤A%" || built.Args[6] != "GB32960" || built.Args[7] != 8 || built.Args[8] != 16 {
t.Fatalf("args = %#v", built.Args)
}
if len(built.CountArgs) != 7 || built.CountArgs[0] != "%粤A%" || built.CountArgs[6] != "GB32960" {
t.Fatalf("count args = %#v", built.CountArgs)
}
}
func TestBuildDailyMileageSQL(t *testing.T) {