fix(pipeline): 变更按业务日回填,异常自动收口,载体管道加游标

- 渠道判定/成本快照返回受影响业务日,任务按日重算事实与总账
- 人工配置或载体补齐后,manual_config_missing / carrier_missing 自动关闭
- carrier_dispatch 使用水位游标,避免每轮从 id=0 重扫
This commit is contained in:
yml2213
2026-10-31 20:41:43 +08:00
parent 42d5874411
commit a62f6e1ec8
5 changed files with 244 additions and 12 deletions
@@ -175,6 +175,37 @@ export async function listExceptionQueue(input: {
*
* 批量版本:一次处理一批订单项,避免逐条 UPDATE。
*/
export async function resolveExceptionsForItemsByReason(
orderItemIds: number[],
reason: string,
): Promise<number> {
const ids = orderItemIds.filter((id) => Number.isInteger(id) && id > 0)
if (ids.length === 0) return 0
const now = nowIso()
const result = await query<{ id: number }>(
`
WITH removed AS (
DELETE FROM cost_exception_items i
USING cost_exceptions ce
WHERE i.exception_id = ce.id
AND i.order_item_id = ANY($1::bigint[])
AND ce.reason = $3
RETURNING i.exception_id
)
UPDATE cost_exceptions ce
SET status = 'resolved', resolved_at = $2, item_count = 0, last_seen_at = $2
WHERE ce.id IN (SELECT DISTINCT exception_id FROM removed)
AND ce.status <> 'resolved'
AND NOT EXISTS (
SELECT 1 FROM cost_exception_items i WHERE i.exception_id = ce.id
)
RETURNING ce.id
`,
[ids, now, reason],
)
return result.rowCount || 0
}
export async function resolveExceptionsForItems(orderItemIds: number[]): Promise<number> {
const ids = orderItemIds.filter((id) => Number.isInteger(id) && id > 0)
if (ids.length === 0) return 0
@@ -1,4 +1,5 @@
import { query, withTransaction } from '../../db/client.js'
import { resolveExceptionsForItemsByReason } from './cost-exception-service.js'
import type { JsonObject } from '../../types/json.js'
import { createHttpError } from '../../utils/http.js'
import { formatShanghaiDate, shiftDate } from '../../utils/finance-date-range.js'
@@ -424,7 +425,7 @@ export async function rematchManualFulfillments(input: Record<string, unknown> =
const dateTo = normalizeOptionalDate(input.dateTo ?? input.date_to, '结束日期')
const now = nowIso()
const result = await query<{ updated_count: number }>(
const result = await query<{ id: number }>(
`
WITH target AS (
SELECT
@@ -454,11 +455,17 @@ export async function rematchManualFulfillments(input: Record<string, unknown> =
updated_at = $5
FROM target
WHERE oi.id = target.order_item_id
RETURNING 1
RETURNING oi.id
`,
[skuKey, onlyUnmatched, dateFrom, dateTo, now],
)
// 配置补上以后,「人工 SKU 未配置」异常要立即收口,不能等人工去点。
const updatedIds = result.rows.map((row) => Number(row.id))
if (updatedIds.length > 0) {
await resolveExceptionsForItemsByReason(updatedIds, 'manual_config_missing')
}
// 配置生效后,把 SKU 对应的成本版本挂上(若已配置商品)。
await attachCostVersionsForManualConfigs()