/** * 自动发货 SKU Tab:上游 SKU 目录、采购成本配置与订单项快照同步。 * * 自动发货只有 affiliate-dash 一个上游渠道,SKU 固定归一为 `aff:`, * 不写渠道映射,也不维护游戏分类。 * 成本按上游商品配置采购单价(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(null) const [importingBaseline, setImportingBaseline] = useState(false) // 价格弹窗状态 const [priceModalOpen, setPriceModalOpen] = useState(false) const [selectedProduct, setSelectedProduct] = useState(null) const [priceSubmitting, setPriceSubmitting] = useState(false) const [priceForm] = Form.useForm() 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 ( <> 自动发货 SKU {total} } extra={ canConfig ? ( ), }, ]} /> setPriceModalOpen(false)} onOk={() => priceForm.submit()} confirmLoading={priceSubmitting} okText="保存并生效" cancelText="取消" destroyOnClose >
{selectedProduct?.canonicalName}({selectedProduct?.canonicalSku}) 追溯至历史订单 (推荐) 自近一个月业务起点(30 天前)生效,重算快照后可直接消除近一个月 cost_missing 异常 自当前时间立即生效(仅对后续订单生效) 指定生效日期 prev.effectiveMode !== cur.effectiveMode}> {({ getFieldValue }) => getFieldValue('effectiveMode') === 'custom' ? ( ) : null }
) }