"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SystemLogModel = void 0; const database_1 = require("../config/database"); class SystemLogModel { static async getAll(limit, offset) { let query = 'SELECT * FROM system_logs ORDER BY created_at DESC'; const params = []; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } return await (0, database_1.executeQuery)(query, params); } static async getById(id) { const query = 'SELECT * FROM system_logs WHERE id = ?'; const logs = await (0, database_1.executeQuery)(query, [id]); return logs.length > 0 ? logs[0] : null; } static async getByLevel(level, limit, offset) { let query = 'SELECT * FROM system_logs WHERE level = ? ORDER BY created_at DESC'; const params = [level]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } return await (0, database_1.executeQuery)(query, params); } static async getBySource(source, limit, offset) { let query = 'SELECT * FROM system_logs WHERE source = ? ORDER BY created_at DESC'; const params = [source]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } return await (0, database_1.executeQuery)(query, params); } static async getByModule(module, limit, offset) { let query = 'SELECT * FROM system_logs WHERE module = ? ORDER BY created_at DESC'; const params = [module]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } return await (0, database_1.executeQuery)(query, params); } static async getByTimeRange(startTime, endTime, limit, offset) { let query = 'SELECT * FROM system_logs WHERE created_at BETWEEN ? AND ? ORDER BY created_at DESC'; const params = [startTime, endTime]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } return await (0, database_1.executeQuery)(query, params); } static async getCount() { const query = 'SELECT COUNT(*) as count FROM system_logs'; const result = await (0, database_1.executeQuery)(query); return result[0].count; } static async getLevelStats(startTime, endTime) { let query = ` SELECT level, COUNT(*) as count FROM system_logs `; const params = []; if (startTime && endTime) { query += ' WHERE created_at BETWEEN ? AND ?'; params.push(startTime, endTime); } query += ' GROUP BY level'; return await (0, database_1.executeQuery)(query, params); } static async getSourceStats(startTime, endTime) { let query = ` SELECT source, COUNT(*) as count FROM system_logs `; const params = []; if (startTime && endTime) { query += ' WHERE created_at BETWEEN ? AND ?'; params.push(startTime, endTime); } query += ' GROUP BY source'; return await (0, database_1.executeQuery)(query, params); } static async getDailyStats(days = 7) { const query = ` SELECT DATE(created_at) as date, COUNT(*) as total, SUM(CASE WHEN level = 'info' THEN 1 ELSE 0 END) as info, SUM(CASE WHEN level = 'warn' THEN 1 ELSE 0 END) as warn, SUM(CASE WHEN level = 'error' THEN 1 ELSE 0 END) as error, SUM(CASE WHEN level = 'debug' THEN 1 ELSE 0 END) as debug FROM system_logs WHERE created_at >= DATE_SUB(CURRENT_DATE(), INTERVAL ? DAY) GROUP BY DATE(created_at) ORDER BY date DESC `; return await (0, database_1.executeQuery)(query, [days]); } static async create(systemLog) { const query = ` INSERT INTO system_logs (level, message, source, module, user_id, username, ip_address, details) VALUES (?, ?, ?, ?, ?, ?, ?, ?) `; const params = [ systemLog.level, systemLog.message, systemLog.source, systemLog.module || null, systemLog.user_id || null, systemLog.username || null, systemLog.ip_address || null, systemLog.details || null ]; const result = await (0, database_1.executeQuery)(query, params); return { ...systemLog, id: result.insertId, created_at: new Date() }; } static async search(searchTerm, limit, offset) { let query = ` SELECT * FROM system_logs WHERE message LIKE ? OR source LIKE ? OR module LIKE ? OR details LIKE ? ORDER BY created_at DESC `; const params = [ `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%` ]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } return await (0, database_1.executeQuery)(query, params); } static async getSearchCount(searchTerm) { const query = ` SELECT COUNT(*) as count FROM system_logs WHERE message LIKE ? OR source LIKE ? OR module LIKE ? OR details LIKE ? `; const params = [ `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%` ]; const result = await (0, database_1.executeQuery)(query, params); return result[0].count; } static async cleanup(daysToKeep = 30) { const query = 'DELETE FROM system_logs WHERE created_at < DATE_SUB(NOW(), INTERVAL ? DAY)'; const result = await (0, database_1.executeQuery)(query, [daysToKeep]); return result.affectedRows; } static async getRecent(limit = 10) { const query = 'SELECT * FROM system_logs ORDER BY created_at DESC LIMIT ?'; return await (0, database_1.executeQuery)(query, [limit]); } static async getByUserId(userId, limit, offset) { let query = 'SELECT * FROM system_logs WHERE user_id = ? ORDER BY created_at DESC'; const params = [userId]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } return await (0, database_1.executeQuery)(query, params); } static async getByUsername(username, limit, offset) { let query = 'SELECT * FROM system_logs WHERE username = ? ORDER BY created_at DESC'; const params = [username]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } return await (0, database_1.executeQuery)(query, params); } static async getFullStats() { try { const totalQuery = 'SELECT COUNT(*) as count FROM system_logs'; const totalResult = await (0, database_1.executeQuery)(totalQuery); const total = totalResult[0].count; const levelQuery = ` SELECT level, COUNT(*) as count FROM system_logs GROUP BY level `; const levelStats = await (0, database_1.executeQuery)(levelQuery); const byLevel = {}; levelStats.forEach((stat) => { byLevel[stat.level] = stat.count; }); const sourceQuery = ` SELECT source, COUNT(*) as count FROM system_logs GROUP BY source `; const sourceStats = await (0, database_1.executeQuery)(sourceQuery); const bySource = {}; sourceStats.forEach((stat) => { bySource[stat.source] = stat.count; }); const todayQuery = ` SELECT COUNT(*) as count FROM system_logs WHERE DATE(created_at) = CURDATE() `; const todayResult = await (0, database_1.executeQuery)(todayQuery); const today = todayResult[0].count; const weekQuery = ` SELECT COUNT(*) as count FROM system_logs WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) `; const weekResult = await (0, database_1.executeQuery)(weekQuery); const week = weekResult[0].count; const monthQuery = ` SELECT COUNT(*) as count FROM system_logs WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) `; const monthResult = await (0, database_1.executeQuery)(monthQuery); const month = monthResult[0].count; return { total, byLevel, bySource, byTimeRange: { today, week, month } }; } catch (error) { console.error('获取系统日志统计信息失败:', error); return { total: 0, byLevel: {}, bySource: {}, byTimeRange: { today: 0, week: 0, month: 0 } }; } } static async getCountByMultipleConditions(conditions, startTime, endTime, fuzzyFields) { let query = 'SELECT COUNT(*) as count FROM system_logs WHERE 1=1'; const params = []; for (const [key, value] of Object.entries(conditions)) { if (value !== undefined && value !== null) { if (fuzzyFields && fuzzyFields.includes(key)) { query += ` AND ${key} LIKE ?`; params.push(`%${value}%`); } else if (Array.isArray(value)) { const placeholders = value.map(() => '?').join(', '); query += ` AND ${key} IN (${placeholders})`; params.push(...value); } else { query += ` AND ${key} = ?`; params.push(value); } } } if (startTime && endTime) { query += ' AND created_at BETWEEN ? AND ?'; params.push(startTime, endTime); } const result = await (0, database_1.executeQuery)(query, params); return result[0].count; } static async getByMultipleConditions(conditions, startTime, endTime, limit, offset, fuzzyFields) { let query = 'SELECT * FROM system_logs WHERE 1=1'; const params = []; for (const [key, value] of Object.entries(conditions)) { if (value !== undefined && value !== null) { if (fuzzyFields && fuzzyFields.includes(key)) { query += ` AND ${key} LIKE ?`; params.push(`%${value}%`); } else if (Array.isArray(value)) { const placeholders = value.map(() => '?').join(', '); query += ` AND ${key} IN (${placeholders})`; params.push(...value); } else { query += ` AND ${key} = ?`; params.push(value); } } } if (startTime && endTime) { query += ' AND created_at BETWEEN ? AND ?'; params.push(startTime, endTime); } query += ' ORDER BY created_at DESC'; if (limit !== undefined && offset !== undefined) { query += ' LIMIT ? OFFSET ?'; params.push(limit, offset); } return await (0, database_1.executeQuery)(query, params); } static async getCountByTimeRange(startTime, endTime) { const query = 'SELECT COUNT(*) as count FROM system_logs WHERE created_at BETWEEN ? AND ?'; const result = await (0, database_1.executeQuery)(query, [startTime, endTime]); return result[0].count; } static async getCountByLevel(level) { const query = 'SELECT COUNT(*) as count FROM system_logs WHERE level = ?'; const result = await (0, database_1.executeQuery)(query, [level]); return result[0].count; } static async getCountBySource(source) { const query = 'SELECT COUNT(*) as count FROM system_logs WHERE source = ?'; const result = await (0, database_1.executeQuery)(query, [source]); return result[0].count; } static async getCountByModule(module) { const query = 'SELECT COUNT(*) as count FROM system_logs WHERE module = ?'; const result = await (0, database_1.executeQuery)(query, [module]); return result[0].count; } static async getCountByUserId(userId) { const query = 'SELECT COUNT(*) as count FROM system_logs WHERE user_id = ?'; const result = await (0, database_1.executeQuery)(query, [userId]); return result[0].count; } static async getCountByUsername(username) { const query = 'SELECT COUNT(*) as count FROM system_logs WHERE username = ?'; const result = await (0, database_1.executeQuery)(query, [username]); return result[0].count; } } exports.SystemLogModel = SystemLogModel; //# sourceMappingURL=systemLog.js.map