import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import { normalizeOverseasPage, type OverseasPagePayload } from './lastMileOrders' export type OverseasLocationType = 'VIRTUAL' | 'NORMAL' | 'SHARED_OVERFLOW' export type OverseasLocationStatus = 'ACTIVE' | 'IDLE' | 'OCCUPIED' | 'INACTIVE' | 'DISABLED' export interface OverseasLocationDto { id: string warehouseId: string warehouseCode?: string | null warehouseName?: string | null locationCode: string locationName?: string | null locationType: OverseasLocationType sharedOverflow: boolean status: OverseasLocationStatus maxLoadKg?: number | string | null occupiedPalletCount?: number | null version: number remark?: string | null createdAt?: string | null updatedAt?: string | null } export interface SaveOverseasLocationPayload { locationCode: string locationType: 'NORMAL' maxLoadKg: number status: 'ACTIVE' | 'INACTIVE' remark?: string | null } export interface OverseasLocationQuery { page?: number; size?: number; keyword?: string; locationType?: OverseasLocationType | ''; status?: OverseasLocationStatus | '' } function base(warehouseId: string) { return `/platform/v1/overseas-warehouse-resources/${encodeURIComponent(warehouseId)}/locations` } export async function listOverseasLocations(warehouseId: string, query: OverseasLocationQuery = {}) { const response = await http.get>, ApiEnvelope>>(base(warehouseId), { params: { page: query.page ?? 1, size: query.size ?? 20, ...query } }) return normalizeOverseasPage(unwrapApiResponse(response)) } export async function listAllOverseasLocations(warehouseIds: string[], query: OverseasLocationQuery = {}) { const results = await Promise.all(warehouseIds.map((warehouseId) => listOverseasLocations(warehouseId, { ...query, page: 1, size: 200 }))) const records = results.flatMap((result) => result.records) return { records, total: records.length } } export async function createOverseasLocation(warehouseId: string, payload: SaveOverseasLocationPayload) { const response = await http.post, ApiEnvelope>(base(warehouseId), payload) return unwrapApiResponse(response) } export async function updateOverseasLocation(warehouseId: string, locationId: string, payload: SaveOverseasLocationPayload & { version: number }) { const response = await http.put, ApiEnvelope>(`${base(warehouseId)}/${encodeURIComponent(locationId)}`, payload) return unwrapApiResponse(response) } export async function changeOverseasLocationStatus(warehouseId: string, locationId: string, payload: SaveOverseasLocationPayload & { version: number }) { const response = await http.put, ApiEnvelope>(`${base(warehouseId)}/${encodeURIComponent(locationId)}`, payload) return unwrapApiResponse(response) } export async function deleteOverseasLocation(warehouseId: string, locationId: string, version: number) { const response = await http.delete, ApiEnvelope>(`${base(warehouseId)}/${encodeURIComponent(locationId)}`, { params: { version } }) return unwrapApiResponse(response) }