feat(cost-center): 卡券商品创建即生成渠道规则,补齐成本项预设

- 新建 tpv: 商品时自动写 third_party_voucher 规则,避免新卡券 SKU 停在待分类
- 成本项预设新增「第三方卡券采购成本」
This commit is contained in:
yml2213
2026-11-01 14:46:14 +08:00
parent 624c0ef4af
commit fca836eb4b
3 changed files with 32 additions and 0 deletions
@@ -16,6 +16,7 @@ import { nowIso } from '../../utils/time.js'
import { normalizeMatchText, normalizeSkuSeriesEquivalence } from '../product-match/normalize.js'
import { resolveSkuNameQuantity } from '../product-match/pricing.js'
import { setAdminAffiliateProductPrice } from './affiliate-product-service.js'
import { ensureVoucherRuleForSku } from '../fulfillment/channel-assign-service.js'
import { syncAdminWorkProductRulePrice } from '../worker-platform/admin-worker-catalog-service.js'
import {
AFFILIATE_TASK_AGG_JOIN,
@@ -345,6 +346,10 @@ export async function createAdminProduct(payload: JsonObject, _session: AdminSes
errorCode: 'cost_product_sku_exists',
})
}
// 卡券没有载体证据,创建商品时顺手把渠道规则补上,避免新 SKU 停在待分类。
if (canonicalSku.startsWith('tpv:')) {
await ensureVoucherRuleForSku(canonicalSku.slice(4))
}
return { product: mapProduct(inserted) }
}
@@ -553,6 +553,25 @@ export const CHANNEL_RULE_KEY_TYPES = MATCH_KEY_TYPES
export const CHANNEL_RULE_SOURCES = RULE_SOURCES
export const CHANNEL_VALUES = FULFILLMENT_CHANNELS
/** 为单个卡券 SKU 建规则(商品创建时调用,幂等)。 */
export async function ensureVoucherRuleForSku(skuCode: string): Promise<boolean> {
const key = String(skuCode || '').trim()
if (!key) return false
const result = 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
)
VALUES ('', '', 'sku_code', $1, 'third_party_voucher', 70, 'voucher', TRUE, '卡券商品自动生成', NOW(), NOW())
ON CONFLICT (provider, shop_id, match_key_type, match_key, channel) DO NOTHING
RETURNING id
`,
[key],
)
return (result.rowCount || 0) > 0
}
/**
* 为卡券商品(tpv: 前缀)批量补渠道规则。
*
@@ -171,6 +171,14 @@ export const COST_ITEM_PRESETS: CostItemPreset[] = [
pricingMode: 'per_item',
unitName: '件',
},
{
key: 'third_party_voucher_cost',
label: '第三方卡券采购成本',
name: '第三方卡券采购成本',
costModel: 'third_party_voucher_cost',
pricingMode: 'per_item',
unitName: '件',
},
{
key: 'work_order_reward',
label: '接单平台打手报酬',