fix(fulfillment): 冻结已分配订单的内部SKU事实

This commit is contained in:
yml2213
2026-11-03 12:38:40 +08:00
parent e6763882f5
commit ac3bb1f686
3 changed files with 109 additions and 24 deletions
@@ -91,7 +91,7 @@ export async function replaceOrderItemsWithExecutor(
for (const update of plan.updates) {
const effectiveItem = preserveAssignedOrderItemState(
existingById.get(update.orderItemId),
existingById.get(Number(update.orderItemId)),
update.item,
)
await executor(
@@ -107,8 +107,8 @@ export async function replaceOrderItemsWithExecutor(
WHERE id = $7
`,
[
update.item.skuCode,
update.item.skuName,
effectiveItem.skuCode,
effectiveItem.skuName,
update.item.quantity,
safeJsonStringify(effectiveItem.specJson || '{}'),
safeJsonStringify(effectiveItem.itemSnapshotJson || effectiveItem.specJson || '{}'),
@@ -197,6 +197,8 @@ export function preserveAssignedOrderItemState(
return {
...incoming,
skuCode: existing.sku_code,
skuName: existing.sku_name,
specJson: {
...incomingSpec,
...(frozenInternalSku ? { skuNick: frozenInternalSku } : {}),
@@ -342,25 +344,43 @@ export async function updateOrderItemSourceSnapshot(
orderItemId: number | string,
input: { skuName: string; itemSnapshotJson: string; updatedAt: string; specJson?: string },
): Promise<OrderItemRow | null> {
const specClause = input.specJson ? ', spec_json = $5::jsonb' : ''
const params: unknown[] = [
input.skuName,
input.itemSnapshotJson,
input.updatedAt,
Number(orderItemId),
]
if (input.specJson) {
params.push(input.specJson)
}
return withTransaction(async (client) => {
const selected = await client.query<OrderItemRow>(
`SELECT * FROM order_items WHERE id = $1 FOR UPDATE`,
[Number(orderItemId)],
)
const existing = selected.rows[0]
if (!existing) return null
const result = await query<{ id: number }>(
`
UPDATE order_items
SET sku_name = $1, item_snapshot_json = $2::jsonb, updated_at = $3 ${specClause}
WHERE id = $4
RETURNING id
`,
params,
)
return result.rows[0] ? getOrderItemById(orderItemId) : null
const effectiveItem = preserveAssignedOrderItemState(existing, {
skuCode: existing.sku_code,
skuName: input.skuName,
quantity: existing.quantity,
specJson: input.specJson || existing.spec_json || '{}',
itemSnapshotJson: input.itemSnapshotJson,
createdAt: String(existing.created_at || input.updatedAt),
updatedAt: input.updatedAt,
})
const specClause = input.specJson ? ', spec_json = $5::jsonb' : ''
const params: unknown[] = [
effectiveItem.skuName,
effectiveItem.itemSnapshotJson,
effectiveItem.updatedAt,
Number(orderItemId),
]
if (input.specJson) {
params.push(safeJsonStringify(effectiveItem.specJson || '{}'))
}
const result = await client.query<OrderItemRow>(
`
UPDATE order_items
SET sku_name = $1, item_snapshot_json = $2::jsonb, updated_at = $3 ${specClause}
WHERE id = $4
RETURNING *
`,
params,
)
return result.rows[0] || null
})
}