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
@@ -27,7 +27,7 @@ export type ReconcileFulfillmentModesResult = {
* 统一履约判定(纯函数,供单测与文档对照)。
*
* 优先级固定:工单 → 自动发货任务 → 历史平台任务 → 人工。
* 核销码、provider、商品 carrier_type 都不参与判定。
* 核销码、provider、商品来源都不参与判定。
*/
export function classifyFulfillmentMode({
hasWorkOrder,
@@ -102,43 +102,6 @@ export async function decideOrderItemsFulfillmentModes(
[ids, String(reason || 'order_sync')],
)
// 已有的 open 成本异常必须跟着履约方式一起改,否则历史平台会继续显示成人工。
if ((result.rowCount || 0) > 0) {
await syncOpenCostExceptionCarrierTypes(ids)
}
return { updatedCount: result.rowCount || 0 }
}
export async function syncOpenCostExceptionCarrierTypes(
orderItemIds?: Array<number | string>,
): Promise<{ updatedCount: number }> {
const ids = Array.isArray(orderItemIds)
? Array.from(
new Set(
orderItemIds
.map((value) => Number(value))
.filter((value) => Number.isInteger(value) && value > 0),
),
)
: null
if (ids && ids.length === 0) return { updatedCount: 0 }
const result = await query(
`
UPDATE cost_exceptions ce
SET carrier_type = oi.fulfillment_mode
FROM order_items oi
WHERE ce.subject_type = 'order_item'
AND ce.order_item_id = oi.id
AND ce.status = 'open'
AND oi.fulfillment_mode IS NOT NULL
AND ce.carrier_type IS DISTINCT FROM oi.fulfillment_mode
${ids ? 'AND oi.id = ANY($1::bigint[])' : ''}
`,
ids ? [ids] : [],
)
return { updatedCount: result.rowCount || 0 }
}