115 lines
3.6 KiB
JavaScript
115 lines
3.6 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const { Client } = require('pg');
|
|
|
|
async function main() {
|
|
console.log('=== STARTING SKU MASTER TSV IMPORT ===');
|
|
|
|
const client = new Client({
|
|
host: 'paddleocr-db',
|
|
port: 5432,
|
|
user: 'postgres',
|
|
password: 'postgres',
|
|
database: 'dopfm'
|
|
});
|
|
|
|
try {
|
|
await client.connect();
|
|
console.log('Connected to database.');
|
|
|
|
// 1. Alter table to add new packaging columns if they don't exist
|
|
console.log('Ensuring table schema has new packaging columns...');
|
|
await client.query(`
|
|
ALTER TABLE sku_master ADD COLUMN IF NOT EXISTS standar_jumlah VARCHAR(50);
|
|
ALTER TABLE sku_master ADD COLUMN IF NOT EXISTS berat_kemasan NUMERIC(10, 3);
|
|
ALTER TABLE sku_master ADD COLUMN IF NOT EXISTS isi_outer_kg NUMERIC(10, 3);
|
|
ALTER TABLE sku_master ADD COLUMN IF NOT EXISTS isi_outer_pac INTEGER;
|
|
ALTER TABLE sku_master ADD COLUMN IF NOT EXISTS jenis_outer VARCHAR(50);
|
|
`);
|
|
console.log('Table schema verified/updated.');
|
|
|
|
// 2. Truncate old data
|
|
console.log('Clearing old SKU master data...');
|
|
await client.query('TRUNCATE TABLE sku_master RESTART IDENTITY CASCADE');
|
|
console.log('Old SKU master data cleared.');
|
|
|
|
// 3. Read and parse TSV file
|
|
const tsvPath = path.join(__dirname, 'sku_master.tsv');
|
|
if (!fs.existsSync(tsvPath)) {
|
|
throw new Error(`File not found at ${tsvPath}`);
|
|
}
|
|
|
|
const tsvContent = fs.readFileSync(tsvPath, 'utf8');
|
|
const lines = tsvContent.split(/\r?\n/);
|
|
|
|
let insertCount = 0;
|
|
let skipCount = 0;
|
|
|
|
console.log(`Parsing ${lines.length} lines from TSV...`);
|
|
|
|
// We start from line 5 (0-indexed 4 is the header row, lines before are title headers)
|
|
for (let i = 5; i < lines.length; i++) {
|
|
const line = lines[i].trim();
|
|
if (!line) continue;
|
|
|
|
const cols = line.split('\t').map(c => c.trim());
|
|
if (cols.length < 3) {
|
|
skipCount++;
|
|
continue;
|
|
}
|
|
|
|
const noSku = cols[1];
|
|
const namaItem = cols[2];
|
|
|
|
// Verify SKU code format (must be standard 8-digit)
|
|
if (!noSku || !/^\d{8}$/.test(noSku)) {
|
|
skipCount++;
|
|
continue;
|
|
}
|
|
|
|
const standarJumlah = cols[3] || null;
|
|
|
|
// Parse numeric columns
|
|
const beratKemasan = cols[4] ? parseFloat(cols[4].replace(',', '.')) : null;
|
|
const isiOuterKg = cols[5] ? parseFloat(cols[5].replace(',', '.')) : null;
|
|
const isiOuterPac = cols[6] ? parseInt(cols[6], 10) : null;
|
|
const jenisOuter = cols[7] || null;
|
|
|
|
await client.query(`
|
|
INSERT INTO sku_master (
|
|
no_sku, nama_item, standar_jumlah, berat_kemasan, isi_outer_kg, isi_outer_pac, jenis_outer
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
ON CONFLICT (no_sku) DO UPDATE SET
|
|
nama_item = EXCLUDED.nama_item,
|
|
standar_jumlah = EXCLUDED.standar_jumlah,
|
|
berat_kemasan = EXCLUDED.berat_kemasan,
|
|
isi_outer_kg = EXCLUDED.isi_outer_kg,
|
|
isi_outer_pac = EXCLUDED.isi_outer_pac,
|
|
jenis_outer = EXCLUDED.jenis_outer
|
|
`, [
|
|
noSku,
|
|
namaItem,
|
|
standarJumlah,
|
|
isNaN(beratKemasan) ? null : beratKemasan,
|
|
isNaN(isiOuterKg) ? null : isiOuterKg,
|
|
isNaN(isiOuterPac) ? null : isiOuterPac,
|
|
jenisOuter
|
|
]);
|
|
|
|
insertCount++;
|
|
}
|
|
|
|
console.log(`\nImport Completed Successfully:`);
|
|
console.log(`- Inserted/Updated: ${insertCount} SKU records`);
|
|
console.log(`- Skipped (headers/invalid): ${skipCount} lines`);
|
|
|
|
} catch (err) {
|
|
console.error('Import process failed:', err);
|
|
} finally {
|
|
await client.end();
|
|
console.log('Database connection closed.');
|
|
}
|
|
}
|
|
|
|
main();
|