From 0dfe363d35e54d469889b09baf0b7212b676af47 Mon Sep 17 00:00:00 2001 From: yml2213 Date: Sat, 22 Aug 2026 15:41:01 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=89=93=E6=89=8B=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E6=A6=82=E8=A7=88=E6=9F=A5=E8=AF=A2=E4=B8=BA=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E5=8F=AF=E7=94=A8=E7=9A=84UNION=E5=88=86=E6=94=AF?= =?UTF-8?q?=E9=81=BF=E5=85=8Dwork=5Forders=E5=85=A8=E8=A1=A8=E6=89=AB?= =?UTF-8?q?=E6=8F=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../worker-platform/work-order-query-repo.ts | 145 +++++++++++------- 1 file changed, 91 insertions(+), 54 deletions(-) diff --git a/apps/backend/src/repositories/worker-platform/work-order-query-repo.ts b/apps/backend/src/repositories/worker-platform/work-order-query-repo.ts index aab56ab7..777f505c 100644 --- a/apps/backend/src/repositories/worker-platform/work-order-query-repo.ts +++ b/apps/backend/src/repositories/worker-platform/work-order-query-repo.ts @@ -365,74 +365,111 @@ export async function getWorkerOrderOverview(workerId: number | string): Promise const normalizedWorkerId = Number(workerId) const result = await query( ` - WITH worker_orders AS ( + WITH worker_base AS ( SELECT - CASE - WHEN ( - CASE - WHEN wos.id IS NOT NULL AND wo.status NOT IN ('accepted', 'cancelled', 'problem') - THEN CASE wos.status - WHEN 'joined' THEN 'in_progress' - WHEN 'submitted' THEN 'pending_acceptance' - WHEN 'accepted' THEN 'accepted' - WHEN 'cancelled' THEN 'cancelled' - ELSE wo.status - END - ELSE wo.status - END - ) = 'in_progress' AND ( - EXISTS ( - SELECT 1 - FROM worker_order_cancel_requests wcr - WHERE wcr.work_order_id = wo.id - AND wcr.worker_id = $1 - AND wcr.status = 'pending' - ) OR EXISTS ( - SELECT 1 - FROM worker_order_feedbacks wf - WHERE wf.work_order_id = wo.id - AND wf.worker_id = $1 - AND wf.status = 'pending' - ) - ) THEN 'problem' - ELSE CASE - WHEN wos.id IS NOT NULL AND wo.status NOT IN ('accepted', 'cancelled', 'problem') - THEN CASE wos.status - WHEN 'joined' THEN 'in_progress' - WHEN 'submitted' THEN 'pending_acceptance' - WHEN 'accepted' THEN 'accepted' - WHEN 'cancelled' THEN 'cancelled' - ELSE wo.status - END - ELSE wo.status - END - END AS status, - wo.acceptance_json, + wo.id, + wo.status AS wo_status, + wos.status AS wos_status, wos.id IS NOT NULL AS is_worker_share, CASE WHEN wos.id IS NOT NULL THEN wos.share_reward WHEN wo.assigned_worker_id = $1 THEN wo.reward_amount ELSE 0 - END AS worker_settlement_amount + END AS worker_settlement_amount, + wo.acceptance_json FROM work_orders wo LEFT JOIN work_order_shares wos ON wos.work_order_id = wo.id AND wos.worker_id = $1 AND wos.status != 'cancelled' WHERE wo.assigned_worker_id = $1 - OR ( - wo.last_assigned_worker_id = $1 - 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' - ) + UNION + SELECT + wo.id, + wo.status, + wos.status, + wos.id IS NOT NULL, + CASE + WHEN wos.id IS NOT NULL THEN wos.share_reward + WHEN wo.assigned_worker_id = $1 THEN wo.reward_amount + ELSE 0 + END, + wo.acceptance_json + FROM work_orders wo + LEFT JOIN work_order_shares wos + ON wos.work_order_id = wo.id + AND wos.worker_id = $1 + AND wos.status != 'cancelled' + WHERE wo.last_assigned_worker_id = $1 + 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 wos.id IS NOT NULL + UNION + SELECT + wo.id, + wo.status, + wos.status, + TRUE, + wos.share_reward, + wo.acceptance_json + FROM work_orders wo + INNER JOIN work_order_shares wos + ON wos.work_order_id = wo.id + AND wos.worker_id = $1 + AND wos.status != 'cancelled' + ), + worker_orders AS ( + SELECT + CASE + WHEN ( + CASE + WHEN is_worker_share AND wo_status NOT IN ('accepted', 'cancelled', 'problem') + THEN CASE wos_status + WHEN 'joined' THEN 'in_progress' + WHEN 'submitted' THEN 'pending_acceptance' + WHEN 'accepted' THEN 'accepted' + WHEN 'cancelled' THEN 'cancelled' + ELSE wo_status + END + ELSE wo_status + END + ) = 'in_progress' AND ( + EXISTS ( + SELECT 1 + FROM worker_order_cancel_requests wcr + WHERE wcr.work_order_id = worker_base.id + AND wcr.worker_id = $1 + AND wcr.status = 'pending' + ) OR EXISTS ( + SELECT 1 + FROM worker_order_feedbacks wf + WHERE wf.work_order_id = worker_base.id + AND wf.worker_id = $1 + AND wf.status = 'pending' + ) + ) THEN 'problem' + ELSE CASE + WHEN is_worker_share AND wo_status NOT IN ('accepted', 'cancelled', 'problem') + THEN CASE wos_status + WHEN 'joined' THEN 'in_progress' + WHEN 'submitted' THEN 'pending_acceptance' + WHEN 'accepted' THEN 'accepted' + WHEN 'cancelled' THEN 'cancelled' + ELSE wo_status + END + ELSE wo_status + END + END AS status, + acceptance_json, + is_worker_share, + worker_settlement_amount + FROM worker_base ) SELECT status,