优化打手接单上限统计

This commit is contained in:
yml2213
2026-08-16 10:06:12 +08:00
parent b9e4d007aa
commit ad84388450
4 changed files with 44 additions and 34 deletions
@@ -2324,10 +2324,19 @@ async function enqueueDepositUnfreezeWithClient(
export async function countWorkerActiveOrders(workerId: number | string): Promise<number> {
const result = await query<{ total: number }>(
`
SELECT COUNT(*)::int AS total
FROM work_orders
WHERE assigned_worker_id = $1
AND status IN ('in_progress', 'pending_acceptance', 'problem')
SELECT (
SELECT COUNT(*)::int
FROM work_orders
WHERE assigned_worker_id = $1
AND status = 'in_progress'
) + (
SELECT COUNT(*)::int
FROM work_order_shares wos
INNER JOIN work_orders wo ON wo.id = wos.work_order_id
WHERE wos.worker_id = $1
AND wos.status = 'joined'
AND wo.status = 'open'
) AS total
`,
[Number(workerId)],
)
@@ -2443,10 +2452,19 @@ async function getOutstandingDepositAmountWithClient(
async function countWorkerActiveOrdersWithClient(client: PoolClient, workerId: number): Promise<number> {
const result = await client.query<{ total: number }>(
`
SELECT COUNT(*)::int AS total
FROM work_orders
WHERE assigned_worker_id = $1
AND status IN ('in_progress', 'pending_acceptance', 'problem')
SELECT (
SELECT COUNT(*)::int
FROM work_orders
WHERE assigned_worker_id = $1
AND status = 'in_progress'
) + (
SELECT COUNT(*)::int
FROM work_order_shares wos
INNER JOIN work_orders wo ON wo.id = wos.work_order_id
WHERE wos.worker_id = $1
AND wos.status = 'joined'
AND wo.status = 'open'
) AS total
`,
[workerId],
)
@@ -494,9 +494,10 @@ export function requireActiveWorkerSession(session: WorkerSession | null | undef
export async function getWorkerProfile(session: WorkerSession) {
const worker = await getRequiredWorker(session.workerId)
const [financeSummary, acceptedOrderCount, timeoutOrderCount, financeConfig] = await Promise.all([
const [financeSummary, acceptedOrderCount, activeOrderCount, timeoutOrderCount, financeConfig] = await Promise.all([
getWorkerFinanceRequestSummary(session.workerId),
countWorkerAcceptedOrders(session.workerId),
countWorkerActiveOrders(session.workerId),
countWorkerTimeoutEvents(session.workerId),
Promise.resolve(getWorkerFinanceConfig()),
])
@@ -507,6 +508,7 @@ export async function getWorkerProfile(session: WorkerSession) {
permissions,
summary: {
acceptedOrderCount,
activeOrderCount,
timeoutOrderCount,
pendingWithdrawAmount: financeSummary.pendingWithdrawAmount,
approvedWithdrawAmount: financeSummary.approvedWithdrawAmount,
@@ -33,7 +33,6 @@ import type { CollectField, WorkOrder, WorkerUser } from '@/types/worker-platfor
import { formatDateTime } from '@/utils/date-time'
type HallSummary = {
activeCount: number
pendingAcceptanceCount: number
problemCount: number
}
@@ -82,34 +81,24 @@ export default function WorkerHallPage() {
const hallSummaryQuery = useQuery({
queryKey: ['worker-hall-summary'],
queryFn: async () => {
const [inProgressResponse, pendingAcceptanceResponse, problemResponse] =
await Promise.all([
fetchWorkerMyOrders({
status: 'in_progress',
page: 1,
pageSize: 1,
}),
fetchWorkerMyOrders({
status: 'pending_acceptance',
page: 1,
pageSize: 1,
}),
fetchWorkerMyOrders({
status: 'problem',
page: 1,
pageSize: 1,
}),
])
const [pendingAcceptanceResponse, problemResponse] = await Promise.all([
fetchWorkerMyOrders({
status: 'pending_acceptance',
page: 1,
pageSize: 1,
}),
fetchWorkerMyOrders({
status: 'problem',
page: 1,
pageSize: 1,
}),
])
const pendingAcceptanceCount =
pendingAcceptanceResponse.data.pagination.total
const problemCount = problemResponse.data.pagination.total
return {
activeCount:
inProgressResponse.data.pagination.total +
pendingAcceptanceCount +
problemCount,
pendingAcceptanceCount,
problemCount,
} satisfies HallSummary
@@ -121,7 +110,7 @@ export default function WorkerHallPage() {
const pagination = ordersQuery.data?.data.pagination
const categories = ordersQuery.data?.data.categories || []
const worker = profileQuery.data?.data.worker
const activeCount = hallSummaryQuery.data?.activeCount || 0
const activeCount = Number(profileQuery.data?.data.summary.activeOrderCount || 0)
const pendingAcceptanceCount = hallSummaryQuery.data?.pendingAcceptanceCount || 0
const problemCount = hallSummaryQuery.data?.problemCount || 0
const maxActiveOrders = Number(worker?.level?.permissions.maxActiveOrders || 0)
@@ -215,7 +204,7 @@ export default function WorkerHallPage() {
note:
remainingSlots === null
? '当前等级待配置'
: `待验收 ${pendingAcceptanceCount} 单 / 问题单 ${problemCount}`,
: `待验收 ${pendingAcceptanceCount} 单 / 问题单 ${problemCount}(不占上限)`,
},
]
@@ -38,6 +38,7 @@ export type WorkerUser = {
export type WorkerProfileSummary = {
acceptedOrderCount: number
activeOrderCount: number
timeoutOrderCount: number
pendingWithdrawAmount: number
approvedWithdrawAmount: number