furniture-3d-system-Wu/furniture-3d-system/src/server/services/supply-chain-product-archive-enrichment-sync.mapper.ts
2026-08-11 17:19:29 +08:00

222 lines
7.5 KiB
TypeScript

import {
JST_SKU_PRICE_ARCHIVE_SOURCE_CODE,
JST_STOCK_MONITOR_ARCHIVE_SOURCE_CODE,
JST_SUPPLIER_ARCHIVE_SOURCE_CODE,
JST_WAREHOUSE_ARCHIVE_SOURCE_CODE,
PRODUCT_CATALOG_COST_ARCHIVE_SOURCE_CODE,
WAREHOUSE_CONFIG_ARCHIVE_SOURCE_CODE,
} from '@/lib/supply-chain-product-archive-field-source'
import type { JstSkuPriceProfile } from '@/server/repositories/jst-sku-price.repository'
import type { JstStockMonitorProfile } from '@/server/repositories/jst-stock-monitor.repository'
import type { JstSupplierProfile } from '@/server/repositories/jst-supplier-profile.repository'
import type { JstWarehouseProfile } from '@/server/repositories/jst-warehouse-profile.repository'
import type { ProductCatalogCostPriceProfile } from '@/server/repositories/product-catalog-cost-price.repository'
import type {
ProductArchiveEnrichmentPatch,
ProductArchiveEnrichmentSource,
ProductArchiveEnrichmentTarget,
} from '@/server/repositories/supply-chain-product-archive-enrichment-sync.repository'
import type { ProductArchiveWarehouseConfigRecord } from '@/server/repositories/supply-chain-product-archive-warehouse-config.repository'
const JST_SOURCE_SYSTEM = '聚水潭 JSReportReplica'
const JST_SOURCE_SCHEMA = 'rpt'
const PRODUCT_DEVELOPMENT_SOURCE_SYSTEM = '家具产品开发系统'
const EXTERNAL_SOURCE_SCHEMA = 'external'
const CONFIG_SOURCE_SYSTEM = '供应链中心'
const PUBLIC_SOURCE_SCHEMA = 'public'
export type ProductArchiveEnrichmentUpdate = {
patch: ProductArchiveEnrichmentPatch
source: ProductArchiveEnrichmentSource
}
export type ProductArchiveEnrichmentLookups = {
suppliers: readonly JstSupplierProfile[]
stocks: readonly JstStockMonitorProfile[]
warehouses: readonly JstWarehouseProfile[]
prices: readonly JstSkuPriceProfile[]
catalogCosts: readonly ProductCatalogCostPriceProfile[]
warehouseConfigs: readonly ProductArchiveWarehouseConfigRecord[]
}
function normalize(value: string | null | undefined) {
return value?.trim().toLocaleLowerCase('zh-CN') ?? ''
}
function normalizeIdentity(value: string | null | undefined) {
return value?.trim().toLocaleUpperCase('zh-CN') ?? ''
}
function supplierStatusLabel(code: string) {
if (code === '1') return '合作'
if (code === '2') return '不合作'
return ''
}
function findStockProfile(
target: ProductArchiveEnrichmentTarget,
profiles: readonly JstStockMonitorProfile[],
) {
const barcode = normalizeIdentity(target.barcode)
const exact = profiles.find(
(profile) => normalizeIdentity(profile.barcode) === barcode,
)
if (exact) return exact
const sku = normalizeIdentity(target.sku)
if (!sku) return undefined
const candidates = profiles.filter(
(profile) => normalizeIdentity(profile.sku) === sku,
)
return candidates.length === 1 ? candidates[0] : undefined
}
function source(
sourceCode: string,
sourceSystem: string,
sourceSchema: string,
sourceObject: string,
sourceRecordId: string,
sourceColumns: ProductArchiveEnrichmentSource['sourceColumns'],
): ProductArchiveEnrichmentSource {
return {
sourceCode,
sourceSystem,
sourceSchema,
sourceObject,
sourceRecordId,
sourceColumns,
}
}
export function buildProductArchiveEnrichmentUpdates(
target: ProductArchiveEnrichmentTarget,
lookups: ProductArchiveEnrichmentLookups,
): ProductArchiveEnrichmentUpdate[] {
const supplierProfile = lookups.suppliers.find(
(profile) => normalize(profile.supplierName) === normalize(target.supplierName),
)
const stockProfile = findStockProfile(target, lookups.stocks)
const warehouseProfile = lookups.warehouses.find(
(profile) => normalize(profile.warehouseName) === normalize(target.inOutWarehouse),
)
const warehouseConfig = lookups.warehouseConfigs.find(
(config) => normalize(config.warehouseName) === normalize(target.inOutWarehouse),
)
const price = lookups.prices.find(
(profile) => normalizeIdentity(profile.barcode) === normalizeIdentity(target.barcode),
)
const catalogCost = lookups.catalogCosts.find(
(profile) => normalizeIdentity(profile.barcode) === normalizeIdentity(target.barcode),
)
return [
{
patch: {
region: supplierProfile?.region ?? '',
supplierType: supplierProfile?.supplierType ?? '',
supplierStatus: supplierStatusLabel(supplierProfile?.supplierStatusCode ?? ''),
...(supplierProfile?.purchaser ? { purchaseOwner: supplierProfile.purchaser } : {}),
settlementMethod: supplierProfile?.settlementMethod ?? '',
boardProvider: supplierProfile?.boardProvider ?? '',
penaltyStatus: supplierProfile?.penaltyStatus ?? '',
},
source: source(
JST_SUPPLIER_ARCHIVE_SOURCE_CODE,
JST_SOURCE_SYSTEM,
JST_SOURCE_SCHEMA,
'js_supplier',
supplierProfile?.supplierName || target.supplierName || target.barcode,
{
region: 'region',
supplierType: 'scmtype',
supplierStatus: 'coopersgn',
purchaseOwner: 'buyer',
settlementMethod: 'paycyclesgn',
boardProvider: 'undefined2',
penaltyStatus: 'undefined1',
},
),
},
{
patch: {
...(stockProfile?.minimum
? { minimumOrderQuantity: stockProfile.minimum }
: {}),
firstLeadTime: stockProfile?.produceCycle ?? '',
palletSpec: stockProfile?.trayParameter ?? '',
dailySales: stockProfile?.purchaseSale ?? '',
sales3d: stockProfile?.threeAvgSale ?? '',
sales7d: stockProfile?.sevenAvgSale ?? '',
sales15d: stockProfile?.fifteenAvgSale ?? '',
},
source: source(
JST_STOCK_MONITOR_ARCHIVE_SOURCE_CODE,
JST_SOURCE_SYSTEM,
JST_SOURCE_SCHEMA,
'pm_stockMonitor',
stockProfile?.barcode || target.barcode,
{
minimumOrderQuantity: 'minimum',
firstLeadTime: 'produceCycle',
palletSpec: 'trayParameter',
dailySales: 'purchaseSale',
sales3d: 'threeAvgSale',
sales7d: 'sevenAvgSale',
sales15d: 'fifteenAvgSale',
},
),
},
{
patch: { warehouseType: warehouseProfile?.warehouseType ?? '' },
source: source(
JST_WAREHOUSE_ARCHIVE_SOURCE_CODE,
JST_SOURCE_SYSTEM,
JST_SOURCE_SCHEMA,
'jst_warehouseInfo',
warehouseProfile?.warehouseName || target.inOutWarehouse || target.barcode,
{ warehouseType: 'remark2' },
),
},
{
patch: {
warehouseHead: warehouseConfig?.warehouseHead ?? '',
storageType: warehouseConfig?.storageType ?? '',
},
source: source(
WAREHOUSE_CONFIG_ARCHIVE_SOURCE_CODE,
CONFIG_SOURCE_SYSTEM,
PUBLIC_SOURCE_SCHEMA,
'SupplyChainProductArchiveWarehouseConfig',
warehouseConfig?.id || target.inOutWarehouse || target.barcode,
{ warehouseHead: 'warehouseHead', storageType: 'storageType' },
),
},
{
patch: {
purchasePrice: price?.purchasePrice ?? '',
costPrice: price?.costPrice ?? '',
},
source: source(
JST_SKU_PRICE_ARCHIVE_SOURCE_CODE,
JST_SOURCE_SYSTEM,
JST_SOURCE_SCHEMA,
'jst_sku',
price?.barcode || target.barcode,
{ purchasePrice: 'purchase_price', costPrice: 'cost_price' },
),
},
{
patch: { costPriceSecondary: catalogCost?.costPrice ?? '' },
source: source(
PRODUCT_CATALOG_COST_ARCHIVE_SOURCE_CODE,
PRODUCT_DEVELOPMENT_SOURCE_SYSTEM,
EXTERNAL_SOURCE_SCHEMA,
'product_catalog_wide_v1',
catalogCost?.barcode || target.barcode,
{ costPriceSecondary: 'cost_price' },
),
},
]
}