优化履约待办分桶与补载体扫描
This commit is contained in:
@@ -1060,6 +1060,7 @@ export type PendingGroupRow = {
|
||||
sku_codes: string[]
|
||||
pending_reasons: string[]
|
||||
pending_subreasons: string[]
|
||||
queue_buckets: FulfillmentQueueBucket[]
|
||||
configured_channels: string[]
|
||||
locked_item_count: number
|
||||
item_count: number
|
||||
@@ -1141,6 +1142,20 @@ const PENDING_ISSUE_CATEGORIES = [
|
||||
'after_sales',
|
||||
'system',
|
||||
] as const
|
||||
export const FULFILLMENT_QUEUE_BUCKETS = [
|
||||
'actionable',
|
||||
'configuration',
|
||||
'operator_review',
|
||||
'terminal',
|
||||
] as const
|
||||
export type FulfillmentQueueBucket = (typeof FULFILLMENT_QUEUE_BUCKETS)[number]
|
||||
|
||||
function normalizeQueueBucket(value: unknown): FulfillmentQueueBucket | '' {
|
||||
const bucket = String(value || '').trim()
|
||||
return (FULFILLMENT_QUEUE_BUCKETS as readonly string[]).includes(bucket)
|
||||
? (bucket as FulfillmentQueueBucket)
|
||||
: ''
|
||||
}
|
||||
|
||||
/** 待分类队列:按全局内部 SKU 聚合(跨平台、跨店铺合并为一行)。 */
|
||||
export async function listPendingChannelGroups(input: {
|
||||
@@ -1148,17 +1163,28 @@ export async function listPendingChannelGroups(input: {
|
||||
pageSize?: number
|
||||
keyword?: string
|
||||
issueCategory?: string
|
||||
}): Promise<{ items: PendingGroupRow[]; total: number; page: number; pageSize: number }> {
|
||||
queueBucket?: string
|
||||
}): Promise<{
|
||||
items: PendingGroupRow[]
|
||||
total: number
|
||||
page: number
|
||||
pageSize: number
|
||||
bucketCounts: Record<FulfillmentQueueBucket, number>
|
||||
}> {
|
||||
const page = Math.max(1, Math.trunc(Number(input.page) || 1))
|
||||
const pageSize = Math.min(200, Math.max(1, Math.trunc(Number(input.pageSize) || 30)))
|
||||
const keyword = String(input.keyword || '').trim()
|
||||
const queueBucket = normalizeQueueBucket(input.queueBucket)
|
||||
const issueCategoryInput = String(input.issueCategory || '').trim()
|
||||
const issueCategory = (PENDING_ISSUE_CATEGORIES as readonly string[]).includes(issueCategoryInput)
|
||||
? issueCategoryInput
|
||||
: ''
|
||||
const params: unknown[] = []
|
||||
const clauses = [`v.fulfillment_status = 'pending'`]
|
||||
if (issueCategory) {
|
||||
if (queueBucket) {
|
||||
params.push(queueBucket)
|
||||
clauses.push(`v.queue_bucket = $${params.length}`)
|
||||
} else if (issueCategory) {
|
||||
params.push(issueCategory)
|
||||
clauses.push(`v.issue_category = $${params.length}`)
|
||||
}
|
||||
@@ -1172,13 +1198,49 @@ export async function listPendingChannelGroups(input: {
|
||||
}
|
||||
const where = clauses.join(' AND ')
|
||||
|
||||
const bucketCountParams: unknown[] = []
|
||||
let bucketCountKeyword = ''
|
||||
if (keyword) {
|
||||
bucketCountParams.push(`%${keyword}%`)
|
||||
bucketCountKeyword = `AND (
|
||||
v.internal_sku ILIKE $${bucketCountParams.length}
|
||||
OR v.sku_code ILIKE $${bucketCountParams.length}
|
||||
OR v.sku_name ILIKE $${bucketCountParams.length}
|
||||
)`
|
||||
}
|
||||
const bucketCountRows = await query<{ queue_bucket: FulfillmentQueueBucket; count: number }>(
|
||||
`
|
||||
WITH pending_items AS (
|
||||
SELECT
|
||||
v.queue_bucket,
|
||||
${PENDING_STATE_GROUP_KEY_SQL} AS group_key
|
||||
FROM v_fulfillment_queue v
|
||||
WHERE v.fulfillment_status = 'pending'
|
||||
${bucketCountKeyword}
|
||||
)
|
||||
SELECT queue_bucket, COUNT(DISTINCT group_key)::int AS count
|
||||
FROM pending_items
|
||||
GROUP BY queue_bucket
|
||||
`,
|
||||
bucketCountParams,
|
||||
)
|
||||
const bucketCounts: Record<FulfillmentQueueBucket, number> = {
|
||||
actionable: 0,
|
||||
configuration: 0,
|
||||
operator_review: 0,
|
||||
terminal: 0,
|
||||
}
|
||||
for (const row of bucketCountRows.rows) {
|
||||
bucketCounts[row.queue_bucket] = Number(row.count || 0)
|
||||
}
|
||||
|
||||
const total = await query<{ count: number }>(
|
||||
`
|
||||
WITH pending_items AS (
|
||||
SELECT
|
||||
v.internal_sku,
|
||||
${PENDING_STATE_GROUP_KEY_SQL} AS group_key
|
||||
FROM v_fulfillment_item_state v
|
||||
FROM v_fulfillment_queue v
|
||||
WHERE ${where}
|
||||
)
|
||||
SELECT COUNT(DISTINCT group_key)::int AS count
|
||||
@@ -1194,7 +1256,7 @@ export async function listPendingChannelGroups(input: {
|
||||
SELECT
|
||||
v.*,
|
||||
${PENDING_STATE_GROUP_KEY_SQL} AS group_key
|
||||
FROM v_fulfillment_item_state v
|
||||
FROM v_fulfillment_queue v
|
||||
WHERE ${where}
|
||||
),
|
||||
grouped AS (
|
||||
@@ -1217,6 +1279,7 @@ export async function listPendingChannelGroups(input: {
|
||||
NULL
|
||||
) AS pending_reasons,
|
||||
array_remove(array_agg(DISTINCT issue_code), '') AS pending_subreasons,
|
||||
array_agg(DISTINCT queue_bucket) AS queue_buckets,
|
||||
COUNT(*) FILTER (WHERE fulfillment_locked)::int AS locked_item_count,
|
||||
COUNT(*)::int AS item_count,
|
||||
COALESCE(SUM(revenue_amount_fen), 0)::bigint AS revenue_fen,
|
||||
@@ -1284,6 +1347,7 @@ export async function listPendingChannelGroups(input: {
|
||||
grouped.sku_codes,
|
||||
grouped.pending_reasons,
|
||||
grouped.pending_subreasons,
|
||||
grouped.queue_buckets,
|
||||
COALESCE(cfg.channels, ARRAY[]::text[]) AS configured_channels,
|
||||
grouped.locked_item_count,
|
||||
grouped.item_count,
|
||||
@@ -1326,9 +1390,10 @@ export async function listPendingChannelGroups(input: {
|
||||
v.shop_id,
|
||||
v.shop_name,
|
||||
${PENDING_STATE_GROUP_KEY_SQL} AS group_key
|
||||
FROM v_fulfillment_item_state v
|
||||
FROM v_fulfillment_queue v
|
||||
WHERE v.fulfillment_status = 'pending'
|
||||
AND ($2::text = '' OR v.issue_category = $2)
|
||||
AND ($3::text = '' OR v.queue_bucket = $3)
|
||||
)
|
||||
SELECT
|
||||
group_key,
|
||||
@@ -1353,7 +1418,7 @@ export async function listPendingChannelGroups(input: {
|
||||
usage_count DESC,
|
||||
shop_name
|
||||
`,
|
||||
[groupKeys, issueCategory],
|
||||
[groupKeys, issueCategory, queueBucket],
|
||||
)
|
||||
|
||||
const seen = new Set<string>()
|
||||
@@ -1377,6 +1442,7 @@ export async function listPendingChannelGroups(input: {
|
||||
total: total.rows[0]?.count || 0,
|
||||
page,
|
||||
pageSize,
|
||||
bucketCounts,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1453,6 +1519,7 @@ export async function retryPendingCarrierGroup(input: {
|
||||
orderItemIds: batchIds,
|
||||
limit: batchIds.length,
|
||||
includeLocked: input.includeLocked === true,
|
||||
includeNonActionable: true,
|
||||
retryInactiveCarrier: input.retryInactiveCarrier === true,
|
||||
})
|
||||
workOrdersCreated += carrier.workOrdersCreated
|
||||
@@ -1532,6 +1599,7 @@ export async function unlockPendingChannelGroup(input: {
|
||||
groupKey?: string
|
||||
internalSku?: string
|
||||
issueCategory?: string
|
||||
queueBucket?: string
|
||||
}): Promise<{ updated: number }> {
|
||||
const scope = resolvePendingGroupScopeInput(input)
|
||||
if (!scope) {
|
||||
@@ -1551,13 +1619,17 @@ export async function unlockPendingChannelGroup(input: {
|
||||
where.push(`state.provider = $1 AND state.shop_id = $2 AND state.sku_code = $3`)
|
||||
}
|
||||
const issueCategory = String(input.issueCategory || '').trim()
|
||||
if ((PENDING_ISSUE_CATEGORIES as readonly string[]).includes(issueCategory)) {
|
||||
const queueBucket = normalizeQueueBucket(input.queueBucket)
|
||||
if (queueBucket) {
|
||||
params.push(queueBucket)
|
||||
where.push(`state.queue_bucket = $${params.length}`)
|
||||
} else if ((PENDING_ISSUE_CATEGORIES as readonly string[]).includes(issueCategory)) {
|
||||
params.push(issueCategory)
|
||||
where.push(`state.issue_category = $${params.length}`)
|
||||
}
|
||||
const matched = await query<{ order_item_id: number }>(
|
||||
`SELECT state.order_item_id
|
||||
FROM v_fulfillment_item_state state
|
||||
FROM v_fulfillment_queue state
|
||||
WHERE ${where.join(' AND ')}
|
||||
ORDER BY state.order_item_id`,
|
||||
params,
|
||||
@@ -1623,6 +1695,7 @@ export async function deletePendingChannelGroup(input: {
|
||||
groupKey?: string
|
||||
internalSku?: string
|
||||
issueCategory?: string
|
||||
queueBucket?: string
|
||||
adminUserId: number | null
|
||||
}): Promise<DeletePendingChannelGroupResult> {
|
||||
const scope = resolvePendingGroupScopeInput(input)
|
||||
@@ -1644,7 +1717,16 @@ export async function deletePendingChannelGroup(input: {
|
||||
itemWhere.push(`o.provider = $1 AND o.shop_id = $2 AND oi.sku_code = $3`)
|
||||
}
|
||||
const issueCategory = String(input.issueCategory || '').trim()
|
||||
if ((PENDING_ISSUE_CATEGORIES as readonly string[]).includes(issueCategory)) {
|
||||
const queueBucket = normalizeQueueBucket(input.queueBucket)
|
||||
if (queueBucket) {
|
||||
itemParams.push(queueBucket)
|
||||
itemWhere.push(`EXISTS (
|
||||
SELECT 1
|
||||
FROM v_fulfillment_queue state
|
||||
WHERE state.order_item_id = oi.id
|
||||
AND state.queue_bucket = $${itemParams.length}
|
||||
)`)
|
||||
} else if ((PENDING_ISSUE_CATEGORIES as readonly string[]).includes(issueCategory)) {
|
||||
itemParams.push(issueCategory)
|
||||
itemWhere.push(`EXISTS (
|
||||
SELECT 1
|
||||
@@ -1761,6 +1843,7 @@ export async function assignPendingChannelGroup(input: {
|
||||
mappedInternalSku?: string
|
||||
channel: string
|
||||
issueCategory?: string
|
||||
queueBucket?: string
|
||||
adminUserId: number | null
|
||||
note?: string | undefined
|
||||
}): Promise<{
|
||||
@@ -1777,6 +1860,13 @@ export async function assignPendingChannelGroup(input: {
|
||||
errorCode: 'pending_group_key_required',
|
||||
})
|
||||
}
|
||||
const queueBucket = normalizeQueueBucket(input.queueBucket)
|
||||
if (queueBucket === 'terminal') {
|
||||
throw createHttpError('售后或已终止订单不能重新指定履约渠道', {
|
||||
statusCode: 409,
|
||||
errorCode: 'terminal_fulfillment_bucket_not_assignable',
|
||||
})
|
||||
}
|
||||
if (scope.type !== 'internal_sku') {
|
||||
const mappedInternalSku = canonicalizeInternalSku(input.mappedInternalSku)
|
||||
if (!mappedInternalSku || !containsCjk(mappedInternalSku)) {
|
||||
@@ -1788,7 +1878,15 @@ export async function assignPendingChannelGroup(input: {
|
||||
const updateParams: unknown[] = [scope.provider, scope.shopId, scope.skuCode, mappedInternalSku]
|
||||
const issueCategory = String(input.issueCategory || '').trim()
|
||||
let issueCategoryFilter = ''
|
||||
if ((PENDING_ISSUE_CATEGORIES as readonly string[]).includes(issueCategory)) {
|
||||
if (queueBucket) {
|
||||
updateParams.push(queueBucket)
|
||||
issueCategoryFilter = `AND EXISTS (
|
||||
SELECT 1
|
||||
FROM v_fulfillment_queue state
|
||||
WHERE state.order_item_id = oi.id
|
||||
AND state.queue_bucket = $${updateParams.length}
|
||||
)`
|
||||
} else if ((PENDING_ISSUE_CATEGORIES as readonly string[]).includes(issueCategory)) {
|
||||
updateParams.push(issueCategory)
|
||||
issueCategoryFilter = `AND EXISTS (
|
||||
SELECT 1
|
||||
@@ -1829,6 +1927,7 @@ export async function assignPendingChannelGroup(input: {
|
||||
internalSku: mappedInternalSku,
|
||||
channel: input.channel,
|
||||
issueCategory: input.issueCategory || '',
|
||||
queueBucket,
|
||||
orderItemIds: updated.rows.map((row) => Number(row.id)),
|
||||
adminUserId: input.adminUserId,
|
||||
note: input.note,
|
||||
@@ -1838,6 +1937,7 @@ export async function assignPendingChannelGroup(input: {
|
||||
internalSku: scope.internalSku,
|
||||
channel: input.channel,
|
||||
issueCategory: input.issueCategory || '',
|
||||
queueBucket,
|
||||
adminUserId: input.adminUserId,
|
||||
note: input.note,
|
||||
})
|
||||
@@ -1848,6 +1948,7 @@ export async function assignChannelByInternalSku(input: {
|
||||
internalSku: string
|
||||
channel: string
|
||||
issueCategory?: string
|
||||
queueBucket?: string
|
||||
orderItemIds?: number[]
|
||||
adminUserId: number | null
|
||||
note?: string | undefined
|
||||
@@ -1878,6 +1979,13 @@ export async function assignChannelByInternalSku(input: {
|
||||
}
|
||||
|
||||
const issueCategory = String(input.issueCategory || '').trim()
|
||||
const queueBucket = normalizeQueueBucket(input.queueBucket)
|
||||
if (queueBucket === 'terminal') {
|
||||
throw createHttpError('售后或已终止订单不能重新指定履约渠道', {
|
||||
statusCode: 409,
|
||||
errorCode: 'terminal_fulfillment_bucket_not_assignable',
|
||||
})
|
||||
}
|
||||
const requestedOrderItemIds = [
|
||||
...new Set(
|
||||
(input.orderItemIds || []).map(Number).filter((id) => Number.isInteger(id) && id > 0),
|
||||
@@ -1887,7 +1995,7 @@ export async function assignChannelByInternalSku(input: {
|
||||
const idsResult = await query<{ id: number }>(
|
||||
`
|
||||
SELECT state.order_item_id AS id
|
||||
FROM v_fulfillment_item_state state
|
||||
FROM v_fulfillment_queue state
|
||||
WHERE state.internal_sku = $1
|
||||
AND (
|
||||
state.fulfillment_status = 'pending'
|
||||
@@ -1897,12 +2005,20 @@ export async function assignChannelByInternalSku(input: {
|
||||
)
|
||||
)
|
||||
AND ($3::text = '' OR state.issue_category = $3)
|
||||
AND ($6::text = '' OR state.queue_bucket = $6)
|
||||
AND (
|
||||
$4::boolean = FALSE
|
||||
OR state.order_item_id = ANY($5::bigint[])
|
||||
)
|
||||
`,
|
||||
[internalSku, input.channel, issueCategory, requestedOrderItemIds.length > 0, idParams],
|
||||
[
|
||||
internalSku,
|
||||
input.channel,
|
||||
issueCategory,
|
||||
requestedOrderItemIds.length > 0,
|
||||
idParams,
|
||||
queueBucket,
|
||||
],
|
||||
)
|
||||
|
||||
const draftProductId = await ensureDraftProductForChannel(internalSku, input.channel)
|
||||
|
||||
Reference in New Issue
Block a user