- 归一新增 tpv:<SKU> 直达,卡券单无需手工映射 - 成本配置/总览/异常支持 third_party_voucher 与 archived 维度 - 新增卡券采购价面板,写入 sys_third_party_voucher_cost - 修正 man: 商品改价误用不存在的 sys_manual_cost
143 lines
4.7 KiB
TypeScript
143 lines
4.7 KiB
TypeScript
/**
|
||
* 第三方卡券采购价:只统计渠道,不产生履约动作。
|
||
*
|
||
* 商品编码固定为 `tpv:<sku>`;成本写入 `sys_third_party_voucher_cost` 的
|
||
* 商品级版本,由成本快照管道消费。
|
||
*/
|
||
import { ReloadOutlined } from '@ant-design/icons'
|
||
import { Button, Card, Form, Input, InputNumber, Modal, Tag, Typography, message } from 'antd'
|
||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||
import { useState } from 'react'
|
||
|
||
import PercentTable from '@/components/common/PercentTable'
|
||
import { fetchAdminProductCatalog, setAdminProductPrice } from '@/services/admin'
|
||
import type { ProductCatalogItem } from '@/types/admin'
|
||
import { useCostCenter } from './CostCenterContext'
|
||
|
||
const PAGE_SIZE = 100
|
||
|
||
export default function VoucherSkuPanel() {
|
||
const queryClient = useQueryClient()
|
||
const { canConfig, refreshAll } = useCostCenter()
|
||
const [page] = useState(1)
|
||
const [selected, setSelected] = useState<ProductCatalogItem | null>(null)
|
||
const [form] = Form.useForm<{ amountFen: number; changeReason?: string }>()
|
||
|
||
const query = useQuery({
|
||
queryKey: ['cost-center-voucher-products', page],
|
||
queryFn: () =>
|
||
fetchAdminProductCatalog({
|
||
carrierType: 'third_party_voucher',
|
||
page,
|
||
pageSize: PAGE_SIZE,
|
||
}),
|
||
})
|
||
|
||
const saveMutation = useMutation({
|
||
mutationFn: (values: { amountFen: number; changeReason?: string }) =>
|
||
setAdminProductPrice(selected!.id, {
|
||
amountFen: Math.round(values.amountFen * 100),
|
||
changeReason: values.changeReason || '第三方卡券采购价调整',
|
||
}),
|
||
onSuccess: () => {
|
||
message.success('采购价已保存')
|
||
setSelected(null)
|
||
form.resetFields()
|
||
void queryClient.invalidateQueries({ queryKey: ['cost-center-voucher-products'] })
|
||
refreshAll()
|
||
},
|
||
onError: (error) => message.error(error instanceof Error ? error.message : '保存失败'),
|
||
})
|
||
|
||
const items = query.data?.data.items || []
|
||
const total = query.data?.data.pagination.total || 0
|
||
|
||
const columns = [
|
||
{ title: '商品', dataIndex: 'canonicalName', ellipsis: true },
|
||
{ title: '编码', dataIndex: 'canonicalSku', width: 220 },
|
||
{
|
||
title: '采购价',
|
||
width: 140,
|
||
render: (_: unknown, row: ProductCatalogItem) =>
|
||
row.activeCostAmountFen === null || row.activeCostAmountFen === undefined ? (
|
||
<Tag color="orange">未配价</Tag>
|
||
) : (
|
||
`¥${(row.activeCostAmountFen / 100).toFixed(2)}`
|
||
),
|
||
},
|
||
{
|
||
title: '生效时间',
|
||
dataIndex: 'activeCostEffectiveFrom',
|
||
width: 180,
|
||
render: (value?: string | null) => value || '—',
|
||
},
|
||
{
|
||
title: '操作',
|
||
width: 110,
|
||
render: (_: unknown, row: ProductCatalogItem) => (
|
||
<Button
|
||
size="small"
|
||
disabled={!canConfig}
|
||
onClick={() => {
|
||
setSelected(row)
|
||
form.setFieldsValue({
|
||
amountFen:
|
||
row.activeCostAmountFen === null || row.activeCostAmountFen === undefined
|
||
? 0
|
||
: Number((row.activeCostAmountFen / 100).toFixed(2)),
|
||
changeReason: '第三方卡券采购价调整',
|
||
})
|
||
}}
|
||
>
|
||
改价
|
||
</Button>
|
||
),
|
||
},
|
||
]
|
||
|
||
return (
|
||
<Card
|
||
title={`卡券商品采购价(${total})`}
|
||
extra={
|
||
<Button icon={<ReloadOutlined />} onClick={() => void query.refetch()}>
|
||
刷新
|
||
</Button>
|
||
}
|
||
>
|
||
<Typography.Paragraph type="secondary">
|
||
卡券渠道只统计:命中规则后不创建任务、不发领取页,只记收入与采购成本。 商品编码以{' '}
|
||
<Typography.Text code>tpv:</Typography.Text> 开头。
|
||
</Typography.Paragraph>
|
||
<PercentTable
|
||
rowKey="id"
|
||
size="small"
|
||
loading={query.isFetching}
|
||
dataSource={items}
|
||
columns={columns}
|
||
pagination={{ pageSize: 20, showSizeChanger: false }}
|
||
/>
|
||
|
||
<Modal
|
||
title={`设置采购价:${selected?.canonicalName || ''}`}
|
||
open={Boolean(selected)}
|
||
onCancel={() => setSelected(null)}
|
||
onOk={() => form.submit()}
|
||
confirmLoading={saveMutation.isPending}
|
||
>
|
||
<Form form={form} layout="vertical" onFinish={(values) => saveMutation.mutate(values)}>
|
||
<Form.Item
|
||
name="amountFen"
|
||
label="采购价(元)"
|
||
rules={[{ required: true, message: '请输入采购价' }]}
|
||
>
|
||
<InputNumber min={0.01} precision={2} style={{ width: '100%' }} />
|
||
</Form.Item>
|
||
<Form.Item name="changeReason" label="变更原因">
|
||
<Input />
|
||
</Form.Item>
|
||
</Form>
|
||
</Modal>
|
||
</Card>
|
||
)
|
||
}
|