import type { MonitoringVehicle } from './types'; import type { VehicleRecentDay } from './api'; export function monitoringMileageValue(vehicle: MonitoringVehicle): number | string { if (vehicle.periodMileageKm !== undefined) return vehicle.periodMileageKm ?? '—'; if (vehicle.mileageAnomaly) return '数据异常'; if (!vehicle.isDataSynced) return '—'; return Math.max(0, vehicle.dailyKm || 0); } export function hasDailyMileage(day: VehicleRecentDay): boolean { return day.isDataSynced && Number.isFinite(day.dailyKm) && day.dailyKm >= 0; } export function recentMileageStats(days: VehicleRecentDay[]) { const validDays = days.filter(hasDailyMileage); const totalKm = validDays.reduce((sum, day) => sum + day.dailyKm, 0); return { totalKm, synced: validDays.length, avg: validDays.length ? totalKm / validDays.length : 0, max: Math.max(1, ...validDays.map(day => day.dailyKm)), totalDays: days.length, }; } export function mileageQualityLabel(vehicle: MonitoringVehicle): string | null { if (!vehicle.qualityStatus) return null; return `${vehicle.validDays ?? 0}天有效 · ${vehicle.missingDays ?? 0}天缺失 · ${vehicle.anomalyDays ?? 0}天异常`; }