Files
ITAM-test/fix_pc_codes.js
Taehoon a30f99f0ad feat: improve asset code generation and re-sequence assets by year
- Enhanced backend asset code generation logic to handle multiple tables
- Integrated asset code generation button in HWModal
- Included utility scripts for asset code migration and DB synchronization
- Resolved issues with missing purchase dates and duplicate asset codes
2026-06-02 14:40:06 +09:00

93 lines
2.8 KiB
JavaScript

import mysql from 'mysql2/promise';
import dotenv from 'dotenv';
dotenv.config();
const { DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT } = process.env;
async function run() {
const pool = mysql.createPool({
host: DB_HOST,
user: DB_USER,
password: DB_PASS,
database: DB_NAME,
port: parseInt(DB_PORT || '3306')
});
console.log('🔄 Step 1: Updating purchase_date for PC-000000 assets to 2015-12-01...');
const [updateResult] = await pool.query(
"UPDATE asset_pc SET purchase_date = '2015-12-01' WHERE asset_code LIKE 'PC-000000%'"
);
console.log(`✅ Updated ${updateResult.affectedRows} records.`);
console.log('🔄 Step 2: Re-sequencing all PC asset codes by Year...');
const [pcs] = await pool.query('SELECT * FROM asset_pc');
const getPrefix = (type) => {
const t = type || '';
if (t.includes('공용PC') || t.includes('개인PC') || t.includes('서버PC') || t === 'PC') return 'PC';
return 'ETC';
};
const parseYYYYMM = (dateStr) => {
if (!dateStr) return '000000';
const clean = dateStr.replace(/[^0-9]/g, '');
if (clean.length >= 6) return clean.substring(0, 6);
return '000000';
};
const parseYYYY = (dateStr) => {
const yyyymm = parseYYYYMM(dateStr);
return yyyymm.substring(0, 4);
};
// Group and sort
const mapped = pcs.map(item => {
const prefix = getPrefix(item.asset_type);
const yyyymm = parseYYYYMM(item.purchase_date);
const yyyy = yyyymm.substring(0, 4);
const sortKey = [
item.purchase_date || '9999-99-99',
item.model_name || '',
item.mainboard || '',
item.os || '',
item.id
].join('|');
return { ...item, prefix, yyyymm, yyyy, sortKey };
});
mapped.sort((a, b) => a.sortKey.localeCompare(b.sortKey));
const counters = {};
const updates = mapped.map(item => {
const groupKey = `${item.prefix}-${item.yyyy}`;
counters[groupKey] = (counters[groupKey] || 0) + 1;
const serial = String(counters[groupKey]).padStart(4, '0');
const newCode = `${item.prefix}-${item.yyyymm}-${serial}`;
return { id: item.id, asset_code: newCode };
});
console.log('💾 Reflecting changes to DB...');
const connection = await pool.getConnection();
try {
await connection.beginTransaction();
// Clear codes to avoid unique constraint issues
await connection.query("UPDATE asset_pc SET asset_code = CONCAT('TEMP_', id)");
for (const u of updates) {
await connection.query("UPDATE asset_pc SET asset_code = ? WHERE id = ?", [u.asset_code, u.id]);
}
await connection.commit();
console.log(`✅ Re-sequencing completed for ${updates.length} PCs.`);
} catch (err) {
await connection.rollback();
console.error('❌ Failed to re-sequence:', err);
} finally {
connection.release();
}
await pool.end();
}
run().catch(console.error);