refactor(fulfillment): 收敛配置与商品关联写入

This commit is contained in:
yml2213
2026-11-05 21:01:11 +08:00
parent fcd0c137d5
commit b424333a30
15 changed files with 481 additions and 182 deletions
@@ -22,6 +22,7 @@ import {
buildTemplateProductSku,
inferTemplateUnitName,
} from './work-order-template-product-service.js'
import { attachFulfillmentConfigurations } from '../fulfillment/fulfillment-state-service.js'
export { buildTemplateProductSku, inferTemplateUnitName }
@@ -621,10 +622,10 @@ function compareMappingCandidates(left: TemplateMappingCandidate, right: Templat
async function resetImportedTemplateProductBootstrap(range: { fromDate: string; toDate: string }) {
let resetOrderItemCount = 0
for (let batch = 0; batch < 20; batch += 1) {
const result = await query<{ id: number }>(
const candidates = await query<{ id: number; expected_product_id: number }>(
`
WITH candidates AS (
SELECT oi.id
SELECT oi.id, oi.canonical_product_id AS expected_product_id
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
JOIN channel_product_mappings m
@@ -655,17 +656,22 @@ async function resetImportedTemplateProductBootstrap(range: { fromDate: string;
BETWEEN $1::date AND $2::date
LIMIT 20000
)
UPDATE order_items oi
SET canonical_product_id = NULL,
updated_at = NOW()
FROM candidates c
WHERE oi.id = c.id
RETURNING oi.id
SELECT DISTINCT id, expected_product_id
FROM candidates
ORDER BY id
`,
[range.fromDate, range.toDate],
)
resetOrderItemCount += result.rowCount || 0
if ((result.rowCount || 0) < 20000) break
if (candidates.rows.length === 0) break
const reset = await attachFulfillmentConfigurations({
mappings: candidates.rows.map((row) => ({
orderItemId: Number(row.id),
canonicalProductId: null,
expectedCanonicalProductId: Number(row.expected_product_id),
})),
})
resetOrderItemCount += reset.length
if (candidates.rows.length < 20000) break
}
const deleted = await query(
`