Files
order_site/apps/frontend/src/pages/admin/cost-center/CostPreviewModal.tsx
T
yml2213 6cd6401eca refactor(finance)!: 事实维度与接口字段统一改名 channel
- migration 114 将 sales_daily_facts.fulfillment_mode 重命名为 channel
- DTO/查询参数/前端类型从 fulfillmentMode 统一为 channel
- 成本中心类型 FulfillmentMode 更名为 FulfillmentChannel,并补齐卡券/待分类/归档取值
- 销售事实响应分组改为 byChannel
2026-10-31 20:26:15 +08:00

212 lines
7.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 成本解析弹窗:按订单项查看「映射到的商品 + 命中的成本版本 + 异常」。
*
* 从「成本异常」行和「订单详情 - 商品明细」进入;
* 人工履约不回版本金额,而是回人工 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.channel] || preview.context.channel}
</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>
)
}