feat(platform): enrich history locations with plates

This commit is contained in:
lingniu
2026-07-03 23:14:40 +08:00
parent 55b069e27d
commit a644a6617c
2 changed files with 53 additions and 3 deletions

View File

@@ -285,9 +285,60 @@ func (s *ProductionStore) HistoryLocationsFromTDengine(ctx context.Context, quer
if built.CountText == "" {
total = len(items)
}
if err := s.enrichHistoryLocationPlates(ctx, items); err != nil {
return Page[HistoryLocationRow]{}, err
}
return Page[HistoryLocationRow]{Items: items, Total: total, Limit: limit, Offset: offset}, nil
}
func (s *ProductionStore) enrichHistoryLocationPlates(ctx context.Context, items []HistoryLocationRow) error {
vins := make([]string, 0)
seen := map[string]struct{}{}
for _, item := range items {
vin := strings.TrimSpace(item.VIN)
if vin == "" {
continue
}
if _, ok := seen[vin]; ok {
continue
}
seen[vin] = struct{}{}
vins = append(vins, vin)
}
if len(vins) == 0 {
return nil
}
placeholders := make([]string, len(vins))
args := make([]any, len(vins))
for index, vin := range vins {
placeholders[index] = "?"
args[index] = vin
}
rows, err := s.db.QueryContext(ctx, `SELECT vin, COALESCE(plate, '') FROM vehicle_identity_binding WHERE vin IN (`+strings.Join(placeholders, ",")+`)`, args...)
if err != nil {
return err
}
defer rows.Close()
plates := map[string]string{}
for rows.Next() {
var vin, plate string
if err := rows.Scan(&vin, &plate); err != nil {
return err
}
plates[vin] = plate
}
if err := rows.Err(); err != nil {
return err
}
for index := range items {
if strings.TrimSpace(items[index].Plate) != "" {
continue
}
items[index].Plate = plates[items[index].VIN]
}
return nil
}
func (s *ProductionStore) RawFrames(ctx context.Context, query RawFrameQuery) (Page[RawFrameRow], error) {
if s.tdengine == nil {
return Page[RawFrameRow]{Items: []RawFrameRow{}, Total: 0, Limit: query.Limit, Offset: query.Offset}, nil