import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' export interface ContainerArrivalTime { sourceContainerBookingId: string estimatedWarehouseArrivalAt: string | null actualWarehouseArrivalAt: string | null version: number planCount: number plannedDate?: string | null isDefault?: boolean } function normalizeArrival(item: ContainerArrivalTime): ContainerArrivalTime { if (typeof item.planCount !== 'number' && (typeof item.planCount !== 'string' || !/^(0|[1-9]\d*)$/.test(item.planCount))) throw new Error('到仓记录的入库计划数量无效,请刷新后重试') const count = Number(item.planCount) if (item.planCount == null || !Number.isSafeInteger(count) || count < 0) throw new Error('到仓记录的入库计划数量无效,请刷新后重试') return { ...item, version: Number(item.version), planCount: count } } const queryPath = '/platform/v1/last-leg/orders/container-arrival-times/query' function operationId(containerBookingId: string) { return `container-expected-arrival-${containerBookingId}-${Date.now()}-${crypto.randomUUID()}` } export async function listContainerArrivalTimes(sourceContainerBookingIds: string[]) { if (!sourceContainerBookingIds.length) return [] const response = await http.post, ApiEnvelope>( queryPath, { sourceContainerBookingIds }, ) return (unwrapApiResponse(response) || []).map(normalizeArrival) } export async function updateContainerExpectedArrival( sourceContainerBookingId: string, body: { estimatedWarehouseArrivalAt: string; expectedVersion: number; remark?: string }, ) { const response = await http.put, ApiEnvelope>( `/platform/v1/last-leg/orders/container-summaries/${encodeURIComponent(sourceContainerBookingId)}/expected-arrival`, body, { headers: { 'Idempotency-Key': operationId(sourceContainerBookingId) } }, ) const result = unwrapApiResponse(response) return normalizeArrival(result) }