feat(risk-control): 新增 IP/账号手动封禁,并收口财务同步的全表更新

- 风控:worker_risk_bans 表 + 前置封禁中间件(登录一并拦截)+ 后台列表/新增/解封
- 财务:成本快照补商品维度收口到本批 order_item_id,不再整表扫 899MB 的 order_items
- 性能:order_items 覆盖索引(迁移 110)、大厅 pageSize 上限 20、已读/上传接口限流
This commit is contained in:
yml2213
2026-10-30 20:15:14 +08:00
parent 1b170d3f45
commit fb485f7e36
17 changed files with 1373 additions and 18 deletions
@@ -164,6 +164,7 @@ export async function normalizeManualProducts(
// 回填订单项:按 id 分批,每批重新取日期参数,避免单条语句超过 15s。
let updatedItemCount = 0
let updatedSnapshotCount = 0
let lastId = 0
for (let batch = 0; batch < 40; batch += 1) {
const range = buildRangeClause(3)
@@ -197,26 +198,29 @@ export async function normalizeManualProducts(
updatedItemCount += rowCount
const ids = updated.rows as Array<{ id: number }>
lastId = Number(ids[ids.length - 1]?.id || lastId)
// 已有成本快照补商品维度:**只处理本批刚归一过的订单项**。
// 原来是无条件全表条件更新,要扫 900MB 的 order_items(堆 + 两个位图索引回表),
// 生产实测每次 15~16 秒、必然撞 statement_timeout,而且失败会把后半段
// (销售事实重算、影子总账同步)整条链跳过。收口到本批 id 后走 idx_cost_snapshots_item,
// 毫秒级完成,且结果完全一样(唯一会变的行就是这些刚补上商品的订单项)。
if (ids.length > 0) {
const updatedSnapshots = await query(
`
UPDATE cost_snapshots cs
SET canonical_product_id = oi.canonical_product_id
FROM order_items oi
WHERE oi.id = ANY($1::bigint[])
AND cs.order_item_id = oi.id
AND cs.canonical_product_id IS NULL
AND oi.canonical_product_id IS NOT NULL
`,
[ids.map((row) => Number(row.id))],
)
updatedSnapshotCount += updatedSnapshots.rowCount || 0
}
if (rowCount < NORMALIZE_BATCH_SIZE) break
}
// 已有实际成本快照补上商品维度,让毛利能按商品归集。
let updatedSnapshotCount = 0
if (updatedItemCount > 0) {
const updatedSnapshots = await query(
`
UPDATE cost_snapshots cs
SET canonical_product_id = oi.canonical_product_id
FROM order_items oi
WHERE cs.order_item_id = oi.id
AND cs.canonical_product_id IS NULL
AND oi.fulfillment_mode = 'manual'
AND oi.canonical_product_id IS NOT NULL
`,
)
updatedSnapshotCount = updatedSnapshots.rowCount || 0
}
let resolvedExceptionCount = 0
if (input.resolveExceptions !== false && updatedItemCount > 0) {
const resolved = await query(
@@ -0,0 +1,60 @@
import assert from 'node:assert/strict'
import test from 'node:test'
/**
* 2026-10-30 生产事故复盘:cost-finance-sync 每 15 分钟的「成本快照补商品维度」
* 原先是一条无条件全表更新,要扫 899MB 的 order_items,实测 15.2s / 16.5s 撞
* statement_timeout,并且失败会把后面的「销售事实重算 + 影子总账同步」整条跳过。
*
* 修复口径:只更新本批刚归一过的 order_item_id(走 idx_cost_snapshots_item),
* 结果完全一样但毫秒级完成。这里用源码断言把它钉住。
*/
test('成本快照补商品维度必须按本批 order_item_id 收口,不能再全表更新', async () => {
const { readFile } = await import('node:fs/promises')
const src = await readFile(
new URL('./manual-product-normalization-service.ts', import.meta.url),
'utf8',
)
assert.ok(
src.includes('oi.id = ANY($1::bigint[])'),
'成本快照更新缺少本批 id 收口:会退化成扫 900MB 的 order_items(实测 15 秒超时)',
)
assert.ok(
src.includes('AND cs.order_item_id = oi.id'),
'收口后仍要按 order_item_id 关联,不能改成按 subject_id 猜',
)
// 更新必须发生在分批循环内部(本批 ids 还在作用域里),不能在循环之后
const loopStart = src.indexOf('for (let batch = 0; batch < 40; batch += 1) {')
const loopEnd = src.indexOf('let resolvedExceptionCount = 0')
const snapshotUpdateAt = src.indexOf('UPDATE cost_snapshots cs')
assert.ok(loopStart > 0 && loopEnd > loopStart, '找不到归一的分批循环')
assert.ok(
snapshotUpdateAt > loopStart && snapshotUpdateAt < loopEnd,
'成本快照更新必须留在分批循环内:循环外拿不到本批 ids,就只能退回全表条件',
)
// 兜底:不允许再出现「无 id 限制 + fulfillment_mode 条件」的老写法
const legacyPattern = /UPDATE cost_snapshots cs[\s\S]{0,400}?oi\.fulfillment_mode = 'manual'/
assert.equal(
legacyPattern.test(src),
false,
'不能再按 fulfillment_mode 全表更新(会整表 bitmap 回表)',
)
})
test('迁移 110 提供 order_items(fulfillment_mode, canonical_product_id) 覆盖索引', async () => {
const { readFile } = await import('node:fs/promises')
const migration = await readFile(
new URL('../../db/migrations/110_order_items_fulfillment_product_index.sql', import.meta.url),
'utf8',
)
assert.ok(migration.includes('idx_order_items_fulfillment_product'))
assert.ok(migration.includes('(fulfillment_mode, canonical_product_id)'))
assert.ok(migration.includes('INCLUDE (id)'))
assert.ok(
!/^\s*CREATE INDEX CONCURRENTLY/m.test(migration),
'迁移在事务内执行,不能使用 CONCURRENTLY',
)
})