import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { PageResult } from './overseasMasterData' export interface HandoverSelection { recordVersion: number; vehicleId: string; driverId: string; expectedDriverEndAt: string; handedOverAt: string } export interface HandoverCommand { selection: HandoverSelection; cargoHash: string; location: string; reason?: string; idempotencyKey: string } export interface HandoverRelease { recordVersion: number; resourceKind: 'VEHICLE' | 'DRIVER'; actualEndAt: string; location: string; vehicleStatus?: string; idempotencyKey: string } export interface HandoverResource { vehicleId: string; vehiclePlate: string | null; driverId: string; driverName: string | null; driverPhone: string | null } type Numeric = number | string export interface HandoverCargo { customerId: string; dispatchNo: string; itemId: string; palletId: string; destinationKey: string; packages: Numeric } export interface HandoverDelivery { customerId: string; dispatchNo: string; deliveryNo: string; destinationKey: string; packages: Numeric; terminalAt: string | null } export interface HandoverLoad { palletSpaces: Numeric; weightKg: Numeric; volumeCbm: Numeric } export interface HandoverInspection { remainingCargo: HandoverCargo[]; completedDeliveries: HandoverDelivery[]; remainingPalletIds: string[]; occupied: HandoverLoad } export interface HandoverPreview { tripNo: string; recordVersion: Numeric; cargoHash: string; previous: HandoverResource; replacement: HandoverResource; capacity: HandoverLoad; cargo: HandoverInspection; driverQualification: { canDispatch: boolean; expiringSoon: boolean; reason: string; licenseExpiresOn: string | null } } export interface HandoverEnded { actualEndAt: string; location: string; vehicleStatus: string | null } export interface HandoverRecord { handoverNo: string; tripNo: string; recordVersion: Numeric snapshot: { previous: HandoverResource; replacement: HandoverResource; handedOverAt: string; location: string; reason: string; expectedDriverEndAt: string; capacity: HandoverLoad; cargo: HandoverInspection; recordedAt: string } vehicleRelease: HandoverEnded | null; driverRelease: HandoverEnded | null } const path = (tripNo: string) => `/platform/v1/last-leg/fleet/trips/${encodeURIComponent(tripNo)}/handovers` export function handoverNumber(value: Numeric): number { if ((typeof value !== 'number' && typeof value !== 'string') || String(value).trim() === '') throw new Error('交接接口缺少数值') const result = Number(value) if (!Number.isFinite(result) || result < 0) throw new Error('交接接口数值无效') return result } export function handoverVersion(value: Numeric): number { const result = handoverNumber(value) if (!Number.isSafeInteger(result)) throw new Error('交接接口版本或数量无效') return result } export async function listTripHandovers(tripNo: string, page = 1, size = 10): Promise> { const result = unwrapApiResponse(await http.get, ApiEnvelope<{ records: HandoverRecord[]; page: Numeric; size: Numeric; total: Numeric; pages: Numeric }>>(path(tripNo), { params: { page, size } })) return { records: result.records, page: handoverVersion(result.page), size: handoverVersion(result.size), total: handoverVersion(result.total), pages: handoverVersion(result.pages) } } export async function previewTripHandover(tripNo: string, selection: HandoverSelection): Promise { return unwrapApiResponse(await http.post, ApiEnvelope>(`${path(tripNo)}/preview`, selection)) } export async function confirmTripHandover(tripNo: string, command: HandoverCommand): Promise { return unwrapApiResponse(await http.post, ApiEnvelope>(path(tripNo), command)) } export async function releaseHandoverResource(tripNo: string, handoverNo: string, command: HandoverRelease): Promise { return unwrapApiResponse(await http.post, ApiEnvelope>(`${path(tripNo)}/${encodeURIComponent(handoverNo)}/resources/release`, command)) }