- 移除自动发货商品同步、SKU 回填、按工单回填、人工商品归一等旧接口与按钮 - 商品目录只保留「从接单模板初始化」与手工新建 - 自动发货面板移除已失效的「重算成本快照」入口
423 lines
15 KiB
TypeScript
423 lines
15 KiB
TypeScript
/**
|
||
* 自动发货 SKU Tab:上游 SKU 目录、采购成本配置与订单项快照同步。
|
||
*
|
||
* 自动发货只有 affiliate-dash 一个上游渠道,SKU 固定归一为 `aff:<skuCode>`,
|
||
* 不写渠道映射,也不维护游戏分类。
|
||
* 成本按上游商品配置采购单价(cost_versions),并在改价/初次配置后重算成本快照。
|
||
*/
|
||
import { DownloadOutlined, EditOutlined, ReloadOutlined, SyncOutlined } from '@ant-design/icons'
|
||
import {
|
||
Alert,
|
||
App,
|
||
Button,
|
||
Card,
|
||
DatePicker,
|
||
Form,
|
||
Input,
|
||
InputNumber,
|
||
Modal,
|
||
Radio,
|
||
Space,
|
||
Switch,
|
||
Tag,
|
||
Typography,
|
||
} from 'antd'
|
||
import PercentTable from '@/components/common/PercentTable'
|
||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||
import dayjs from 'dayjs'
|
||
import { useState } from 'react'
|
||
|
||
import {
|
||
fetchAdminProductCatalog,
|
||
importAdminAffiliateDashPricesBaseline,
|
||
setAdminAffiliateProductPrice,
|
||
updateAdminProductCatalog,
|
||
} from '@/services/admin'
|
||
import type { ProductCatalogItem } from '@/types/admin'
|
||
import { ADMIN_DEFAULT_PAGE_SIZE, buildAdminTablePagination } from '@/utils/admin-pagination'
|
||
import { formatAdminDateTime } from '@/utils/admin-time'
|
||
|
||
import { useCostCenter } from './CostCenterContext'
|
||
|
||
interface PriceFormValues {
|
||
priceYuan: number
|
||
effectiveMode: 'retroactive' | 'now' | 'custom'
|
||
customEffectiveDate?: dayjs.Dayjs
|
||
changeReason?: string
|
||
}
|
||
|
||
export function AffiliateSkuPanel() {
|
||
const { message, modal } = App.useApp()
|
||
const queryClient = useQueryClient()
|
||
const { canConfig, refreshAll } = useCostCenter()
|
||
|
||
const [page, setPage] = useState(1)
|
||
const [pageSize, setPageSize] = useState(ADMIN_DEFAULT_PAGE_SIZE)
|
||
const [statusUpdatingId, setStatusUpdatingId] = useState<number | null>(null)
|
||
const [importingBaseline, setImportingBaseline] = useState(false)
|
||
|
||
// 价格弹窗状态
|
||
const [priceModalOpen, setPriceModalOpen] = useState(false)
|
||
const [selectedProduct, setSelectedProduct] = useState<ProductCatalogItem | null>(null)
|
||
const [priceSubmitting, setPriceSubmitting] = useState(false)
|
||
const [priceForm] = Form.useForm<PriceFormValues>()
|
||
|
||
const productsQuery = useQuery({
|
||
queryKey: ['admin-cost-center-products', 'affiliate_dash', page, pageSize],
|
||
queryFn: () =>
|
||
fetchAdminProductCatalog({
|
||
carrierType: 'affiliate_dash',
|
||
page,
|
||
pageSize,
|
||
}),
|
||
})
|
||
|
||
const products = productsQuery.data?.data.items || []
|
||
const total = productsQuery.data?.data.pagination.total || 0
|
||
|
||
function handleImportBaseline() {
|
||
modal.confirm({
|
||
title: '导入上游采购成本基线',
|
||
content:
|
||
'将从系统内置的 38 条上游商品价格清单导入采购成本版本(生效时间默认覆盖最近一个月(30 天)订单)。是否继续?',
|
||
okText: '确认导入',
|
||
cancelText: '取消',
|
||
onOk: async () => {
|
||
setImportingBaseline(true)
|
||
try {
|
||
const res = await importAdminAffiliateDashPricesBaseline()
|
||
message.success(
|
||
`已成功导入上游价格基线:新增商品 ${res.data.createdProductCount} 个,新增成本版本 ${res.data.createdVersionCount} 个,更新生效时间 ${res.data.updatedVersionCount} 个`,
|
||
)
|
||
refreshAll()
|
||
} catch (error) {
|
||
message.error(error instanceof Error ? error.message : '导入上游价格基线失败')
|
||
} finally {
|
||
setImportingBaseline(false)
|
||
}
|
||
},
|
||
})
|
||
}
|
||
|
||
function openPriceModal(product: ProductCatalogItem) {
|
||
setSelectedProduct(product)
|
||
const currentYuan =
|
||
product.activeCostAmountFen !== null && product.activeCostAmountFen !== undefined
|
||
? Number((product.activeCostAmountFen / 100).toFixed(2))
|
||
: undefined
|
||
|
||
priceForm.setFieldsValue({
|
||
priceYuan: currentYuan,
|
||
effectiveMode: 'retroactive',
|
||
changeReason: currentYuan ? '上游采购成本调整' : '首次配置自动发货采购成本',
|
||
})
|
||
setPriceModalOpen(true)
|
||
}
|
||
|
||
async function submitPrice(values: PriceFormValues) {
|
||
if (!selectedProduct) return
|
||
const amountFen = Math.round(Number(values.priceYuan) * 100)
|
||
if (!amountFen || amountFen <= 0) {
|
||
message.error('请输入有效的采购单价')
|
||
return
|
||
}
|
||
|
||
let effectiveFrom: string | undefined
|
||
let retroactive = false
|
||
|
||
if (values.effectiveMode === 'retroactive') {
|
||
retroactive = true
|
||
effectiveFrom = dayjs().subtract(30, 'day').format('YYYY-MM-DDTHH:mm:ss+08:00')
|
||
} else if (values.effectiveMode === 'now') {
|
||
effectiveFrom = new Date().toISOString()
|
||
} else if (values.effectiveMode === 'custom' && values.customEffectiveDate) {
|
||
effectiveFrom = values.customEffectiveDate.startOf('day').toISOString()
|
||
}
|
||
|
||
setPriceSubmitting(true)
|
||
try {
|
||
await setAdminAffiliateProductPrice(selectedProduct.id, {
|
||
amountFen,
|
||
effectiveFrom,
|
||
retroactive,
|
||
changeReason: values.changeReason,
|
||
})
|
||
message.success(`「${selectedProduct.canonicalName}」采购单价已设置为 ¥${values.priceYuan}`)
|
||
setPriceModalOpen(false)
|
||
refreshAll()
|
||
} catch (error) {
|
||
message.error(error instanceof Error ? error.message : '设置采购成本失败')
|
||
} finally {
|
||
setPriceSubmitting(false)
|
||
}
|
||
}
|
||
|
||
async function toggleStatus(row: ProductCatalogItem, checked: boolean) {
|
||
if (!canConfig) return
|
||
setStatusUpdatingId(row.id)
|
||
try {
|
||
await updateAdminProductCatalog(row.id, { status: checked ? 'active' : 'archived' })
|
||
message.success(checked ? 'SKU 已启用' : 'SKU 已归档')
|
||
await queryClient.invalidateQueries({ queryKey: ['admin-cost-center-products'] })
|
||
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-overview'] })
|
||
} catch (error) {
|
||
message.error(error instanceof Error ? error.message : '更新 SKU 状态失败')
|
||
} finally {
|
||
setStatusUpdatingId(null)
|
||
}
|
||
}
|
||
|
||
return (
|
||
<>
|
||
<Card
|
||
title={
|
||
<Space size={6} wrap>
|
||
<Typography.Text strong>自动发货 SKU</Typography.Text>
|
||
<Tag color="green">{total}</Tag>
|
||
</Space>
|
||
}
|
||
extra={
|
||
canConfig ? (
|
||
<Space wrap>
|
||
<Button
|
||
icon={<DownloadOutlined />}
|
||
loading={importingBaseline}
|
||
onClick={handleImportBaseline}
|
||
>
|
||
导入基线价格
|
||
</Button>
|
||
<Button
|
||
icon={<ReloadOutlined />}
|
||
loading={productsQuery.isFetching}
|
||
onClick={() => void productsQuery.refetch()}
|
||
/>
|
||
</Space>
|
||
) : null
|
||
}
|
||
>
|
||
<Alert
|
||
type="info"
|
||
showIcon
|
||
style={{ marginBottom: 12 }}
|
||
message="自动发货订单直接按 aff:<skuCode> 归一,不走渠道映射。点击「配置成本」录入采购单价;缺少成本价时由成本快照管道自动标记并补齐。"
|
||
/>
|
||
<PercentTable
|
||
tableLayout="fixed"
|
||
rowKey="id"
|
||
size="small"
|
||
dataSource={products}
|
||
loading={productsQuery.isLoading}
|
||
scroll={{ x: 1180 }}
|
||
pagination={buildAdminTablePagination({
|
||
current: page,
|
||
pageSize,
|
||
total,
|
||
showTotal: (count) => `共 ${count} 个 SKU`,
|
||
onChange: (nextPage, nextPageSize) => {
|
||
setPage(nextPage)
|
||
setPageSize(nextPageSize)
|
||
},
|
||
})}
|
||
columns={[
|
||
{
|
||
title: 'SKU 名称 / 编码',
|
||
dataIndex: 'canonicalName',
|
||
width: 300,
|
||
ellipsis: true,
|
||
render: (value: string, row) => (
|
||
<Space direction="vertical" size={0} style={{ width: '100%' }}>
|
||
<Typography.Text strong ellipsis={{ tooltip: value }}>
|
||
{value}
|
||
</Typography.Text>
|
||
<Typography.Text
|
||
type="secondary"
|
||
style={{ fontSize: 12 }}
|
||
ellipsis={{
|
||
tooltip: String(row.attributes?.skuCode || row.canonicalSku),
|
||
}}
|
||
>
|
||
{String(row.attributes?.skuCode || row.canonicalSku)}
|
||
</Typography.Text>
|
||
</Space>
|
||
),
|
||
},
|
||
{
|
||
title: '采购单价 (元)',
|
||
width: 140,
|
||
render: (_, row) => {
|
||
if (row.activeCostAmountFen !== null && row.activeCostAmountFen !== undefined) {
|
||
return (
|
||
<Space direction="vertical" size={0}>
|
||
<Typography.Text strong style={{ color: '#cf1322', fontSize: 14 }}>
|
||
¥{(row.activeCostAmountFen / 100).toFixed(2)}
|
||
</Typography.Text>
|
||
{row.activeCostEffectiveFrom ? (
|
||
<Typography.Text type="secondary" style={{ fontSize: 11 }}>
|
||
生效: {String(row.activeCostEffectiveFrom).slice(0, 10)}
|
||
</Typography.Text>
|
||
) : null}
|
||
</Space>
|
||
)
|
||
}
|
||
return <Tag color="error">未配置</Tag>
|
||
},
|
||
},
|
||
{
|
||
title: '履约任务',
|
||
width: 110,
|
||
render: (_, row) => `${row.affiliateTaskCount} 单`,
|
||
},
|
||
{
|
||
title: '最近履约',
|
||
width: 170,
|
||
render: (_, row) =>
|
||
row.affiliateLastFulfilledAt
|
||
? formatAdminDateTime(row.affiliateLastFulfilledAt)
|
||
: '-',
|
||
},
|
||
{
|
||
title: '成本版本',
|
||
dataIndex: 'costVersionCount',
|
||
width: 100,
|
||
render: (value: number) =>
|
||
value > 0 ? (
|
||
<Typography.Text type="success">{value} 个版本</Typography.Text>
|
||
) : (
|
||
<Tag>未配置</Tag>
|
||
),
|
||
},
|
||
{
|
||
title: '状态',
|
||
dataIndex: 'status',
|
||
width: 110,
|
||
render: (value: string, row) => (
|
||
<Space size={6}>
|
||
<Switch
|
||
size="small"
|
||
checked={value === 'active'}
|
||
loading={statusUpdatingId === row.id}
|
||
disabled={
|
||
!canConfig || (statusUpdatingId !== null && statusUpdatingId !== row.id)
|
||
}
|
||
onChange={(checked) => void toggleStatus(row, checked)}
|
||
/>
|
||
<Typography.Text
|
||
type={value === 'active' ? 'success' : 'secondary'}
|
||
style={{ fontSize: 12 }}
|
||
>
|
||
{value === 'active' ? '启用' : '归档'}
|
||
</Typography.Text>
|
||
</Space>
|
||
),
|
||
},
|
||
{
|
||
title: '操作',
|
||
width: 110,
|
||
fixed: 'right',
|
||
render: (_, row) => (
|
||
<Button
|
||
type="link"
|
||
size="small"
|
||
icon={<EditOutlined />}
|
||
disabled={!canConfig}
|
||
onClick={() => openPriceModal(row)}
|
||
>
|
||
{row.activeCostAmountFen !== null && row.activeCostAmountFen !== undefined
|
||
? '改价'
|
||
: '配置成本'}
|
||
</Button>
|
||
),
|
||
},
|
||
]}
|
||
/>
|
||
</Card>
|
||
|
||
<Modal
|
||
title={`配置自动发货采购成本 - ${selectedProduct?.canonicalName || ''}`}
|
||
open={priceModalOpen}
|
||
onCancel={() => setPriceModalOpen(false)}
|
||
onOk={() => priceForm.submit()}
|
||
confirmLoading={priceSubmitting}
|
||
okText="保存并生效"
|
||
cancelText="取消"
|
||
destroyOnClose
|
||
>
|
||
<Form form={priceForm} layout="vertical" onFinish={submitPrice}>
|
||
<Form.Item label="标准商品">
|
||
<Typography.Text strong>
|
||
{selectedProduct?.canonicalName}({selectedProduct?.canonicalSku})
|
||
</Typography.Text>
|
||
</Form.Item>
|
||
|
||
<Form.Item
|
||
name="priceYuan"
|
||
label="采购单价 (元)"
|
||
rules={[
|
||
{ required: true, message: '请输入采购单价' },
|
||
{
|
||
type: 'number',
|
||
min: 0.01,
|
||
message: '采购单价必须大于 0',
|
||
},
|
||
]}
|
||
extra="上游每发货 1 件扣除的采购成本金额"
|
||
>
|
||
<InputNumber
|
||
style={{ width: '100%' }}
|
||
precision={2}
|
||
min={0.01}
|
||
step={1}
|
||
addonAfter="元 / 件"
|
||
placeholder="例如: 15.00"
|
||
/>
|
||
</Form.Item>
|
||
|
||
<Form.Item
|
||
name="effectiveMode"
|
||
label="生效时间范围"
|
||
rules={[{ required: true, message: '请选择生效时间' }]}
|
||
>
|
||
<Radio.Group>
|
||
<Space direction="vertical">
|
||
<Radio value="retroactive">
|
||
<Space direction="vertical" size={0}>
|
||
<Typography.Text strong>追溯至历史订单 (推荐)</Typography.Text>
|
||
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
|
||
自近一个月业务起点(30 天前)生效,重算快照后可直接消除近一个月 cost_missing
|
||
异常
|
||
</Typography.Text>
|
||
</Space>
|
||
</Radio>
|
||
<Radio value="now">
|
||
<span>自当前时间立即生效(仅对后续订单生效)</span>
|
||
</Radio>
|
||
<Radio value="custom">
|
||
<span>指定生效日期</span>
|
||
</Radio>
|
||
</Space>
|
||
</Radio.Group>
|
||
</Form.Item>
|
||
|
||
<Form.Item noStyle shouldUpdate={(prev, cur) => prev.effectiveMode !== cur.effectiveMode}>
|
||
{({ getFieldValue }) =>
|
||
getFieldValue('effectiveMode') === 'custom' ? (
|
||
<Form.Item
|
||
name="customEffectiveDate"
|
||
label="指定生效日期"
|
||
rules={[{ required: true, message: '请选择生效日期' }]}
|
||
>
|
||
<DatePicker style={{ width: '100%' }} placeholder="选择生效日期" />
|
||
</Form.Item>
|
||
) : null
|
||
}
|
||
</Form.Item>
|
||
|
||
<Form.Item name="changeReason" label="变更说明 / 备注">
|
||
<Input placeholder="例如:上游供货商调价" />
|
||
</Form.Item>
|
||
</Form>
|
||
</Modal>
|
||
</>
|
||
)
|
||
}
|