fix(cost): 快照新单优先、费用口径与回填健壮性

- 成本快照拆热/冷双通道:新单走热通道分钟级入账,历史项冷通道轮询重算
- 通用成本只允许平台费/支付费自动全量,售后损失/其他必须按商品配置
- 比例费用按扣除已确认退款后的净收入计算
- 已入账 actual 快照不再被后续预估覆盖
- 受影响日期超过 31 天时按整段重算,不再静默丢弃;关账跳过会提示
- 通用费用解析不到时计数告警;已入队的成本缺失不再重复 upsert
- 快照替换与写入放入同一事务,避免事实任务读到空窗
This commit is contained in:
yml2213
2026-11-01 14:46:08 +08:00
parent 34832139b0
commit cf0d600c7d
4 changed files with 304 additions and 79 deletions
@@ -76,6 +76,7 @@ function item(patch: Record<string, unknown> = {}) {
work_order_status: null as string | null,
reward_amount: null as number | null,
revenue_fen: 10000,
refund_fen: 0,
basis_date: '2026-10-31',
...patch,
}
@@ -274,3 +275,82 @@ test('通用成本模型按商品/全局版本解析且能与渠道成本共存'
assert.equal(generic.length, 1)
assert.equal(generic[0]?.totalAmountFen, 600)
})
test('售后损失/其他成本只有商品级版本才自动入账', () => {
const items: CostItemRow[] = [
{
id: 30,
code: 'sys_after_sales_loss',
pricing_mode: 'per_order',
cost_model: 'after_sales_loss',
},
]
const globalVersion: CostVersionRow[] = [
{
id: 903,
cost_item_id: 30,
scope_type: 'global',
canonical_product_id: null,
work_product_rule_id: null,
amount_fen: 500,
rate_bps: 0,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
assert.equal(
resolveGenericCosts(item(), items, globalVersion, new Map([[30, 'per_order']])).length,
0,
'全局售后损失不能套到所有订单',
)
const productVersion: CostVersionRow[] = globalVersion.map((row) => ({
...row,
scope_type: 'product',
canonical_product_id: 11,
}))
assert.equal(
resolveGenericCosts(item(), items, productVersion, new Map([[30, 'per_order']])).length,
1,
'商品级售后损失可以入账',
)
})
test('比例费用按退款后的净收入计算', () => {
const feeItems = new Map<string, CostItemRow>([
[
'sys_platform_fee',
{
id: 20,
code: 'sys_platform_fee',
pricing_mode: 'percent_of_revenue',
cost_model: 'platform_fee',
},
],
])
const feeVersions: CostVersionRow[] = [
{
id: 904,
cost_item_id: 20,
scope_type: 'global',
canonical_product_id: null,
work_product_rule_id: null,
amount_fen: 0,
rate_bps: 300,
effective_from: '2026-09-01T00:00:00+08:00',
},
]
const noRefund = resolveGenericCosts(
item({ revenue_fen: 10000, refund_fen: 0 }),
[...feeItems.values()],
feeVersions,
new Map([[20, 'percent_of_revenue']]),
)
const withRefund = resolveGenericCosts(
item({ revenue_fen: 10000, refund_fen: 4000 }),
[...feeItems.values()],
feeVersions,
new Map([[20, 'percent_of_revenue']]),
)
assert.equal(noRefund[0]?.totalAmountFen, 300)
assert.equal(withRefund[0]?.totalAmountFen, 180)
})