fix(pipeline): 变更按业务日回填,异常自动收口,载体管道加游标
- 渠道判定/成本快照返回受影响业务日,任务按日重算事实与总账 - 人工配置或载体补齐后,manual_config_missing / carrier_missing 自动关闭 - carrier_dispatch 使用水位游标,避免每轮从 id=0 重扫
This commit is contained in:
@@ -18,6 +18,7 @@ function buildItem(patch: Partial<ChannelCandidateItem> = {}): ChannelCandidateI
|
||||
normalized_sku_name: '套装-测试',
|
||||
has_work_order: false,
|
||||
has_affiliate_task: false,
|
||||
basis_date: '2026-10-31',
|
||||
...patch,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ export type ChannelCandidateItem = {
|
||||
normalized_sku_name: string | null
|
||||
has_work_order: boolean
|
||||
has_affiliate_task: boolean
|
||||
basis_date: string
|
||||
}
|
||||
|
||||
export type ChannelDecision = {
|
||||
@@ -142,6 +143,8 @@ export type AssignChannelsResult = {
|
||||
/** 下一轮起点;到达表尾时回绕到 0,保证漏判的订单还能被重新评估。 */
|
||||
nextCursor: number
|
||||
byChannel: Record<string, number>
|
||||
/** 本轮被判定的订单所属业务日,供事实/总账按日回填。 */
|
||||
affectedDates: string[]
|
||||
}
|
||||
|
||||
/** 批量判定:默认只扫 pending 且未锁定的订单项。 */
|
||||
@@ -165,7 +168,9 @@ export async function assignPendingOrderItemChannels(
|
||||
lastId: cursor,
|
||||
nextCursor: cursor,
|
||||
byChannel: {},
|
||||
affectedDates: [],
|
||||
}
|
||||
const affectedDates = new Set<string>()
|
||||
|
||||
for (let batch = 0; batch < maxBatches; batch += 1) {
|
||||
const rows = await query<ChannelCandidateItem>(
|
||||
@@ -178,6 +183,8 @@ export async function assignPendingOrderItemChannels(
|
||||
oi.sku_name,
|
||||
oi.manual_sku_key,
|
||||
oi.normalized_sku_name,
|
||||
(COALESCE(o.paid_at, o.created_at) AT TIME ZONE 'Asia/Shanghai')::date::text
|
||||
AS basis_date,
|
||||
EXISTS (
|
||||
SELECT 1 FROM work_orders wo WHERE wo.order_item_id = oi.id
|
||||
) AS has_work_order,
|
||||
@@ -213,6 +220,7 @@ export async function assignPendingOrderItemChannels(
|
||||
|
||||
for (const item of rows.rows) {
|
||||
const decision = resolveChannelDecision(item, rules)
|
||||
if (decision.channel) affectedDates.add(item.basis_date)
|
||||
if (decision.conflict) {
|
||||
result.conflicted += 1
|
||||
await upsertAggregatedException({
|
||||
@@ -258,6 +266,7 @@ export async function assignPendingOrderItemChannels(
|
||||
}
|
||||
|
||||
result.nextCursor = reachedEnd ? 0 : cursor
|
||||
result.affectedDates = [...affectedDates].sort()
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -543,3 +552,45 @@ async function loadEnabledRules(): Promise<ChannelRule[]> {
|
||||
export const CHANNEL_RULE_KEY_TYPES = MATCH_KEY_TYPES
|
||||
export const CHANNEL_RULE_SOURCES = RULE_SOURCES
|
||||
export const CHANNEL_VALUES = FULFILLMENT_CHANNELS
|
||||
|
||||
/**
|
||||
* 为卡券商品(tpv: 前缀)批量补渠道规则。
|
||||
*
|
||||
* 卡券没有履约载体证据,只能靠规则判定;商品目录建好后一次补齐,
|
||||
* 避免运营按 SKU 逐条点「指定渠道」。
|
||||
*/
|
||||
export async function ensureVoucherChannelRules(): Promise<{
|
||||
created: number
|
||||
productCount: number
|
||||
}> {
|
||||
const created = await query<{ id: number }>(
|
||||
`
|
||||
INSERT INTO sku_channel_rules (
|
||||
provider, shop_id, match_key_type, match_key, channel, priority,
|
||||
source, enabled, note, created_at, updated_at
|
||||
)
|
||||
SELECT
|
||||
'', '', 'sku_code', substring(p.canonical_sku from 5), 'third_party_voucher', 70,
|
||||
'voucher', TRUE, '卡券商品自动生成', NOW(), NOW()
|
||||
FROM product_catalog p
|
||||
WHERE p.status = 'active'
|
||||
AND p.canonical_sku LIKE 'tpv:%'
|
||||
AND btrim(substring(p.canonical_sku from 5)) <> ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM sku_channel_rules r
|
||||
WHERE r.provider = ''
|
||||
AND r.shop_id = ''
|
||||
AND r.match_key_type = 'sku_code'
|
||||
AND r.match_key = substring(p.canonical_sku from 5)
|
||||
AND r.channel = 'third_party_voucher'
|
||||
)
|
||||
ON CONFLICT DO NOTHING
|
||||
RETURNING id
|
||||
`,
|
||||
)
|
||||
const total = await query<{ count: number }>(
|
||||
`SELECT COUNT(*)::int AS count FROM product_catalog
|
||||
WHERE status = 'active' AND canonical_sku LIKE 'tpv:%'`,
|
||||
)
|
||||
return { created: created.rowCount || 0, productCount: Number(total.rows[0]?.count || 0) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user