import { useQuery } from '@tanstack/react-query' import { ReloadOutlined } from '@ant-design/icons' import { Alert, Button, Checkbox, Empty, Input, Modal, Space, Tag, Typography } from 'antd' import type { TableColumnsType } from 'antd' import { useState } from 'react' import PercentTable from '@/components/common/PercentTable' import { fetchPendingPistolMatches } from '@/services/admin/consignment' import type { PendingPistolMatchPair } from '@/types/consignment' import { formatDateTime } from '@/utils/date-time' const { Text } = Typography function resolveScoreTag(pair: PendingPistolMatchPair) { if (pair.quantitySufficient === false) return { color: 'orange', label: '数量不足' } if (pair.matchType === 'exact') { return pair.autoMatchable === false ? { color: 'gold', label: '需人工' } : { color: 'green', label: '精确' } } if (pair.matchType === 'conflict') return { color: 'default', label: '不匹配' } if (pair.autoMatchable) return { color: 'blue', label: '可自动' } if (pair.score >= 90) return { color: 'blue', label: '非精确' } if (pair.score >= 70) return { color: 'orange', label: '非精确' } return { color: 'gold', label: '需核对' } } function resolveOrderStatusTag(status?: string) { if (status === 'internal_matching') return { color: 'purple', label: '内部匹配中' } if (status === 'unassigned') return { color: 'default', label: '待分配' } return { color: 'blue', label: '待抢单' } } export default function ConsignmentPendingMatchModal({ open, onClose, categoryId, categoryName, }: { open: boolean onClose: () => void /** 只统计该寄卖分类;不传 = 全部 */ categoryId?: number | null categoryName?: string }) { const [keyword, setKeyword] = useState('') const [includeConflicts, setIncludeConflicts] = useState(false) const query = useQuery({ queryKey: ['admin-pending-pistol-matches', categoryId ?? null, keyword, includeConflicts], queryFn: () => fetchPendingPistolMatches({ categoryId: categoryId ?? undefined, keyword, includeConflicts, limit: 300, }), enabled: open, refetchOnWindowFocus: false, retry: false, }) const data = query.data?.data const pairs = data?.pairs || [] const columns: TableColumnsType = [ { title: '匹配打分', key: 'score', width: 90, render: (_, record) => { const tag = resolveScoreTag(record) return ( {record.score} {tag.label} ) }, }, { title: '订单信息', key: 'order', width: 280, render: (_, record) => ( 工单 {record.workOrderNo ? ( {record.workOrderNo} ) : ( - )} 平台 {record.platformOrderId ? ( {record.platformOrderId} ) : ( - )} 录单 {record.workOrderCreatedAt ? formatDateTime(record.workOrderCreatedAt) : '-'} {resolveOrderStatusTag(record.workOrderStatus).label} {Number(record.totalQuantity || 1) > 1 ? ( 拼单剩余 {record.remainingQuantity ?? 0} / {record.totalQuantity} 份 ) : null} {Number(record.requiredQuantity || 0) > 0 ? ( {record.sharingEnabled ? `拼单还需 ${record.requiredQuantity} 份 / 该库存剩 ${record.availableQuantity ?? '-'} 件` : `需求 ${record.requiredQuantity} 件 / 该库存剩 ${record.availableQuantity ?? '-'} 件`} {record.quantitySufficient === false ? '(整单汇总不足)' : ''} ) : null} ), }, { title: '商品名', key: 'productName', width: 220, render: (_, record) => ( {record.productName || '-'} ), }, { title: '寄售信息', key: 'consignment', width: 280, render: (_, record) => ( 寄售 {record.consignmentNo || record.inventoryNo || '-'} {record.inventoryNo && record.inventoryNo !== record.consignmentNo ? ( 仓库 {record.inventoryNo} ) : null} 入库 {record.inventoryInboundAt ? formatDateTime(record.inventoryInboundAt) : '-'} ), }, { title: '寄售物品', key: 'itemName', width: 220, render: (_, record) => ( {record.itemName || '-'} ), }, { title: '匹配说明', key: 'reason', width: 300, render: (_, record) => ( {record.reason || '-'} {record.conflicts.length > 0 ? ( {record.conflicts.join(';')} ) : null} {(record.matchDetails || []) .filter((detail) => detail.status !== 'match' && detail.status !== 'ignored') .slice(0, 4) .map((detail) => ( {detail.label}:订单「{detail.orderValue || '-'}」/ 库存「 {detail.inventoryValue || '-'}」 ))} ), }, ] return ( {categoryName ? 分类:{categoryName} : null} 待匹配 {data?.pendingOrderCount ?? 0} 单 在库待匹配 {data?.inStockInventoryCount ?? 0} 件 匹配组合 {data?.pairCount ?? 0} 条 精确 {data?.exactCount ?? 0} 非精确 {data?.partialCount ?? 0} 数量不足 {data?.quantityShortCount ?? 0} 可自动 {data?.autoMatchableCount ?? 0} {includeConflicts ? 不匹配 {data?.conflictCount ?? 0} : null} setKeyword(value.trim())} /> setIncludeConflicts(event.target.checked)} > 显示 0 分不匹配项 {query.isError ? ( ) : null} {data?.truncated ? ( ) : null} rowKey={(record) => `${record.workOrderId}-${record.inventoryId}`} size="small" loading={query.isFetching} dataSource={pairs} columns={columns} scroll={{ y: 460 }} pagination={{ pageSize: 20, showSizeChanger: false, showTotal: (total) => `共 ${total} 条`, }} locale={{ emptyText: ( ), }} /> ) }