chenluhua1980
2025-11-13 23c351be52a0567ac089b28e8f534c3ebf08b9a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "stdafx.h"
#include "SystemLogManager.h"
#include "UserManager.h"
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <ctime>
 
// ¾²Ì¬³ÉÔ±³õʼ»¯
std::mutex SystemLogManager::m_mutex;
 
// »ñÈ¡µ¥ÀýʵÀý
SystemLogManager& SystemLogManager::getInstance() {
    static SystemLogManager instance;
    return instance;
}
 
// ¹¹Ô캯Êý
SystemLogManager::SystemLogManager() : m_pDB(nullptr) {
    m_pDB = new BL::SQLiteDatabase();
}
 
// Îö¹¹º¯Êý
SystemLogManager::~SystemLogManager() {
    if (m_pDB) {
        delete m_pDB;
        m_pDB = nullptr;
    }
}
 
// ³õʼ»¯ÈÕÖ¾±í
bool SystemLogManager::initSystemLogTable() {
    // »ñÈ¡¿ÉÖ´ÐÐÎļþ·¾¶
    char szPath[MAX_PATH];
    GetModuleFileName(NULL, szPath, MAX_PATH);
    std::string exePath(szPath);
    std::string dbDir = exePath.substr(0, exePath.find_last_of("\\/")) + "\\DB";
 
    // ´´½¨ DB Ä¿Â¼
    if (!CreateDirectory(dbDir.c_str(), NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
        throw std::runtime_error("´´½¨Êý¾Ý¿âĿ¼ʧ°Ü");
    }
 
    // ¹¹ÔìÊý¾Ý¿â·¾¶
    std::string dbPath = dbDir + "\\SystemLogManager.db";
 
    // Á¬½ÓÊý¾Ý¿â
    if (!m_pDB->connect(dbPath, true)) {
        throw std::runtime_error("Á¬½ÓÈÕÖ¾Êý¾Ý¿âʧ°Ü");
    }
 
    // ´´½¨ÈÕÖ¾±í SQL Óï¾ä
    const std::string createTableQuery = R"(
        CREATE TABLE IF NOT EXISTS system_logs (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            log_type TEXT NOT NULL,
            event TEXT NOT NULL,
            username TEXT NOT NULL,
            timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
        )
    )";
 
    return m_pDB->executeQuery(createTableQuery);
}
 
// ÖÕÖ¹Êý¾Ý¿âÁ¬½Ó
void SystemLogManager::termSystemLogTable() {
    if (!m_pDB) {
        return;
    }
 
    m_pDB->disconnect();
}
 
// Ìí¼ÓÈÕÖ¾£¨Ê¹Óõ±Ç°Óû§£©
bool SystemLogManager::log(LogType logType, const std::string& event) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    cleanOldLogs();
 
    std::string username = UserManager::getInstance().getCurrentUser();
    if (username.empty()) {
        username = "SYSTEM";
    }
 
    std::ostringstream query;
    query << "INSERT INTO system_logs (log_type, event, username) VALUES ("
        << "'" << logTypeToString(logType) << "', "
        << "'" << event << "', "
        << "'" << username << "')";
 
    std::lock_guard<std::mutex> lock(m_mutex);
    return m_pDB->executeQuery(query.str());
}
 
// Ìí¼ÓÈÕÖ¾£¨Ö¸¶¨Óû§£©
bool SystemLogManager::log(LogType logType, const std::string& event, const std::string& username) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    cleanOldLogs();
 
    std::ostringstream query;
    query << "INSERT INTO system_logs (log_type, event, username) VALUES ("
        << "'" << logTypeToString(logType) << "', "
        << "'" << event << "', "
        << "'" << username << "')";
 
    std::lock_guard<std::mutex> lock(m_mutex);
    return m_pDB->executeQuery(query.str());
}
 
// »ñÈ¡ÈÕÖ¾ÄÚÈÝ
std::vector<std::vector<std::string>> SystemLogManager::getLogs(int startPosition, int count) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    if (startPosition == -1 && count == -1) {
        query << "SELECT id, log_type, event, username, datetime(timestamp, 'localtime') FROM system_logs";
    }
    else {
        query << "SELECT id, log_type, event, username, datetime(timestamp, 'localtime') FROM system_logs "
            << "LIMIT " << count << " OFFSET " << startPosition;
    }
 
    return m_pDB->fetchResults(query.str());
}
 
// »ñȡɸѡºóµÄÈÕÖ¾Êý¾Ý
std::vector<std::vector<std::string>> SystemLogManager::getFilteredLogs(
    const std::string& logType,
    const std::string& username,
    const std::string& description,
    const std::string& startTime,
    const std::string& endTime,
    int pageNumber,
    int pageSize)
{
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    query << "SELECT id, log_type, event, username, datetime(timestamp, 'localtime') FROM system_logs WHERE 1=1";
 
    if (logType != "ALL") {
        query << " AND log_type = '" << logType << "'";
    }
    if (username != "ALL") {
        query << " AND username = '" << username << "'";
    }
    if (!description.empty()) {
        query << " AND event LIKE '%" << description << "%'";
    }
    if (!startTime.empty()) {
        query << " AND timestamp >= '" << startTime << "'";
    }
    if (!endTime.empty()) {
        query << " AND timestamp <= '" << endTime << "'";
    }
 
    int offset = (pageNumber - 1) * pageSize;
    query << " ORDER BY timestamp DESC LIMIT " << pageSize << " OFFSET " << offset;
 
    return m_pDB->fetchResults(query.str());
}
 
// »ñÈ¡·ûºÏÌõ¼þµÄÈÕÖ¾×ÜÊý
int SystemLogManager::getTotalLogCount(
    const std::string& logType,
    const std::string& username,
    const std::string& description,
    const std::string& startTime,
    const std::string& endTime)
{
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    query << "SELECT COUNT(*) FROM system_logs WHERE 1=1";
 
    if (logType != "ALL") {
        query << " AND log_type = '" << logType << "'";
    }
    if (username != "ALL") {
        query << " AND username = '" << username << "'";
    }
    if (!description.empty()) {
        query << " AND event LIKE '%" << description << "%'";
    }
    if (!startTime.empty()) {
        query << " AND timestamp >= '" << startTime << "'";
    }
    if (!endTime.empty()) {
        query << " AND timestamp <= '" << endTime << "'";
    }
 
    auto results = m_pDB->fetchResults(query.str());
    return (!results.empty() && !results[0].empty()) ? std::stoi(results[0][0]) : 0;
}
 
// ÇåÀí³¬¹ýÖ¸¶¨ÌìÊýµÄ¾ÉÈÕÖ¾
void SystemLogManager::cleanOldLogs(int daysToKeep) {
    if (!m_pDB) {
        throw std::runtime_error("Database connection is not set.");
    }
 
    std::ostringstream query;
    query << "DELETE FROM system_logs WHERE timestamp < datetime('now', '-" << daysToKeep << " days')";
    m_pDB->executeQuery(query.str());
}
 
// ×ª»»ÈÕÖ¾ÀàÐÍΪ×Ö·û´®
std::string SystemLogManager::logTypeToString(LogType logType) {
    switch (logType) {
    case LogType::Info:
        return "ÐÅÏ¢";
    case LogType::Error:
        return "´íÎó";
    case LogType::Operation:
        return "²Ù×÷";
    default:
        return "δ֪";
    }
}