OrderNumberResolver, validation, and cleaner services had incomplete PostgreSQL support - they only handled SQL Server and MySQL, causing PostgreSQL to fall through to MySQL code paths with invalid syntax (backticks, ? placeholders) and missing schema.table name splitting. Changes: - Add PostgreSQL SQL generation ($N params, double-quoted identifiers) in OrderNumberResolver, validation-application-service, production-input-service, and validation-database - Add PostgreSQL to database factory functions in validation-database and cleaner-application-service - Add UPPER, LOWER, and 40+ common SQL functions to SQL_KEYWORDS to prevent prepareSql() from quoting them as identifiers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
112 lines
3.8 KiB
TypeScript
112 lines
3.8 KiB
TypeScript
import fs from 'fs'
|
|
import { ConfigManager } from '../config/config-manager'
|
|
import { SqlServerService } from '../database/sql-server'
|
|
import type { ValidationDatabaseService } from './validation-database'
|
|
import { getValidationTableName } from './validation-database'
|
|
|
|
export function readProductionIds(filePath: string): string[] {
|
|
const content = fs.readFileSync(filePath, 'utf-8')
|
|
return content
|
|
.split('\n')
|
|
.map((line) => line.trim())
|
|
.filter((line) => line.length > 0)
|
|
}
|
|
|
|
export function identifyInputType(input: string): 'production_id' | 'order_number' | 'unknown' {
|
|
if (/^SC\d{14}$/.test(input)) {
|
|
return 'order_number'
|
|
}
|
|
if (/^\d{2}[A-Za-z]\d{1,6}$/.test(input)) {
|
|
return 'production_id'
|
|
}
|
|
return 'unknown'
|
|
}
|
|
|
|
export async function getSourceNumbersFromInputs(
|
|
inputs: string[],
|
|
dbService: ValidationDatabaseService
|
|
): Promise<string[]> {
|
|
const productionIds: string[] = []
|
|
const orderNumbers: string[] = []
|
|
const configManager = ConfigManager.getInstance()
|
|
const dbType = configManager.getDatabaseType()
|
|
|
|
for (const item of inputs) {
|
|
const type = identifyInputType(item)
|
|
if (type === 'order_number') {
|
|
orderNumbers.push(item)
|
|
} else if (type === 'production_id') {
|
|
productionIds.push(item)
|
|
}
|
|
}
|
|
|
|
if (productionIds.length > 0) {
|
|
const contractTableName = getValidationTableName('productionContractData_26年压力表合同数据')
|
|
const batchSize = 2000
|
|
|
|
if (dbType === 'sqlserver') {
|
|
const sql = await import('mssql')
|
|
const allOrderNumbers: string[] = []
|
|
|
|
for (let i = 0; i < productionIds.length; i += batchSize) {
|
|
const batch = productionIds.slice(i, i + batchSize)
|
|
const placeholders = batch.map((_, idx) => `@p${idx}`).join(',')
|
|
const params: Record<string, { value: string; type: typeof sql.default.NVarChar }> = {}
|
|
|
|
batch.forEach((id, idx) => {
|
|
params[`p${idx}`] = { value: id, type: sql.default.NVarChar }
|
|
})
|
|
|
|
const contractSql = `
|
|
SELECT DISTINCT 生产订单号
|
|
FROM ${contractTableName}
|
|
WHERE 总排号 IN (${placeholders})
|
|
`
|
|
const contractResult = await (dbService as SqlServerService).queryWithParams(
|
|
contractSql,
|
|
params
|
|
)
|
|
allOrderNumbers.push(
|
|
...contractResult.rows.map((row: Record<string, unknown>) => row.生产订单号 as string)
|
|
)
|
|
}
|
|
|
|
orderNumbers.push(...allOrderNumbers)
|
|
} else if (dbType === 'postgresql') {
|
|
const allOrderNumbers: string[] = []
|
|
|
|
for (let i = 0; i < productionIds.length; i += batchSize) {
|
|
const batch = productionIds.slice(i, i + batchSize)
|
|
const placeholders = batch.map((_, idx) => `$${idx + 1}`).join(',')
|
|
const contractSql = `
|
|
SELECT DISTINCT "生产订单号"
|
|
FROM ${contractTableName}
|
|
WHERE "总排号" IN (${placeholders})
|
|
`
|
|
const contractResult = await dbService.query(contractSql, batch)
|
|
allOrderNumbers.push(...contractResult.rows.map((row) => row.生产订单号 as string))
|
|
}
|
|
|
|
orderNumbers.push(...allOrderNumbers)
|
|
} else {
|
|
const allOrderNumbers: string[] = []
|
|
|
|
for (let i = 0; i < productionIds.length; i += batchSize) {
|
|
const batch = productionIds.slice(i, i + batchSize)
|
|
const placeholders = batch.map(() => '?').join(',')
|
|
const contractSql = `
|
|
SELECT DISTINCT 生产订单号
|
|
FROM ${contractTableName}
|
|
WHERE 总排号 IN (${placeholders})
|
|
`
|
|
const contractResult = await dbService.query(contractSql, batch)
|
|
allOrderNumbers.push(...contractResult.rows.map((row) => row.生产订单号 as string))
|
|
}
|
|
|
|
orderNumbers.push(...allOrderNumbers)
|
|
}
|
|
}
|
|
|
|
return [...new Set(orderNumbers)]
|
|
}
|