新增大厅订单容量控制
This commit is contained in:
@@ -85,3 +85,21 @@ test('未指定打手的运营状态统计使用一次全表拼单汇总', () =>
|
||||
)
|
||||
assert.doesNotMatch(query.joins, /worker_visible/)
|
||||
})
|
||||
|
||||
test('大厅容量先确定全局候选集合,再应用页面筛选条件', () => {
|
||||
const query = buildWorkOrderWhere({
|
||||
statuses: ['open'],
|
||||
keyword: '测试商品',
|
||||
categoryId: 3,
|
||||
hallCandidateLimit: 50,
|
||||
visibleAfterIso: '2026-08-21T00:00:00.000Z',
|
||||
excludeFilledSharing: true,
|
||||
})
|
||||
|
||||
assert.match(query.whereClause, /candidate\.status = 'open'/)
|
||||
assert.match(query.whereClause, /ORDER BY candidate\.hall_queued_at ASC NULLS LAST/)
|
||||
assert.match(query.whereClause, /LIMIT \$4/)
|
||||
const candidateSql = query.whereClause.slice(query.whereClause.indexOf('candidate.status'))
|
||||
assert.doesNotMatch(candidateSql, /candidate\.category_id|candidate.*测试商品/)
|
||||
assert.deepEqual(query.params, [['open'], '%测试商品%', 3, 50, '2026-08-21T00:00:00.000Z'])
|
||||
})
|
||||
|
||||
@@ -294,6 +294,7 @@ export type WorkOrderRow = {
|
||||
sharing_total_quantity: number
|
||||
sharing_unit_reward: number
|
||||
published_at: string | null
|
||||
hall_queued_at: string | null
|
||||
pinned_at: string | null
|
||||
assigned_at: string | null
|
||||
submitted_at: string | null
|
||||
@@ -345,6 +346,8 @@ export type ListInput = {
|
||||
workerLevelIds?: number[]
|
||||
categoryId?: number
|
||||
workerSharingId?: number
|
||||
/** 大厅候选集合的总展示上限;筛选与分页在候选集合内执行。 */
|
||||
hallCandidateLimit?: number
|
||||
visibleAfterIso?: string
|
||||
/** 抢单大厅隐藏已被拼完的工单。 */
|
||||
excludeFilledSharing?: boolean
|
||||
|
||||
@@ -271,6 +271,8 @@ export async function joinWorkOrderShare(input: {
|
||||
/** 立即抢单时在事务锁内承接全部剩余份数。 */
|
||||
takeRemaining?: boolean
|
||||
maxActiveOrders: number
|
||||
hallCandidateLimit?: number
|
||||
visibleAfterIso?: string
|
||||
now: string
|
||||
}): Promise<{
|
||||
share: WorkOrderShareRow | null
|
||||
@@ -299,6 +301,16 @@ export async function joinWorkOrderShare(input: {
|
||||
if (!workOrder || workOrder.status !== 'open') {
|
||||
return { share: null, failureReason: 'work_order_not_open' }
|
||||
}
|
||||
if (
|
||||
input.hallCandidateLimit &&
|
||||
!(await isWorkOrderWithinHallCapacityWithClient(client, {
|
||||
workOrderId: input.workOrderId,
|
||||
hallCandidateLimit: input.hallCandidateLimit,
|
||||
visibleAfterIso: input.visibleAfterIso,
|
||||
}))
|
||||
) {
|
||||
return { share: null, failureReason: 'work_order_not_open' }
|
||||
}
|
||||
if (workOrder.sharing_enabled !== true) {
|
||||
return { share: null, failureReason: 'sharing_disabled' }
|
||||
}
|
||||
@@ -1291,6 +1303,7 @@ export async function listWorkOrders({
|
||||
workerLevelIds = [],
|
||||
categoryId = 0,
|
||||
workerSharingId = 0,
|
||||
hallCandidateLimit = 0,
|
||||
visibleAfterIso = '',
|
||||
excludeFilledSharing = false,
|
||||
vipEvidencePending = false,
|
||||
@@ -1307,6 +1320,7 @@ export async function listWorkOrders({
|
||||
workerLevelIds,
|
||||
categoryId,
|
||||
workerSharingId,
|
||||
hallCandidateLimit,
|
||||
visibleAfterIso,
|
||||
excludeFilledSharing,
|
||||
vipEvidencePending,
|
||||
@@ -1520,6 +1534,7 @@ export async function updateWorkOrder(
|
||||
| 'sharing_total_quantity'
|
||||
| 'sharing_unit_reward'
|
||||
| 'published_at'
|
||||
| 'hall_queued_at'
|
||||
| 'pinned_at'
|
||||
| 'assigned_at'
|
||||
| 'submitted_at'
|
||||
@@ -1546,12 +1561,13 @@ export async function updateWorkOrder(
|
||||
sharing_total_quantity = $9,
|
||||
sharing_unit_reward = $10,
|
||||
published_at = $11,
|
||||
pinned_at = $12,
|
||||
assigned_at = $13,
|
||||
submitted_at = $14,
|
||||
accepted_at = $15,
|
||||
updated_at = $16
|
||||
WHERE id = $17
|
||||
hall_queued_at = $12,
|
||||
pinned_at = $13,
|
||||
assigned_at = $14,
|
||||
submitted_at = $15,
|
||||
accepted_at = $16,
|
||||
updated_at = $17
|
||||
WHERE id = $18
|
||||
`,
|
||||
[
|
||||
next.status,
|
||||
@@ -1565,6 +1581,7 @@ export async function updateWorkOrder(
|
||||
toPositiveInteger(next.sharing_total_quantity, 1),
|
||||
toPositiveInteger(next.sharing_unit_reward, 0),
|
||||
next.published_at || null,
|
||||
next.hall_queued_at || null,
|
||||
next.pinned_at || null,
|
||||
next.assigned_at || null,
|
||||
next.submitted_at || null,
|
||||
@@ -1642,6 +1659,8 @@ export async function grabWorkOrder(input: {
|
||||
workerId: number
|
||||
depositAmount: number
|
||||
maxActiveOrders: number
|
||||
hallCandidateLimit?: number
|
||||
visibleAfterIso?: string
|
||||
deadlineAt: string | null
|
||||
now: string
|
||||
}): Promise<GrabWorkOrderResult> {
|
||||
@@ -1664,6 +1683,20 @@ export async function grabWorkOrder(input: {
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
input.hallCandidateLimit &&
|
||||
!(await isWorkOrderWithinHallCapacityWithClient(client, {
|
||||
workOrderId: input.workOrderId,
|
||||
hallCandidateLimit: input.hallCandidateLimit,
|
||||
visibleAfterIso: input.visibleAfterIso,
|
||||
}))
|
||||
) {
|
||||
return {
|
||||
order: null,
|
||||
failureReason: 'work_order_not_open',
|
||||
}
|
||||
}
|
||||
|
||||
const result = await client.query<{ id: number }>(
|
||||
`
|
||||
UPDATE work_orders
|
||||
@@ -1887,6 +1920,7 @@ export async function cancelWorkerWorkOrder(input: {
|
||||
assigned_worker_id = NULL,
|
||||
assigned_at = NULL,
|
||||
deadline_at = NULL,
|
||||
hall_queued_at = $1,
|
||||
updated_at = $1
|
||||
WHERE id = $2
|
||||
`,
|
||||
@@ -2370,7 +2404,8 @@ export async function reviewWorkerCancelRequest(input: {
|
||||
)
|
||||
await client.query(
|
||||
`UPDATE work_orders
|
||||
SET status = 'open', submitted_at = NULL, published_at = COALESCE(published_at, $1), updated_at = $1
|
||||
SET status = 'open', submitted_at = NULL, published_at = COALESCE(published_at, $1),
|
||||
hall_queued_at = COALESCE(hall_queued_at, $1), updated_at = $1
|
||||
WHERE id = $2`,
|
||||
[input.now, workOrder.id],
|
||||
)
|
||||
@@ -2401,7 +2436,7 @@ export async function reviewWorkerCancelRequest(input: {
|
||||
await client.query(
|
||||
`UPDATE work_orders
|
||||
SET status = 'open', assigned_worker_id = NULL, assigned_at = NULL, deadline_at = NULL,
|
||||
published_at = $1, updated_at = $1
|
||||
published_at = $1, hall_queued_at = $1, updated_at = $1
|
||||
WHERE id = $2`,
|
||||
[input.now, workOrder.id],
|
||||
)
|
||||
@@ -2608,6 +2643,7 @@ export async function returnAssignedWorkOrderToHall(input: {
|
||||
assigned_at = NULL,
|
||||
deadline_at = NULL,
|
||||
published_at = $1,
|
||||
hall_queued_at = $1,
|
||||
updated_at = $1
|
||||
WHERE id = $2
|
||||
`,
|
||||
@@ -3713,6 +3749,7 @@ export async function settleOverdueWorkOrder(input: {
|
||||
submitted_at = CASE WHEN $1 = 'open' THEN NULL ELSE submitted_at END,
|
||||
problem_note = CASE WHEN $1 = 'open' THEN '' ELSE problem_note END,
|
||||
published_at = CASE WHEN $1 = 'open' THEN $2 ELSE published_at END,
|
||||
hall_queued_at = CASE WHEN $1 = 'open' THEN $2 ELSE hall_queued_at END,
|
||||
updated_at = $2
|
||||
WHERE id = $3
|
||||
`,
|
||||
@@ -4379,6 +4416,7 @@ export function buildWorkOrderWhere({
|
||||
workerLevelIds = [],
|
||||
categoryId = 0,
|
||||
workerSharingId = 0,
|
||||
hallCandidateLimit = 0,
|
||||
visibleAfterIso = '',
|
||||
excludeFilledSharing = false,
|
||||
vipEvidencePending = false,
|
||||
@@ -4531,9 +4569,13 @@ export function buildWorkOrderWhere({
|
||||
params.push(categoryId)
|
||||
filters.push(`wo.category_id = $${params.length}`)
|
||||
}
|
||||
if (hallCandidateLimit > 0) {
|
||||
params.push(hallCandidateLimit)
|
||||
filters.push(`wo.id IN (${buildHallCandidateIdsSql(`$${params.length}`)})`)
|
||||
}
|
||||
if (visibleAfterIso) {
|
||||
params.push(visibleAfterIso)
|
||||
filters.push(`(wo.published_at IS NULL OR wo.published_at <= $${params.length})`)
|
||||
filters.push(`(wo.hall_queued_at IS NULL OR wo.hall_queued_at <= $${params.length})`)
|
||||
}
|
||||
if (excludeFilledSharing) {
|
||||
filters.push(`(
|
||||
@@ -4548,6 +4590,52 @@ export function buildWorkOrderWhere({
|
||||
}
|
||||
}
|
||||
|
||||
/** 大厅容量先选出全局队首,分类、关键词和 VIP 延迟只能在该集合内继续筛选。 */
|
||||
function buildHallCandidateIdsSql(limitPlaceholder: string) {
|
||||
return `
|
||||
SELECT candidate.id
|
||||
FROM work_orders candidate
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
work_order_id,
|
||||
SUM(quantity) FILTER (WHERE status != 'cancelled') AS active_share_quantity
|
||||
FROM work_order_shares
|
||||
GROUP BY work_order_id
|
||||
) candidate_shares ON candidate_shares.work_order_id = candidate.id
|
||||
WHERE candidate.status = 'open'
|
||||
AND (
|
||||
candidate.sharing_enabled IS NOT TRUE
|
||||
OR COALESCE(candidate_shares.active_share_quantity, 0) < candidate.sharing_total_quantity
|
||||
)
|
||||
ORDER BY candidate.hall_queued_at ASC NULLS LAST, candidate.id ASC
|
||||
LIMIT ${limitPlaceholder}
|
||||
`
|
||||
}
|
||||
|
||||
/** 抢单操作在事务中复核容量与当前打手的可见延迟,避免通过订单 ID 绕过大厅队列。 */
|
||||
async function isWorkOrderWithinHallCapacityWithClient(
|
||||
client: PoolClient,
|
||||
input: { workOrderId: number; hallCandidateLimit: number; visibleAfterIso?: string | undefined },
|
||||
) {
|
||||
const result = await client.query<{ visible: boolean }>(
|
||||
`
|
||||
SELECT EXISTS(
|
||||
SELECT 1
|
||||
FROM work_orders wo
|
||||
WHERE wo.id = $1
|
||||
AND wo.id IN (${buildHallCandidateIdsSql('$2')})
|
||||
AND ($3::timestamptz IS NULL OR wo.hall_queued_at IS NULL OR wo.hall_queued_at <= $3)
|
||||
) AS visible
|
||||
`,
|
||||
[
|
||||
input.workOrderId,
|
||||
input.hallCandidateLimit,
|
||||
input.visibleAfterIso ? input.visibleAfterIso : null,
|
||||
],
|
||||
)
|
||||
return result.rows[0]?.visible === true
|
||||
}
|
||||
|
||||
function buildWorkProductRuleWhere({
|
||||
enabled = null,
|
||||
keyword = '',
|
||||
|
||||
Reference in New Issue
Block a user