feat(cost-center): 统一接单模板成本并自动同步商品

This commit is contained in:
yml2213
2026-11-02 16:14:51 +08:00
parent 55e3c4e21b
commit 290890e848
22 changed files with 963 additions and 508 deletions
@@ -95,7 +95,7 @@ export function ProductCatalogPanel() {
const queryClient = useQueryClient()
const { canConfig, productSourceFilter, refreshAll } = useCostCenter()
const [productForm] = Form.useForm<ProductFormValues>()
// 初始化/回填只处理最近 30 天,避免很早以前的乱 SKU 再次进入商品与映射。
// 历史修复只处理最近 30 天,避免很早以前的乱 SKU 再次进入商品与映射。
const [backfillRange, setBackfillRange] = useState<[Dayjs, Dayjs]>([
dayjs().subtract(29, 'day').startOf('day'),
dayjs().endOf('day'),
@@ -177,7 +177,7 @@ export function ProductCatalogPanel() {
const data = response.data
bootstrapSyncOptionsRef.current.includeDisabledTemplates = false
modal.confirm({
title: '从接单模板全量同步商品与映射',
title: '修复接单模板历史商品与映射',
width: 720,
content: (
<Space direction="vertical" size={8}>
@@ -276,7 +276,7 @@ export function ProductCatalogPanel() {
? `(含停用模板 ${applied.disabledTemplateCount} 个,已建为归档商品)`
: ''
message.success(
`已同步 ${applied.templateCount} 个模板商品:新建 ${applied.createdProductCount} 个、刷新 ${applied.updatedProductCount} 个、复用 ${applied.reusedProductCount} 个${disabledText};新增 ${applied.createdMappingCount} 条映射,同步 ${applied.templateCostVersionCount} 条模板发单金额,回填 ${applied.mappedOrderItemCount} 个订单项${rebuildText}${conflictText}`,
`已同步 ${applied.templateCount} 个模板商品:新建 ${applied.createdProductCount} 个、刷新 ${applied.updatedProductCount} 个、复用 ${applied.reusedProductCount} 个${disabledText};新增 ${applied.createdMappingCount} 条映射,回填 ${applied.mappedOrderItemCount} 个订单项${rebuildText}${conflictText}`,
)
refreshAll()
} catch (error) {
@@ -338,6 +338,7 @@ export function ProductCatalogPanel() {
}
function openSetProductPrice(item: ProductCatalogItem) {
const isTemplateProduct = item.canonicalSku.startsWith('tpl:')
setPricingProduct(item)
priceForm.resetFields()
priceForm.setFieldsValue({
@@ -345,9 +346,9 @@ export function ProductCatalogPanel() {
item.activeCostAmountFen !== null && item.activeCostAmountFen !== undefined
? (item.activeCostAmountFen / 100).toFixed(2)
: undefined,
pricingMode: isTemplateProduct ? item.templatePricingMode || 'fixed' : undefined,
effectiveScope: 'immediate',
changeReason: '在商品与成本中心统一调整价格',
syncOpenOrders: true,
syncSnapshots: true,
})
setPriceModalOpen(true)
@@ -355,10 +356,10 @@ export function ProductCatalogPanel() {
async function submitProductPrice(values: {
amountYuan: number | string
pricingMode?: 'fixed' | 'unit'
effectiveScope: 'immediate' | 'custom'
effectiveFromCustom?: string
changeReason?: string
syncOpenOrders?: boolean
}) {
if (!pricingProduct) return
const yuan = Number(values.amountYuan)
@@ -376,9 +377,10 @@ export function ProductCatalogPanel() {
try {
const response = await setAdminProductPrice(pricingProduct.id, {
amountFen,
pricingMode: values.pricingMode,
effectiveFrom,
changeReason: values.changeReason || '在商品与成本中心统一调整价格',
syncOpenOrders: values.syncOpenOrders !== false,
syncOpenOrders: true,
})
const result = response.data
let extraHint = ''
@@ -386,6 +388,12 @@ export function ProductCatalogPanel() {
extraHint = `,已同步 ${result.syncedOrders.updatedCount} 笔工单大厅待抢单`
}
message.success(`标准价格配置成功(¥${(amountFen / 100).toFixed(2)})${extraHint}`)
if (pricingProduct.canonicalSku.startsWith('tpl:') && !pricingProduct.templateEnabled) {
message.info('成本已配置;请回接单模板启用该模板后开始自动接单')
}
void queryClient.invalidateQueries({
queryKey: ['admin-worker-platform-product-rules'],
})
setPriceModalOpen(false)
refreshAll()
} catch (error) {
@@ -536,6 +544,16 @@ export function ProductCatalogPanel() {
<Typography.Text type="secondary" style={{ fontSize: 11 }}>
单位:{row.unitName || '件'}
</Typography.Text>
{row.canonicalSku.startsWith('tpl:') ? (
<Tag color={row.templateEnabled ? 'green' : 'orange'} style={{ marginInlineEnd: 0 }}>
{row.templateEnabled ? '模板已启用' : '模板待启用'}
</Tag>
) : null}
{row.templatePricingMode ? (
<Tag color="geekblue" style={{ marginInlineEnd: 0 }}>
{row.templatePricingMode === 'unit' ? '按件计价' : '固定金额'}
</Tag>
) : null}
</Space>
</div>
),
@@ -720,7 +738,7 @@ export function ProductCatalogPanel() {
loading={bootstrapPreviewing || bootstrapApplying}
onClick={() => void runTemplateBootstrap()}
>
从接单模板初始化
修复历史商品/映射
</Button>
</>
) : null}
@@ -949,16 +967,38 @@ export function ProductCatalogPanel() {
</div>
</div>
{pricingProduct.canonicalSku.startsWith('tpl:') ? (
<Form.Item
name="pricingMode"
label="接单计价方式"
rules={[{ required: true, message: '请选择计价方式' }]}
extra="保存后会同步到接单模板;固定金额按整单,按件单价按 SKU 数量计算"
>
<Radio.Group optionType="button" buttonStyle="solid">
<Radio.Button value="fixed">固定金额</Radio.Button>
<Radio.Button value="unit">按件计价</Radio.Button>
</Radio.Group>
</Form.Item>
) : null}
<Form.Item
name="amountYuan"
label={
pricingProduct.source === 'affiliate_dash' ? '采购单价(元)' : '标准结算单价(元)'
pricingProduct.source === 'affiliate_dash'
? '采购单价(元)'
: pricingProduct.canonicalSku.startsWith('tpl:')
? pricingProduct.templateSharingEnabled
? '拼单总成本(元)'
: '接单成本(元)'
: '标准结算单价(元)'
}
rules={[{ required: true, message: '请填写价格' }]}
extra={
pricingProduct.source === 'affiliate_dash'
? '上游每发货 1 件扣除的采购成本金额'
: '接单打手发单/验收的基础结算单价'
: pricingProduct.canonicalSku.startsWith('tpl:')
? '固定金额按整单计;按件计价时填写每个 SKU 的接单成本'
: '接单打手发单/验收的基础结算单价'
}
>
<InputNumber
@@ -983,8 +1023,13 @@ export function ProductCatalogPanel() {
只影响之后的新订单;历史订单的成本与成交价已固化,不会被改价覆盖
</div>
</Radio>
<Radio value="custom">
<Radio value="custom" disabled={pricingProduct.canonicalSku.startsWith('tpl:')}>
<strong>指定生效时间(不能早于当前时间)</strong>
{pricingProduct.canonicalSku.startsWith('tpl:') ? (
<div style={{ color: '#cf1322', fontSize: 12, marginTop: 2 }}>
接单模板当前只支持立即生效,避免模板价格与成本版本不同步
</div>
) : null}
</Radio>
</Radio.Group>
</Form.Item>
@@ -1010,17 +1055,6 @@ export function ProductCatalogPanel() {
<Input placeholder="例如:成本中心统一定价调整 / 上游采购成本调整" />
</Form.Item>
{pricingProduct.canonicalSku.startsWith('tpl:') ? (
<Form.Item name="syncOpenOrders" valuePropName="checked" style={{ marginBottom: 12 }}>
<Checkbox>
<strong>立即同步至接单大厅待抢单工单(推荐)</strong>
<div style={{ color: '#64748b', fontSize: 12 }}>
接单大厅中所有未领取的该模板工单将立即同步为新价格;已领取的工单保持原价不变。
</div>
</Checkbox>
</Form.Item>
) : null}
{pricingProduct.source === 'affiliate_dash' ? (
<Form.Item name="syncSnapshots" valuePropName="checked" style={{ marginBottom: 12 }}>
<Checkbox>
@@ -1,18 +1,18 @@
/**
* 发单价格 Tab:只读展示「接单模板 → 发单金额」的当前生效版本。
* 发单价格 Tab:展示由标准商品成本自动投影出的接单模板当前生效版本。
*
* 接单平台的成本以打手报酬为主,金额由接单模板(work_product_rules)决定,
* 这里只做核对视图;改价请改模板后点「同步模板发单金额」。
* 这里只做核对视图;改价在「标准商品」中完成并自动同步。
* 人工与自动发货的成本分别在各自 SKU 配置里维护,不在这里重复配置。
*/
import { SearchOutlined, SyncOutlined } from '@ant-design/icons'
import { Alert, App, Button, Card, Input, Select, Space, Tag, Typography } from 'antd'
import { SearchOutlined } from '@ant-design/icons'
import { Alert, Card, Input, Select, Space, Tag, Typography } from 'antd'
import PercentTable from '@/components/common/PercentTable'
import type { TableColumnsType } from 'antd'
import { useQuery } from '@tanstack/react-query'
import { useState } from 'react'
import { fetchAdminCostVersions, syncAdminWorkOrderTemplateCostVersions } from '@/services/admin'
import { fetchAdminCostVersions } from '@/services/admin'
import type { CostVersion } from '@/types/admin'
import { ADMIN_DEFAULT_PAGE_SIZE, buildAdminTablePagination } from '@/utils/admin-pagination'
import { formatAdminDateTime } from '@/utils/admin-time'
@@ -22,14 +22,12 @@ import { useCostCenter } from './CostCenterContext'
import { formatRate } from './shared'
export function WorkOrderPricePanel() {
const { message } = App.useApp()
const { canConfig, refreshAll } = useCostCenter()
const { canConfig } = useCostCenter()
const [keyword, setKeyword] = useState('')
const [status, setStatus] = useState('active')
const [page, setPage] = useState(1)
const [pageSize, setPageSize] = useState(ADMIN_DEFAULT_PAGE_SIZE)
const [syncing, setSyncing] = useState(false)
const versionsQuery = useQuery({
queryKey: ['admin-cost-center-cost-versions', 'work_order', keyword, status, page, pageSize],
@@ -46,22 +44,6 @@ export function WorkOrderPricePanel() {
const versions = versionsQuery.data?.data.items || []
const total = versionsQuery.data?.data.pagination.total || 0
async function syncTemplateCosts() {
setSyncing(true)
try {
const response = await syncAdminWorkOrderTemplateCostVersions()
const result = response.data
message.success(
`已同步 ${result.activeVersionCount} 条模板发单金额(新建 ${result.createdCount},停用 ${result.retiredCount}),关联 ${result.linkedSnapshotCount} 条成本快照`,
)
refreshAll()
} catch (error) {
message.error(error instanceof Error ? error.message : '同步接单模板发单金额失败')
} finally {
setSyncing(false)
}
}
const columns: TableColumnsType<CostVersion> = [
{
title: '接单规则 / 模板',
@@ -176,24 +158,14 @@ export function WorkOrderPricePanel() {
<Tag color="blue">{total} 条</Tag>
</Space>
}
extra={
canConfig ? (
<Button
icon={<SyncOutlined />}
loading={syncing}
onClick={() => void syncTemplateCosts()}
>
同步模板发单金额
</Button>
) : null
}
extra={canConfig ? <Tag color="green">自动同步</Tag> : null}
>
<Alert
type="info"
showIcon
style={{ marginBottom: 12 }}
message="接单平台的成本只看这张表;改价请改接单模板,再点「同步模板发单金额」"
description="人工客服的成本来自「人工 SKU 配置」里的消耗积分,自动发货的成本在「自动发货 SKU」里维护,二者都不在这里配置;系统内置成本项只有人工履约、自动发货采购、接单平台报酬 3 条。"
message="接单平台成本的唯一写入口是【标准商品 → 配置成本】"
description="改价保存后会自动更新成本版本、接单模板运行价格和本表;未领取的大厅工单同步新价,已领取工单保持原价。人工客服和自动发货成本分别在各自 SKU 配置中维护。"
/>
<Space style={{ marginBottom: 16 }} wrap>
<Input