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,259 @@
/**
* 成本中心外壳:顶部来源切换、总览统计与 Tab 定义。
*
* 各 Tab 由独立 Panel 组件承载,自己取数、自己维护表单与弹窗,
* 避免单文件堆积成不可维护的巨型页面。
*/
import { ReloadOutlined } from '@ant-design/icons'
import { Alert, Button, Card, Col, Row, Statistic, Tabs, Tag, Typography } from 'antd'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { useEffect } from 'react'
import PageHeader from '@/components/admin/PageHeader'
import { usePersistedState } from '@/hooks/usePersistedState'
import { fetchAdminCostCenterOptions, fetchAdminCostCenterOverview } from '@/services/admin'
import { hasAdminPermission } from '@/utils/admin-auth'
import { AffiliateSkuPanel } from './AffiliateSkuPanel'
import { ChannelMappingPanel } from './ChannelMappingPanel'
import { CostExceptionPanel } from './CostExceptionPanel'
import { CostItemPanel } from './CostItemPanel'
import { CostPreviewPanel } from './CostPreviewPanel'
import { CostVersionPanel } from './CostVersionPanel'
import { ProductCatalogPanel } from './ProductCatalogPanel'
import { CostCenterContext } from './CostCenterContext'
import type { CostScope } from './shared'
const SCOPES: Array<{ key: CostScope; label: string; hint: string }> = [
{
key: 'all',
label: '全部',
hint: '不区分来源,查看全部商品、映射、成本项与版本。',
},
{
key: 'affiliate_dash',
label: '自动发货',
hint: '只按上游货源 SKU 配采购成本,不维护商品目录和渠道映射。',
},
{
key: 'work_order',
label: '接单平台',
hint: '需要打手接单履约的商品;成本以打手报酬为主,可按接单规则或商品配价。',
},
{
key: 'manual',
label: '人工客服',
hint: '客服手工处理的商品;成本按人工实际支出逐单登记。',
},
]
const SCOPE_TAG_COLORS: Record<CostScope, string> = {
all: 'default',
affiliate_dash: 'green',
work_order: 'blue',
manual: 'orange',
}
function tabKeysForScope(scope: CostScope) {
return [
...(scope === 'all' || scope === 'work_order' ? ['products', 'mappings'] : []),
...(scope === 'affiliate_dash' ? ['affiliate-skus'] : []),
'cost-items',
'cost-versions',
'exceptions',
'preview',
]
}
export default function CostCenterShell() {
const queryClient = useQueryClient()
const canConfig = hasAdminPermission('finance.config')
const [scope, setScope] = usePersistedState<CostScope>(
'admin-cost-center:scope',
'all',
(value): value is CostScope => SCOPES.some((item) => item.key === value),
)
const [activeTab, setActiveTab] = usePersistedState<string>('admin-cost-center:tab', 'products')
const overviewQuery = useQuery({
queryKey: ['admin-cost-center-overview'],
queryFn: fetchAdminCostCenterOverview,
})
const optionsQuery = useQuery({
queryKey: ['admin-cost-center-options'],
queryFn: fetchAdminCostCenterOptions,
})
const overview = overviewQuery.data?.data
const options = optionsQuery.data?.data
const visibleTabKeys = tabKeysForScope(scope)
useEffect(() => {
if (!visibleTabKeys.includes(activeTab)) {
setActiveTab(visibleTabKeys[0] ?? 'cost-items')
}
}, [activeTab, setActiveTab, visibleTabKeys])
function refreshAll() {
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-overview'] })
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-options'] })
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-products'] })
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-mappings'] })
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-cost-items'] })
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-cost-versions'] })
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-exceptions'] })
}
const scopeLabel =
scope === 'all' ? '全部' : (SCOPES.find((item) => item.key === scope)?.label ?? '')
const productSourceFilter = scope === 'all' ? undefined : scope
const fulfillmentModeFilter =
scope === 'all' ? undefined : (scope as 'affiliate_dash' | 'work_order' | 'manual')
return (
<CostCenterContext.Provider
value={{
canConfig,
scope,
scopeLabel,
productSourceFilter,
fulfillmentModeFilter,
overview,
options,
products: [],
mappingGroups: [],
costItems: [],
versions: [],
exceptions: [],
productsLoading: false,
mappingsLoading: false,
costItemsLoading: false,
versionsLoading: false,
exceptionsLoading: false,
refreshAll,
}}
>
<section className="page-stack">
<PageHeader
title="商品与成本中心"
description="先按来源(自动发货 / 接单平台 / 人工)分区,再把商品 SKU 归一、成本金额收口到版本;历史成本靠快照固化。"
extra={
<Button
icon={<ReloadOutlined />}
onClick={refreshAll}
loading={overviewQuery.isFetching}
>
刷新
</Button>
}
/>
<Row gutter={[16, 16]}>
{[
['标准商品', overview?.productCount || 0],
['渠道映射', overview?.mappingCount || 0],
['成本项目', overview?.costItemCount || 0],
['生效版本', overview?.activeVersionCount || 0],
['待处理异常', overview?.openExceptionCount || 0],
['未映射订单项', overview?.unmappedOrderItemCount || 0],
].map(([label, value]) => (
<Col xs={12} md={8} xl={4} key={label}>
<Card>
<Statistic title={label} value={value as number} />
</Card>
</Col>
))}
</Row>
<Card className="cost-center-carrier-card">
<Tabs
activeKey={scope}
onChange={(key) => {
const next = key as CostScope
setScope(next)
setActiveTab(
next === 'all' || next === 'work_order'
? 'products'
: next === 'affiliate_dash'
? 'affiliate-skus'
: 'cost-items',
)
}}
items={SCOPES.map((item) => {
const productCount =
item.key === 'all'
? overview?.productCount
: overview?.productSourceCounts?.[item.key]
const costItemCount = item.key === 'all' ? overview?.costItemCount : undefined
return {
key: item.key,
label: (
<span className="cost-center-scope-tab">
<span>{item.label}</span>
<Tag color={SCOPE_TAG_COLORS[item.key]}>
{Number(productCount || 0)} 商品
{costItemCount === undefined ? '' : ` / ${Number(costItemCount)} 成本项`}
</Tag>
</span>
),
children: <Typography.Text type="secondary">{item.hint}</Typography.Text>,
}
})}
/>
</Card>
{!canConfig ? (
<Alert
type="info"
showIcon
message="当前账号为只读权限"
description="可以查看商品、映射、成本版本和异常;新增/修改需要 finance.config 权限。"
/>
) : null}
<Tabs
activeKey={activeTab}
onChange={setActiveTab}
items={[
{
key: 'products',
label: '标准商品',
children: <ProductCatalogPanel />,
},
{
key: 'mappings',
label: '渠道映射',
children: <ChannelMappingPanel />,
},
{
key: 'affiliate-skus',
label: '自动发货 SKU',
children: <AffiliateSkuPanel />,
},
{
key: 'cost-items',
label: '成本项目',
children: <CostItemPanel />,
},
{
key: 'cost-versions',
label: '成本版本',
children: <CostVersionPanel />,
},
{
key: 'exceptions',
label: '成本异常',
children: <CostExceptionPanel />,
},
{
key: 'preview',
label: '成本预览',
children: <CostPreviewPanel />,
},
].filter((item) => visibleTabKeys.includes(item.key))}
/>
</section>
</CostCenterContext.Provider>
)
}