import type { ReplenishmentDemandSortField } from '@/api/storage.management/management' export interface ReplenishmentDemandQueryForm { pageSize: number sortField: ReplenishmentDemandSortField sortOrder: 'ASC' | 'DESC' } export interface ReplenishmentDemandListItem { stock_id: number stock_code?: string stock_name?: string pick_zone_quantity?: number storage_zone_quantity?: number total_demand?: number shortage?: number replenishment_needed?: number replenishment_status?: string replenishment_locations?: Array<{ space_code?: string space_name?: string available_quantity?: number }> [key: string]: unknown } export interface ReplenishmentDemandSummary { total_skus?: number total_shortage_skus?: number total_replenishment_needed?: number total_true_shortage_skus?: number [key: string]: number | undefined } export const defaultQueryForm = (): ReplenishmentDemandQueryForm => ({ pageSize: 20, sortField: 'shortage', sortOrder: 'DESC', }) export const sortFieldOptions: Array<{ label: string; value: ReplenishmentDemandSortField }> = [ { label: 'SKU ID', value: 'stock_id' }, { label: '分拣区库存', value: 'pick_zone_quantity' }, { label: '存货区库存', value: 'storage_zone_quantity' }, { label: '总需求量', value: 'total_demand' }, { label: '缺口数量', value: 'shortage' }, { label: '建议补货量', value: 'replenishment_needed' }, ] export const sortOrderOptions = [ { label: '降序', value: 'DESC' as const }, { label: '升序', value: 'ASC' as const }, ] export const replenishmentStatusLabelMap: Record = { true_shortage: '真实缺货', replenishable: '可补货', sufficient: '充足', } export const replenishmentStatusTagMap: Record = { true_shortage: 'danger', replenishable: 'warning', sufficient: 'success', } export function isReplenishmentDemandSuccess(res: any): boolean { return !!(res && (res.code === 1 || res.code === 200 || res.flag === 'success')) } export function parseReplenishmentDemandResponse(res: any): { list: ReplenishmentDemandListItem[] summary: ReplenishmentDemandSummary pagination: { page: number; pageSize: number; total: number } } { const data = res?.data && typeof res.data === 'object' ? res.data : res const list = (data?.list ?? res?.list ?? []) as ReplenishmentDemandListItem[] const summary = (data?.summary ?? res?.summary ?? {}) as ReplenishmentDemandSummary const paginationRaw = data?.pagination ?? res?.pagination ?? {} const total = Number( paginationRaw?.total ?? data?.total ?? res?.total ?? list.length ) return { list, summary, pagination: { page: Number(paginationRaw?.page ?? data?.page ?? 1), pageSize: Number(paginationRaw?.pageSize ?? data?.pageSize ?? 20), total: Number.isFinite(total) ? total : list.length, }, } } export function exportReplenishmentDemandCsv(rows: ReplenishmentDemandListItem[], filename = '补货需求.csv') { if (!rows.length) return const headers = [ 'SKU编码', '商品名称', '分拣区库存', '存货区库存', '总需求量', '缺口数量', '建议补货量', '补货状态', '补货库位数', ] const lines = rows.map((row) => [ row.stock_code ?? '', row.stock_name ?? '', row.pick_zone_quantity ?? '', row.storage_zone_quantity ?? '', row.total_demand ?? '', row.shortage ?? '', row.replenishment_needed ?? '', replenishmentStatusLabelMap[row.replenishment_status ?? ''] ?? row.replenishment_status ?? '', row.replenishment_locations?.length ?? 0, ]) const csv = [headers, ...lines] .map((line) => line.map((cell) => `"${String(cell).replace(/"/g, '""')}"`).join(',')) .join('\n') const blob = new Blob(['\uFEFF' + csv], { type: 'text/csv;charset=utf-8;' }) const url = URL.createObjectURL(blob) const link = document.createElement('a') link.href = url link.download = filename link.click() URL.revokeObjectURL(url) }