Files
order_site/apps/frontend/src/components/admin/ConsignmentInventoryMatchModal.tsx
T
zhaoxing e39f640dac feat(consignment): 统一寄卖匹配看板口径并收口占用与重匹配
- 看板与引擎统一数量、模板规格、正向规格信号判定,纳入 internal_matching/unassigned

- 库存侧改走新引擎匹配,移除旧 /fulfill 手动入口与旧弹窗

- 验收时将 reserved allocation 收口为 consumed,补份额级收口

- 拼单重新匹配通过 material 排除全部原库存/原卖家

- 开发/测试环境放宽迁移顺序校验,生产保持严格;修正集成测试份额报酬口径
2026-11-12 15:17:09 +08:00

251 lines
8.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useQuery } from '@tanstack/react-query'
import { useMemo, useState } from 'react'
import { Alert, App, Button, Checkbox, Empty, Input, Modal, Space, Tag, Typography } from 'antd'
import type { TableColumnsType } from 'antd'
import PercentTable from '@/components/common/PercentTable'
import {
matchAdminConsignmentWorkOrder,
fetchPendingPistolMatches,
} from '@/services/admin/consignment'
import type { PendingPistolMatchPair } from '@/types/consignment'
import { formatDateTime } from '@/utils/date-time'
const { Text } = Typography
/**
* 库存侧「匹配订单」:从一条在库现货出发,复用待匹配看板的统一判定
* (profile、模板规格、数量、重新匹配排除),落库走新引擎 `/consignment/match`。
*
* 旧 `/fulfill` 链路只做整单、且不认数量拆分,这里不再暴露。
*/
export default function ConsignmentInventoryMatchModal({
open,
inventoryId,
inventoryLabel,
onClose,
onMatched,
}: {
open: boolean
inventoryId: number | null
inventoryLabel?: string
onClose: () => void
onMatched?: () => void
}) {
const { message } = App.useApp()
const [selectedId, setSelectedId] = useState<number | null>(null)
const [allowNonExact, setAllowNonExact] = useState(false)
const [reason, setReason] = useState('')
const [submitting, setSubmitting] = useState(false)
const query = useQuery({
queryKey: ['admin-consignment-inventory-match-orders', inventoryId ?? null],
queryFn: () =>
fetchPendingPistolMatches({
inventoryId: Number(inventoryId),
includeConflicts: true,
limit: 1000,
}),
enabled: open && Boolean(inventoryId),
refetchOnWindowFocus: false,
retry: false,
})
const pairs = useMemo(() => query.data?.data?.pairs ?? [], [query.data])
const selected = useMemo(
() => pairs.find((pair) => pair.workOrderId === selectedId) || null,
[pairs, selectedId],
)
const selectedBlocked = selected
? selected.quantitySufficient === false || (selected.matchType === 'conflict' && !allowNonExact)
: true
const needReason = Boolean(selected && selected.matchType !== 'exact')
const columns: TableColumnsType<PendingPistolMatchPair> = [
{
title: '选择',
key: 'select',
width: 64,
render: (_, record) => {
const disabled =
record.quantitySufficient === false || (record.matchType === 'conflict' && !allowNonExact)
return (
<Button
size="small"
type={selectedId === record.workOrderId ? 'primary' : 'default'}
disabled={disabled}
onClick={() => setSelectedId(record.workOrderId)}
>
{selectedId === record.workOrderId ? '已选' : '选择'}
</Button>
)
},
},
{
title: '工单',
key: 'order',
width: 220,
render: (_, record) => (
<Space direction="vertical" size={2}>
<Text copyable={{ text: record.workOrderNo }} style={{ fontSize: 12, fontWeight: 600 }}>
{record.workOrderNo || '-'}
</Text>
<Text type="secondary" style={{ fontSize: 11 }}>
{record.platformOrderId || '-'} · {record.workOrderStatus || '-'}
</Text>
<Text type="secondary" style={{ fontSize: 11 }}>
录单 {record.workOrderCreatedAt ? formatDateTime(record.workOrderCreatedAt) : '-'}
</Text>
</Space>
),
},
{
title: '商品名',
key: 'productName',
width: 200,
render: (_, record) => (
<Text ellipsis={{ tooltip: record.productName }} style={{ maxWidth: '100%' }}>
{record.productName || '-'}
</Text>
),
},
{
title: '规格判定',
key: 'match',
width: 120,
render: (_, record) => {
if (record.quantitySufficient === false) return <Tag color="orange">数量不足</Tag>
if (record.matchType === 'exact') return <Tag color="green">精确</Tag>
if (record.matchType === 'conflict') return <Tag>不匹配</Tag>
return <Tag color="blue">非精确</Tag>
},
},
{
title: '数量',
key: 'quantity',
width: 130,
render: (_, record) => (
<Text
type={record.quantitySufficient === false ? 'danger' : 'secondary'}
style={{ fontSize: 12 }}
>
{record.sharingEnabled
? `拼单还需 ${record.requiredQuantity ?? 1} 份 / 该库存剩 ${record.availableQuantity ?? '-'} 件`
: `需求 ${record.requiredQuantity ?? 1} 件 / 该库存剩 ${record.availableQuantity ?? '-'} 件`}
</Text>
),
},
{
title: '匹配说明',
key: 'reason',
render: (_, record) => (
<Space direction="vertical" size={2}>
<Text style={{ fontSize: 12 }}>{record.reason || '-'}</Text>
{record.conflicts.length > 0 ? (
<Text type="danger" style={{ fontSize: 11 }}>
{record.conflicts.join(';')}
</Text>
) : null}
</Space>
),
},
]
function handleClose() {
setSelectedId(null)
setAllowNonExact(false)
setReason('')
onClose()
}
async function handleSubmit() {
if (!selected || !inventoryId) return
setSubmitting(true)
try {
const res = await matchAdminConsignmentWorkOrder({
workOrderId: selected.workOrderId,
inventoryIds: [Number(inventoryId)],
allowNonExact: allowNonExact || selected.matchType !== 'exact',
reason: reason.trim(),
})
if (res.data?.matched === false) {
message.warning('工单状态或库存已变化,请刷新后重试')
await query.refetch()
return
}
message.success('已用该寄卖库存匹配工单')
setSelectedId(null)
setAllowNonExact(false)
setReason('')
onMatched?.()
handleClose()
} catch (error) {
message.error(error instanceof Error ? error.message : '匹配失败,请刷新后重试')
void query.refetch()
} finally {
setSubmitting(false)
}
}
return (
<Modal
title={`匹配订单 · ${inventoryLabel || `库存 #${inventoryId ?? ''}`}`}
open={open}
width="min(1200px, 94vw)"
onCancel={handleClose}
okText="确认匹配"
cancelText="取消"
okButtonProps={{ disabled: !selected || selectedBlocked || (needReason && !reason.trim()) }}
confirmLoading={submitting}
onOk={() => void handleSubmit()}
destroyOnHidden
>
<Space direction="vertical" size={12} style={{ width: '100%' }}>
<Alert
type="info"
showIcon
message="按寄卖配置与数量判定候选工单;多数量库存会按剩余量扣减,不会被整批消耗。"
description="精确匹配可直接提交;非精确或规格冲突必须勾选强制匹配并填写原因。拼单工单必须一次拼满,单条库存不够时请到工单侧匹配。"
/>
<Space wrap size={12}>
<Checkbox
checked={allowNonExact}
onChange={(event) => setAllowNonExact(event.target.checked)}
>
允许非精确/冲突强制匹配
</Checkbox>
<Input
style={{ width: 360 }}
placeholder="强制匹配原因(非精确必填)"
value={reason}
onChange={(event) => setReason(event.target.value)}
disabled={!allowNonExact && selected?.matchType === 'exact'}
/>
<Button loading={query.isFetching} onClick={() => void query.refetch()}>
刷新
</Button>
</Space>
{query.isError ? (
<Alert
type="error"
showIcon
message="读取可匹配工单失败"
description={query.error instanceof Error ? query.error.message : '请稍后重试'}
/>
) : null}
<PercentTable<PendingPistolMatchPair>
rowKey={(record) => `${record.workOrderId}-${record.inventoryId}`}
size="small"
loading={query.isFetching}
dataSource={pairs}
columns={columns}
pagination={{ pageSize: 10, showSizeChanger: false }}
locale={{
emptyText: <Empty description="这条库存当前没有可匹配的工单" />,
}}
/>
</Space>
</Modal>
)
}