feat(platform): include identity resolution in vehicle detail
This commit is contained in:
@@ -75,6 +75,18 @@ func TestHandlerVehicleDetailResolvesPlateToVIN(t *testing.T) {
|
|||||||
if !strings.Contains(rec.Body.String(), `"vin":"LB9A32A24R0LS1426"`) {
|
if !strings.Contains(rec.Body.String(), `"vin":"LB9A32A24R0LS1426"`) {
|
||||||
t.Fatalf("response should resolve plate to VIN: %s", rec.Body.String())
|
t.Fatalf("response should resolve plate to VIN: %s", rec.Body.String())
|
||||||
}
|
}
|
||||||
|
var body struct {
|
||||||
|
Data VehicleDetail `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.Resolution == nil || !body.Data.Resolution.Resolved || body.Data.Resolution.VIN != "LB9A32A24R0LS1426" {
|
||||||
|
t.Fatalf("vehicle detail should include canonical identity resolution, got %+v body=%s", body.Data.Resolution, rec.Body.String())
|
||||||
|
}
|
||||||
|
if len(body.Data.Resolution.Protocols) < 2 {
|
||||||
|
t.Fatalf("vehicle detail resolution should include source protocols, got %+v", body.Data.Resolution.Protocols)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandlerVehicleDetailAcceptsKeyword(t *testing.T) {
|
func TestHandlerVehicleDetailAcceptsKeyword(t *testing.T) {
|
||||||
|
|||||||
@@ -67,18 +67,19 @@ type VehicleIdentityResolution struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type VehicleDetail struct {
|
type VehicleDetail struct {
|
||||||
VIN string `json:"vin"`
|
VIN string `json:"vin"`
|
||||||
LookupKey string `json:"lookupKey"`
|
LookupKey string `json:"lookupKey"`
|
||||||
LookupResolved bool `json:"lookupResolved"`
|
LookupResolved bool `json:"lookupResolved"`
|
||||||
Identity *VehicleRow `json:"identity,omitempty"`
|
Resolution *VehicleIdentityResolution `json:"resolution,omitempty"`
|
||||||
RealtimeSummary *VehicleRealtimeRow `json:"realtimeSummary,omitempty"`
|
Identity *VehicleRow `json:"identity,omitempty"`
|
||||||
Sources []string `json:"sources"`
|
RealtimeSummary *VehicleRealtimeRow `json:"realtimeSummary,omitempty"`
|
||||||
SourceStatus []VehicleSourceStatus `json:"sourceStatus"`
|
Sources []string `json:"sources"`
|
||||||
Realtime []RealtimeLocationRow `json:"realtime"`
|
SourceStatus []VehicleSourceStatus `json:"sourceStatus"`
|
||||||
History Page[HistoryLocationRow] `json:"history"`
|
Realtime []RealtimeLocationRow `json:"realtime"`
|
||||||
Raw Page[RawFrameRow] `json:"raw"`
|
History Page[HistoryLocationRow] `json:"history"`
|
||||||
Mileage Page[DailyMileageRow] `json:"mileage"`
|
Raw Page[RawFrameRow] `json:"raw"`
|
||||||
Quality Page[QualityIssueRow] `json:"quality"`
|
Mileage Page[DailyMileageRow] `json:"mileage"`
|
||||||
|
Quality Page[QualityIssueRow] `json:"quality"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type VehicleSourceStatus struct {
|
type VehicleSourceStatus struct {
|
||||||
|
|||||||
@@ -58,9 +58,8 @@ func (s *Service) Vehicles(ctx context.Context, query url.Values) (Page[VehicleR
|
|||||||
|
|
||||||
func (s *Service) ResolveVehicleIdentity(ctx context.Context, keyword string, protocol string) (VehicleIdentityResolution, error) {
|
func (s *Service) ResolveVehicleIdentity(ctx context.Context, keyword string, protocol string) (VehicleIdentityResolution, error) {
|
||||||
keyword = strings.TrimSpace(keyword)
|
keyword = strings.TrimSpace(keyword)
|
||||||
result := VehicleIdentityResolution{LookupKey: keyword, Protocols: []string{}}
|
|
||||||
if keyword == "" {
|
if keyword == "" {
|
||||||
return result, nil
|
return VehicleIdentityResolution{LookupKey: keyword, Protocols: []string{}}, nil
|
||||||
}
|
}
|
||||||
vehicleQuery := url.Values{"keyword": {keyword}, "limit": {"20"}}
|
vehicleQuery := url.Values{"keyword": {keyword}, "limit": {"20"}}
|
||||||
if protocol = strings.TrimSpace(protocol); protocol != "" {
|
if protocol = strings.TrimSpace(protocol); protocol != "" {
|
||||||
@@ -70,42 +69,7 @@ func (s *Service) ResolveVehicleIdentity(ctx context.Context, keyword string, pr
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return VehicleIdentityResolution{}, err
|
return VehicleIdentityResolution{}, err
|
||||||
}
|
}
|
||||||
identity := resolveVehicleIdentity(keyword, vehicles.Items)
|
return buildVehicleIdentityResolution(keyword, vehicles.Items), nil
|
||||||
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) {
|
func (s *Service) VehicleCoverage(ctx context.Context, query url.Values) (Page[VehicleCoverageRow], error) {
|
||||||
@@ -128,12 +92,13 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return VehicleDetail{}, err
|
return VehicleDetail{}, err
|
||||||
}
|
}
|
||||||
|
resolution := buildVehicleIdentityResolution(keyword, vehicles.Items)
|
||||||
identity := resolveVehicleIdentity(keyword, vehicles.Items)
|
identity := resolveVehicleIdentity(keyword, vehicles.Items)
|
||||||
resolvedVIN := ""
|
resolvedVIN := ""
|
||||||
if identity != nil && strings.TrimSpace(identity.VIN) != "" {
|
if identity != nil && strings.TrimSpace(identity.VIN) != "" {
|
||||||
resolvedVIN = identity.VIN
|
resolvedVIN = identity.VIN
|
||||||
} else if isLikelyVIN(keyword) {
|
} else if resolution.Resolved {
|
||||||
resolvedVIN = keyword
|
resolvedVIN = resolution.VIN
|
||||||
}
|
}
|
||||||
queryVIN := resolvedVIN
|
queryVIN := resolvedVIN
|
||||||
if queryVIN == "" {
|
if queryVIN == "" {
|
||||||
@@ -152,6 +117,7 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string
|
|||||||
VIN: "",
|
VIN: "",
|
||||||
LookupKey: keyword,
|
LookupKey: keyword,
|
||||||
LookupResolved: false,
|
LookupResolved: false,
|
||||||
|
Resolution: &resolution,
|
||||||
Sources: []string{},
|
Sources: []string{},
|
||||||
SourceStatus: []VehicleSourceStatus{},
|
SourceStatus: []VehicleSourceStatus{},
|
||||||
Realtime: []RealtimeLocationRow{},
|
Realtime: []RealtimeLocationRow{},
|
||||||
@@ -207,6 +173,7 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string
|
|||||||
VIN: resolvedVIN,
|
VIN: resolvedVIN,
|
||||||
LookupKey: keyword,
|
LookupKey: keyword,
|
||||||
LookupResolved: resolvedVIN != "",
|
LookupResolved: resolvedVIN != "",
|
||||||
|
Resolution: &resolution,
|
||||||
Identity: identity,
|
Identity: identity,
|
||||||
RealtimeSummary: summary,
|
RealtimeSummary: summary,
|
||||||
Sources: sourceNames(sourceStatus),
|
Sources: sourceNames(sourceStatus),
|
||||||
@@ -484,3 +451,44 @@ func appendIfMissing(values []string, value string) []string {
|
|||||||
}
|
}
|
||||||
return append(values, value)
|
return append(values, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildVehicleIdentityResolution(keyword string, vehicles []VehicleRow) VehicleIdentityResolution {
|
||||||
|
keyword = strings.TrimSpace(keyword)
|
||||||
|
result := VehicleIdentityResolution{LookupKey: keyword, Protocols: []string{}}
|
||||||
|
identity := resolveVehicleIdentity(keyword, vehicles)
|
||||||
|
if identity == nil || strings.TrimSpace(identity.VIN) == "" {
|
||||||
|
if isLikelyVIN(keyword) {
|
||||||
|
result.Resolved = true
|
||||||
|
result.VIN = keyword
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
result.Resolved = true
|
||||||
|
result.VIN = identity.VIN
|
||||||
|
result.Plate = identity.Plate
|
||||||
|
result.Phone = identity.Phone
|
||||||
|
result.OEM = identity.OEM
|
||||||
|
for _, vehicle := range vehicles {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ export interface VehicleDetail {
|
|||||||
vin: string;
|
vin: string;
|
||||||
lookupKey: string;
|
lookupKey: string;
|
||||||
lookupResolved: boolean;
|
lookupResolved: boolean;
|
||||||
|
resolution?: VehicleIdentityResolution;
|
||||||
identity?: VehicleRow;
|
identity?: VehicleRow;
|
||||||
realtimeSummary?: VehicleRealtimeRow;
|
realtimeSummary?: VehicleRealtimeRow;
|
||||||
sources: string[];
|
sources: string[];
|
||||||
|
|||||||
@@ -73,15 +73,16 @@ export function VehicleDetail({
|
|||||||
const identity = detail?.identity;
|
const identity = detail?.identity;
|
||||||
const summary = detail?.realtimeSummary;
|
const summary = detail?.realtimeSummary;
|
||||||
const latest = detail?.realtime[0];
|
const latest = detail?.realtime[0];
|
||||||
const resolvedVIN = detail?.vin || summary?.vin || identity?.vin || latest?.vin || query.keyword;
|
const resolution = detail?.resolution;
|
||||||
const hasResolvedVIN = detail?.lookupResolved ?? isLikelyVIN(resolvedVIN);
|
const resolvedVIN = resolution?.vin || detail?.vin || summary?.vin || identity?.vin || latest?.vin || query.keyword;
|
||||||
|
const hasResolvedVIN = resolution?.resolved ?? detail?.lookupResolved ?? isLikelyVIN(resolvedVIN);
|
||||||
const displayVIN = hasResolvedVIN ? resolvedVIN : '-';
|
const displayVIN = hasResolvedVIN ? resolvedVIN : '-';
|
||||||
const displayLookupKey = detail?.lookupKey || query.keyword;
|
const displayLookupKey = resolution?.lookupKey || detail?.lookupKey || query.keyword;
|
||||||
const protocols = useMemo(() => detail?.sources ?? [], [detail?.sources]);
|
const protocols = useMemo(() => resolution?.protocols?.length ? resolution.protocols : detail?.sources ?? [], [detail?.sources, resolution?.protocols]);
|
||||||
const latestRaw = detail?.raw.items[0];
|
const latestRaw = detail?.raw.items[0];
|
||||||
const qualityCount = detail?.quality.total ?? 0;
|
const qualityCount = detail?.quality.total ?? 0;
|
||||||
const online = summary?.online || identity?.online || false;
|
const online = resolution?.online || summary?.online || identity?.online || false;
|
||||||
const lastSeen = summary?.lastSeen || latest?.lastSeen || '-';
|
const lastSeen = resolution?.lastSeen || summary?.lastSeen || latest?.lastSeen || '-';
|
||||||
const formKey = `${query.keyword}-${query.protocol ?? ''}`;
|
const formKey = `${query.keyword}-${query.protocol ?? ''}`;
|
||||||
const qualityTable = (
|
const qualityTable = (
|
||||||
<Table
|
<Table
|
||||||
@@ -131,9 +132,9 @@ export function VehicleDetail({
|
|||||||
{ key: '查询关键词', value: displayLookupKey },
|
{ key: '查询关键词', value: displayLookupKey },
|
||||||
{ key: 'VIN', value: displayVIN },
|
{ key: 'VIN', value: displayVIN },
|
||||||
{ key: '解析状态', value: <Tag color={hasResolvedVIN ? 'green' : 'orange'}>{hasResolvedVIN ? '已解析 VIN' : '未解析到 VIN'}</Tag> },
|
{ key: '解析状态', value: <Tag color={hasResolvedVIN ? 'green' : 'orange'}>{hasResolvedVIN ? '已解析 VIN' : '未解析到 VIN'}</Tag> },
|
||||||
{ key: '车牌', value: summary?.plate || identity?.plate || latest?.plate || '-' },
|
{ key: '车牌', value: resolution?.plate || summary?.plate || identity?.plate || latest?.plate || '-' },
|
||||||
{ key: '手机号', value: summary?.phone || identity?.phone || '-' },
|
{ key: '手机号', value: resolution?.phone || summary?.phone || identity?.phone || '-' },
|
||||||
{ key: 'OEM', value: summary?.oem || identity?.oem || '-' },
|
{ key: 'OEM', value: resolution?.oem || summary?.oem || identity?.oem || '-' },
|
||||||
{ key: '在线', value: <StatusTag status={online ? 'ok' : 'offline'} /> },
|
{ key: '在线', value: <StatusTag status={online ? 'ok' : 'offline'} /> },
|
||||||
{
|
{
|
||||||
key: '数据来源',
|
key: '数据来源',
|
||||||
|
|||||||
Reference in New Issue
Block a user