refactor(cost-center): 共享层按职责拆为四个模块
cost-center-shared.ts 804 行同时装枚举、行类型、映射、归一化,任何一处改动都会 让 8 个引用方重新编译。按职责拆开,本文件退化为统一出口(export *): - cost-center-constants.ts 131 枚举、常量、SQL 片段 - cost-center-types.ts 220 行结构与 DTO 类型 - cost-center-mappers.ts 250 行映射与口径判断(含 CostPreview) - cost-center-normalizers.ts 254 入参归一化与断言 同时消掉一个类型环:CostPreview 用 ReturnType<typeof mapProduct> 反推 mapper 的 返回类型,导致 types → mappers → types 互指;CostPreview 随 mapper 一起放进 mappers,依赖恢复单向。 出口保持向后兼容,8 个引用方无需改动;71 个导出逐一比对无缺失。 验证:typecheck / lint / 773 测试通过。
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
/**
|
||||
* 成本中心共享层 · 行映射与口径判断。
|
||||
*
|
||||
* 把数据库行映射成接口 DTO,并提供「成本模型是否适用于履约方式」这类口径函数。
|
||||
*/
|
||||
import { GENERIC_COST_MODELS, PRODUCT_SOURCE_PREFIXES } from './cost-center-constants.js'
|
||||
import type { FulfillmentMode } from './cost-center-constants.js'
|
||||
import { parseJsonObject } from './cost-center-normalizers.js'
|
||||
import type {
|
||||
CostExceptionDraft,
|
||||
CostExceptionRow,
|
||||
CostItemRow,
|
||||
CostLine,
|
||||
CostVersionRow,
|
||||
MappingRow,
|
||||
MissingCostItem,
|
||||
ProductRow,
|
||||
} from './cost-center-types.js'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 履约方式 / 商品来源 / 成本模型
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* 由 canonical_sku 前缀推导商品来源。
|
||||
* 返回空串表示未分类;未分类商品不允许反推履约方式。
|
||||
*/
|
||||
export function productSourceOf(canonicalSku: string): string {
|
||||
const sku = String(canonicalSku || '').trim()
|
||||
for (const item of PRODUCT_SOURCE_PREFIXES) {
|
||||
if (sku.startsWith(item.prefix)) return item.source
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
/** 商品来源筛选的 SQL 片段:按 canonical_sku 前缀过滤。 */
|
||||
export function productSourceFilterSql(paramIndex: number, column = 'p.canonical_sku'): string {
|
||||
return `(
|
||||
($${paramIndex} = 'affiliate_dash' AND ${column} LIKE 'aff:%')
|
||||
OR ($${paramIndex} = 'work_order' AND (${column} LIKE 'tpl:%' OR ${column} LIKE 'wo:%'))
|
||||
OR ($${paramIndex} = 'manual' AND ${column} LIKE 'man:%')
|
||||
)`
|
||||
}
|
||||
|
||||
/**
|
||||
* 成本模型是否适用于该履约方式。
|
||||
*
|
||||
* 专属模型只对对应履约方式生效;通用模型(平台费/支付费/售后损失/其他)
|
||||
* 对所有履约方式生效。
|
||||
*/
|
||||
export function costModelAppliesTo(costModel: string, fulfillmentMode: string) {
|
||||
if ((GENERIC_COST_MODELS as readonly string[]).includes(costModel)) return true
|
||||
if (costModel === 'affiliate_purchase') return fulfillmentMode === 'affiliate_dash'
|
||||
if (costModel === 'work_order_reward') return fulfillmentMode === 'work_order'
|
||||
if (costModel === 'manual_actual') return fulfillmentMode === 'manual'
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 成本模型适用范围的 SQL 片段。
|
||||
* 参数是「履约方式」的 SQL 表达式(可以是占位符 $n,也可以是列引用)。
|
||||
*/
|
||||
export function costModelScopeSql(fulfillmentModeExpression: string): string {
|
||||
return `(
|
||||
ci.cost_model IN (${GENERIC_COST_MODELS.map((model) => `'${model}'`).join(', ')})
|
||||
OR (ci.cost_model = 'affiliate_purchase'
|
||||
AND ${fulfillmentModeExpression} = 'affiliate_dash')
|
||||
OR (ci.cost_model = 'work_order_reward'
|
||||
AND ${fulfillmentModeExpression} = 'work_order')
|
||||
OR (ci.cost_model = 'manual_actual'
|
||||
AND ${fulfillmentModeExpression} = 'manual')
|
||||
)`
|
||||
}
|
||||
|
||||
/** 人工支出分类的默认推断:按成本模型归类。 */
|
||||
export function inferExpenseCategory(costModel: string | undefined) {
|
||||
if (costModel === 'affiliate_purchase') return 'goods_purchase'
|
||||
if (costModel === 'manual_actual') return 'service_fee'
|
||||
if (costModel === 'other') return 'freight'
|
||||
if (costModel === 'payment_fee' || costModel === 'platform_fee') return 'service_fee'
|
||||
return 'other'
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 行映射
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function mapProduct(row: ProductRow) {
|
||||
return {
|
||||
id: Number(row.id),
|
||||
canonicalSku: row.canonical_sku,
|
||||
canonicalName: row.canonical_name,
|
||||
category: row.category || '',
|
||||
source: productSourceOf(row.canonical_sku),
|
||||
rootCategoryId: row.root_category_id ? Number(row.root_category_id) : null,
|
||||
rootCategoryName: row.root_category_name || row.category || '',
|
||||
subCategoryId: row.sub_category_id ? Number(row.sub_category_id) : null,
|
||||
subCategoryName: row.sub_category_name || '',
|
||||
unitName: row.unit_name,
|
||||
status: row.status,
|
||||
attributes: parseJsonObject(row.attributes_json),
|
||||
mappingCount: Number(row.mapping_count || 0),
|
||||
costVersionCount: Number(row.cost_version_count || 0),
|
||||
affiliateTaskCount: Number(row.affiliate_task_count || 0),
|
||||
affiliateLastFulfilledAt: row.affiliate_last_fulfilled_at || null,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
export function mapMapping(row: MappingRow) {
|
||||
return {
|
||||
id: Number(row.id),
|
||||
provider: row.provider,
|
||||
shopId: row.shop_id,
|
||||
platformSkuCode: row.platform_sku_code,
|
||||
platformSkuName: row.platform_sku_name,
|
||||
platformSkuNameKey: row.platform_sku_name_key || '',
|
||||
matchType: row.match_type || 'sku_code',
|
||||
canonicalProductId: Number(row.canonical_product_id),
|
||||
canonicalSku: row.canonical_sku,
|
||||
canonicalName: row.canonical_name,
|
||||
quantityMultiplier: Number(row.quantity_multiplier),
|
||||
confidence: row.confidence,
|
||||
source: row.source,
|
||||
enabled: row.enabled !== false,
|
||||
note: row.note || '',
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
export function mapCostItem(row: CostItemRow) {
|
||||
return {
|
||||
id: Number(row.id),
|
||||
code: row.code,
|
||||
name: row.name,
|
||||
costModel: row.cost_model,
|
||||
pricingMode: row.pricing_mode,
|
||||
unitName: row.unit_name,
|
||||
currency: row.currency,
|
||||
status: row.status,
|
||||
note: row.note || '',
|
||||
versionCount: Number(row.version_count || 0),
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
export function mapCostVersion(row: CostVersionRow) {
|
||||
const revenueItemCount = Number(row.revenue_item_count || 0)
|
||||
const revenueFen = Number(row.revenue_fen || 0)
|
||||
const rewardAvgFen = Number(row.reward_avg_fen || 0)
|
||||
const rewardFen = Number(row.reward_fen || 0)
|
||||
const ruleUnitPriceFen = Number(row.rule_unit_price_fen || 0)
|
||||
const ruleRewardAmountFen = Number(row.rule_reward_amount || 0)
|
||||
const amountKind =
|
||||
row.cost_item_code === 'sys_work_order_reward' &&
|
||||
row.scope_type === 'rule' &&
|
||||
ruleUnitPriceFen > 0 &&
|
||||
ruleRewardAmountFen === 0
|
||||
? 'unit_price'
|
||||
: 'fixed'
|
||||
return {
|
||||
id: Number(row.id),
|
||||
costItemId: Number(row.cost_item_id),
|
||||
costItemCode: row.cost_item_code,
|
||||
costItemName: row.cost_item_name,
|
||||
costItemCostModel: row.cost_item_cost_model || 'other',
|
||||
pricingMode: row.pricing_mode,
|
||||
scopeType: row.scope_type,
|
||||
provider: row.provider || '',
|
||||
shopId: row.shop_id || '',
|
||||
canonicalProductId: row.canonical_product_id ? Number(row.canonical_product_id) : null,
|
||||
canonicalSku: row.canonical_sku || '',
|
||||
canonicalName: row.canonical_name || '',
|
||||
workProductRuleId: row.work_product_rule_id ? Number(row.work_product_rule_id) : null,
|
||||
workProductRuleName: row.work_product_rule_name || '',
|
||||
workProductRuleKey: row.work_product_rule_key || '',
|
||||
ruleRewardAmountFen,
|
||||
ruleUnitPriceFen,
|
||||
revenueItemCount,
|
||||
revenueFen,
|
||||
revenueAvgFen: revenueItemCount > 0 ? Math.round(revenueFen / revenueItemCount) : 0,
|
||||
rewardAvgFen,
|
||||
rewardFen,
|
||||
amountKind,
|
||||
isSystemPlaceholder:
|
||||
row.cost_item_code === 'sys_work_order_reward' &&
|
||||
row.scope_type === 'global' &&
|
||||
Number(row.amount_fen) === 0,
|
||||
amountFen: Number(row.amount_fen),
|
||||
rateBps: Number(row.rate_bps),
|
||||
effectiveFrom: row.effective_from,
|
||||
effectiveTo: row.effective_to,
|
||||
source: row.source,
|
||||
status: row.status,
|
||||
changeReason: row.change_reason || '',
|
||||
createdByAdminUserId: row.created_by_admin_user_id
|
||||
? Number(row.created_by_admin_user_id)
|
||||
: null,
|
||||
approvedByAdminUserId: row.approved_by_admin_user_id
|
||||
? Number(row.approved_by_admin_user_id)
|
||||
: null,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
export function mapCostException(row: CostExceptionRow) {
|
||||
return {
|
||||
id: Number(row.id),
|
||||
subjectType: row.subject_type,
|
||||
subjectId: Number(row.subject_id),
|
||||
orderItemId: row.order_item_id ? Number(row.order_item_id) : null,
|
||||
workOrderId: row.work_order_id ? Number(row.work_order_id) : null,
|
||||
fulfillmentMode: row.fulfillment_mode || '',
|
||||
reason: row.reason,
|
||||
status: row.status,
|
||||
detail: parseJsonObject(row.detail_json),
|
||||
createdAt: row.created_at,
|
||||
resolvedAt: row.resolved_at,
|
||||
resolvedByAdminUserId: row.resolved_by_admin_user_id
|
||||
? Number(row.resolved_by_admin_user_id)
|
||||
: null,
|
||||
resolutionNote: row.resolution_note || '',
|
||||
}
|
||||
}
|
||||
|
||||
export type CostPreview = {
|
||||
context: {
|
||||
orderItemId: number | null
|
||||
orderId: number | null
|
||||
provider: string
|
||||
shopId: string
|
||||
skuCode: string
|
||||
skuName: string
|
||||
fulfillmentMode: FulfillmentMode
|
||||
quantity: number
|
||||
normalizedQuantity: number
|
||||
revenueFen: number
|
||||
workOrderId: number | null
|
||||
workProductRuleId: number | null
|
||||
}
|
||||
mapping: ReturnType<typeof mapMapping> | null
|
||||
product: ReturnType<typeof mapProduct> | null
|
||||
lines: CostLine[]
|
||||
missingCostItems: MissingCostItem[]
|
||||
totalCostFen: number
|
||||
exceptions: CostExceptionDraft[]
|
||||
}
|
||||
Reference in New Issue
Block a user