修复(成本中心): 接单模板商品被商品接口误归档导致与接单模板数量不一致
生产只读核对(2026-12-03):接单模板 972 条,商品与成本中心「接单平台」只有 957 个, 差的 15 个模板商品被 admin_audit_logs 中的 cost_product_updated 记录逐条对上,操作人来自 售后账号,时间分布到 2026-11-28,说明该路径仍在持续制造不一致。 根因: - updateAdminProduct 只改 product_catalog.status,不校验商品是否被接单模板引用,也不写归档原因; - 商品归档后列表默认只查 active、前端无 archived 入口,运营再也看不到也点不回来; - 迁移 164 的回填要求商品当时 active,更早归档的模板 canonical_product_id 永久为 NULL; - 保存模板的 ON CONFLICT DO UPDATE 不含 status、「修复历史商品/映射」不写关联也不重置状态, 所以人工修几次都只会得到“链接修好、状态仍是 archived”的中间态。 改动: - 新增迁移 192:按 tpl:<rule_key> 唯一身份公式回填关联、恢复被误归档的模板商品 (保留 previousStatus/repairedAt/repairReason 供回滚),假草稿与已删模板商品不动; 末尾断言启用模板与商品一一对齐、同一 SKU 不得命中多条模板,任一不满足整份回滚。 - 商品接口拒绝归档/删除仍被模板引用的商品(409 cost_product_template_bound); attributes_json 改为合并,避免编辑商品时抹掉模板属性。 - 保存模板一律把商品置为 active 并写回关联,模板删除才归档;模板商品 SKU 已被 非模板商品占用时 409 cost_template_sku_occupied,不再劫持他人商品。 - 「修复历史商品/映射」统一走模板同步入口,默认包含停用模板。 - 模板成本版本与模板标识统一用同一条身份公式,去掉 LIKE 'tpl:%' + substring 旧写法。 - 前端:模板商品状态开关禁用并提示;跨页缓存失效补齐。 - 权威文档 3.4.4 落地新口径(模板存在 ⇒ 商品 active,只有删除模板才归档)。 - 新增 7 个真实 PostgreSQL 集成测试。 验证:backend check 通过(1559 用例 / fail 0)、frontend check 通过、集成测试 7/7 通过、 迁移 192 在生产同形态夹具上验证幂等且断言通过;生产上线前只读预检 4 项均为 0 行。
This commit is contained in:
@@ -29,6 +29,7 @@ import {
|
||||
Switch,
|
||||
Tabs,
|
||||
Tag,
|
||||
Tooltip,
|
||||
Typography,
|
||||
} from 'antd'
|
||||
import type { TableColumnsType } from 'antd'
|
||||
@@ -63,7 +64,7 @@ import {
|
||||
type ProductFormValues,
|
||||
} from './shared'
|
||||
|
||||
/** 全量同步选项:停用模板默认不建商品,勾选后建成「归档」商品。 */
|
||||
/** 全量同步选项:默认同时同步停用模板(否则停用模板的商品永远建不出来、也不能先配成本)。 */
|
||||
type BootstrapSyncOptions = { includeDisabledTemplates: boolean }
|
||||
|
||||
function DisabledTemplateSyncOption({
|
||||
@@ -82,9 +83,10 @@ function DisabledTemplateSyncOption({
|
||||
onChange(event.target.checked)
|
||||
}}
|
||||
>
|
||||
<strong>同时为停用模板建商品(归档状态,不参与成本统计)</strong>
|
||||
<strong>同时为停用模板建商品(用于先配成本)</strong>
|
||||
<div style={{ color: '#64748b', fontSize: 12, marginTop: 2 }}>
|
||||
默认只同步启用模板;停用模板一般是历史遗留,建出来的商品会以「归档」出现在商品列表里。
|
||||
默认同时同步停用模板;取消勾选则只同步启用模板。停用模板的商品同样建成启用状态,
|
||||
方便先配成本再启用模板。
|
||||
</div>
|
||||
</Checkbox>
|
||||
)
|
||||
@@ -108,7 +110,7 @@ export function ProductCatalogPanel() {
|
||||
const [productStatusUpdatingId, setProductStatusUpdatingId] = useState<number | null>(null)
|
||||
const [bootstrapPreviewing, setBootstrapPreviewing] = useState(false)
|
||||
const [bootstrapApplying, setBootstrapApplying] = useState(false)
|
||||
const bootstrapSyncOptionsRef = useRef<BootstrapSyncOptions>({ includeDisabledTemplates: false })
|
||||
const bootstrapSyncOptionsRef = useRef<BootstrapSyncOptions>({ includeDisabledTemplates: true })
|
||||
const [productPage, setProductPage] = useState(1)
|
||||
const [productPageSize, setProductPageSize] = useState(ADMIN_DEFAULT_PAGE_SIZE)
|
||||
const [productModalOpen, setProductModalOpen] = useState(false)
|
||||
@@ -176,7 +178,7 @@ export function ProductCatalogPanel() {
|
||||
try {
|
||||
const response = await previewAdminTemplateProductBootstrap(backfillPayload())
|
||||
const data = response.data
|
||||
bootstrapSyncOptionsRef.current.includeDisabledTemplates = false
|
||||
bootstrapSyncOptionsRef.current.includeDisabledTemplates = true
|
||||
modal.confirm({
|
||||
title: '修复接单模板历史商品与映射',
|
||||
width: 720,
|
||||
@@ -193,7 +195,8 @@ export function ProductCatalogPanel() {
|
||||
<strong>{data.disabledTemplateCount}</strong>)。
|
||||
</span>
|
||||
<span>
|
||||
启用模板全部建标准商品:将新建 <strong>{data.productCreateCount}</strong> 个、复用{' '}
|
||||
启用模板全部建标准商品(停用模板可勾选一并建):将新建{' '}
|
||||
<strong>{data.productCreateCount}</strong> 个、复用{' '}
|
||||
<strong>{data.productExistingCount}</strong>{' '}
|
||||
个(已有模板商品会同步刷新名称、分类、发单金额)。
|
||||
</span>
|
||||
@@ -215,7 +218,7 @@ export function ProductCatalogPanel() {
|
||||
</span>
|
||||
{data.disabledTemplateCount > 0 ? (
|
||||
<DisabledTemplateSyncOption
|
||||
initialValue={false}
|
||||
initialValue={true}
|
||||
onChange={(value) => {
|
||||
bootstrapSyncOptionsRef.current = { includeDisabledTemplates: value }
|
||||
}}
|
||||
@@ -274,10 +277,13 @@ export function ProductCatalogPanel() {
|
||||
: ''
|
||||
const disabledText =
|
||||
applied.disabledTemplateCount > 0
|
||||
? `(含停用模板 ${applied.disabledTemplateCount} 个,已建为归档商品)`
|
||||
? `(含停用模板 ${applied.disabledTemplateCount} 个,商品同样建成启用状态)`
|
||||
: ''
|
||||
// 「复用」只在真的复用时才提示:模板商品恒由模板同步生成,恒显示 0 会误导运营。
|
||||
const reusedText =
|
||||
applied.reusedProductCount > 0 ? `、复用 ${applied.reusedProductCount} 个` : ''
|
||||
message.success(
|
||||
`已同步 ${applied.templateCount} 个模板商品:新建 ${applied.createdProductCount} 个、刷新 ${applied.updatedProductCount} 个、复用 ${applied.reusedProductCount} 个${disabledText};新增 ${applied.createdMappingCount} 条映射,回填 ${applied.mappedOrderItemCount} 个订单项${rebuildText}${conflictText}`,
|
||||
`已同步 ${applied.templateCount} 个模板商品:新建 ${applied.createdProductCount} 个、刷新 ${applied.updatedProductCount} 个${reusedText}${disabledText};新增 ${applied.createdMappingCount} 条映射,回填 ${applied.mappedOrderItemCount} 个订单项${rebuildText}${conflictText}`,
|
||||
)
|
||||
refreshAll()
|
||||
} catch (error) {
|
||||
@@ -317,6 +323,21 @@ export function ProductCatalogPanel() {
|
||||
setProductModalOpen(true)
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品编辑/改状态/删除后的统一失效。
|
||||
*
|
||||
* 成本中心用前缀谓词(含 admin-cost-center-exceptions* 等子查询),
|
||||
* 商品的模板归属由模板写回,所以同时失效接单模板列表。
|
||||
*/
|
||||
async function invalidateProductQueries() {
|
||||
await Promise.all([
|
||||
queryClient.invalidateQueries({
|
||||
predicate: (query) => String(query.queryKey[0] || '').startsWith('admin-cost-center'),
|
||||
}),
|
||||
queryClient.invalidateQueries({ queryKey: ['admin-worker-platform-product-rules'] }),
|
||||
])
|
||||
}
|
||||
|
||||
async function submitProduct(values: ProductFormValues) {
|
||||
try {
|
||||
if (editingProduct) {
|
||||
@@ -332,7 +353,7 @@ export function ProductCatalogPanel() {
|
||||
message.success('标准商品已创建')
|
||||
}
|
||||
setProductModalOpen(false)
|
||||
refreshAll()
|
||||
await invalidateProductQueries()
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? error.message : '保存标准商品失败')
|
||||
}
|
||||
@@ -442,9 +463,7 @@ export function ProductCatalogPanel() {
|
||||
try {
|
||||
await updateAdminProductCatalog(row.id, { status: checked ? 'active' : 'archived' })
|
||||
message.success(checked ? '标准商品已启用' : '标准商品已归档')
|
||||
await queryClient.invalidateQueries({ queryKey: ['admin-cost-center-products'] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-overview'] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['admin-cost-center-options'] })
|
||||
await invalidateProductQueries()
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? error.message : '更新商品状态失败')
|
||||
} finally {
|
||||
@@ -467,7 +486,7 @@ export function ProductCatalogPanel() {
|
||||
try {
|
||||
await deleteAdminProductCatalog(item.id)
|
||||
message.success('标准商品已删除')
|
||||
refreshAll()
|
||||
await invalidateProductQueries()
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? error.message : '删除标准商品失败')
|
||||
}
|
||||
@@ -630,25 +649,39 @@ export function ProductCatalogPanel() {
|
||||
title: '状态',
|
||||
dataIndex: 'status',
|
||||
width: 110,
|
||||
render: (value: ProductCatalogItem['status'], row) => (
|
||||
<Space size={6}>
|
||||
render: (value: ProductCatalogItem['status'], row) => {
|
||||
const templateManaged = row.templateRuleId != null
|
||||
const switchNode = (
|
||||
<Switch
|
||||
size="small"
|
||||
checked={value === 'active'}
|
||||
loading={productStatusUpdatingId === row.id}
|
||||
disabled={
|
||||
!canConfig || (productStatusUpdatingId !== null && productStatusUpdatingId !== row.id)
|
||||
templateManaged ||
|
||||
!canConfig ||
|
||||
(productStatusUpdatingId !== null && productStatusUpdatingId !== row.id)
|
||||
}
|
||||
onChange={(checked) => void toggleProductStatus(row, checked)}
|
||||
/>
|
||||
<Typography.Text
|
||||
type={value === 'active' ? 'success' : 'secondary'}
|
||||
style={{ fontSize: 12 }}
|
||||
>
|
||||
{value === 'active' ? '启用' : '归档'}
|
||||
</Typography.Text>
|
||||
</Space>
|
||||
),
|
||||
)
|
||||
return (
|
||||
<Space size={6}>
|
||||
{templateManaged ? (
|
||||
<Tooltip title="接单模板商品的状态由模板管理;删除模板后自动归档">
|
||||
<span style={{ display: 'inline-block' }}>{switchNode}</span>
|
||||
</Tooltip>
|
||||
) : (
|
||||
switchNode
|
||||
)}
|
||||
<Typography.Text
|
||||
type={value === 'active' ? 'success' : 'secondary'}
|
||||
style={{ fontSize: 12 }}
|
||||
>
|
||||
{value === 'active' ? '启用' : '归档'}
|
||||
</Typography.Text>
|
||||
</Space>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
@@ -931,8 +964,15 @@ export function ProductCatalogPanel() {
|
||||
<Form.Item name="unitName" label="单位">
|
||||
<Input placeholder="件 / 个 / 颗" />
|
||||
</Form.Item>
|
||||
<Form.Item name="status" label="状态">
|
||||
<Form.Item
|
||||
name="status"
|
||||
label="状态"
|
||||
extra={
|
||||
editingProduct?.templateRuleId != null ? '接单模板商品的状态由模板管理' : undefined
|
||||
}
|
||||
>
|
||||
<Select
|
||||
disabled={editingProduct?.templateRuleId != null}
|
||||
options={[
|
||||
{ value: 'active', label: '启用' },
|
||||
{ value: 'archived', label: '归档' },
|
||||
|
||||
@@ -318,6 +318,20 @@ export default function ProductRulesPanel() {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板写入口会同步写回标准商品(状态 / 名称 / 成本口径),
|
||||
* 所以保存、启停、删除模板后都要按前缀失效整个成本中心(含 admin-cost-center-exceptions*),
|
||||
* 不再逐个列举查询键。
|
||||
*/
|
||||
async function invalidateAfterTemplateWrite() {
|
||||
await Promise.all([
|
||||
queryClient.invalidateQueries({ queryKey: ['admin-worker-platform-product-rules'] }),
|
||||
queryClient.invalidateQueries({
|
||||
predicate: (query) => String(query.queryKey[0] || '').startsWith('admin-cost-center'),
|
||||
}),
|
||||
])
|
||||
}
|
||||
|
||||
async function saveRule(values: {
|
||||
ruleId?: number
|
||||
ruleKey?: string
|
||||
@@ -392,15 +406,10 @@ export default function ProductRulesPanel() {
|
||||
resetRuleForm()
|
||||
}
|
||||
await Promise.all([
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['admin-worker-platform-product-rules'],
|
||||
}),
|
||||
invalidateAfterTemplateWrite(),
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['admin-worker-platform-summary'],
|
||||
}),
|
||||
queryClient.invalidateQueries({
|
||||
predicate: (query) => String(query.queryKey[0] || '').startsWith('admin-cost-center'),
|
||||
}),
|
||||
])
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? error.message : '保存失败')
|
||||
@@ -441,12 +450,7 @@ export default function ProductRulesPanel() {
|
||||
autoCreate: rule.autoCreate,
|
||||
})
|
||||
message.success(`规则「${rule.ruleKey}」已${enabled ? '启用' : '停用'}`)
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ['admin-worker-platform-product-rules'],
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
predicate: (query) => String(query.queryKey[0] || '').startsWith('admin-cost-center'),
|
||||
})
|
||||
await invalidateAfterTemplateWrite()
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? error.message : '修改状态失败')
|
||||
}
|
||||
@@ -459,12 +463,12 @@ export default function ProductRulesPanel() {
|
||||
resetRuleForm()
|
||||
}
|
||||
message.success('接单模板已删除')
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ['admin-worker-platform-product-rules'],
|
||||
})
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ['admin-worker-platform-product-mappings'],
|
||||
})
|
||||
await Promise.all([
|
||||
invalidateAfterTemplateWrite(),
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['admin-worker-platform-product-mappings'],
|
||||
}),
|
||||
])
|
||||
} catch (error) {
|
||||
message.error(error instanceof Error ? error.message : '删除失败')
|
||||
}
|
||||
|
||||
@@ -66,6 +66,8 @@ export interface ProductCatalogItem {
|
||||
costVersionCount: number
|
||||
activeCostAmountFen?: number | null
|
||||
activeCostEffectiveFrom?: string | null
|
||||
/** 接管该商品的接单模板 id;非空表示状态由模板管理。 */
|
||||
templateRuleId?: number | null
|
||||
templatePricingMode?: 'fixed' | 'unit' | null
|
||||
templateEnabled?: boolean | null
|
||||
templateSharingEnabled?: boolean | null
|
||||
|
||||
Reference in New Issue
Block a user