测试修复-2

This commit is contained in:
yml2213
2026-08-20 22:48:03 +08:00
parent 92afc32525
commit 1c8eda2b5f
@@ -1292,24 +1292,14 @@ export async function listWorkOrders({
const offset = (page - 1) * pageSize const offset = (page - 1) * pageSize
const orderBy = const orderBy =
sort === 'worker_claimed_at_desc' && workerSharingId sort === 'worker_claimed_at_desc' && workerSharingId
? (() => { ? `COALESCE(
params.push(workerSharingId)
const workerIdPlaceholder = `$${params.length}`
return `COALESCE(
CASE CASE
WHEN wo.assigned_worker_id = ${workerIdPlaceholder} THEN wo.assigned_at WHEN wo.assigned_worker_id = $1 THEN wo.assigned_at
END, END,
( CASE
SELECT wos.created_at WHEN worker_share.status != 'cancelled' THEN worker_share.created_at
FROM work_order_shares wos END
WHERE wos.work_order_id = wo.id
AND wos.worker_id = ${workerIdPlaceholder}
AND wos.status != 'cancelled'
ORDER BY wos.created_at DESC
LIMIT 1
)
) DESC NULLS LAST, wo.id DESC` ) DESC NULLS LAST, wo.id DESC`
})()
: 'wo.id DESC' : 'wo.id DESC'
const effectiveOrderBy = pinnedFirst ? `wo.pinned_at DESC NULLS LAST, ${orderBy}` : orderBy const effectiveOrderBy = pinnedFirst ? `wo.pinned_at DESC NULLS LAST, ${orderBy}` : orderBy
params.push(pageSize, offset) params.push(pageSize, offset)
@@ -1326,7 +1316,7 @@ export async function listWorkOrders({
/** 按筛选条件汇总工单,统计范围不受当前分页影响。 */ /** 按筛选条件汇总工单,统计范围不受当前分页影响。 */
export async function getWorkOrderStatistics(input: ListInput = {}): Promise<WorkOrderStatistics> { export async function getWorkOrderStatistics(input: ListInput = {}): Promise<WorkOrderStatistics> {
const { whereClause, params } = buildWorkOrderWhere(input) const { joins, whereClause, params } = buildWorkOrderWhere(input)
const vipEvidencePendingWhere = ` const vipEvidencePendingWhere = `
wo.status = 'accepted' wo.status = 'accepted'
AND wo.acceptance_json->>'reviewMode' = 'vip_auto' AND wo.acceptance_json->>'reviewMode' = 'vip_auto'
@@ -1362,6 +1352,7 @@ export async function getWorkOrderStatistics(input: ListInput = {}): Promise<Wor
), 0)::bigint AS refund_amount ), 0)::bigint AS refund_amount
FROM work_orders wo FROM work_orders wo
LEFT JOIN orders o ON o.id = wo.order_id LEFT JOIN orders o ON o.id = wo.order_id
${joins}
${whereClause} ${whereClause}
GROUP BY ${statusExpression} GROUP BY ${statusExpression}
`, `,
@@ -4366,12 +4357,35 @@ function buildWorkOrderWhere({
let joins = '' let joins = ''
let workerSharePlaceholder = '' let workerSharePlaceholder = ''
// 拼单表在 (work_order_id, worker_id) 上唯一;提前按打手关联一次即可, // 先从打手的整单、历史单和拼单记录组成可见工单集合,再关联工单详情。
// 避免列表和 COUNT 查询对每一条工单重复执行相关子查询 // 这样 COUNT 和列表都不会先扫描全量工单,再逐行检查该打手的拼单状态
if (workerSharingId) { if (workerSharingId) {
params.push(workerSharingId) params.push(workerSharingId)
workerSharePlaceholder = `$${params.length}` workerSharePlaceholder = `$${params.length}`
joins = ` joins = `
INNER JOIN (
SELECT assigned.id AS work_order_id
FROM work_orders assigned
WHERE assigned.assigned_worker_id = ${workerSharePlaceholder}
UNION
SELECT historical.id AS work_order_id
FROM work_orders historical
WHERE historical.last_assigned_worker_id = ${workerSharePlaceholder}
AND (
historical.status <> 'open'
OR EXISTS (
SELECT 1
FROM work_order_events timeout_event
WHERE timeout_event.work_order_id = historical.id
AND timeout_event.event_type = 'timeout_reopen'
)
)
UNION
SELECT share_source.work_order_id
FROM work_order_shares share_source
WHERE share_source.worker_id = ${workerSharePlaceholder}
AND share_source.status != 'cancelled'
) worker_visible ON worker_visible.work_order_id = wo.id
LEFT JOIN work_order_shares worker_share LEFT JOIN work_order_shares worker_share
ON worker_share.work_order_id = wo.id ON worker_share.work_order_id = wo.id
AND worker_share.worker_id = ${workerSharePlaceholder}` AND worker_share.worker_id = ${workerSharePlaceholder}`
@@ -4451,24 +4465,6 @@ function buildWorkOrderWhere({
params.push(categoryId) params.push(categoryId)
filters.push(`wo.category_id = $${params.length}`) filters.push(`wo.category_id = $${params.length}`)
} }
if (workerSharingId) {
filters.push(
`(wo.assigned_worker_id = ${workerSharePlaceholder}
OR (
wo.last_assigned_worker_id = ${workerSharePlaceholder}
AND (
wo.status <> 'open'
OR EXISTS (
SELECT 1
FROM work_order_events timeout_event
WHERE timeout_event.work_order_id = wo.id
AND timeout_event.event_type = 'timeout_reopen'
)
)
)
OR (worker_share.work_order_id IS NOT NULL AND worker_share.status != 'cancelled'))`,
)
}
if (visibleAfterIso) { if (visibleAfterIso) {
params.push(visibleAfterIso) params.push(visibleAfterIso)
filters.push(`(wo.published_at IS NULL OR wo.published_at <= $${params.length})`) filters.push(`(wo.published_at IS NULL OR wo.published_at <= $${params.length})`)