feat(platform): add batch vehicle overview api

This commit is contained in:
lingniu
2026-07-04 03:05:27 +08:00
parent 1a86e5d38c
commit f996fa0df9
5 changed files with 141 additions and 0 deletions

View File

@@ -36,6 +36,13 @@ type RawFrameQuery struct {
Offset int `json:"offset"`
}
type VehicleOverviewBatchQuery struct {
Keywords []string `json:"keywords"`
Protocol string `json:"protocol"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
type Service struct {
store Store
runtime RuntimeInfo
@@ -125,6 +132,33 @@ func (s *Service) VehicleServiceOverview(ctx context.Context, keyword string, pr
return *overview, nil
}
func (s *Service) VehicleServiceOverviews(ctx context.Context, query VehicleOverviewBatchQuery) (Page[VehicleServiceOverview], error) {
keywords := normalizedKeywords(query.Keywords)
total := len(keywords)
if query.Offset < 0 {
query.Offset = 0
}
if query.Limit <= 0 || query.Limit > 200 {
query.Limit = 200
}
if query.Offset >= total {
return Page[VehicleServiceOverview]{Items: []VehicleServiceOverview{}, Total: total, Limit: query.Limit, Offset: query.Offset}, nil
}
end := query.Offset + query.Limit
if end > total {
end = total
}
items := make([]VehicleServiceOverview, 0, end-query.Offset)
for _, keyword := range keywords[query.Offset:end] {
overview, err := s.VehicleServiceOverview(ctx, keyword, query.Protocol)
if err != nil {
return Page[VehicleServiceOverview]{}, err
}
items = append(items, overview)
}
return Page[VehicleServiceOverview]{Items: items, Total: total, Limit: query.Limit, Offset: query.Offset}, nil
}
func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string) (VehicleDetail, error) {
keyword := strings.TrimSpace(vin)
protocol = strings.TrimSpace(protocol)
@@ -515,6 +549,23 @@ func cloneValues(values url.Values) url.Values {
return cloned
}
func normalizedKeywords(values []string) []string {
out := make([]string, 0, len(values))
seen := map[string]struct{}{}
for _, value := range values {
value = strings.TrimSpace(value)
if value == "" {
continue
}
if _, ok := seen[value]; ok {
continue
}
seen[value] = struct{}{}
out = append(out, value)
}
return out
}
func vehicleSourceStatus(vehicles []VehicleRow, realtime []RealtimeLocationRow, history []HistoryLocationRow, raw []RawFrameRow, mileage []DailyMileageRow) []VehicleSourceStatus {
statusByProtocol := map[string]*VehicleSourceStatus{}
ensure := func(protocol string) *VehicleSourceStatus {