feat(platform): resolve vehicle identity before navigation
This commit is contained in:
@@ -27,6 +27,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *Handler) routes() {
|
||||
h.mux.HandleFunc("GET /api/dashboard/summary", h.handleDashboardSummary)
|
||||
h.mux.HandleFunc("GET /api/vehicles", h.handleVehicles)
|
||||
h.mux.HandleFunc("GET /api/vehicles/resolve", h.handleVehicleResolve)
|
||||
h.mux.HandleFunc("GET /api/vehicles/coverage", h.handleVehicleCoverage)
|
||||
h.mux.HandleFunc("GET /api/vehicles/detail", h.handleVehicleDetail)
|
||||
h.mux.HandleFunc("GET /api/realtime/vehicles", h.handleVehicleRealtime)
|
||||
@@ -51,6 +52,17 @@ func (h *Handler) handleVehicles(w http.ResponseWriter, r *http.Request) {
|
||||
h.write(w, r, data, err)
|
||||
}
|
||||
|
||||
func (h *Handler) handleVehicleResolve(w http.ResponseWriter, r *http.Request) {
|
||||
keyword := firstNonEmpty(r.URL.Query().Get("keyword"), r.URL.Query().Get("vin"))
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
if keyword == "" {
|
||||
httpx.WriteError(w, http.StatusBadRequest, "VEHICLE_KEY_REQUIRED", "车辆关键词不能为空", "", traceID(r))
|
||||
return
|
||||
}
|
||||
data, err := h.service.ResolveVehicleIdentity(r.Context(), keyword, r.URL.Query().Get("protocol"))
|
||||
h.write(w, r, data, err)
|
||||
}
|
||||
|
||||
func (h *Handler) handleVehicleCoverage(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := h.service.VehicleCoverage(r.Context(), r.URL.Query())
|
||||
h.write(w, r, data, err)
|
||||
|
||||
@@ -117,6 +117,47 @@ func TestHandlerVehicleDetailKeepsUnresolvedLookupOutOfVIN(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerVehicleResolveReturnsCanonicalIdentity(t *testing.T) {
|
||||
handler := NewHandler(NewService(NewMockStore()))
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/vehicles/resolve?keyword=粤AG18312", nil)
|
||||
handler.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Data VehicleIdentityResolution `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
if !body.Data.Resolved || body.Data.VIN != "LB9A32A24R0LS1426" || body.Data.Plate != "粤AG18312" {
|
||||
t.Fatalf("resolve should return canonical identity, got %+v body=%s", body.Data, rec.Body.String())
|
||||
}
|
||||
if len(body.Data.Protocols) < 2 {
|
||||
t.Fatalf("resolve should include available source protocols, got %+v", body.Data.Protocols)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerVehicleResolveKeepsUnresolvedKeyword(t *testing.T) {
|
||||
handler := NewHandler(NewService(NewMockStore()))
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/vehicles/resolve?keyword=64646848247", nil)
|
||||
handler.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Data VehicleIdentityResolution `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
if body.Data.Resolved || body.Data.VIN != "" || body.Data.LookupKey != "64646848247" {
|
||||
t.Fatalf("unresolved keyword should not fabricate identity, got %+v body=%s", body.Data, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerRealtimeLocations(t *testing.T) {
|
||||
handler := NewHandler(NewService(NewMockStore()))
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
@@ -54,6 +54,18 @@ type VehicleCoverageRow struct {
|
||||
BindingStatus string `json:"bindingStatus"`
|
||||
}
|
||||
|
||||
type VehicleIdentityResolution struct {
|
||||
LookupKey string `json:"lookupKey"`
|
||||
Resolved bool `json:"resolved"`
|
||||
VIN string `json:"vin"`
|
||||
Plate string `json:"plate"`
|
||||
Phone string `json:"phone"`
|
||||
OEM string `json:"oem"`
|
||||
Protocols []string `json:"protocols"`
|
||||
Online bool `json:"online"`
|
||||
LastSeen string `json:"lastSeen"`
|
||||
}
|
||||
|
||||
type VehicleDetail struct {
|
||||
VIN string `json:"vin"`
|
||||
LookupKey string `json:"lookupKey"`
|
||||
|
||||
@@ -56,6 +56,58 @@ func (s *Service) Vehicles(ctx context.Context, query url.Values) (Page[VehicleR
|
||||
return s.store.Vehicles(ctx, query)
|
||||
}
|
||||
|
||||
func (s *Service) ResolveVehicleIdentity(ctx context.Context, keyword string, protocol string) (VehicleIdentityResolution, error) {
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
result := VehicleIdentityResolution{LookupKey: keyword, Protocols: []string{}}
|
||||
if keyword == "" {
|
||||
return result, nil
|
||||
}
|
||||
vehicleQuery := url.Values{"keyword": {keyword}, "limit": {"20"}}
|
||||
if protocol = strings.TrimSpace(protocol); protocol != "" {
|
||||
vehicleQuery.Set("protocol", protocol)
|
||||
}
|
||||
vehicles, err := s.store.Vehicles(ctx, vehicleQuery)
|
||||
if err != nil {
|
||||
return VehicleIdentityResolution{}, err
|
||||
}
|
||||
identity := resolveVehicleIdentity(keyword, vehicles.Items)
|
||||
if identity == nil || strings.TrimSpace(identity.VIN) == "" {
|
||||
if isLikelyVIN(keyword) {
|
||||
result.Resolved = true
|
||||
result.VIN = keyword
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
result.Resolved = true
|
||||
result.VIN = identity.VIN
|
||||
result.Plate = identity.Plate
|
||||
result.Phone = identity.Phone
|
||||
result.OEM = identity.OEM
|
||||
for _, vehicle := range vehicles.Items {
|
||||
if !strings.EqualFold(vehicle.VIN, identity.VIN) {
|
||||
continue
|
||||
}
|
||||
if vehicle.Online {
|
||||
result.Online = true
|
||||
}
|
||||
if vehicle.LastSeen > result.LastSeen {
|
||||
result.LastSeen = vehicle.LastSeen
|
||||
}
|
||||
if result.Plate == "" {
|
||||
result.Plate = vehicle.Plate
|
||||
}
|
||||
if result.Phone == "" {
|
||||
result.Phone = vehicle.Phone
|
||||
}
|
||||
if result.OEM == "" {
|
||||
result.OEM = vehicle.OEM
|
||||
}
|
||||
result.Protocols = appendIfMissing(result.Protocols, vehicle.Protocol)
|
||||
}
|
||||
sort.Strings(result.Protocols)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Service) VehicleCoverage(ctx context.Context, query url.Values) (Page[VehicleCoverageRow], error) {
|
||||
return s.store.VehicleCoverage(ctx, query)
|
||||
}
|
||||
@@ -419,3 +471,16 @@ func sourceNames(statuses []VehicleSourceStatus) []string {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func appendIfMissing(values []string, value string) []string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return values
|
||||
}
|
||||
for _, current := range values {
|
||||
if current == value {
|
||||
return values
|
||||
}
|
||||
}
|
||||
return append(values, value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user