feat(fulfillment): 增加未命中渠道兜底核销
This commit is contained in:
@@ -99,6 +99,19 @@ type VoucherOrderItemTargetRow = {
|
||||
platform: string
|
||||
platform_order_id: string
|
||||
fulfillment_channel: string
|
||||
fulfillment_status: string
|
||||
fulfillment_pending_reason: string
|
||||
}
|
||||
|
||||
type VoucherConsumeTargetInsert = {
|
||||
provider: VoucherConsumeProvider
|
||||
channel: VoucherConsumeChannel
|
||||
trigger_event: VoucherConsumeEvent
|
||||
target_key: string
|
||||
platform_order_id: string
|
||||
order_id: number
|
||||
order_item_id: number
|
||||
due_at: string
|
||||
}
|
||||
|
||||
const RETRY_DELAYS_SECONDS = [10, 30, 60, 300, 600, 900, 1800]
|
||||
@@ -194,6 +207,8 @@ export async function scheduleVoucherConsumeForOrderItems(
|
||||
oi.id,
|
||||
oi.order_id,
|
||||
oi.fulfillment_channel,
|
||||
oi.fulfillment_status,
|
||||
oi.fulfillment_pending_reason,
|
||||
o.provider,
|
||||
o.platform,
|
||||
o.platform_order_id
|
||||
@@ -205,16 +220,7 @@ export async function scheduleVoucherConsumeForOrderItems(
|
||||
[ids],
|
||||
)
|
||||
const policy = getVoucherConsumePolicy()
|
||||
const targets: Array<{
|
||||
provider: VoucherConsumeProvider
|
||||
channel: VoucherConsumeChannel
|
||||
trigger_event: VoucherConsumeEvent
|
||||
target_key: string
|
||||
platform_order_id: string
|
||||
order_id: number
|
||||
order_item_id: number
|
||||
due_at: string
|
||||
}> = []
|
||||
const targets: VoucherConsumeTargetInsert[] = []
|
||||
|
||||
for (const row of rows.rows) {
|
||||
const provider = resolveVoucherProviderFromFields(row.provider, row.platform)
|
||||
@@ -241,6 +247,76 @@ export async function scheduleVoucherConsumeForOrderItems(
|
||||
})
|
||||
}
|
||||
|
||||
return queueVoucherConsumeTargets(targets, event)
|
||||
}
|
||||
|
||||
/**
|
||||
* 未命中任何履约渠道时,按平台兜底策略安排核销。
|
||||
*
|
||||
* 兜底只产生核销任务,不修改订单项渠道或履约状态,后续仍由问题队列处理履约。
|
||||
*/
|
||||
export async function scheduleVoucherConsumeFallbackForOrderItems(
|
||||
orderItemIds: number[],
|
||||
event: Extract<VoucherConsumeEvent, 'order_received'> = 'order_received',
|
||||
): Promise<{ targetCount: number; queuedCount: number; jobIds: number[] }> {
|
||||
const ids = [...new Set(orderItemIds.map(Number).filter((id) => Number.isInteger(id) && id > 0))]
|
||||
if (ids.length === 0) {
|
||||
return { targetCount: 0, queuedCount: 0, jobIds: [] }
|
||||
}
|
||||
|
||||
const rows = await query<VoucherOrderItemTargetRow>(
|
||||
`
|
||||
SELECT
|
||||
oi.id,
|
||||
oi.order_id,
|
||||
oi.fulfillment_channel,
|
||||
oi.fulfillment_status,
|
||||
oi.fulfillment_pending_reason,
|
||||
o.provider,
|
||||
o.platform,
|
||||
o.platform_order_id
|
||||
FROM order_items oi
|
||||
JOIN orders o ON o.id = oi.order_id
|
||||
WHERE oi.id = ANY($1::bigint[])
|
||||
AND oi.fulfillment_status = 'pending'
|
||||
AND oi.fulfillment_pending_reason = 'no_channel_capability'
|
||||
`,
|
||||
[ids],
|
||||
)
|
||||
const policy = getVoucherConsumePolicy()
|
||||
const targets: VoucherConsumeTargetInsert[] = []
|
||||
|
||||
for (const row of rows.rows) {
|
||||
const provider = resolveVoucherProviderFromFields(row.provider, row.platform)
|
||||
if (!provider) continue
|
||||
|
||||
const resolved = resolveVoucherConsumePolicy({
|
||||
provider,
|
||||
channel: 'unmatched',
|
||||
event,
|
||||
policy,
|
||||
})
|
||||
if (!resolved.enabled) continue
|
||||
|
||||
targets.push({
|
||||
provider,
|
||||
channel: 'unmatched',
|
||||
trigger_event: event,
|
||||
target_key: buildVoucherConsumeTargetKey(row.id, `order_item:${row.id}`),
|
||||
platform_order_id: String(row.platform_order_id || '').trim(),
|
||||
order_id: Number(row.order_id),
|
||||
order_item_id: Number(row.id),
|
||||
due_at: new Date(Date.now() + resolved.delaySeconds * 1000).toISOString(),
|
||||
})
|
||||
}
|
||||
|
||||
return queueVoucherConsumeTargets(targets, event)
|
||||
}
|
||||
|
||||
async function queueVoucherConsumeTargets(
|
||||
targets: VoucherConsumeTargetInsert[],
|
||||
event: VoucherConsumeEvent,
|
||||
): Promise<{ targetCount: number; queuedCount: number; jobIds: number[] }> {
|
||||
if (targets.length === 0) {
|
||||
return { targetCount: 0, queuedCount: 0, jobIds: [] }
|
||||
}
|
||||
@@ -292,10 +368,11 @@ export async function scheduleVoucherConsumeForOrderItems(
|
||||
[JSON.stringify(targets)],
|
||||
)
|
||||
|
||||
logIntegration('[voucher-consume/queue]', '订单渠道确定后批量安排核销', {
|
||||
logIntegration('[voucher-consume/queue]', '安排电子凭证核销任务', {
|
||||
event,
|
||||
targetCount: targets.length,
|
||||
queuedCount: inserted.rowCount || 0,
|
||||
channels: [...new Set(targets.map((target) => target.channel))],
|
||||
jobIds: inserted.rows.map((row) => Number(row.id)),
|
||||
})
|
||||
|
||||
@@ -943,7 +1020,8 @@ function normalizeVoucherChannel(value: unknown): VoucherConsumeChannel | null {
|
||||
channel === 'work_order' ||
|
||||
channel === 'manual' ||
|
||||
channel === 'third_party_voucher' ||
|
||||
channel === 'on_account'
|
||||
channel === 'on_account' ||
|
||||
channel === 'unmatched'
|
||||
) {
|
||||
return channel
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user