优化订单打手信息展示
This commit is contained in:
@@ -338,6 +338,7 @@ export type ListInput = {
|
||||
keyword?: string
|
||||
/** 打手类型筛选:internal 内部打手 / external 外部打手 */
|
||||
workerType?: string
|
||||
/** 后台打手列表按主键精确定位。 */
|
||||
workerId?: number
|
||||
/** 后台待验收筛选:普通单按接单人,拼单按参与打手等级。 */
|
||||
workerLevelIds?: number[]
|
||||
|
||||
@@ -483,8 +483,9 @@ export async function listWorkerUsers({
|
||||
status = '',
|
||||
keyword = '',
|
||||
workerType = '',
|
||||
workerId = 0,
|
||||
}: ListInput = {}): Promise<{ items: WorkerUserRow[]; total: number }> {
|
||||
const { whereClause, params } = buildWorkerUserWhere({ status, keyword, workerType })
|
||||
const { whereClause, params } = buildWorkerUserWhere({ status, keyword, workerType, workerId })
|
||||
const totalResult = await query<{ total: number }>(
|
||||
`SELECT COUNT(*)::int AS total FROM worker_users wu ${whereClause}`,
|
||||
params,
|
||||
@@ -1257,7 +1258,8 @@ function buildWorkerUserWhere({
|
||||
status = '',
|
||||
keyword = '',
|
||||
workerType = '',
|
||||
}: Pick<ListInput, 'status' | 'keyword' | 'workerType'>) {
|
||||
workerId = 0,
|
||||
}: Pick<ListInput, 'status' | 'keyword' | 'workerType' | 'workerId'>) {
|
||||
const filters: string[] = []
|
||||
const params: unknown[] = []
|
||||
if (status) {
|
||||
@@ -1274,6 +1276,10 @@ function buildWorkerUserWhere({
|
||||
params.push(workerType)
|
||||
filters.push(`wu.worker_type = $${params.length}`)
|
||||
}
|
||||
if (workerId) {
|
||||
params.push(workerId)
|
||||
filters.push(`wu.id = $${params.length}`)
|
||||
}
|
||||
return {
|
||||
whereClause: filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : '',
|
||||
params,
|
||||
|
||||
@@ -1003,9 +1003,11 @@ export async function deleteAdminWorkProductRule(ruleId: number | string) {
|
||||
export async function listAdminWorkerUsers(query: JsonObject = {}) {
|
||||
const page = normalizePage(query.page)
|
||||
const pageSize = normalizePageSize(query.pageSize)
|
||||
const workerId = normalizeOptionalId(query.workerId ?? query.worker_id) || 0
|
||||
const { items, total } = await listWorkerUsers({
|
||||
page,
|
||||
pageSize,
|
||||
workerId,
|
||||
status: String(query.status || '').trim(),
|
||||
keyword: String(query.keyword || '').trim(),
|
||||
workerType: String(query.workerType || query.worker_type || '').trim(),
|
||||
@@ -1445,7 +1447,7 @@ function mapAdminWorkerWithdrawalAccount(account: WorkerWithdrawalAccountRow) {
|
||||
export async function getAdminWorkOrderEvents(workOrderId: number | string) {
|
||||
await getRequiredWorkOrder(workOrderId)
|
||||
const events = await listWorkOrderEventsByOrderId(workOrderId)
|
||||
return { items: await mapWorkOrderEvents(events) }
|
||||
return { items: await mapWorkOrderEvents(events, { includeWorkerSummary: true }) }
|
||||
}
|
||||
|
||||
export async function listAdminWorkOrders(query: JsonObject = {}) {
|
||||
|
||||
@@ -1686,6 +1686,15 @@ export type WorkOrderEventView = {
|
||||
fromStatus: string
|
||||
toStatus: string
|
||||
payload: JsonObject
|
||||
/** 仅管理端流转记录返回,供查看事件涉及的打手信息。 */
|
||||
worker?: {
|
||||
workerId: number
|
||||
displayName: string
|
||||
username: string
|
||||
workerType: string
|
||||
levelName: string
|
||||
status: string
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1703,33 +1712,64 @@ export async function mapWorkOrderEvents(
|
||||
payload_json: string | Record<string, unknown>
|
||||
created_at: string
|
||||
}>,
|
||||
options: { includeWorkerSummary?: boolean } = {},
|
||||
): Promise<WorkOrderEventView[]> {
|
||||
const workerIds = [
|
||||
...new Set(
|
||||
events
|
||||
.filter((event) => event.actor_type === 'worker')
|
||||
.map((event) => Number(event.actor_id))
|
||||
.filter((id) => Number.isFinite(id) && id > 0),
|
||||
events.map(resolveWorkOrderEventWorkerId).filter((id) => Number.isFinite(id) && id > 0),
|
||||
),
|
||||
]
|
||||
const workerNameById = new Map<number, string>()
|
||||
for (const workerId of workerIds) {
|
||||
const worker = await getWorkerUserById(workerId)
|
||||
if (worker) {
|
||||
workerNameById.set(workerId, worker.display_name || worker.username || `打手#${workerId}`)
|
||||
}
|
||||
}
|
||||
const workers = await Promise.all(workerIds.map((workerId) => getWorkerUserById(workerId)))
|
||||
const workerById = new Map(
|
||||
workers
|
||||
.filter((worker): worker is WorkerUserRow => Boolean(worker))
|
||||
.map((worker) => [Number(worker.id), worker]),
|
||||
)
|
||||
const workerNameById = new Map(
|
||||
[...workerById.entries()].map(([workerId, worker]) => [
|
||||
workerId,
|
||||
worker.display_name || worker.username || `打手#${workerId}`,
|
||||
]),
|
||||
)
|
||||
|
||||
return events.map((event) => ({
|
||||
id: Number(event.id),
|
||||
time: new Date(event.created_at).toISOString(),
|
||||
actorType: event.actor_type,
|
||||
actorName: resolveWorkOrderEventActorName(event, workerNameById),
|
||||
eventType: event.event_type,
|
||||
fromStatus: event.from_status,
|
||||
toStatus: event.to_status,
|
||||
payload: safeParseJson(event.payload_json),
|
||||
}))
|
||||
return events.map((event) => {
|
||||
const workerId = resolveWorkOrderEventWorkerId(event)
|
||||
const worker = workerById.get(workerId)
|
||||
return {
|
||||
id: Number(event.id),
|
||||
time: new Date(event.created_at).toISOString(),
|
||||
actorType: event.actor_type,
|
||||
actorName: resolveWorkOrderEventActorName(event, workerNameById),
|
||||
eventType: event.event_type,
|
||||
fromStatus: event.from_status,
|
||||
toStatus: event.to_status,
|
||||
payload: safeParseJson(event.payload_json),
|
||||
...(options.includeWorkerSummary && worker
|
||||
? {
|
||||
worker: {
|
||||
workerId: Number(worker.id),
|
||||
displayName: worker.display_name || worker.username || `打手#${worker.id}`,
|
||||
username: worker.username,
|
||||
workerType: normalizeWorkerType(worker.worker_type),
|
||||
levelName: worker.level_name || '-',
|
||||
status: worker.status,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** 操作人优先,其次读取事件载荷中的关联打手。 */
|
||||
function resolveWorkOrderEventWorkerId(event: {
|
||||
actor_type: string
|
||||
actor_id: string
|
||||
payload_json: string | Record<string, unknown>
|
||||
}) {
|
||||
const actorWorkerId = event.actor_type === 'worker' ? Number(event.actor_id) : 0
|
||||
if (Number.isInteger(actorWorkerId) && actorWorkerId > 0) return actorWorkerId
|
||||
const payloadWorkerId = Number(safeParseJson(event.payload_json).workerId || 0)
|
||||
return Number.isInteger(payloadWorkerId) && payloadWorkerId > 0 ? payloadWorkerId : 0
|
||||
}
|
||||
|
||||
function resolveWorkOrderEventActorName(
|
||||
|
||||
Reference in New Issue
Block a user