"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthLogModel = void 0; const database_1 = require("../config/database"); class AuthLogModel { static async getAll(limit, offset) { let query = 'SELECT * FROM auth_log 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 auth_log WHERE id = ?'; const logs = await (0, database_1.executeQuery)(query, [id]); return logs.length > 0 ? logs[0] : null; } static async getByClientid(clientid, limit, offset) { let query = 'SELECT * FROM auth_log WHERE clientid = ? ORDER BY created_at DESC'; const params = [clientid]; 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 auth_log 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 getByOperationType(operationType, limit, offset) { let query = 'SELECT * FROM auth_log WHERE operation_type = ? ORDER BY created_at DESC'; const params = [operationType]; 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 getByResult(result, limit, offset) { let query = 'SELECT * FROM auth_log WHERE result = ? ORDER BY created_at DESC'; const params = [result]; 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 auth_log 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 auth_log'; const result = await (0, database_1.executeQuery)(query); return result[0].count; } static async getOperationTypeStats(startTime, endTime) { let query = ` SELECT operation_type, COUNT(*) as count FROM auth_log `; const params = []; if (startTime && endTime) { query += ' WHERE created_at BETWEEN ? AND ?'; params.push(startTime, endTime); } query += ' GROUP BY operation_type'; return await (0, database_1.executeQuery)(query, params); } static async getResultStats(startTime, endTime) { let query = ` SELECT result, COUNT(*) as count FROM auth_log `; const params = []; if (startTime && endTime) { query += ' WHERE created_at BETWEEN ? AND ?'; params.push(startTime, endTime); } query += ' GROUP BY result'; 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 result = 'success' THEN 1 ELSE 0 END) as success, SUM(CASE WHEN result = 'failure' THEN 1 ELSE 0 END) as failure FROM auth_log 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 getHourlyStats(hours = 24) { const query = ` SELECT HOUR(created_at) as hour, COUNT(*) as total, SUM(CASE WHEN result = 'success' THEN 1 ELSE 0 END) as success, SUM(CASE WHEN result = 'failure' THEN 1 ELSE 0 END) as failure FROM auth_log WHERE created_at >= DATE_SUB(NOW(), INTERVAL ? HOUR) GROUP BY HOUR(created_at) ORDER BY hour DESC `; return await (0, database_1.executeQuery)(query, [hours]); } static async getTopClients(limit = 10) { const query = ` SELECT clientid, COUNT(*) as count FROM auth_log WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) GROUP BY clientid ORDER BY count DESC LIMIT ? `; return await (0, database_1.executeQuery)(query, [limit]); } static async getTopIps(limit = 10) { const query = ` SELECT ip_address, COUNT(*) as count FROM auth_log WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) GROUP BY ip_address ORDER BY count DESC LIMIT ? `; return await (0, database_1.executeQuery)(query, [limit]); } static async create(authLog) { const query = ` INSERT INTO auth_log (clientid, username, ip_address, operation_type, result, reason, topic) VALUES (?, ?, ?, ?, ?, ?, ?) `; const params = [ authLog.clientid, authLog.username, authLog.ip_address, authLog.operation_type, authLog.result, authLog.reason || null, authLog.topic || null ]; const result = await (0, database_1.executeQuery)(query, params); return { ...authLog, id: result.insertId, created_at: new Date() }; } static async search(searchTerm, limit, offset) { let query = ` SELECT * FROM auth_log WHERE clientid LIKE ? OR username LIKE ? OR ip_address LIKE ? OR operation_type LIKE ? OR result LIKE ? OR reason LIKE ? OR topic LIKE ? ORDER BY created_at DESC `; const params = [ `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%`, `%${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 auth_log WHERE clientid LIKE ? OR username LIKE ? OR ip_address LIKE ? OR operation_type LIKE ? OR result LIKE ? OR reason LIKE ? OR topic LIKE ? `; const params = [ `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%`, `%${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 auth_log 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 auth_log ORDER BY created_at DESC LIMIT ?'; return await (0, database_1.executeQuery)(query, [limit]); } static async cleanupOldLogs(daysToKeep = 30) { return await this.cleanup(daysToKeep); } static async getFullStats() { try { const totalQuery = 'SELECT COUNT(*) as count FROM auth_log'; const totalResult = await (0, database_1.executeQuery)(totalQuery); const total = totalResult[0].count; const resultQuery = ` SELECT result, COUNT(*) as count FROM auth_log GROUP BY result `; const resultStats = await (0, database_1.executeQuery)(resultQuery); let success = 0; let failure = 0; resultStats.forEach((stat) => { if (stat.result === 'success') { success = stat.count; } else if (stat.result === 'failure') { failure = stat.count; } }); const operationQuery = ` SELECT operation_type, COUNT(*) as count FROM auth_log GROUP BY operation_type `; const operationStats = await (0, database_1.executeQuery)(operationQuery); const byOperationType = {}; operationStats.forEach((stat) => { byOperationType[stat.operation_type] = stat.count; }); const todayQuery = ` SELECT COUNT(*) as count FROM auth_log 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 auth_log 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 auth_log WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) `; const monthResult = await (0, database_1.executeQuery)(monthQuery); const month = monthResult[0].count; return { total, success, failure, byOperationType, byTimeRange: { today, week, month } }; } catch (error) { console.error('获取认证日志统计信息失败:', error); return { total: 0, success: 0, failure: 0, byOperationType: {}, byTimeRange: { today: 0, week: 0, month: 0 } }; } } static async getOperationStats(startTime, endTime) { return await this.getOperationTypeStats(startTime, endTime); } static async getCountByMultipleConditions(conditions, startTime, endTime, fuzzyFields) { let query = 'SELECT COUNT(*) as count FROM auth_log 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 auth_log 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 auth_log WHERE created_at BETWEEN ? AND ?'; const result = await (0, database_1.executeQuery)(query, [startTime, endTime]); return result[0].count; } static async getCountByResult(result) { const query = 'SELECT COUNT(*) as count FROM auth_log WHERE result = ?'; const queryResult = await (0, database_1.executeQuery)(query, [result]); return queryResult[0].count; } static async getCountByIpAddress(ipAddress) { const query = 'SELECT COUNT(*) as count FROM auth_log WHERE ip_address = ?'; const result = await (0, database_1.executeQuery)(query, [ipAddress]); return result[0].count; } static async getCountByOperationType(operationType) { const query = 'SELECT COUNT(*) as count FROM auth_log WHERE operation_type = ?'; const result = await (0, database_1.executeQuery)(query, [operationType]); return result[0].count; } static async getCountByUsername(username) { const query = 'SELECT COUNT(*) as count FROM auth_log WHERE username = ?'; const result = await (0, database_1.executeQuery)(query, [username]); return result[0].count; } static async getByIpAddress(ipAddress, limit, offset) { let query = 'SELECT * FROM auth_log WHERE ip_address = ? ORDER BY created_at DESC'; const params = [ipAddress]; if (limit !== undefined && offset !== undefined) { query += ' LIMIT ? OFFSET ?'; params.push(limit, offset); } return await (0, database_1.executeQuery)(query, params); } static async getCountByClientid(clientid) { const query = 'SELECT COUNT(*) as count FROM auth_log WHERE clientid = ?'; const result = await (0, database_1.executeQuery)(query, [clientid]); return result[0].count; } } exports.AuthLogModel = AuthLogModel; //# sourceMappingURL=authLog.js.map