feat(supply-chain): sync product archive designer from js_sku_info_manage
按 barcode=sku_id 从 rpt.js_sku_info_manage 回填产品全量档案 designer:空值补来源值,冲突行按用户口径取辛环;写入 fieldSourceMap=JST_SKU_INFO_MANAGE 并保留来源记录。附正式站 systemd timer 安装脚本(每天 09:00 Asia/Shanghai)。
This commit is contained in:
parent
a4e577e376
commit
5f4170919d
@ -19,6 +19,8 @@
|
||||
"sync:supply-chain-product-archive": "tsx scripts/sync-supply-chain-product-archive.ts",
|
||||
"sync:supply-chain-product-archive-daily": "tsx scripts/sync-supply-chain-product-archive-sales-performance-daily.ts",
|
||||
"sync:supply-chain-product-archive-enrichment": "tsx scripts/sync-supply-chain-product-archive-enrichment.ts",
|
||||
"backfill:product-archive-designer": "tsx scripts/backfill-product-archive-designer-from-jst-sku-info-manage.ts",
|
||||
"backfill:product-archive-designer:apply": "tsx scripts/backfill-product-archive-designer-from-jst-sku-info-manage.ts --apply",
|
||||
"sync:supply-chain-product-archive-sales-performance": "tsx scripts/sync-supply-chain-product-archive-sales-performance.ts",
|
||||
"sync:jst-sku-product-archive": "tsx scripts/sync-jst-sku-product-archive.ts",
|
||||
"sync:bom-cost-reference": "tsx scripts/sync-bom-cost-reference.ts",
|
||||
|
||||
@ -0,0 +1,90 @@
|
||||
import { loadEnvConfig } from '@next/env'
|
||||
|
||||
loadEnvConfig(process.cwd())
|
||||
|
||||
function readArgument(name: string) {
|
||||
const prefix = `--${name}=`
|
||||
return process.argv.slice(2).find((argument) => argument.startsWith(prefix))?.slice(prefix.length) ?? ''
|
||||
}
|
||||
|
||||
function readRequired(name: string) {
|
||||
const value = process.env[name]?.trim()
|
||||
if (!value) throw new Error(`${name} is required.`)
|
||||
return value
|
||||
}
|
||||
|
||||
function databaseIdentity(url: string) {
|
||||
const parsed = new URL(url)
|
||||
return [
|
||||
parsed.protocol.replace(/:$/, ''),
|
||||
decodeURIComponent(parsed.username || ''),
|
||||
parsed.hostname,
|
||||
parsed.port || '',
|
||||
parsed.pathname.replace(/^\//, ''),
|
||||
parsed.searchParams.get('schema') ?? '',
|
||||
].join('|')
|
||||
}
|
||||
|
||||
function sourceDatabaseIdentity() {
|
||||
return [
|
||||
'sqlserver',
|
||||
readRequired('JST_REPORT_DB_USER'),
|
||||
readRequired('JST_REPORT_DB_HOST'),
|
||||
process.env.JST_REPORT_DB_PORT?.trim() || '1433',
|
||||
readRequired('JST_REPORT_DB_NAME'),
|
||||
process.env.JST_REPORT_DB_SCHEMA?.trim() || 'rpt',
|
||||
].join('|')
|
||||
}
|
||||
|
||||
/**
|
||||
* 产品全量档案「设计师」按 rpt.js_sku_info_manage 回填。
|
||||
*
|
||||
* 默认 dry-run,只统计会改多少行;必须显式带 --apply 才写库。
|
||||
* 口径:
|
||||
* - 只按 barcode = sku_id 匹配;
|
||||
* - 档案设计师为空的行,补 js_sku_info_manage 的值;
|
||||
* - 档案已有值且与来源不一致的行,默认取 --conflict-designer(默认辛环)。
|
||||
* - 传 --expected-database-identity / --expected-source-database-identity 时,
|
||||
* 会校验目标库与来源库身份,供计划任务无人值守调用。
|
||||
*/
|
||||
async function main() {
|
||||
const apply = process.argv.includes('--apply')
|
||||
const batchSizeValue = Number(readArgument('batch-size') || '200')
|
||||
if (!Number.isSafeInteger(batchSizeValue) || batchSizeValue < 1 || batchSizeValue > 200) {
|
||||
throw new Error('--batch-size must be an integer between 1 and 200.')
|
||||
}
|
||||
const conflictDesigner = readArgument('conflict-designer')
|
||||
const expectedDatabaseIdentity = readArgument('expected-database-identity')
|
||||
if (expectedDatabaseIdentity && expectedDatabaseIdentity !== databaseIdentity(readRequired('DATABASE_URL'))) {
|
||||
throw new Error('expected database identity mismatch')
|
||||
}
|
||||
const expectedSourceIdentity = readArgument('expected-source-database-identity')
|
||||
if (expectedSourceIdentity && expectedSourceIdentity !== sourceDatabaseIdentity()) {
|
||||
throw new Error('expected source identity mismatch')
|
||||
}
|
||||
|
||||
const [
|
||||
{ prisma },
|
||||
{ supplyChainProductArchiveDesignerFillService },
|
||||
] = await Promise.all([
|
||||
import('../src/lib/prisma'),
|
||||
import('../src/server/services/supply-chain-product-archive-designer-fill.service'),
|
||||
])
|
||||
|
||||
try {
|
||||
console.log(apply ? 'mode=apply' : 'mode=dry-run')
|
||||
const result = await supplyChainProductArchiveDesignerFillService.syncAll({
|
||||
apply,
|
||||
batchSize: batchSizeValue,
|
||||
...(conflictDesigner ? { conflictDesigner } : {}),
|
||||
})
|
||||
console.log(JSON.stringify(result, null, 2))
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
void main().catch((error) => {
|
||||
console.error(error instanceof Error ? (error.stack || error.message) : String(error))
|
||||
process.exitCode = 1
|
||||
})
|
||||
@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
BASE="${FURNITURE_PROD_BASE:-/opt/furniture-3d-system-prod}"
|
||||
APP_ROOT="$BASE/current/furniture-3d-system"
|
||||
SERVICE_NAME="${PRODUCT_ARCHIVE_DESIGNER_SERVICE_NAME:-furniture-product-archive-designer}"
|
||||
SYNC_ENV="$BASE/shared/supply-chain-product-archive-sync.env"
|
||||
CALENDAR="${PRODUCT_ARCHIVE_DESIGNER_CALENDAR:-*-*-* 09:00:00 Asia/Shanghai}"
|
||||
|
||||
if [ "$(id -u)" -ne 0 ]; then echo "This installer requires root." >&2; exit 1; fi
|
||||
if [ ! -f "$APP_ROOT/scripts/backfill-product-archive-designer-from-jst-sku-info-manage.ts" ] || [ ! -f "$SYNC_ENV" ]; then
|
||||
echo "Missing deployed designer backfill script or guarded sync environment." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cat >"/etc/systemd/system/$SERVICE_NAME.service" <<EOF
|
||||
[Unit]
|
||||
Description=Publish product archive designer from rpt.js_sku_info_manage
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
[Service]
|
||||
Type=oneshot
|
||||
WorkingDirectory=$APP_ROOT
|
||||
Environment=NODE_ENV=production
|
||||
EnvironmentFile=$BASE/shared/.env
|
||||
EnvironmentFile=$SYNC_ENV
|
||||
ExecStart=/bin/bash -lc 'exec ./node_modules/.bin/tsx scripts/backfill-product-archive-designer-from-jst-sku-info-manage.ts --apply --expected-database-identity="\${SUPPLY_CHAIN_ARCHIVE_EXPECTED_DATABASE_IDENTITY:?Missing expected local identity}" --expected-source-database-identity="\${SUPPLY_CHAIN_ARCHIVE_EXPECTED_SOURCE_DATABASE_IDENTITY:?Missing expected source identity}"'
|
||||
TimeoutStartSec=2h
|
||||
Restart=on-failure
|
||||
RestartSec=30min
|
||||
EOF
|
||||
|
||||
cat >"/etc/systemd/system/$SERVICE_NAME.timer" <<EOF
|
||||
[Unit]
|
||||
Description=Product archive designer sync at Beijing 09:00
|
||||
[Timer]
|
||||
OnCalendar=$CALENDAR
|
||||
Persistent=true
|
||||
Unit=$SERVICE_NAME.service
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
EOF
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now "$SERVICE_NAME.timer"
|
||||
systemctl list-timers --all "$SERVICE_NAME.timer" --no-pager
|
||||
@ -20,6 +20,7 @@ export const JST_SKU_PRICE_ARCHIVE_SOURCE_CODE = 'JST_SKU_PRICE'
|
||||
export const PRODUCT_CATALOG_COST_ARCHIVE_SOURCE_CODE = 'PRODUCT_CATALOG_COST_PRICE'
|
||||
export const WAREHOUSE_CONFIG_ARCHIVE_SOURCE_CODE = 'WAREHOUSE_CONFIG'
|
||||
export const SYSTEM_USER_DEPARTMENT_ARCHIVE_SOURCE_CODE = 'SYSTEM_USER_DEPARTMENT'
|
||||
export const JST_SKU_INFO_MANAGE_ARCHIVE_SOURCE_CODE = 'JST_SKU_INFO_MANAGE'
|
||||
|
||||
export type ProductArchiveFieldSource = {
|
||||
sourceCode: string
|
||||
|
||||
@ -0,0 +1,89 @@
|
||||
import sql from 'mssql'
|
||||
|
||||
export type JstSkuInfoManageDesignerProfile = {
|
||||
barcode: string
|
||||
sku: string
|
||||
designer: string
|
||||
designerDept: string
|
||||
updatedAt: Date | null
|
||||
}
|
||||
|
||||
export interface JstSkuInfoManageDesignerRepository {
|
||||
listDesignerRows(): Promise<JstSkuInfoManageDesignerProfile[]>
|
||||
close(): Promise<void>
|
||||
}
|
||||
|
||||
function requiredEnv(name: string) {
|
||||
const value = process.env[name]?.trim()
|
||||
if (!value) throw new Error(`Missing required environment variable: ${name}`)
|
||||
return value
|
||||
}
|
||||
|
||||
function booleanEnv(name: string, fallback: boolean) {
|
||||
const value = process.env[name]?.trim().toLowerCase()
|
||||
if (!value) return fallback
|
||||
return value === 'true' || value === '1'
|
||||
}
|
||||
|
||||
function safeIdentifier(value: string, label: string) {
|
||||
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(value)) {
|
||||
throw new Error(`${label} is not a valid SQL Server identifier`)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
class JstSkuInfoManageDesignerRepositoryClass
|
||||
implements JstSkuInfoManageDesignerRepository
|
||||
{
|
||||
private pool: sql.ConnectionPool | null = null
|
||||
|
||||
private async getPool() {
|
||||
if (this.pool?.connected) return this.pool
|
||||
this.pool = await new sql.ConnectionPool({
|
||||
server: requiredEnv('JST_REPORT_DB_HOST'),
|
||||
port: Number(process.env.JST_REPORT_DB_PORT?.trim() || '1433'),
|
||||
database: requiredEnv('JST_REPORT_DB_NAME'),
|
||||
user: requiredEnv('JST_REPORT_DB_USER'),
|
||||
password: requiredEnv('JST_REPORT_DB_PASSWORD'),
|
||||
options: {
|
||||
encrypt: booleanEnv('JST_REPORT_DB_ENCRYPT', true),
|
||||
trustServerCertificate: booleanEnv('JST_REPORT_DB_TRUST_SERVER_CERTIFICATE', true),
|
||||
enableArithAbort: true,
|
||||
},
|
||||
pool: { min: 0, max: 4, idleTimeoutMillis: 30_000 },
|
||||
requestTimeout: 120_000,
|
||||
connectionTimeout: 30_000,
|
||||
}).connect()
|
||||
return this.pool
|
||||
}
|
||||
|
||||
// 只取有设计师的行:产品全量档案的「设计师」补充来源固定为这部分数据。
|
||||
async listDesignerRows() {
|
||||
const schema = safeIdentifier(
|
||||
process.env.JST_REPORT_DB_SCHEMA?.trim() || 'rpt',
|
||||
'JST_REPORT_DB_SCHEMA',
|
||||
)
|
||||
const pool = await this.getPool()
|
||||
const result = await pool.request().query<JstSkuInfoManageDesignerProfile>(`
|
||||
SELECT
|
||||
UPPER(LTRIM(RTRIM(CAST(sku_id AS nvarchar(200))))) AS barcode,
|
||||
UPPER(LTRIM(RTRIM(CAST(sku AS nvarchar(200))))) AS sku,
|
||||
LTRIM(RTRIM(CAST(designer AS nvarchar(200)))) AS designer,
|
||||
LTRIM(RTRIM(ISNULL(CAST(designer_dept AS nvarchar(200)), N''))) AS designerDept,
|
||||
update_time AS updatedAt
|
||||
FROM [${schema}].[js_sku_info_manage]
|
||||
WHERE designer IS NOT NULL
|
||||
AND LTRIM(RTRIM(CAST(designer AS nvarchar(200)))) <> ''
|
||||
`)
|
||||
return result.recordset
|
||||
}
|
||||
|
||||
async close() {
|
||||
if (!this.pool) return
|
||||
await this.pool.close()
|
||||
this.pool = null
|
||||
}
|
||||
}
|
||||
|
||||
export const jstSkuInfoManageDesignerRepository =
|
||||
new JstSkuInfoManageDesignerRepositoryClass()
|
||||
@ -0,0 +1,176 @@
|
||||
jest.mock('@/lib/prisma', () => ({ prisma: {} }))
|
||||
|
||||
jest.mock('@/server/repositories/jst-sku-info-manage-designer.repository', () => ({
|
||||
jstSkuInfoManageDesignerRepository: { listDesignerRows: jest.fn(), close: jest.fn() },
|
||||
}))
|
||||
|
||||
jest.mock('@/server/repositories/supply-chain-product-archive-enrichment-sync.repository', () => ({
|
||||
supplyChainProductArchiveEnrichmentSyncRepository: { listTargets: jest.fn(), applyPatch: jest.fn() },
|
||||
}))
|
||||
|
||||
jest.mock('../supply-chain-product-archive-change-log.service', () => ({
|
||||
recordProductArchiveFieldChanges: jest.fn().mockResolvedValue(undefined),
|
||||
}))
|
||||
|
||||
import { recordProductArchiveFieldChanges } from '../supply-chain-product-archive-change-log.service'
|
||||
import {
|
||||
decideProductArchiveDesignerFill,
|
||||
SupplyChainProductArchiveDesignerFillService,
|
||||
} from '../supply-chain-product-archive-designer-fill.service'
|
||||
|
||||
const mockRecordChanges = recordProductArchiveFieldChanges as jest.Mock
|
||||
|
||||
describe('decideProductArchiveDesignerFill', () => {
|
||||
it('skips targets without a source designer', () => {
|
||||
expect(
|
||||
decideProductArchiveDesignerFill({ currentDesigner: '', sourceDesigner: null }),
|
||||
).toEqual({ kind: 'skip' })
|
||||
})
|
||||
|
||||
it('fills an empty archive designer from the source', () => {
|
||||
expect(
|
||||
decideProductArchiveDesignerFill({ currentDesigner: '', sourceDesigner: '灵汐' }),
|
||||
).toEqual({ kind: 'fill', designer: '灵汐' })
|
||||
})
|
||||
|
||||
it('keeps an equal value untouched', () => {
|
||||
expect(
|
||||
decideProductArchiveDesignerFill({ currentDesigner: '辛环', sourceDesigner: '辛环' }),
|
||||
).toEqual({ kind: 'unchanged' })
|
||||
})
|
||||
|
||||
it('takes the agreed conflict designer for mismatched rows', () => {
|
||||
expect(
|
||||
decideProductArchiveDesignerFill({
|
||||
currentDesigner: '李煜',
|
||||
sourceDesigner: '轶尘',
|
||||
conflictDesigner: '辛环',
|
||||
}),
|
||||
).toEqual({ kind: 'override', designer: '辛环' })
|
||||
})
|
||||
|
||||
it('treats a row already on the conflict designer as unchanged', () => {
|
||||
expect(
|
||||
decideProductArchiveDesignerFill({
|
||||
currentDesigner: '辛环',
|
||||
sourceDesigner: '南知北',
|
||||
conflictDesigner: '辛环',
|
||||
}),
|
||||
).toEqual({ kind: 'unchanged' })
|
||||
})
|
||||
|
||||
it('keeps the archive value when no conflict designer is configured', () => {
|
||||
expect(
|
||||
decideProductArchiveDesignerFill({
|
||||
currentDesigner: '辛环',
|
||||
sourceDesigner: '南知北',
|
||||
conflictDesigner: '',
|
||||
}),
|
||||
).toEqual({ kind: 'conflict-kept' })
|
||||
})
|
||||
})
|
||||
|
||||
function createDependencies(input: {
|
||||
targets: Array<{ id: string; barcode: string; sku: string; designer: string }>
|
||||
sourceRows: Array<{ barcode: string; sku: string; designer: string }>
|
||||
}) {
|
||||
const listTargets = jest.fn().mockResolvedValueOnce(input.targets).mockResolvedValueOnce([])
|
||||
const applyPatch = jest.fn().mockResolvedValue({
|
||||
changes: [
|
||||
{ field: 'designer', fieldLabel: '设计师', previousValue: '', nextValue: '辛环' },
|
||||
],
|
||||
sourceInitialized: false,
|
||||
})
|
||||
const listDesignerRows = jest.fn().mockResolvedValue(
|
||||
input.sourceRows.map((row) => ({
|
||||
...row,
|
||||
designerDept: '',
|
||||
updatedAt: new Date('2026-09-27T08:00:00.000Z'),
|
||||
})),
|
||||
)
|
||||
const close = jest.fn().mockResolvedValue(undefined)
|
||||
return {
|
||||
archiveRepository: { listTargets, applyPatch },
|
||||
sourceRepository: { listDesignerRows, close },
|
||||
listTargets,
|
||||
applyPatch,
|
||||
close,
|
||||
}
|
||||
}
|
||||
|
||||
describe('SupplyChainProductArchiveDesignerFillService', () => {
|
||||
beforeEach(() => mockRecordChanges.mockClear())
|
||||
|
||||
it('counts planned changes without writing in dry-run mode', async () => {
|
||||
const dependencies = createDependencies({
|
||||
targets: [
|
||||
{ id: 'archive-1', barcode: 'A-1', sku: 'SKU-1', designer: '' },
|
||||
{ id: 'archive-2', barcode: 'A-2', sku: 'SKU-2', designer: '李煜' },
|
||||
{ id: 'archive-3', barcode: 'A-3', sku: 'SKU-3', designer: '灵汐' },
|
||||
],
|
||||
sourceRows: [
|
||||
{ barcode: 'A-1', sku: 'SKU-1', designer: '灵汐' },
|
||||
{ barcode: 'A-2', sku: 'SKU-2', designer: '轶尘' },
|
||||
{ barcode: 'A-3', sku: 'SKU-3', designer: '灵汐' },
|
||||
{ barcode: 'A-9', sku: 'SKU-9', designer: '财神' },
|
||||
],
|
||||
})
|
||||
const service = new SupplyChainProductArchiveDesignerFillService(dependencies)
|
||||
|
||||
const result = await service.syncAll({ apply: false, batchSize: 200 })
|
||||
|
||||
expect(result).toMatchObject({
|
||||
apply: false,
|
||||
sourceRows: 4,
|
||||
scanned: 3,
|
||||
matched: 3,
|
||||
filled: 1,
|
||||
overridden: 1,
|
||||
unchanged: 1,
|
||||
unmatched: 1,
|
||||
})
|
||||
expect(dependencies.applyPatch).not.toHaveBeenCalled()
|
||||
expect(mockRecordChanges).not.toHaveBeenCalled()
|
||||
expect(dependencies.close).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('fills empty rows as empty-only and overrides conflicts with the agreed designer', async () => {
|
||||
const dependencies = createDependencies({
|
||||
targets: [
|
||||
{ id: 'archive-1', barcode: 'A-1', sku: 'SKU-1', designer: '' },
|
||||
{ id: 'archive-2', barcode: 'A-2', sku: 'SKU-2', designer: '李煜' },
|
||||
],
|
||||
sourceRows: [
|
||||
{ barcode: 'A-1', sku: 'SKU-1', designer: '灵汐' },
|
||||
{ barcode: 'A-2', sku: 'SKU-2', designer: '轶尘' },
|
||||
],
|
||||
})
|
||||
const service = new SupplyChainProductArchiveDesignerFillService(dependencies)
|
||||
|
||||
const result = await service.syncAll({ apply: true, batchSize: 200 })
|
||||
|
||||
expect(result).toMatchObject({ filled: 1, overridden: 1, unchanged: 0 })
|
||||
expect(dependencies.applyPatch).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
expect.objectContaining({
|
||||
archiveId: 'archive-1',
|
||||
patch: { designer: '灵汐' },
|
||||
source: expect.objectContaining({
|
||||
sourceCode: 'JST_SKU_INFO_MANAGE',
|
||||
sourceObject: 'js_sku_info_manage',
|
||||
sourceColumns: { designer: 'designer' },
|
||||
fillEmptyOnly: true,
|
||||
}),
|
||||
}),
|
||||
)
|
||||
expect(dependencies.applyPatch).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
expect.objectContaining({
|
||||
archiveId: 'archive-2',
|
||||
patch: { designer: '辛环' },
|
||||
source: expect.not.objectContaining({ fillEmptyOnly: true }),
|
||||
}),
|
||||
)
|
||||
expect(mockRecordChanges).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
@ -0,0 +1,199 @@
|
||||
import { JST_SKU_INFO_MANAGE_ARCHIVE_SOURCE_CODE } from '@/lib/supply-chain-product-archive-field-source'
|
||||
import {
|
||||
supplyChainProductArchiveEnrichmentSyncRepository,
|
||||
type ProductArchiveEnrichmentPatch,
|
||||
type ProductArchiveEnrichmentSource,
|
||||
type SupplyChainProductArchiveEnrichmentSyncRepository,
|
||||
} from '@/server/repositories/supply-chain-product-archive-enrichment-sync.repository'
|
||||
import {
|
||||
jstSkuInfoManageDesignerRepository,
|
||||
type JstSkuInfoManageDesignerProfile,
|
||||
type JstSkuInfoManageDesignerRepository,
|
||||
} from '@/server/repositories/jst-sku-info-manage-designer.repository'
|
||||
import { recordProductArchiveFieldChanges } from './supply-chain-product-archive-change-log.service'
|
||||
|
||||
const JST_SOURCE_SYSTEM = '聚水潭 JSReportReplica'
|
||||
const JST_SOURCE_SCHEMA = 'rpt'
|
||||
const JST_DESIGNER_SOURCE_OBJECT = 'js_sku_info_manage'
|
||||
|
||||
// 用户 2026-09-28 明确口径:档案设计师与 js_sku_info_manage 不一致的行,统一取辛环。
|
||||
const DEFAULT_CONFLICT_DESIGNER = '辛环'
|
||||
|
||||
export type ProductArchiveDesignerFillDecision =
|
||||
| { kind: 'skip' }
|
||||
| { kind: 'unchanged' }
|
||||
| { kind: 'fill'; designer: string }
|
||||
| { kind: 'override'; designer: string }
|
||||
| { kind: 'conflict-kept' }
|
||||
|
||||
export function decideProductArchiveDesignerFill(input: {
|
||||
currentDesigner: string | null | undefined
|
||||
sourceDesigner: string | null | undefined
|
||||
conflictDesigner?: string | null
|
||||
}): ProductArchiveDesignerFillDecision {
|
||||
const sourceDesigner = input.sourceDesigner?.trim() ?? ''
|
||||
if (!sourceDesigner) return { kind: 'skip' }
|
||||
|
||||
const currentDesigner = input.currentDesigner?.trim() ?? ''
|
||||
if (!currentDesigner) return { kind: 'fill', designer: sourceDesigner }
|
||||
if (currentDesigner === sourceDesigner) return { kind: 'unchanged' }
|
||||
|
||||
const conflictDesigner = input.conflictDesigner?.trim() ?? ''
|
||||
if (!conflictDesigner) return { kind: 'conflict-kept' }
|
||||
if (currentDesigner === conflictDesigner) return { kind: 'unchanged' }
|
||||
return { kind: 'override', designer: conflictDesigner }
|
||||
}
|
||||
|
||||
export type ProductArchiveDesignerFillResult = {
|
||||
apply: boolean
|
||||
conflictDesigner: string
|
||||
sourceRows: number
|
||||
scanned: number
|
||||
matched: number
|
||||
filled: number
|
||||
overridden: number
|
||||
unchanged: number
|
||||
conflictKept: number
|
||||
unmatched: number
|
||||
lastId: string
|
||||
}
|
||||
|
||||
type DesignerFillDependencies = {
|
||||
archiveRepository: Pick<
|
||||
SupplyChainProductArchiveEnrichmentSyncRepository,
|
||||
'listTargets' | 'applyPatch'
|
||||
>
|
||||
sourceRepository: Pick<
|
||||
JstSkuInfoManageDesignerRepository,
|
||||
'listDesignerRows' | 'close'
|
||||
>
|
||||
}
|
||||
|
||||
export class SupplyChainProductArchiveDesignerFillService {
|
||||
constructor(
|
||||
private readonly dependencies: DesignerFillDependencies = {
|
||||
archiveRepository: supplyChainProductArchiveEnrichmentSyncRepository,
|
||||
sourceRepository: jstSkuInfoManageDesignerRepository,
|
||||
},
|
||||
) {}
|
||||
|
||||
async syncAll(input: {
|
||||
apply: boolean
|
||||
batchSize?: number
|
||||
conflictDesigner?: string
|
||||
}): Promise<ProductArchiveDesignerFillResult> {
|
||||
const apply = input.apply === true
|
||||
const batchSize = Math.min(200, Math.max(1, Math.trunc(input.batchSize ?? 200)))
|
||||
const conflictDesigner = input.conflictDesigner?.trim() || DEFAULT_CONFLICT_DESIGNER
|
||||
const result: ProductArchiveDesignerFillResult = {
|
||||
apply,
|
||||
conflictDesigner,
|
||||
sourceRows: 0,
|
||||
scanned: 0,
|
||||
matched: 0,
|
||||
filled: 0,
|
||||
overridden: 0,
|
||||
unchanged: 0,
|
||||
conflictKept: 0,
|
||||
unmatched: 0,
|
||||
lastId: '',
|
||||
}
|
||||
|
||||
try {
|
||||
const sourceRows = await this.dependencies.sourceRepository.listDesignerRows()
|
||||
result.sourceRows = sourceRows.length
|
||||
const byBarcode = new Map<string, JstSkuInfoManageDesignerProfile>()
|
||||
for (const row of sourceRows) {
|
||||
const barcode = row.barcode?.trim().toUpperCase() ?? ''
|
||||
if (barcode && !byBarcode.has(barcode)) byBarcode.set(barcode, row)
|
||||
}
|
||||
|
||||
const matchedBarcodes = new Set<string>()
|
||||
while (true) {
|
||||
const targets = await this.dependencies.archiveRepository.listTargets({
|
||||
afterId: result.lastId || undefined,
|
||||
limit: batchSize,
|
||||
})
|
||||
if (targets.length === 0) break
|
||||
|
||||
for (const target of targets) {
|
||||
result.scanned += 1
|
||||
const barcode = target.barcode?.trim().toUpperCase() ?? ''
|
||||
const profile = barcode ? byBarcode.get(barcode) : undefined
|
||||
if (!profile?.designer?.trim()) continue
|
||||
|
||||
matchedBarcodes.add(barcode)
|
||||
result.matched += 1
|
||||
const decision = decideProductArchiveDesignerFill({
|
||||
currentDesigner: target.designer,
|
||||
sourceDesigner: profile.designer,
|
||||
conflictDesigner,
|
||||
})
|
||||
if (decision.kind === 'skip') continue
|
||||
if (decision.kind === 'unchanged') {
|
||||
result.unchanged += 1
|
||||
continue
|
||||
}
|
||||
if (decision.kind === 'conflict-kept') {
|
||||
result.conflictKept += 1
|
||||
continue
|
||||
}
|
||||
if (!apply) {
|
||||
if (decision.kind === 'fill') result.filled += 1
|
||||
else result.overridden += 1
|
||||
continue
|
||||
}
|
||||
|
||||
const source: ProductArchiveEnrichmentSource = {
|
||||
sourceCode: JST_SKU_INFO_MANAGE_ARCHIVE_SOURCE_CODE,
|
||||
sourceSystem: JST_SOURCE_SYSTEM,
|
||||
sourceSchema: JST_SOURCE_SCHEMA,
|
||||
sourceObject: JST_DESIGNER_SOURCE_OBJECT,
|
||||
sourceRecordId: profile.barcode || profile.sku,
|
||||
sourceColumns: { designer: 'designer' },
|
||||
sourceRecordedAt: profile.updatedAt,
|
||||
...(decision.kind === 'fill' ? { fillEmptyOnly: true } : {}),
|
||||
}
|
||||
const patch: ProductArchiveEnrichmentPatch = { designer: decision.designer }
|
||||
const applied = await this.dependencies.archiveRepository.applyPatch({
|
||||
archiveId: target.id,
|
||||
patch,
|
||||
source,
|
||||
})
|
||||
if (applied.changes.length === 0) {
|
||||
result.unchanged += 1
|
||||
continue
|
||||
}
|
||||
if (decision.kind === 'fill') result.filled += 1
|
||||
else result.overridden += 1
|
||||
if (applied.sourceInitialized) continue
|
||||
await recordProductArchiveFieldChanges({
|
||||
barcode: target.barcode,
|
||||
actor: {
|
||||
source: 'SYSTEM',
|
||||
actorName: `${JST_SOURCE_SYSTEM} / ${JST_SOURCE_SCHEMA} / ${JST_DESIGNER_SOURCE_OBJECT}`,
|
||||
},
|
||||
source: {
|
||||
sourceSystem: JST_SOURCE_SYSTEM,
|
||||
sourceSchema: JST_SOURCE_SCHEMA,
|
||||
sourceObject: JST_DESIGNER_SOURCE_OBJECT,
|
||||
sourceRecordedAt: profile.updatedAt,
|
||||
},
|
||||
changes: applied.changes,
|
||||
})
|
||||
}
|
||||
|
||||
result.lastId = targets[targets.length - 1]?.id ?? result.lastId
|
||||
if (targets.length < batchSize) break
|
||||
}
|
||||
|
||||
result.unmatched = byBarcode.size - matchedBarcodes.size
|
||||
return result
|
||||
} finally {
|
||||
await this.dependencies.sourceRepository.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const supplyChainProductArchiveDesignerFillService =
|
||||
new SupplyChainProductArchiveDesignerFillService()
|
||||
Loading…
x
Reference in New Issue
Block a user