feat(consignment): 待匹配看板同规格同分按录单时间升序

同规格同分的组合不再按工单 id 新单优先,改为录单时间早的(积压更久)
排在前面;录单时间缺失或相同时按工单 id 升序兜底。新旧两个看板构建
函数同步口径,并补充截图场景回归测试。
This commit is contained in:
2026-11-16 14:11:26 +08:00
parent 24d25aa77a
commit ec11022c1b
3 changed files with 85 additions and 14 deletions
@@ -221,9 +221,13 @@ test('匹配看板:不按时间过滤,寄售入库晚于工单创建仍参
assert.equal(result.pairs[0]?.inventoryInboundAt, '2026-11-02T10:00:00.000Z')
})
test('匹配看板:按分数降序,同分新工单优先', () => {
test('匹配看板:按分数降序,同分录单时间早的优先', () => {
const result = buildPendingPistolMatchPairs({
orders: [buildOrder({ id: 1 }), buildOrder({ id: 2, work_order_no: 'WK002' })],
// 故意按「新单在前」传入,验证排序而不是输入顺序。
orders: [
buildOrder({ id: 2, work_order_no: 'WK002', created_at: '2026-11-01T10:00:00.000Z' }),
buildOrder({ id: 1, created_at: '2026-11-01T09:00:00.000Z' }),
],
inventories: [
buildInventory({ id: 10 }),
buildInventory({ id: 11, diamond_level: 2, inbound_at: '2026-11-01T11:00:00.000Z' }),
@@ -233,10 +237,10 @@ test('匹配看板:按分数降序,同分新工单优先', () => {
assert.deepEqual(
result.pairs.map((pair) => [pair.workOrderId, pair.inventoryId, pair.score]),
[
[2, 10, 100],
[1, 10, 100],
[2, 11, 95],
[2, 10, 100],
[1, 11, 95],
[2, 11, 95],
],
)
})
@@ -357,6 +361,54 @@ test('看板新口径:同钻高璀优先于高钻同璀', () => {
)
})
test('看板新口径:同规格同分时录单时间越早排得越靠前', () => {
const profile = buildProfile({
config: { ...DEFAULT_CONSIGNMENT_PROFILE_CONFIG, specs: BUILT_IN_SPECS },
})
const inventory = buildInventory({
id: 901,
category_id: profile.categoryId,
item_name: '安卓血宴1钻 苏尔南',
system: '安卓',
server_zone: '互通',
series: '血宴',
diamond_level: 1,
brilliance_count: 0,
})
const buildSuernanOrder = (id: number, createdAt: string) =>
buildOrder({
id,
work_order_no: `WK${id}`,
product_name: '安卓血宴1钻',
material_json: JSON.stringify({ collect: { fields: { serverZone: '安卓' } } }),
created_at: createdAt,
})
const result = buildPendingConsignmentMatchPairs({
// 故意按新→旧传入(截图现场是最新单排最上),验证改为录单时间早的在前。
orders: [
buildSuernanOrder(603, '2026-11-16T12:44:49.000Z'),
buildSuernanOrder(601, '2026-11-16T12:38:02.000Z'),
buildSuernanOrder(602, '2026-11-16T12:41:02.000Z'),
],
inventories: [inventory],
profileByOrderId: new Map([
[601, profile],
[602, profile],
[603, profile],
]),
includeConflicts: true,
})
assert.deepEqual(
result.pairs.map((pair) => pair.workOrderId),
[601, 602, 603],
)
// 三段配对必须是同规格同分,才轮到录单时间做平手排序。
assert.equal(new Set(result.pairs.map((pair) => pair.score)).size, 1)
assert.equal(new Set(result.pairs.map((pair) => pair.priorityLabel)).size, 1)
})
test('看板新口径:同钻高璀排在低配之前', () => {
const profile = buildProfile({
config: { ...DEFAULT_CONSIGNMENT_PROFILE_CONFIG, specs: BUILT_IN_SPECS },
@@ -101,6 +101,26 @@ function buildPairHaystack(pair: PendingPistolMatchPair): string {
.toLowerCase()
}
/**
* 平手排序:规格与分数都相同时,录单时间早的(积压更久的)排在前面。
* 录单时间缺失或相同时,按工单 id 升序兜底(id 本身即录单先后)。
*/
function comparePendingPairByRecordedAt(
left: PendingPistolMatchPair,
right: PendingPistolMatchPair,
): number {
const leftRecordedAt = Date.parse(String(left.workOrderCreatedAt || ''))
const rightRecordedAt = Date.parse(String(right.workOrderCreatedAt || ''))
if (
Number.isFinite(leftRecordedAt) &&
Number.isFinite(rightRecordedAt) &&
leftRecordedAt !== rightRecordedAt
) {
return leftRecordedAt - rightRecordedAt
}
return left.workOrderId - right.workOrderId
}
/** 纯函数:把待抢单工单与在库现货做笛卡尔匹配并排序,便于单测。 */
export function buildPendingPistolMatchPairs(input: {
orders: WorkOrderRow[]
@@ -212,12 +232,11 @@ export function buildPendingPistolMatchPairs(input: {
const partialCount = pairs.filter((pair) => pair.matchType === 'partial').length
const conflictCount = pairs.filter((pair) => pair.matchType === 'conflict').length
// 分数优先;同分时新工单优先,再按寄售入库时间早的优先。
// 分数优先;同分时录单时间早的优先(积压久的先处理),再按寄售入库时间早的优先。
pairs.sort((left, right) => {
if (right.score !== left.score) return right.score - left.score
if (right.workOrderId !== left.workOrderId) {
return right.workOrderId - left.workOrderId
}
const recordedAtDiff = comparePendingPairByRecordedAt(left, right)
if (recordedAtDiff !== 0) return recordedAtDiff
const leftTime = Date.parse(left.inventoryInboundAt || '') || left.inventoryId
const rightTime = Date.parse(right.inventoryInboundAt || '') || right.inventoryId
if (leftTime !== rightTime) return leftTime - rightTime
@@ -685,9 +704,9 @@ export function buildPendingConsignmentMatchPairs(input: {
if (priorityDiff !== 0) return priorityDiff
}
if (right.score !== left.score) return right.score - left.score
if (right.workOrderId !== left.workOrderId) {
return right.workOrderId - left.workOrderId
}
// 同规格同分时按录单时间先后:录单越早(积压越久)越靠前。
const recordedAtDiff = comparePendingPairByRecordedAt(left, right)
if (recordedAtDiff !== 0) return recordedAtDiff
const leftTime = Date.parse(left.inventoryInboundAt || '') || left.inventoryId
const rightTime = Date.parse(right.inventoryInboundAt || '') || right.inventoryId
if (leftTime !== rightTime) return leftTime - rightTime
@@ -301,14 +301,14 @@ export default function ConsignmentPendingMatchModal({
}
description={
categoryParentName
? `「${categoryName}」是二级分类:会连同上级「${categoryParentName}」中同系列的库存一起匹配(跨枪型不参与)。${
? `「${categoryName}」是二级分类:会连同上级「${categoryParentName}」中同系列的库存一起匹配(跨枪型不参与)。同规格同分时按录单时间先后排序,录单越早越靠前。${
canMatch
? '点「选择」会打开手动匹配并预选该库存,确认后直接占用库存。'
: '当前账号没有履约权限,只能查看。'
}`
: canMatch
? '默认展示全部配对,按 枪型 → 钻石 → 璀璨 排序:完美匹配在前,同钻高璀/高钻同璀/高钻高璀次之;跨枪型不展示。点「选择」会打开手动匹配并预选该库存,确认后直接占用库存。数量不足或规格不匹配时需要人工补选。'
: '默认展示全部配对,按 枪型 → 钻石 → 璀璨 排序;当前账号没有履约权限,只能查看。'
? '默认展示全部配对,按 枪型 → 钻石 → 璀璨 排序:完美匹配在前,同钻高璀/高钻同璀/高钻高璀次之;跨枪型不展示;同规格同分时按录单时间先后排序,录单越早越靠前。点「选择」会打开手动匹配并预选该库存,确认后直接占用库存。数量不足或规格不匹配时需要人工补选。'
: '默认展示全部配对,按 枪型 → 钻石 → 璀璨 排序,同规格同分时录单越早越靠前;当前账号没有履约权限,只能查看。'
}
/>