refactor(finance)!: 事实维度与接口字段统一改名 channel

- migration 114 将 sales_daily_facts.fulfillment_mode 重命名为 channel
- DTO/查询参数/前端类型从 fulfillmentMode 统一为 channel
- 成本中心类型 FulfillmentMode 更名为 FulfillmentChannel,并补齐卡券/待分类/归档取值
- 销售事实响应分组改为 byChannel
This commit is contained in:
yml2213
2026-10-31 20:26:15 +08:00
parent 657e49f324
commit 6cd6401eca
38 changed files with 220 additions and 208 deletions
@@ -14,7 +14,7 @@ import type { AdminSession } from '../admin/admin-auth-service.js'
import { normalizeMatchText, normalizeSkuSeriesEquivalence } from '../product-match/normalize.js'
import { resolveSkuNameQuantity } from '../product-match/pricing.js'
import {
FULFILLMENT_MODES,
FULFILLMENT_CHANNELS,
costModelScopeSql,
mapCostVersion,
mapMapping,
@@ -32,7 +32,7 @@ import type {
CostPreview,
CostResolution,
CostVersionRow,
FulfillmentMode,
FulfillmentChannel,
MappingRow,
MissingCostItem,
OrderItemCostContext,
@@ -49,7 +49,7 @@ export async function previewAdminCost(payload: JsonObject = {}) {
? await resolveCostLines({
provider: context.provider,
shopId: context.shopId,
fulfillmentMode: context.fulfillmentMode,
channel: context.channel,
canonicalProductId: productId,
workProductRuleId: context.workProductRuleId,
quantity: context.normalizedQuantity,
@@ -76,22 +76,22 @@ export async function previewAdminCost(payload: JsonObject = {}) {
.map((item) => `${item.costItemName}(${item.costItemCode})`)
.join('、')}`,
detail: {
fulfillmentMode: context.fulfillmentMode,
channel: context.channel,
costItemIds: resolution.missingCostItems.map((item) => item.costItemId),
},
})
} else if (resolution.applicableCostItemCount === 0) {
exceptions.push({
reason: 'cost_missing',
message: `没有配置适用于「${context.fulfillmentMode}」的成本项`,
detail: { fulfillmentMode: context.fulfillmentMode },
message: `没有配置适用于「${context.channel}」的成本项`,
detail: { channel: context.channel },
})
}
// 人工履约的成本来自「人工 SKU 配置的消耗积分 × 平台换算率」,版本金额只是占位;
// 这里单独给出提示,并从成本行里剔掉 0 元占位,避免预览与实际扣款对不上。
const manualCost =
context.fulfillmentMode === 'manual' ? await resolveManualCostHint(context.orderItemId) : null
context.channel === 'manual' ? await resolveManualCostHint(context.orderItemId) : null
const lines = manualCost
? resolution.lines.filter((line) => line.costItemCode !== 'sys_manual_service')
: resolution.lines
@@ -233,7 +233,7 @@ export async function createAdminCostSnapshot(payload: JsonObject = {}, _session
provider: preview.context.provider,
shopId: preview.context.shopId,
normalizedQuantity: preview.context.normalizedQuantity,
fulfillmentMode: preview.context.fulfillmentMode,
channel: preview.context.channel,
basisAt: preview.context.basisAt,
}),
preview.context.basisAt,
@@ -296,7 +296,7 @@ async function resolveCostContext(payload: JsonObject): Promise<OrderItemCostCon
basis_at: string
work_order_id: number | null
work_product_rule_id: number | null
fulfillment_mode: string | null
channel: string | null
}>(
`
SELECT
@@ -315,7 +315,7 @@ async function resolveCostContext(payload: JsonObject): Promise<OrderItemCostCon
COALESCE(o.paid_at, o.created_at) AS basis_at,
wo.id AS work_order_id,
wo.product_rule_id AS work_product_rule_id,
COALESCE(oi.fulfillment_channel, CASE WHEN oi.fulfillment_status = 'archived' THEN 'archived' ELSE 'pending' END) AS fulfillment_mode
COALESCE(oi.fulfillment_channel, CASE WHEN oi.fulfillment_status = 'archived' THEN 'archived' ELSE 'pending' END) AS channel
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
LEFT JOIN LATERAL (
@@ -358,7 +358,7 @@ async function resolveCostContext(payload: JsonObject): Promise<OrderItemCostCon
basisAt: new Date(row.basis_at).toISOString(),
workOrderId: row.work_order_id ? Number(row.work_order_id) : null,
workProductRuleId: row.work_product_rule_id ? Number(row.work_product_rule_id) : null,
fulfillmentMode: resolveFulfillmentMode(row.fulfillment_mode),
channel: resolveFulfillmentMode(row.channel),
}
}
@@ -366,9 +366,7 @@ async function resolveCostContext(payload: JsonObject): Promise<OrderItemCostCon
const skuCode = normalizeRequiredText(payload.skuCode ?? payload.sku_code, '平台 SKU', 120)
const quantity = Math.max(1, Math.trunc(Number(payload.quantity) || 1))
const skuName = normalizeOptionalText(payload.skuName ?? payload.sku_name, 200)
const fulfillmentMode = resolveFulfillmentMode(
payload.fulfillmentMode ?? payload.fulfillment_mode,
)
const channel = resolveFulfillmentMode(payload.channel ?? payload.channel)
return {
orderItemId: 0,
orderId: 0,
@@ -390,7 +388,7 @@ async function resolveCostContext(payload: JsonObject): Promise<OrderItemCostCon
workProductRuleId: normalizeOptionalId(
payload.workProductRuleId ?? payload.work_product_rule_id,
),
fulfillmentMode,
channel,
}
}
@@ -398,9 +396,9 @@ async function resolveCostContext(payload: JsonObject): Promise<OrderItemCostCon
* 履约渠道只读 order_items.fulfillment_channel / fulfillment_status。
* 订单项还没判定时按人工处理,不允许用 provider 或核销码兜底。
*/
function resolveFulfillmentMode(value: unknown): FulfillmentMode {
function resolveFulfillmentMode(value: unknown): FulfillmentChannel {
const normalized = text(value)
if (normalized && FULFILLMENT_MODES.has(normalized)) return normalized as FulfillmentMode
if (normalized && FULFILLMENT_CHANNELS.has(normalized)) return normalized as FulfillmentChannel
return 'manual'
}
@@ -455,7 +453,7 @@ async function findMappingForContext(context: OrderItemCostContext) {
async function resolveCostLines(input: {
provider: string
shopId: string
fulfillmentMode: FulfillmentMode
channel: FulfillmentChannel
canonicalProductId: number
workProductRuleId: number | null
quantity: number
@@ -514,7 +512,7 @@ WHERE ci.status = 'active'
input.shopId,
input.canonicalProductId,
input.workProductRuleId,
input.fulfillmentMode,
input.channel,
],
)