import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import { listSharedReceivingOverseasWarehouseOptions } from '@/modules/iam' import { normalizeOverseasPage, type OverseasPagePayload } from './lastMileOrders' export interface ReceivingOverseasWarehouseDto { id: string customerId: string warehouseCode: string warehouseName: string warehouseType: string status: string } export interface PalletDto { id: string customerId: string warehouseId: string locationId?: string | null palletCode: string palletType: string fbaWarehouseCode: string capacityCbm: number unitPrice: number status: string remark?: string | null version: number createdAt?: string | null updatedAt?: string | null } export interface SavePalletPayload { palletCode: string palletType: string fbaWarehouseCode: string capacityCbm: number unitPrice: number status: string locationId?: string | null remark?: string | null } export interface UpdatePalletPayload extends SavePalletPayload { version: number } export interface PalletSummaryDto { totalCount: number; idleCount: number; occupiedCount: number; scrappedCount: number } export async function listReceivingOverseasWarehouses() { const records: ReceivingOverseasWarehouseDto[] = (await listSharedReceivingOverseasWarehouseOptions()).map((warehouse) => ({ id: warehouse.id, customerId: '0', warehouseCode: warehouse.warehouseCode, warehouseName: warehouse.warehouseName, warehouseType: 'OVERSEAS', status: 'ACTIVE', })) return { records, page: 1, size: 200, total: records.length, pages: records.length ? 1 : 0 } } export async function listPallets(warehouseId: string | undefined, query: { page: number; size: number; keyword?: string; palletType?: string; status?: string }) { const response = await http.get>, ApiEnvelope>>( warehouseId ? `/platform/v1/overseas-warehouse-resources/${encodeURIComponent(warehouseId)}/pallets` : '/platform/v1/overseas-warehouse-resources/pallets', { params: { ...query } }, ) return normalizeOverseasPage(unwrapApiResponse(response)) } export async function getPalletSummary(warehouseId: string, query: { keyword?: string; palletType?: string }) { const path = warehouseId ? `/platform/v1/overseas-warehouse-resources/${encodeURIComponent(warehouseId)}/pallets/summary` : '/platform/v1/overseas-warehouse-resources/pallets/summary' const response = await http.get, ApiEnvelope>(path, { params: { page: 1, size: 1, ...query } }) const value = unwrapApiResponse(response); return { totalCount: Number(value.totalCount), idleCount: Number(value.idleCount), occupiedCount: Number(value.occupiedCount), scrappedCount: Number(value.scrappedCount) } } export async function getPallet(warehouseId: string, palletId: string) { const response = await http.get, ApiEnvelope>( `/platform/v1/overseas-warehouse-resources/${encodeURIComponent(warehouseId)}/pallets/${encodeURIComponent(palletId)}`, {}, ) return unwrapApiResponse(response) } export async function getPalletLabelPdf(warehouseId: string, palletId: string) { const blob = await http.get( `/platform/v1/overseas-warehouse-resources/${encodeURIComponent(warehouseId)}/pallets/${encodeURIComponent(palletId)}/label`, { responseType: 'blob', timeout: 60_000 }, ) if (blob.type.includes('application/json')) unwrapApiResponse(JSON.parse(await blob.text()) as ApiEnvelope) if (!blob.size || !blob.type.includes('application/pdf')) throw new Error('打印服务未返回有效 PDF,请重试') return blob } export async function createPallet(warehouseId: string, payload: SavePalletPayload) { const response = await http.post, ApiEnvelope>( `/platform/v1/overseas-warehouse-resources/${encodeURIComponent(warehouseId)}/pallets`, payload, ) return unwrapApiResponse(response) } export async function updatePallet(warehouseId: string, palletId: string, payload: UpdatePalletPayload) { const response = await http.put, ApiEnvelope>( `/platform/v1/overseas-warehouse-resources/${encodeURIComponent(warehouseId)}/pallets/${encodeURIComponent(palletId)}`, payload, ) return unwrapApiResponse(response) }