From cd15a4d2d8433354aaa007b76fcd8aaf98e94323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E5=B1=85?= Date: Tue, 3 Nov 2026 15:19:02 +0800 Subject: [PATCH] feat(discontinuation): suppress dedicated warehouse links --- .../架构设计/停产下架管理数据源-20261019.md | 3 + .../scripts/import-jst-shop-goods-export.ts | 123 ++++++++++++------ ...-chain-discontinuation-link-import.test.ts | 52 ++++++++ ...upply-chain-discontinuation-link-import.ts | 33 ++++- 4 files changed, 171 insertions(+), 40 deletions(-) diff --git a/furniture-3d-system/docs/架构设计/停产下架管理数据源-20261019.md b/furniture-3d-system/docs/架构设计/停产下架管理数据源-20261019.md index 73cd9425..24f77617 100644 --- a/furniture-3d-system/docs/架构设计/停产下架管理数据源-20261019.md +++ b/furniture-3d-system/docs/架构设计/停产下架管理数据源-20261019.md @@ -119,6 +119,9 @@ - 默认先执行单次全量导出,连续尝试 3 次;第 1、2 次失败后重置页面再重试, 任一次成功立即结束;只有三次都失败后才切换为每批 10,000 个编码的分批导出。 - 只保留 `online_status='是'` 且 `stock_sync_status='开启同步'` 的导出记录。 +- 编码维度命中专用仓时,链接维度按专用仓所属部门排除对应链接;例如 + `简尚-分销部专用仓` 只排除利润中心为“分销部”的链接,其他部门继续通知。 + 同一备注包含多个专用仓时逐个部门排除。 - 全部批次成功后,在数据库事务中替换当天链接;失败时保留已有完整链接。 - 最终有效写入为 0 时默认回滚;确有业务依据的正常零链接必须显式使用 `--allow-zero-links`,避免异常空文件清空当天快照。 diff --git a/furniture-3d-system/scripts/import-jst-shop-goods-export.ts b/furniture-3d-system/scripts/import-jst-shop-goods-export.ts index 02b2e790..202cc941 100644 --- a/furniture-3d-system/scripts/import-jst-shop-goods-export.ts +++ b/furniture-3d-system/scripts/import-jst-shop-goods-export.ts @@ -4,6 +4,7 @@ import sql from 'mssql' import { Client } from 'pg' import { runDiscontinuationLinkImportTransaction, + shouldExcludeDedicatedWarehouseLink, type DiscontinuationLinkImportStats, } from '@/server/services/supply-chain-discontinuation-link-import' @@ -21,6 +22,7 @@ function code(value: unknown) { type NoticeRecord = { id: string barcode: string + specialWarehouseNote: string } type OwnerRecord = { @@ -126,13 +128,13 @@ async function main() { const statTime = new Date() try { const noticeRows = (await db.query( - `SELECT id, barcode + `SELECT id, barcode, "specialWarehouseNote" FROM "SupplyChainDiscontinuationNotice" WHERE "statDate" = $1::date`, [statDate], )).rows const noticeByCode = new Map( - noticeRows.map((row) => [code(row.barcode), row.id]), + noticeRows.map((row) => [code(row.barcode), row]), ) if (!noticeByCode.size) throw new Error(`${statDate} 没有通知下架编码`) @@ -154,23 +156,31 @@ async function main() { let rawRows = 0 let skippedWithoutNotice = 0 let skippedNotOnline = 0 + let skippedByDedicatedWarehouse = 0 let writtenRows = 0 const insertBatch = async (rows: Array>) => { if (!rows.length) return const columns = 16 const values: unknown[] = [] - const tuples = rows.map((row, rowIndex) => { - const noticeId = noticeByCode.get(code(row.system_sku_code)) - if (!noticeId) return '' + const tuples = rows.map((row) => { + const notice = noticeByCode.get(code(row.system_sku_code)) + if (!notice) return '' const shopId = String(row.shop_id ?? '').trim() const itemId = code(row.product_id) const shopSkuCode = code(row.shop_sku_code) const owner = ownerByItemId.get(itemId) - const offset = rowIndex * columns + if (shouldExcludeDedicatedWarehouseLink( + notice.specialWarehouseNote, + owner?.profitCenter, + )) { + skippedByDedicatedWarehouse += 1 + return '' + } + const offset = values.length values.push( crypto.randomUUID(), - noticeId, + notice.id, `|${code(row.system_sku_code)}|`, shopId, itemId, @@ -215,7 +225,7 @@ async function main() { writtenRows += tuples.length } - await runDiscontinuationLinkImportTransaction({ + const storedStats = await runDiscontinuationLinkImportTransaction({ begin: () => db.query('BEGIN'), deleteExisting: () => db.query( `DELETE FROM "SupplyChainDiscontinuationNoticeLink" @@ -269,39 +279,73 @@ async function main() { writtenRows, skippedWithoutNotice, skippedNotOnline, + skippedByDedicatedWarehouse, allowZeroLinks, } satisfies DiscontinuationLinkImportStats - }) - - const blankItems = (await db.query<{ itemId: string }>( - `SELECT DISTINCT l."itemId" - FROM "SupplyChainDiscontinuationNoticeLink" l - INNER JOIN "SupplyChainDiscontinuationNotice" n ON n.id = l."noticeId" - WHERE n."statDate" = $1::date - AND l."ownerName" = ''`, - [statDate], - )).rows.map((row) => code(row.itemId)).filter(Boolean) - const remoteOwners = await loadRemoteOwners(blankItems) - for (const owner of remoteOwners.values()) { - await db.query( - `UPDATE "SupplyChainDiscontinuationNoticeLink" l - SET "ownerName" = $1, - "deptHead" = $2, - "profitCenter" = $3 - FROM "SupplyChainDiscontinuationNotice" n - WHERE l."noticeId" = n.id - AND n."statDate" = $4::date - AND l."itemId" = $5 + }, async (initialStats) => { + const blankItems = (await db.query<{ itemId: string }>( + `SELECT DISTINCT l."itemId" + FROM "SupplyChainDiscontinuationNoticeLink" l + INNER JOIN "SupplyChainDiscontinuationNotice" n ON n.id = l."noticeId" + WHERE n."statDate" = $1::date AND l."ownerName" = ''`, - [ - owner.ownerName, - owner.deptHead, - owner.profitCenter, - statDate, - owner.itemId, - ], - ) - } + [statDate], + )).rows.map((row) => code(row.itemId)).filter(Boolean) + const remoteOwners = await loadRemoteOwners(blankItems) + for (const owner of remoteOwners.values()) { + await db.query( + `UPDATE "SupplyChainDiscontinuationNoticeLink" l + SET "ownerName" = $1, + "deptHead" = $2, + "profitCenter" = $3 + FROM "SupplyChainDiscontinuationNotice" n + WHERE l."noticeId" = n.id + AND n."statDate" = $4::date + AND l."itemId" = $5 + AND l."ownerName" = ''`, + [ + owner.ownerName, + owner.deptHead, + owner.profitCenter, + statDate, + owner.itemId, + ], + ) + } + + const dedicatedWarehouseLinks = (await db.query<{ + id: string + specialWarehouseNote: string + profitCenter: string + }>( + `SELECT l.id, n."specialWarehouseNote", l."profitCenter" + FROM "SupplyChainDiscontinuationNoticeLink" l + INNER JOIN "SupplyChainDiscontinuationNotice" n ON n.id = l."noticeId" + WHERE n."statDate" = $1::date + AND n."specialWarehouseNote" <> '' + AND l."profitCenter" <> ''`, + [statDate], + )).rows + const excludedIds = dedicatedWarehouseLinks + .filter((row) => shouldExcludeDedicatedWarehouseLink( + row.specialWarehouseNote, + row.profitCenter, + )) + .map((row) => row.id) + for (const ids of chunk(excludedIds, 1_000)) { + await db.query( + `DELETE FROM "SupplyChainDiscontinuationNoticeLink" + WHERE id = ANY($1::text[])`, + [ids], + ) + } + return { + ...initialStats, + writtenRows: Math.max(0, initialStats.writtenRows - excludedIds.length), + skippedByDedicatedWarehouse: + (initialStats.skippedByDedicatedWarehouse ?? 0) + excludedIds.length, + } satisfies DiscontinuationLinkImportStats + }) const stored = await db.query<{ count: number }>( `SELECT COUNT(*)::int AS count @@ -315,7 +359,8 @@ async function main() { rawRows, skippedWithoutNotice, skippedNotOnline, - writtenRows, + skippedByDedicatedWarehouse: storedStats.skippedByDedicatedWarehouse ?? 0, + writtenRows: storedStats.writtenRows, storedRows: stored.rows[0]?.count ?? 0, allowZeroLinks, }, null, 2)) diff --git a/furniture-3d-system/src/server/services/__tests__/supply-chain-discontinuation-link-import.test.ts b/furniture-3d-system/src/server/services/__tests__/supply-chain-discontinuation-link-import.test.ts index 7da1c7c7..8618330a 100644 --- a/furniture-3d-system/src/server/services/__tests__/supply-chain-discontinuation-link-import.test.ts +++ b/furniture-3d-system/src/server/services/__tests__/supply-chain-discontinuation-link-import.test.ts @@ -1,9 +1,25 @@ import { assertLinkImportCanCommit, + dedicatedWarehouseProfitCenters, runDiscontinuationLinkImportTransaction, + shouldExcludeDedicatedWarehouseLink, } from '@/server/services/supply-chain-discontinuation-link-import' describe('discontinuation link import commit guard', () => { + it('maps dedicated warehouses to their responsible profit centers', () => { + expect(dedicatedWarehouseProfitCenters( + '简尚-分销部专用仓、简尚-运营C横林二仓专用仓', + )).toEqual(['分销部', '运营中心C']) + expect(shouldExcludeDedicatedWarehouseLink( + '简尚-分销部睢宁专用仓', + ' 分销部 ', + )).toBe(true) + expect(shouldExcludeDedicatedWarehouseLink( + '简尚-分销部睢宁专用仓', + '运营中心G', + )).toBe(false) + }) + it('rejects an empty file unless zero links is explicitly allowed', () => { expect(() => assertLinkImportCanCommit({ rawRows: 0, @@ -101,4 +117,40 @@ describe('discontinuation link import commit guard', () => { expect(statements).toEqual(['BEGIN', 'DELETE', 'WRITE', 'ROLLBACK']) expect(statements).not.toContain('COMMIT') }) + + it('checks the finalized count before committing a warehouse-filtered snapshot', async () => { + const statements: string[] = [] + await expect(runDiscontinuationLinkImportTransaction({ + begin: async () => { statements.push('BEGIN') }, + deleteExisting: async () => { statements.push('DELETE') }, + commit: async () => { statements.push('COMMIT') }, + rollback: async () => { statements.push('ROLLBACK') }, + }, async () => { + statements.push('WRITE') + return { + rawRows: 10, + writtenRows: 10, + skippedWithoutNotice: 0, + skippedNotOnline: 0, + skippedByDedicatedWarehouse: 0, + allowZeroLinks: false, + } + }, async (stats) => { + statements.push('FILTER') + return { + ...stats, + writtenRows: 0, + skippedByDedicatedWarehouse: 10, + } + })).rejects.toThrow('已回滚并保留旧快照') + + expect(statements).toEqual([ + 'BEGIN', + 'DELETE', + 'WRITE', + 'FILTER', + 'ROLLBACK', + ]) + expect(statements).not.toContain('COMMIT') + }) }) diff --git a/furniture-3d-system/src/server/services/supply-chain-discontinuation-link-import.ts b/furniture-3d-system/src/server/services/supply-chain-discontinuation-link-import.ts index 2dcc768c..20b3b18a 100644 --- a/furniture-3d-system/src/server/services/supply-chain-discontinuation-link-import.ts +++ b/furniture-3d-system/src/server/services/supply-chain-discontinuation-link-import.ts @@ -3,9 +3,36 @@ export type DiscontinuationLinkImportStats = { writtenRows: number skippedWithoutNotice: number skippedNotOnline: number + skippedByDedicatedWarehouse?: number allowZeroLinks: boolean } +function normalizeProfitCenter(value: unknown) { + const normalized = String(value ?? '').replace(/\s+/g, '') + if (normalized === '分销部') return normalized + const operations = normalized.match(/^运营(?:中心)?([A-Z])$/i) + return operations ? `运营中心${operations[1].toUpperCase()}` : normalized +} + +export function dedicatedWarehouseProfitCenters(note: unknown) { + const centers = new Set() + for (const segment of String(note ?? '').split(/[、,,;;]/)) { + if (segment.includes('分销部')) centers.add('分销部') + const operations = segment.match(/运营(?:中心)?\s*([A-Z])/i) + if (operations) centers.add(`运营中心${operations[1].toUpperCase()}`) + } + return Array.from(centers) +} + +export function shouldExcludeDedicatedWarehouseLink( + note: unknown, + profitCenter: unknown, +) { + const normalizedProfitCenter = normalizeProfitCenter(profitCenter) + return Boolean(normalizedProfitCenter) + && dedicatedWarehouseProfitCenters(note).includes(normalizedProfitCenter) +} + export function assertLinkImportCanCommit( input: DiscontinuationLinkImportStats, ) { @@ -33,11 +60,15 @@ export type DiscontinuationLinkImportTransaction = { export async function runDiscontinuationLinkImportTransaction( transaction: DiscontinuationLinkImportTransaction, write: () => Promise, + finalize?: ( + stats: DiscontinuationLinkImportStats, + ) => Promise, ) { await transaction.begin() try { await transaction.deleteExisting() - const stats = await write() + const writtenStats = await write() + const stats = finalize ? await finalize(writtenStats) : writtenStats assertLinkImportCanCommit(stats) await transaction.commit() return stats