refactor(cost-center): 精简成本中心,人工成本以 SKU 配置为唯一真源
- 删除「成本项目」「成本版本」「成本预览」三个 Tab:成本项只有 3 条系统内置,版本金额只是快照挂载点 - 新增只读「发单价格」面板,仅在接单平台来源显示,改价仍走接单模板后同步 - 成本预览改为弹窗,从成本异常行和订单详情「查成本」进入;人工单显示 SKU 配置成本并禁止生成快照 - 成本异常新增按原因聚合摘要接口,列表支持 reason/since 筛选,默认只看近 24 小时 - 迁移 111:把 sys_manual_service 的历史 ¥88 全局版本清零;预估同步与成本缺失判定排除 manual_actual
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
/**
|
||||
* 成本解析弹窗:按订单项查看「映射到的商品 + 命中的成本版本 + 异常」。
|
||||
*
|
||||
* 从「成本异常」行和「订单详情 - 商品明细」进入;
|
||||
* 人工履约不回版本金额,而是回人工 SKU 配置的消耗积分与换算率。
|
||||
*/
|
||||
import { App, Alert, Button, Descriptions, Empty, Modal, Space, Typography } from 'antd'
|
||||
import PercentTable from '@/components/common/PercentTable'
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useState } from 'react'
|
||||
|
||||
import { createAdminCostSnapshot, previewAdminCost } from '@/services/admin'
|
||||
import type { CostPreview } from '@/types/admin'
|
||||
import { hasAdminPermission } from '@/utils/admin-auth'
|
||||
import { formatMoney } from '../panels/shared'
|
||||
|
||||
import { costModelLabel, providerLabel } from './shared'
|
||||
|
||||
const FULFILLMENT_MODE_LABELS: Record<string, string> = {
|
||||
work_order: '接单平台',
|
||||
affiliate_dash: '自动发货',
|
||||
legacy_platform: '历史平台',
|
||||
manual: '人工客服',
|
||||
}
|
||||
|
||||
const PLATFORM_LABELS: Record<string, string> = {
|
||||
nanhang: '南航积分',
|
||||
lewan: '乐玩积分',
|
||||
rock: 'ROCK 积分',
|
||||
fund_cash: '备用金现金',
|
||||
}
|
||||
|
||||
export type CostPreviewModalProps = {
|
||||
open: boolean
|
||||
orderItemId: number | null
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
export function CostPreviewModal({ open, orderItemId, onClose }: CostPreviewModalProps) {
|
||||
const { message } = App.useApp()
|
||||
const queryClient = useQueryClient()
|
||||
const canConfig = hasAdminPermission('finance.config')
|
||||
const [snapshotting, setSnapshotting] = useState(false)
|
||||
|
||||
const previewQuery = useQuery({
|
||||
queryKey: ['admin-cost-preview', orderItemId],
|
||||
queryFn: () => previewAdminCost({ orderItemId }),
|
||||
enabled: open && Boolean(orderItemId),
|
||||
retry: false,
|
||||
})
|
||||
const preview = (previewQuery.data?.data || null) as CostPreview | null
|
||||
const manualCost = preview?.manualCost || null
|
||||
|
||||
async function createSnapshot() {
|
||||
if (!orderItemId) return
|
||||
setSnapshotting(true)
|
||||
try {
|
||||
const response = await createAdminCostSnapshot({ orderItemId })
|
||||
const created = response.data.createdCount
|
||||
const exceptionCreated = response.data.exceptionCreatedCount
|
||||
message.success(
|
||||
created > 0
|
||||
? `已生成 ${created} 条成本快照${exceptionCreated > 0 ? `,新增 ${exceptionCreated} 条异常` : ''}`
|
||||
: `没有新增快照${exceptionCreated > 0 ? `,已记录 ${exceptionCreated} 条异常` : ',已有快照保持不变'}`,
|
||||
)
|
||||
await queryClient.invalidateQueries({ queryKey: ['admin-cost-preview', orderItemId] })
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? error.message : '生成成本快照失败')
|
||||
} finally {
|
||||
setSnapshotting(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal
|
||||
open={open}
|
||||
onCancel={onClose}
|
||||
title="成本解析"
|
||||
width={920}
|
||||
footer={
|
||||
<Space>
|
||||
<Button onClick={onClose}>关闭</Button>
|
||||
{canConfig && !manualCost ? (
|
||||
<Button
|
||||
type="primary"
|
||||
loading={snapshotting}
|
||||
disabled={
|
||||
!preview ||
|
||||
preview.exceptions.some((item) => item.reason === 'product_mapping_missing')
|
||||
}
|
||||
onClick={() => void createSnapshot()}
|
||||
>
|
||||
生成成本快照
|
||||
</Button>
|
||||
) : null}
|
||||
</Space>
|
||||
}
|
||||
>
|
||||
{!orderItemId ? (
|
||||
<Empty description="缺少订单项 ID" />
|
||||
) : previewQuery.isLoading ? (
|
||||
<Empty description="正在解析成本…" />
|
||||
) : previewQuery.error ? (
|
||||
<Alert
|
||||
type="error"
|
||||
showIcon
|
||||
message={
|
||||
previewQuery.error instanceof Error ? previewQuery.error.message : '成本解析失败'
|
||||
}
|
||||
/>
|
||||
) : !preview ? (
|
||||
<Empty description="没有可展示的成本解析结果" />
|
||||
) : (
|
||||
<Space direction="vertical" size={16} style={{ width: '100%' }}>
|
||||
<Descriptions bordered size="small" column={2}>
|
||||
<Descriptions.Item label="订单项">#{preview.context.orderItemId}</Descriptions.Item>
|
||||
<Descriptions.Item label="来源/店铺">
|
||||
{providerLabel(preview.context.provider)} / {preview.context.shopId || '全局'}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="SKU">
|
||||
{preview.context.skuCode} · {preview.context.skuName || '-'}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="履约方式">
|
||||
{FULFILLMENT_MODE_LABELS[preview.context.fulfillmentMode] ||
|
||||
preview.context.fulfillmentMode}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="标准商品">
|
||||
{preview.product
|
||||
? `${preview.product.canonicalName}(${preview.product.canonicalSku})`
|
||||
: '未映射'}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="版本成本合计">
|
||||
<Typography.Text strong>{formatMoney(preview.totalCostFen)}</Typography.Text>
|
||||
</Descriptions.Item>
|
||||
</Descriptions>
|
||||
|
||||
{manualCost ? (
|
||||
<Alert
|
||||
type="info"
|
||||
showIcon
|
||||
message={
|
||||
manualCost.costMode === 'fixed' && manualCost.costFen !== null
|
||||
? `人工成本 ${formatMoney(manualCost.costFen)}(${PLATFORM_LABELS[manualCost.platformKey] || manualCost.platformKey} ${manualCost.creditQuantity} 积分)`
|
||||
: `人工成本不固定(${PLATFORM_LABELS[manualCost.platformKey] || manualCost.platformKey})`
|
||||
}
|
||||
description={
|
||||
manualCost.costMode === 'fixed'
|
||||
? `金额 = 人工 SKU 配置的消耗积分 ${manualCost.creditQuantity} × 换算率 ${formatMoney(manualCost.unitPriceFen)},在客服「确认履约」时扣减;成本版本的 0 元占位不参与计算,也不会生成预估快照。`
|
||||
: '该 SKU 未配置固定消耗积分,客服确认履约时需要手填本次实际成本;成本版本不参与计算。'
|
||||
}
|
||||
/>
|
||||
) : null}
|
||||
|
||||
{preview.exceptions.length > 0 ? (
|
||||
<Alert
|
||||
type="warning"
|
||||
showIcon
|
||||
message="成本异常"
|
||||
description={preview.exceptions.map((item) => item.message).join(';')}
|
||||
/>
|
||||
) : null}
|
||||
|
||||
{preview.lines.length === 0 ? (
|
||||
<Empty
|
||||
description={
|
||||
manualCost ? '人工成本按 SKU 配置结算,无版本成本行' : '没有命中成本版本'
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<PercentTable
|
||||
tableLayout="fixed"
|
||||
rowKey={(row: CostPreview['lines'][number]) =>
|
||||
`${row.costItemId}-${row.costVersionId}`
|
||||
}
|
||||
size="small"
|
||||
pagination={false}
|
||||
dataSource={preview.lines}
|
||||
columns={[
|
||||
{ title: '成本项', dataIndex: 'costItemName', width: 220 },
|
||||
{
|
||||
title: '成本模型',
|
||||
dataIndex: 'costModel',
|
||||
width: 150,
|
||||
render: (value: string) => costModelLabel(value),
|
||||
},
|
||||
{ title: '范围', dataIndex: 'scopeType', width: 150 },
|
||||
{
|
||||
title: '数量',
|
||||
dataIndex: 'quantity',
|
||||
width: 100,
|
||||
render: (value: number) => Number(value || 0).toFixed(3),
|
||||
},
|
||||
{
|
||||
title: '单位成本',
|
||||
dataIndex: 'unitAmountFen',
|
||||
width: 120,
|
||||
render: (value: number) => formatMoney(value),
|
||||
},
|
||||
{
|
||||
title: '合计',
|
||||
dataIndex: 'totalAmountFen',
|
||||
width: 120,
|
||||
render: (value: number) => formatMoney(value),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
)}
|
||||
</Space>
|
||||
)}
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user