refactor(finance): 成本模型与履约方式正交,拆分成本中心巨型文件

后端:
- 删除 services/admin/admin-cost-center-service.ts(4,248 行),拆为
  services/cost-center/ 下 5 个模块:
  cost-center-shared(类型/枚举/归一化/映射/costModelScopeSql)、
  cost-center-overview(总览与选项)、
  product-catalog(商品与渠道映射)、
  template-bootstrap(模板引导、自动发货同步、工单证据回填)、
  cost-definition(成本项/版本/异常/预览/快照)
- cost_items.carrier_type / cost_type 改为单一 cost_model;专属模型只对
  对应履约方式生效,通用模型对所有履约方式生效
- cost_snapshots / cost_exceptions 删除载体列;按履约方式筛异常改用
  IN 半连接(248k 异常下 EXISTS 逐行回表 2.7s -> 170ms)
- sales_daily_facts.carrier_type -> fulfillment_mode
- 人工支出按 cost_model 归类;履约方式只读 order_items.fulfillment_mode
- 移除 syncOpenCostExceptionCarrierTypes:异常表已无载体列可同步

前端:
- AdminCostCenterPage.tsx(2,887 行 -> 18 行转发),拆为
  pages/admin/cost-center/ 下 8 个 Panel + shared + CostCenterContext
- CostCarrierType 删除;新增 FulfillmentMode / ProductSource / CostModel
- 销售毛利页 carrierType -> fulfillmentMode

同步补两个索引:idx_order_items_fulfillment_id(异常半连接)、
idx_work_categories_name(商品目录分类 lateral join)。
This commit is contained in:
yml2213
2026-10-23 16:06:25 +08:00
parent 5c549a2dc5
commit 63ae32d18c
33 changed files with 8107 additions and 7335 deletions
@@ -0,0 +1,202 @@
/**
* 成本中心前端共享层:类型、常量、标签与页面上下文。
*
* 口径(2026-10-23 重写):
* 1. 履约方式唯一真源是 order_items.fulfillment_mode,前端只读不改;
* 2. 商品来源由 canonical_sku 前缀推导,前端不再存「商品载体」字段;
* 3. 成本项用 cost_model 表达「成本怎么算」,没有「适用载体」这一列。
*/
import type { Dayjs } from 'dayjs'
import type {
ChannelProductMapping,
CostItem,
CostModel,
CostVersion,
ProductSource,
} from '@/types/admin'
/** 顶部来源切换:全部 / 自动发货 / 接单平台 / 人工。 */
export type CostScope = 'all' | 'affiliate_dash' | 'work_order' | 'manual'
export type CostItemPreset = {
key: string
label: string
name: string
costModel: CostModel
pricingMode: CostItem['pricingMode']
unitName: string
}
export type ProductFormValues = {
canonicalSku: string
canonicalName: string
category?: string
unitName?: string
status?: 'active' | 'archived'
}
export type MappingFormValues = {
provider: string
shopId?: string
platformSkuCode?: string
platformSkuName?: string
matchType?: ChannelProductMapping['matchType']
canonicalProductId: number
quantityMultiplier?: number
confidence?: 'exact' | 'rule' | 'manual'
source?: 'manual' | 'import' | 'rule'
enabled?: boolean
note?: string
}
export type CostItemFormValues = {
name: string
costModel: CostModel
pricingMode: CostItem['pricingMode']
unitName?: string
status?: 'active' | 'archived'
note?: string
}
export type CostVersionFormValues = {
costItemId: number
scopeType: CostVersion['scopeType']
provider?: string
shopId?: string
canonicalProductId?: number
workProductRuleId?: number
amountYuan?: number
rateBps?: number
effectiveFrom?: Dayjs
effectiveTo?: Dayjs
status?: 'draft' | 'active'
changeReason?: string
}
/** 成本模型:只表达「这笔成本怎么算」。 */
export const COST_MODEL_OPTIONS = [
{ value: 'affiliate_purchase', label: '上游采购成本' },
{ value: 'work_order_reward', label: '接单平台打手报酬' },
{ value: 'manual_actual', label: '人工客服实际支出' },
{ value: 'platform_fee', label: '平台服务费' },
{ value: 'payment_fee', label: '支付通道费' },
{ value: 'after_sales_loss', label: '售后损失' },
{ value: 'other', label: '其他' },
]
/** 商品来源:由 canonical_sku 前缀推导。 */
export const PRODUCT_SOURCE_OPTIONS = [
{ value: 'affiliate_dash', label: '自动发货' },
{ value: 'work_order', label: '接单平台' },
{ value: 'manual', label: '人工客服' },
]
export const PRODUCT_SOURCE_TAG_COLORS: Record<ProductSource, string> = {
affiliate_dash: 'green',
work_order: 'blue',
manual: 'orange',
'': 'default',
}
export const PRICING_OPTIONS = [
{ value: 'per_item', label: '按件' },
{ value: 'per_order', label: '按单' },
{ value: 'percent_of_revenue', label: '按收入比例' },
]
export const SCOPE_OPTIONS = [
{ value: 'global', label: '全局' },
{ value: 'provider', label: '按来源' },
{ value: 'shop', label: '按店铺' },
{ value: 'product', label: '按标准商品' },
{ value: 'rule', label: '按接单规则' },
]
export const PROVIDER_LABELS: Record<string, string> = {
'91kaquan': '91 卡券(快手)',
pdd: '拼多多',
douyin_mashida: '抖音码速达',
}
export const MAPPING_MATCH_TYPE_OPTIONS: Array<{
value: ChannelProductMapping['matchType']
label: string
}> = [
{ value: 'sku_code', label: 'SKU 编码(唯一)' },
{ value: 'sku_code_name', label: 'SKU 编码 + 精确名称' },
{ value: 'sku_code_series', label: 'SKU 编码 + 名称系列' },
{ value: 'sku_name_series', label: 'SKU 名称系列兜底' },
]
/** 常用成本项模板:避免每次手填编码/模型/计价方式。 */
export const COST_ITEM_PRESETS: CostItemPreset[] = [
{
key: 'affiliate_purchase',
label: '上游采购成本',
name: '自动发货上游采购成本',
costModel: 'affiliate_purchase',
pricingMode: 'per_item',
unitName: '件',
},
{
key: 'work_order_reward',
label: '接单平台打手报酬',
name: '接单平台打手报酬',
costModel: 'work_order_reward',
pricingMode: 'per_order',
unitName: '单',
},
{
key: 'manual_actual',
label: '人工客服实际支出',
name: '人工客服履约成本',
costModel: 'manual_actual',
pricingMode: 'per_order',
unitName: '单',
},
{
key: 'platform_fee',
label: '平台服务费',
name: '平台服务费',
costModel: 'platform_fee',
pricingMode: 'percent_of_revenue',
unitName: '单',
},
{
key: 'payment_fee',
label: '支付通道费',
name: '支付通道费',
costModel: 'payment_fee',
pricingMode: 'percent_of_revenue',
unitName: '单',
},
{
key: 'after_sales_loss',
label: '售后损失',
name: '售后损失',
costModel: 'after_sales_loss',
pricingMode: 'per_order',
unitName: '单',
},
]
export function labelOf(options: Array<{ value: string; label: string }>, value: string) {
return options.find((item) => item.value === value)?.label || value || '-'
}
export function mappingMatchTypeLabel(value: ChannelProductMapping['matchType']) {
return MAPPING_MATCH_TYPE_OPTIONS.find((item) => item.value === value)?.label || value || '-'
}
export function providerLabel(value: string) {
return PROVIDER_LABELS[value] || value || '-'
}
export function formatRate(rateBps: number) {
return `${((Number(rateBps || 0) || 0) / 100).toFixed(2)}%`
}
export function costModelLabel(value: string) {
return labelOf(COST_MODEL_OPTIONS, value)
}