import { overseasHttp } from '@/shared/api/overseasHttp' import { unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import { normalizeOverseasPage, type OverseasPagePayload } from './lastMileOrders' import type { DeliveryDocumentSummary } from './deliveryDocuments' export interface DeliveryOrderQuery { page: number size: number customerId?: string documentNo?: string dispatchNo?: string waybillNo?: string warehouseId?: string status?: string vehiclePlate?: string driverName?: string deliveryMethod?: string plannedFrom?: string plannedTo?: string } export interface DeliveryOrderRow { version: number allowedActions: string[] bolNo?: string | null documentSummary?: DeliveryDocumentSummary id: string customerId: string customerName?: string | null deliveryNo: string outboundNo: string dispatchNo: string destinationWarehouseId?: string | null destinationWarehouseCode?: string | null destinationWarehouseName?: string | null destinationType?: 'FBA' | 'PRIVATE' | null deliveryAddress?: string | null contactName?: string | null contactPhone?: string | null overseasWarehouseId?: string | null overseasWarehouseName?: string | null status: string waybillNo?: string | null deliveryMethod?: string | null carrierName?: string | null pickupMethod?: string | null loadingType?: string | null vehicleType?: string | null vehicleSize?: string | null vehiclePlate?: string | null driverName?: string | null driverPhone?: string | null appointmentAccount?: string | null sellerName?: string | null palletCount: number packageCount: number totalWeightKg?: number | null totalVolumeCbm?: number | null plannedDeliveryAt?: string | null dispatchedAt?: string | null expectedArrivalAt?: string | null signedAt?: string | null remark?: string | null createdAt: string } export interface DeliverySkuRow { inboundCode?: string | null; customerName?: string | null; destinationWarehouseCode?: string | null; receivingAddress?: string | null; shipmentNo?: string | null; fbaTrackingNo?: string | null; skuCode?: string | null; productName?: string | null; packageCount: number; weightKg?: number | null; volumeCbm?: number | null } export interface DeliveryFeeRow { id: string; feeDirection: string; feeType: string; currency: string; amount: number; remark?: string | null } export interface DeliveryOperationRow { id: string; operatedAt: string; operationNode: string; operatorName?: string | null; result: string } export interface DeliveryOrderDetail { order: DeliveryOrderRow; skus: DeliverySkuRow[]; fees: DeliveryFeeRow[]; operations: DeliveryOperationRow[] } export interface DeliveryReturnSource { outboundPalletItemId: string inventoryPositionId: string palletNo?: string | null skuCode?: string | null maxReturnPackages: number } export interface DeliveryReturnRelation { inboundOrderId: string inboundCode?: string | null lastMileOrderNo?: string | null customerId: string returnSources: DeliveryReturnSource[] } export interface MarkDeliveryReturnPayload { inboundOrderId: string reason: string returnDirection: 'OVERSEAS_WAREHOUSE' | 'CUSTOMER' | 'OTHER' returnLogisticsNo?: string items: Array<{ outboundPalletItemId: string; inventoryPositionId: string; packageCount: number }> idempotencyKey: string } export async function listDeliveryOrders(query: DeliveryOrderQuery) { const { customerId, ...params } = query const response = await overseasHttp.get>, ApiEnvelope>>( '/platform/v1/last-leg/delivery-orders', { params: { ...params, ...(customerId ? { customerId } : {}) } }, ) return normalizeOverseasPage(unwrapApiResponse(response)) } export async function getDeliveryOrder(deliveryNo: string, customerId: string) { const response = await overseasHttp.get, ApiEnvelope>( `/platform/v1/last-leg/delivery-orders/${encodeURIComponent(deliveryNo)}`, { params: { customerId, page: 1, size: 200 } }, ) const detail = unwrapApiResponse(response) // Java Long values are serialized as decimal strings, including the version. const rawVersion: unknown = detail.order.version const version = typeof rawVersion === 'string' && /^(0|[1-9]\d*)$/.test(rawVersion) ? Number(rawVersion) : rawVersion if (typeof version !== 'number' || !Number.isSafeInteger(version) || version < 0) { throw new Error('派送单缺少有效版本,请刷新后重试') } return { ...detail, order: { ...detail.order, version } } } export async function listDeliveryReturnInboundOptions(deliveryNo: string, customerId: string) { const response = await overseasHttp.get, ApiEnvelope>( `/platform/v1/last-leg/delivery-orders/${encodeURIComponent(deliveryNo)}/return-inbound-options`, { params: { customerId } }, ) return unwrapApiResponse(response) } export async function markDeliveryReturn(deliveryNo: string, customerId: string, payload: MarkDeliveryReturnPayload) { const response = await overseasHttp.post, ApiEnvelope<{ id: string; exceptionNo: string; status: string }>>( `/platform/v1/last-leg/delivery-orders/${encodeURIComponent(deliveryNo)}/mark-return`, payload, { params: { customerId } }, ) return unwrapApiResponse(response) }