diff --git a/apps/backend/src/routes/admin/worker-platform/consignment.ts b/apps/backend/src/routes/admin/worker-platform/consignment.ts index c1fe3db0..a54e6793 100644 --- a/apps/backend/src/routes/admin/worker-platform/consignment.ts +++ b/apps/backend/src/routes/admin/worker-platform/consignment.ts @@ -43,7 +43,10 @@ import { listInventoryMatchableOrders, listWorkOrderInventoryMatchOptions, } from '../../../services/worker-platform/consignment-fulfillment-service.js' -import { listPendingConsignmentMatches } from '../../../services/worker-platform/consignment-pending-match-service.js' +import { + listPendingConsignmentMatches, + listPendingSuernanPistolMatches, +} from '../../../services/worker-platform/consignment-pending-match-service.js' import { listConsignmentMatchLogs } from '../../../services/worker-platform/consignment-match-log-service.js' import { buildTemplateSpecProposalForCategory } from '../../../services/worker-platform/consignment-template-spec-service.js' import { @@ -405,7 +408,26 @@ router.post( // ---------------- 匹配与履约 ---------------- -// 全局待抢单匹配看板:按寄卖配置匹配待抢单工单 × 在库待匹配现货,只读。 +// 一键查询待匹配订单:恢复为简单只读的待抢单苏尔南手枪看板。 +router.get( + '/worker-platform/consignment/pending-suernan-pistol-matches', + requireAdminPermission('consignment.view'), + createJsonHandler( + (req) => + listPendingSuernanPistolMatches({ + keyword: String(req.query.keyword || ''), + includeConflicts: String(req.query.includeConflicts || '') === 'true', + limit: Number(req.query.limit || 0) || undefined, + }), + { + successMessage: 'ok', + errorMessage: '读取待匹配手枪订单失败', + scope: '[admin/consignment/pending-suernan-pistol-matches]', + }, + ), +) + +// 库存侧手动匹配候选:按完整寄卖配置匹配待抢单工单 × 在库待匹配现货,只读。 router.get( '/worker-platform/consignment/pending-pistol-matches', requireAdminPermission('consignment.view'), diff --git a/apps/backend/src/services/worker-platform/consignment-pending-match-service.test.ts b/apps/backend/src/services/worker-platform/consignment-pending-match-service.test.ts index 3604f0b7..8f4a1b9d 100644 --- a/apps/backend/src/services/worker-platform/consignment-pending-match-service.test.ts +++ b/apps/backend/src/services/worker-platform/consignment-pending-match-service.test.ts @@ -245,6 +245,18 @@ test('匹配看板:按分数降序,同分录单时间早的优先', () => { ) }) +test('匹配看板:在库待匹配件数按剩余数量统计,不按库存行数统计', () => { + const result = buildPendingPistolMatchPairs({ + orders: [buildOrder()], + inventories: [ + buildInventory({ id: 10, quantity: 3, remaining_quantity: 2 }), + buildInventory({ id: 11, quantity: 5, remaining_quantity: 4 }), + ], + }) + + assert.equal(result.inStockInventoryCount, 6) +}) + const BUILT_IN_SPECS = [ ['system', '操作系统', 10], ['serverZone', '游戏区服', 20], diff --git a/apps/backend/src/services/worker-platform/consignment-pending-match-service.ts b/apps/backend/src/services/worker-platform/consignment-pending-match-service.ts index 5f41bdc8..e512a471 100644 --- a/apps/backend/src/services/worker-platform/consignment-pending-match-service.ts +++ b/apps/backend/src/services/worker-platform/consignment-pending-match-service.ts @@ -49,25 +49,15 @@ export type PendingPistolMatchPair = { platformOrderId: string productName: string workOrderCreatedAt: string | null - /** 工单用户付款金额(分);未知为 null */ - userPaymentFen: number | null inventoryId: number consignmentNo: string inventoryNo: string itemName: string - /** 该库存售价(分):sale_price_fen 优先,缺失回退 cost_price_fen */ - salePriceFen: number inventoryInboundAt: string | null matchType: 'exact' | 'partial' | 'conflict' score: number reason: string conflicts: string[] - /** 枪型 → 钻石 → 璀璨 的排序层级文案 */ - priorityLabel: string - seriesMatched: boolean - seriesSpecified: boolean - diamondGap: number - brillianceGap: number } export type PendingPistolMatchResult = { @@ -147,7 +137,7 @@ export function buildPendingPistolMatchPairs(input: { serverZone: fields.serverZone, series: fields.series, }) - return { order, specs, userPaymentFen: resolveUserPaymentAmountFenForDisplay(order) } + return { order, specs } }) .filter(({ order, specs }) => { const text = [ @@ -161,6 +151,7 @@ export function buildPendingPistolMatchPairs(input: { const preparedInventories = input.inventories.filter((inventory) => { if (inventory.status !== 'in_stock' || inventory.allow_matching !== true) return false + if (Number(inventory.remaining_quantity ?? 1) <= 0) return false return ( detectItemCategory( `${inventory.item_name} ${inventory.series || ''} ${inventory.category_name || ''}`, @@ -178,30 +169,10 @@ export function buildPendingPistolMatchPairs(input: { ) const pairs: PendingPistolMatchPair[] = [] - for (const { order, specs, userPaymentFen } of preparedOrders) { - const orderSeriesInfo = resolveOrderSeriesInfo({ - categoryId: order.category_id, - productName: order.product_name, - parsedSeries: String(specs.series || ''), - }) + for (const { order, specs } of preparedOrders) { for (const inventory of preparedInventories) { - const inventorySpecValues = resolveConsignmentInventorySpecValues(inventory) - if (isCrossSeriesInventory(orderSeriesInfo, String(inventorySpecValues.series || ''))) { - continue - } const scored = scoreConsignmentMatchDetails(specs, inventory) if (scored.matchType === 'conflict' && !includeConflicts) continue - const priority = resolveConsignmentMatchPriorityFromSpecs({ - orderValues: { - series: orderSeriesInfo.series, - diamondLevel: specs.diamondLevel, - brillianceCount: specs.brillianceCount, - }, - inventoryValues: inventorySpecValues, - orderSeriesInfo, - exact: scored.matchType === 'exact', - fallbackConflict: scored.matchType === 'conflict', - }) const pair: PendingPistolMatchPair = { workOrderId: Number(order.id), @@ -209,12 +180,10 @@ export function buildPendingPistolMatchPairs(input: { platformOrderId: String(order.platform_order_id || ''), productName: String(order.product_name || ''), workOrderCreatedAt: order.created_at ? String(order.created_at) : null, - userPaymentFen, inventoryId: Number(inventory.id), consignmentNo: String(inventory.source_submission_no || inventory.inventory_no || ''), inventoryNo: String(inventory.inventory_no || ''), itemName: String(inventory.item_name || ''), - salePriceFen: inventorySalePriceFen(inventory), inventoryInboundAt: inventory.inbound_at ? String(inventory.inbound_at) : inventory.created_at @@ -224,11 +193,6 @@ export function buildPendingPistolMatchPairs(input: { score: scored.score, reason: scored.reason, conflicts: scored.conflicts, - priorityLabel: priority.priorityLabel, - seriesMatched: priority.seriesMatched, - seriesSpecified: priority.seriesSpecified, - diamondGap: priority.diamondGap, - brillianceGap: priority.brillianceGap, } if (keyword && !buildPairHaystack(pair).includes(keyword)) continue @@ -253,7 +217,10 @@ export function buildPendingPistolMatchPairs(input: { return { pendingOrderCount: preparedOrders.length, - inStockInventoryCount: preparedInventories.length, + inStockInventoryCount: preparedInventories.reduce( + (sum, inventory) => sum + Math.max(0, Number(inventory.remaining_quantity ?? 1)), + 0, + ), pairCount: pairs.length, exactCount, partialCount, @@ -263,6 +230,64 @@ export function buildPendingPistolMatchPairs(input: { } } +/** + * 旧版「待抢单手枪匹配」查询:完整读取全部 open 工单,不再只取最近 500 单。 + * 看板本身仍是只读工具,不参与自动匹配或工单状态修改。 + */ +async function listAllOpenWorkOrdersForPistolBoard(): Promise { + const pageSize = OPEN_ORDER_FETCH_LIMIT + const items: WorkOrderRow[] = [] + let page = 1 + let total = 0 + + do { + const result = await listWorkOrders({ + statuses: [WORK_ORDER_STATUS.OPEN], + page, + pageSize, + includeTotal: true, + sort: 'id_desc', + }) + total = result.total + items.push(...result.items) + if (result.items.length === 0) break + page += 1 + } while (items.length < total) + + return items +} + +/** + * 后台「寄售仓库 → 一键查询待匹配订单」的简单版。 + * + * 固定口径: + * - 只查非拼单 open 工单; + * - 只查苏尔南手枪 × 在库手枪; + * - 使用内置五字段固定权重评分; + * - 默认隐藏冲突,允许操作者显式查看; + * - 查询全部 open 工单后再过滤,避免关键字只能命中最近一页。 + */ +export async function listPendingSuernanPistolMatches( + input: { + keyword?: string | undefined + includeConflicts?: boolean | undefined + limit?: number | undefined + } = {}, +): Promise { + const [orders, inventories] = await Promise.all([ + listAllOpenWorkOrdersForPistolBoard(), + listActiveConsignmentInventoriesForMatching(), + ]) + + return buildPendingPistolMatchPairs({ + orders, + inventories, + keyword: input.keyword, + includeConflicts: input.includeConflicts, + limit: input.limit, + }) +} + export type PendingConsignmentMatchDetail = { key: string label: string @@ -273,6 +298,16 @@ export type PendingConsignmentMatchDetail = { /** 看板配对:在旧字段基础上补充规格明细、订单剩余数量与命中的寄卖配置。 */ export type PendingConsignmentMatchPair = PendingPistolMatchPair & { + /** 工单用户付款金额(分);未知为 null */ + userPaymentFen: number | null + /** 该库存售价(分):sale_price_fen 优先,缺失回退 cost_price_fen */ + salePriceFen: number + /** 枪型 → 钻石 → 璀璨的排序层级文案 */ + priorityLabel: string + seriesMatched: boolean + seriesSpecified: boolean + diamondGap: number + brillianceGap: number matchDetails: PendingConsignmentMatchDetail[] remainingQuantity: number totalQuantity: number diff --git a/apps/frontend/src/components/admin/ConsignmentPendingMatchModal.tsx b/apps/frontend/src/components/admin/ConsignmentPendingMatchModal.tsx index c9861d0c..d3bcc26d 100644 --- a/apps/frontend/src/components/admin/ConsignmentPendingMatchModal.tsx +++ b/apps/frontend/src/components/admin/ConsignmentPendingMatchModal.tsx @@ -1,76 +1,40 @@ import { useQuery } from '@tanstack/react-query' import { ReloadOutlined } from '@ant-design/icons' -import { Alert, Button, Empty, Input, Modal, Space, Tag, Typography } from 'antd' +import { Alert, Button, Checkbox, Empty, Input, Modal, Space, Tag, Typography } from 'antd' import type { TableColumnsType } from 'antd' -import { useMemo, useState } from 'react' +import { useState } from 'react' import PercentTable from '@/components/common/PercentTable' -import { fetchPendingPistolMatches } from '@/services/admin/consignment' -import type { PendingPistolMatchPair } from '@/types/consignment' -import { formatFenMoney } from '@/utils/consignment-payment-price-diff' +import { fetchPendingSuernanPistolMatches } from '@/services/admin/consignment' +import type { PendingSuernanPistolMatchPair } from '@/types/consignment' import { formatDateTime } from '@/utils/date-time' -import { resolveConsignmentPriorityTagColor } from '@/utils/consignment-match-priority' - -import ConsignmentPriceWithDiff from './ConsignmentPriceWithDiff' -import ConsignmentWorkOrderMatchModal from './ConsignmentWorkOrderMatchModal' 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: '精确' } - } +function resolveScoreTag(pair: PendingSuernanPistolMatchPair) { + if (pair.matchType === 'exact') return { 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, - categoryParentName, - canMatch = false, - onMatched, }: { open: boolean onClose: () => void - /** 只统计该寄卖分类;不传 = 全部 */ - categoryId?: number | null - categoryName?: string - /** 二级分类的上级分类名:用于说明「会带上上级同系列库存」 */ - categoryParentName?: string - /** 有 consignment.fulfill 权限时才显示「选择」列 */ - canMatch?: boolean - /** 快速匹配成功后通知外层刷新库存 */ - onMatched?: () => void }) { const [keyword, setKeyword] = useState('') - const [selectTarget, setSelectTarget] = useState<{ - workOrderId: number - inventoryId: number - } | null>(null) + const [includeConflicts, setIncludeConflicts] = useState(false) const query = useQuery({ - queryKey: ['admin-pending-pistol-matches', categoryId ?? null, keyword], + queryKey: ['admin-pending-suernan-pistol-matches', keyword, includeConflicts], queryFn: () => - fetchPendingPistolMatches({ - categoryId: categoryId ?? undefined, + fetchPendingSuernanPistolMatches({ keyword, - // 默认展示全部配对:冲突项(0 分)也要能看到,便于人工核对。 - includeConflicts: true, + includeConflicts, limit: 300, }), enabled: open, @@ -78,11 +42,9 @@ export default function ConsignmentPendingMatchModal({ retry: false, }) const data = query.data?.data - // 后端已按「分数 + 系列/钻石/璀璨优先级」排好序;这里必须保持原顺序, - // 否则同分项会被重排回默认序,枪型匹配的候选反而沉底。 - const pairs = useMemo(() => data?.pairs || [], [data?.pairs]) + const pairs = data?.pairs || [] - const columns: TableColumnsType = [ + const columns: TableColumnsType = [ { title: '匹配打分', key: 'score', @@ -138,30 +100,6 @@ export default function ConsignmentPendingMatchModal({ 录单 {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} ), }, @@ -221,93 +159,28 @@ export default function ConsignmentPendingMatchModal({ ), }, - { - title: '工单用户付款金额', - key: 'userPayment', - width: 140, - render: (_, record) => ( - {formatFenMoney(record.userPaymentFen)} - ), - }, - { - title: '售价', - key: 'salePrice', - width: 180, - render: (_, record) => ( - - ), - }, { title: '匹配说明', key: 'reason', width: 300, render: (_, record) => ( - {record.priorityLabel ? ( - - {record.priorityLabel} - - ) : null} {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 || '-'}」 - - ))} ), }, - ...(canMatch - ? [ - { - title: '操作', - key: 'action', - width: 100, - render: (_: unknown, record: PendingPistolMatchPair) => ( - - ), - }, - ] - : []), ] return ( - {categoryName ? 分类:{categoryName} : null} - 待匹配 {data?.pendingOrderCount ?? 0} 单 + 待抢单 {data?.pendingOrderCount ?? 0} 单 在库待匹配 {data?.inStockInventoryCount ?? 0} 件 匹配组合 {data?.pairCount ?? 0} 条 精确 {data?.exactCount ?? 0} 非精确 {data?.partialCount ?? 0} - 数量不足 {data?.quantityShortCount ?? 0} - 可自动 {data?.autoMatchableCount ?? 0} - 不匹配 {data?.conflictCount ?? 0} + {includeConflicts ? 不匹配 {data?.conflictCount ?? 0} : null} @@ -353,6 +209,12 @@ export default function ConsignmentPendingMatchModal({ style={{ width: 420 }} onSearch={(value) => setKeyword(value.trim())} /> + setIncludeConflicts(event.target.checked)} + > + 显示 0 分不匹配项 +