style: 리팩토링 및 CSS 통합 작업 완료 (하드코딩 스타일 제거)
This commit is contained in:
72
rebuild_asset_codes.cjs
Normal file
72
rebuild_asset_codes.cjs
Normal file
@@ -0,0 +1,72 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
require('dotenv').config({ override: true });
|
||||
|
||||
const TYPE_PREFIX_MAP = {
|
||||
'서버': 'SVR', '워크스테이션': 'SVR', '개인PC': 'PC', '공용PC': 'PC', '서버PC': 'PC',
|
||||
'저장시스템_렉(NAS)': 'DSS', '저장시스템_렉(DAS)': 'DSS', '저장시스템_미니(NAS)': 'DSS', '저장시스템_미니(DAS)':'DSS',
|
||||
'저장매체': 'STM', 'HDD': 'HDD', 'SSD': 'SSD',
|
||||
'노트북': 'NBK', '태블릿': 'TAB',
|
||||
'드론': 'DRO', '측량장비': 'SUR', '보조기기': 'SUR', '허브': 'NET',
|
||||
'구독SW': 'SW', '영구SW': 'SW', '내부' : 'SW_INT', '외부':'SW_EXT'
|
||||
};
|
||||
|
||||
const CAT_PREFIX_MAP = {
|
||||
'서버': 'SVR', 'PC': 'PC', '저장매체': 'STM', '네트워크': 'NET',
|
||||
'공간정보장비': 'SUR', 'PC부품': 'PRT', '업무지원장비': 'EQP', '시설자산': 'FUR'
|
||||
};
|
||||
|
||||
function getPrefix(cat, type) {
|
||||
return TYPE_PREFIX_MAP[type] || TYPE_PREFIX_MAP[cat] || CAT_PREFIX_MAP[cat] || 'ETC';
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const pool = mysql.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_NAME,
|
||||
port: parseInt(process.env.DB_PORT || '3306')
|
||||
});
|
||||
|
||||
const connection = await pool.getConnection();
|
||||
try {
|
||||
const [rows] = await connection.query('SELECT id, category, asset_type, purchase_date, asset_code FROM asset_core ORDER BY purchase_date ASC, id ASC');
|
||||
console.log(`Found ${rows.length} assets to process.`);
|
||||
|
||||
// Grouping by prefix and YYMM
|
||||
const groups = {};
|
||||
|
||||
rows.forEach(row => {
|
||||
const prefix = getPrefix(row.category, row.asset_type);
|
||||
const datePart = (row.purchase_date && row.purchase_date.length >= 7)
|
||||
? row.purchase_date.replace(/-/g, '').substring(0, 6) // YYYYMM
|
||||
: '000000';
|
||||
|
||||
const groupKey = `${prefix}-${datePart}`;
|
||||
if (!groups[groupKey]) groups[groupKey] = [];
|
||||
groups[groupKey].push(row);
|
||||
});
|
||||
|
||||
let updatedCount = 0;
|
||||
for (const groupKey in groups) {
|
||||
const groupAssets = groups[groupKey];
|
||||
for (let i = 0; i < groupAssets.length; i++) {
|
||||
const asset = groupAssets[i];
|
||||
const nextNum = i + 1;
|
||||
const newCode = `${groupKey}-${String(nextNum).padStart(4, '0')}`;
|
||||
|
||||
if (asset.asset_code !== newCode) {
|
||||
await connection.query('UPDATE asset_core SET asset_code = ? WHERE id = ?', [newCode, asset.id]);
|
||||
updatedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ Successfully rebuilt asset codes. Total updated: ${updatedCount}`);
|
||||
} catch (err) {
|
||||
console.error('❌ Error rebuilding asset codes:', err);
|
||||
} finally {
|
||||
connection.release();
|
||||
await pool.end();
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user