feat(finance): 商品渠道能力、自动发货转人工与成本异常收口

- 商品可同时支持自动发货/人工,新增渠道能力与成本冲销字段
- 卡券缺商品时按内部中文 SKU 自动建 tpv: 商品并补渠道规则
- 自动发货失败转人工并冲销原渠道成本
- 配置成本后按最早缺失订单生效并立即补算、关闭异常
- tpv: 商品来源计入第三方卡券
This commit is contained in:
yml2213
2026-11-02 13:17:15 +08:00
parent 86b5347e40
commit 3c4c81003f
16 changed files with 532 additions and 34 deletions
@@ -106,11 +106,12 @@ export const WORK_ORDER_TEMPLATE_COST_BASELINE = '2026-07-10T00:00:00+08:00'
*/
export const PRODUCT_SOURCE_PREFIXES: Array<{
prefix: string
source: 'affiliate_dash' | 'work_order' | 'manual'
source: 'affiliate_dash' | 'work_order' | 'third_party_voucher' | 'manual'
}> = [
{ prefix: 'aff:', source: 'affiliate_dash' },
{ prefix: 'tpl:', source: 'work_order' },
{ prefix: 'wo:', source: 'work_order' },
{ prefix: 'tpv:', source: 'third_party_voucher' },
{ prefix: 'man:', source: 'manual' },
]
@@ -298,3 +298,10 @@ test('成本模型与商品来源必须覆盖第三方卡券渠道', () => {
assert.ok(productSourceFilterSql(1).includes("'third_party_voucher'"))
assert.ok(productSourceFilterSql(1).includes("'tpv:%'"))
})
test('tpv: 商品来源归为第三方卡券', async () => {
const { productSourceOf } = await import('./cost-center-mappers.js')
const assert = (await import('node:assert/strict')).default
assert.equal(productSourceOf('tpv:模拟卡券'), 'third_party_voucher')
assert.equal(productSourceOf('man:模拟人工'), 'manual')
})
@@ -295,6 +295,25 @@ export async function resolveExceptionsForItemsByReason(
return result.rowCount || 0
}
/**
* 清理“头还在、明细已空”的孤儿异常。
* 并发收口时可能出现明细已删但头未关闭,按 item 再查也找不到它。
*/
export async function resolveOrphanCostExceptions(): Promise<number> {
const result = await query<{ id: number }>(
`
UPDATE cost_exceptions ce
SET status = 'resolved', resolved_at = NOW(), item_count = 0, last_seen_at = NOW()
WHERE ce.status <> 'resolved'
AND NOT EXISTS (
SELECT 1 FROM cost_exception_items i WHERE i.exception_id = ce.id
)
RETURNING ce.id
`,
)
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
@@ -177,7 +177,7 @@ export async function createAdminCostSnapshot(payload: JsonObject = {}, _session
WHERE subject_type = 'order_item'
AND subject_id = $1
AND cost_item_id = $2
AND superseded_at IS NULL
AND superseded_at IS NULL AND reversed_at IS NULL
FOR UPDATE
`,
[orderItemId, line.costItemId],
@@ -194,7 +194,7 @@ export async function createAdminCostSnapshot(payload: JsonObject = {}, _session
continue
}
await client.query(
`UPDATE cost_snapshots SET superseded_at = $1 WHERE id = $2 AND superseded_at IS NULL`,
`UPDATE cost_snapshots SET superseded_at = $1 WHERE id = $2 AND superseded_at IS NULL AND reversed_at IS NULL`,
[now, current.id],
)
supersededCount += 1
@@ -212,7 +212,7 @@ export async function createAdminCostSnapshot(payload: JsonObject = {}, _session
$7, $8, $9, $10, 'resolved', $11::jsonb, $12, $13, $13
)
ON CONFLICT (subject_type, subject_id, cost_item_id)
WHERE superseded_at IS NULL
WHERE superseded_at IS NULL AND reversed_at IS NULL
DO NOTHING
RETURNING *
`,
@@ -4,6 +4,7 @@ import { nowIso } from '../../utils/time.js'
import { GENERIC_COST_MODELS } from './cost-center-constants.js'
import {
resolveExceptionsForItems,
resolveExceptionsForItemsByReason,
upsertAggregatedExceptionsBatch,
type AggregatedExceptionEntry,
} from './cost-exception-service.js'
@@ -371,6 +372,7 @@ async function processCostRows(
const inserts: Array<{ item: CostCandidateItem; resolution: NonNullable<CostResolution> }> = []
const supersedeIds: number[] = []
const missingEntries: AggregatedExceptionEntry[] = []
const resolvedMissingIds: number[] = []
for (const item of rows) {
const desired: Array<NonNullable<CostResolution>> = []
@@ -381,15 +383,25 @@ async function processCostRows(
if (channelResolution) {
desired.push(channelResolution)
} else if (code) {
result.missing += 1
// 已在队列里的缺失项不再重复写三条 SQL,等价格补齐后自然收口。
if (!openMissing.has(Number(item.id))) {
missingEntries.push({
reason: 'cost_missing',
scopeKey: `cost_missing|${item.fulfillment_channel}|${item.canonical_product_id}|${item.work_product_rule_id ?? '-'}`,
orderItemId: Number(item.id),
detail: { channel: item.fulfillment_channel },
})
// 历史版本可能已 retired,但订单已有不可变快照:这单不算缺成本,
// 否则“改价后旧订单快照还在、异常却重新冒出来”。
const channelCostItem = ctx.channelCostItems.get(code)
const hasActiveChannelSnapshot = channelCostItem
? existingMap.get(Number(item.id))?.has(Number(channelCostItem.id))
: false
if (hasActiveChannelSnapshot) {
resolvedMissingIds.push(Number(item.id))
} else {
result.missing += 1
// 已在队列里的缺失项不再重复写三条 SQL,等价格补齐后自然收口。
if (!openMissing.has(Number(item.id))) {
missingEntries.push({
reason: 'cost_missing',
scopeKey: `cost_missing|${item.fulfillment_channel}|${item.canonical_product_id}|${item.work_product_rule_id ?? '-'}`,
orderItemId: Number(item.id),
detail: { channel: item.fulfillment_channel },
})
}
}
}
@@ -429,6 +441,11 @@ async function processCostRows(
}
}
if (resolvedMissingIds.length > 0) {
await resolveExceptionsForItemsByReason(resolvedMissingIds, 'cost_missing').catch(
() => undefined,
)
}
if (missingEntries.length > 0) {
await upsertAggregatedExceptionsBatch(missingEntries)
}
@@ -583,7 +600,7 @@ async function loadActiveSnapshots(
`
SELECT id, order_item_id, cost_item_id, cost_version_id, total_amount_fen, snapshot_stage
FROM cost_snapshots
WHERE superseded_at IS NULL
WHERE superseded_at IS NULL AND reversed_at IS NULL
AND order_item_id = ANY($1::bigint[])
`,
[itemIds],
@@ -655,7 +672,7 @@ async function insertSnapshots(
product_id, quantity, unit_amount_fen, total_amount_fen, stage,
channel, basis_at
)
ON CONFLICT (subject_type, subject_id, cost_item_id) WHERE superseded_at IS NULL
ON CONFLICT (subject_type, subject_id, cost_item_id) WHERE superseded_at IS NULL AND reversed_at IS NULL
DO NOTHING
`,
[
@@ -18,6 +18,12 @@ import { resolveSkuNameQuantity } from '../product-match/pricing.js'
import { setAdminAffiliateProductPrice } from './affiliate-product-service.js'
import { resolvePriceEffectiveFrom } from './cost-center-normalizers.js'
import { ensureVoucherRuleForSku } from '../fulfillment/channel-assign-service.js'
import { syncCostSnapshotsForItems } from './cost-snapshot-service.js'
import {
resolveExceptionsForItemsByReason,
resolveOrphanCostExceptions,
} from './cost-exception-service.js'
import { orderItemBasisDates, refreshFactsForDates } from '../finance/refresh-facts.js'
import { syncAdminWorkProductRulePrice } from '../worker-platform/admin-worker-catalog-service.js'
import {
AFFILIATE_TASK_AGG_JOIN,
@@ -1372,7 +1378,7 @@ export async function setAdminProductPrice(
}
const now = nowIso()
const effectiveFrom = resolvePriceEffectiveFrom(payload)
let effectiveFrom = resolvePriceEffectiveFrom(payload)
const changeReason = String(payload.changeReason || '在商品与成本中心统一调整价格').trim()
// 2. 如果是接单模板商品 (tpl:)
@@ -1548,6 +1554,35 @@ export async function setAdminProductPrice(
)
const existing = existingResult.rows[0]
const existingTime = existing ? Date.parse(existing.effective_from) : null
// 首次配置/补价:如果这个商品还有未算成本的订单,默认从最早缺失订单当天生效,
// 否则当天早些时候的订单会因为“版本生效时间晚于下单时间”永远算不到成本。
effectiveFrom = resolvePriceEffectiveFrom(payload)
const requestedEffective = payload.effectiveFrom ?? payload.effective_from
if (!requestedEffective) {
const earliest = await query<{ basis_date: string | null }>(
`
SELECT MIN((COALESCE(o.paid_at, o.created_at) AT TIME ZONE 'Asia/Shanghai')::date)::text AS basis_date
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
WHERE oi.canonical_product_id = $1
AND oi.fulfillment_status = 'assigned'
AND NOT EXISTS (
SELECT 1 FROM cost_snapshots cs
WHERE cs.order_item_id = oi.id
AND cs.cost_item_id = $2
AND cs.superseded_at IS NULL
AND cs.reversed_at IS NULL
)
`,
[product.id, costItemId],
)
const earliestDate = String(earliest.rows[0]?.basis_date || '').trim()
const earliestTime = earliestDate ? Date.parse(`${earliestDate}T00:00:00+08:00`) : NaN
if (Number.isFinite(earliestTime) && (existingTime === null || earliestTime < existingTime)) {
effectiveFrom = `${earliestDate}T00:00:00+08:00`
}
}
const targetTime = Date.parse(effectiveFrom)
let costVersionId = existing?.id || 0
@@ -1606,6 +1641,59 @@ export async function setAdminProductPrice(
costVersionId = inserted.rows[0]?.id || 0
}
// 配置后立刻补算该商品下缺成本的订单,避免用户看到“已配置但异常还在”。
const missingItems = await query<{ id: number }>(
`
SELECT oi.id
FROM order_items oi
WHERE oi.canonical_product_id = $1
AND oi.fulfillment_status = 'assigned'
AND NOT EXISTS (
SELECT 1 FROM cost_snapshots cs
WHERE cs.order_item_id = oi.id
AND cs.cost_item_id = $2
AND cs.superseded_at IS NULL
AND cs.reversed_at IS NULL
)
ORDER BY oi.id
LIMIT 2000
`,
[product.id, costItemId],
)
if (missingItems.rows.length > 0) {
const ids = missingItems.rows.map((row) => Number(row.id))
await syncCostSnapshotsForItems(ids).catch(() => undefined)
const dates = await orderItemBasisDates(ids)
await refreshFactsForDates(dates).catch(() => undefined)
}
// 已有快照但异常还挂着的(并发/历史遗留),保存成本时一并收口。
const coveredItems = await query<{ id: number }>(
`
SELECT oi.id
FROM order_items oi
WHERE oi.canonical_product_id = $1
AND oi.fulfillment_status = 'assigned'
AND EXISTS (
SELECT 1 FROM cost_snapshots cs
WHERE cs.order_item_id = oi.id
AND cs.cost_item_id = $2
AND cs.superseded_at IS NULL
AND cs.reversed_at IS NULL
)
ORDER BY oi.id
LIMIT 2000
`,
[product.id, costItemId],
)
if (coveredItems.rows.length > 0) {
await resolveExceptionsForItemsByReason(
coveredItems.rows.map((row) => Number(row.id)),
'cost_missing',
).catch(() => undefined)
}
await resolveOrphanCostExceptions().catch(() => undefined)
return {
success: true,
product,
@@ -1,7 +1,11 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { buildMappingIndex, resolveProductId } from './product-normalize-service.js'
import {
buildMappingIndex,
resolveProductId,
voucherCanonicalSku,
} from './product-normalize-service.js'
const mappings = [
{
@@ -134,3 +138,24 @@ test('商品归一:第三方卡券按 tpv:SKU 直接命中', () => {
41,
)
})
test('三方卡券自动建商品时优先使用内部中文 SKU', () => {
assert.equal(
voucherCanonicalSku({
manual_sku_key: '11月改名卡',
sku_name: '11月改名卡',
sku_code: '192592489694733',
}),
'tpv:11月改名卡',
)
assert.equal(
voucherCanonicalSku({
manual_sku_key: '192592489694733',
sku_name: '11月改名卡',
sku_code: '192592489694733',
}),
'tpv:11月改名卡',
)
assert.equal(voucherCanonicalSku({ manual_sku_key: '', sku_name: '', sku_code: 'x' }), 'tpv:x')
assert.equal(voucherCanonicalSku({}), null)
})
@@ -5,7 +5,8 @@ import { readWatermarkItemId } from '../watermark-store.js'
/**
* 管道 B:商品归一。
*
* 只写 `order_items.canonical_product_id`,不建商品、不改渠道、不碰成本。
* 只写 `order_items.canonical_product_id`,不改渠道、不碰成本;
* 例外:三方卡券缺商品时按内部中文 SKU 自动建 `tpv:` 商品并补渠道规则。
*
* 解析顺序(与旧的四条回填路径统一):
* 1. 自动发货渠道:`aff:<sku_code>` 商品直接命中(该渠道的商品就是 SKU 本身);
@@ -43,6 +44,7 @@ type ItemRow = {
shop_id: string
sku_code: string
sku_name: string
manual_sku_key: string | null
normalized_sku_name: string | null
normalized_sku_series: string | null
fulfillment_channel: string | null
@@ -110,6 +112,94 @@ export function buildMappingIndex(
return index
}
/** 三方卡券的内部 SKU:优先中文,其次名称/平台码。 */
export function pickVoucherInternalSku(item: {
manual_sku_key?: string | null
sku_name?: string | null
sku_code?: string | null
}): string {
const candidates = [item.manual_sku_key, item.sku_name, item.sku_code]
.map((value) => String(value || '').trim())
.filter(Boolean)
return candidates.find((value) => /[\u4e00-\u9fff]/.test(value)) || candidates[0] || ''
}
export function voucherCanonicalSku(item: {
manual_sku_key?: string | null
sku_name?: string | null
sku_code?: string | null
}): string | null {
const internal = pickVoucherInternalSku(item)
return internal ? `tpv:${internal}` : null
}
/**
* 三方卡券没有预先建库时,按内部 SKU 自动建 `tpv:` 商品并补渠道规则。
* 建完后该商品会出现在「商品与成本中心 → 第三方卡券」,运营直接设采购价。
*/
async function ensureVoucherProductForItem(item: ItemRow): Promise<number | null> {
const canonicalSku = voucherCanonicalSku(item)
const internalSku = pickVoucherInternalSku(item)
if (!canonicalSku || !internalSku) return null
const existing = await query<{ id: number; status: string }>(
'SELECT id, status FROM product_catalog WHERE canonical_sku = $1 LIMIT 1',
[canonicalSku],
)
let productId = Number(existing.rows[0]?.id || 0)
if (!productId) {
const created = await query<{ id: number }>(
`
INSERT INTO product_catalog (
canonical_sku, canonical_name, category, unit_name, status,
attributes_json, created_at, updated_at
)
VALUES ($1, $2, '卡券', '张', 'active', '{"source":"third_party_voucher","auto_created":true}'::jsonb, NOW(), NOW())
ON CONFLICT (canonical_sku) DO NOTHING
RETURNING id
`,
[canonicalSku, internalSku],
)
productId = Number(created.rows[0]?.id || 0)
if (!productId) {
const again = await query<{ id: number }>(
'SELECT id FROM product_catalog WHERE canonical_sku = $1 LIMIT 1',
[canonicalSku],
)
productId = Number(again.rows[0]?.id || 0)
}
}
if (!productId) return null
const current = await query<{ status: string }>(
'SELECT status FROM product_catalog WHERE id = $1',
[productId],
)
if (current.rows[0]?.status !== 'active') return null
await query(
'UPDATE order_items SET canonical_product_id = $2, updated_at = NOW() WHERE id = $1 AND canonical_product_id IS NULL',
[item.id, productId],
)
const skuCode = String(item.sku_code || '').trim()
if (skuCode) {
await query(
`
INSERT INTO sku_channel_rules (
provider, shop_id, match_key_type, match_key, channel, priority,
source, enabled, note, created_at, updated_at
)
VALUES ($1, $2, 'sku_code', $3, 'third_party_voucher', 10, 'voucher', TRUE,
'卡券订单自动建商品后生成', NOW(), NOW())
ON CONFLICT (provider, shop_id, match_key_type, match_key, channel) DO NOTHING
`,
[item.provider, item.shop_id, skuCode],
)
}
return productId
}
/** 纯函数:按索引解析商品,便于单测。 */
export function resolveProductId(item: ItemRow, index: MappingIndex): number | null {
const provider = String(item.provider || '').trim()
@@ -204,6 +294,7 @@ export async function normalizeOrderItemProducts(
o.shop_id,
oi.sku_code,
oi.sku_name,
oi.manual_sku_key,
oi.normalized_sku_name,
oi.normalized_sku_series,
oi.fulfillment_channel,
@@ -235,7 +326,10 @@ export async function normalizeOrderItemProducts(
const matched: Array<{ id: number; productId: number }> = []
for (const item of rows.rows) {
const productId = resolveProductId(item, index)
let productId = resolveProductId(item, index)
if (!productId && item.fulfillment_channel === 'third_party_voucher') {
productId = await ensureVoucherProductForItem(item)
}
if (!productId) {
result.unmatched += 1
await upsertAggregatedException({
@@ -338,6 +432,7 @@ export async function normalizeProductsForItems(itemIds: number[]): Promise<{
o.shop_id,
oi.sku_code,
oi.sku_name,
oi.manual_sku_key,
oi.normalized_sku_name,
oi.normalized_sku_series,
oi.fulfillment_channel,
@@ -357,7 +452,10 @@ export async function normalizeProductsForItems(itemIds: number[]): Promise<{
const matched: Array<{ id: number; productId: number }> = []
let unmatched = 0
for (const item of rows.rows) {
const productId = resolveProductId(item, index)
let productId = resolveProductId(item, index)
if (!productId && item.fulfillment_channel === 'third_party_voucher') {
productId = await ensureVoucherProductForItem(item)
}
if (!productId) {
unmatched += 1
await upsertAggregatedException({
@@ -451,7 +451,7 @@ export async function syncAdminWorkOrderTemplateCostVersions(ruleId?: number) {
JOIN rule_version rv ON rv.work_product_rule_id = wo.product_rule_id
WHERE cs.order_item_id = wo.order_item_id
AND cs.cost_item_id = rv.cost_item_id
AND cs.superseded_at IS NULL
AND cs.superseded_at IS NULL AND reversed_at IS NULL
AND cs.cost_version_id <> rv.cost_version_id
AND EXISTS (
SELECT 1 FROM cost_versions gv