From 552b0d6e5d9f02c5f50e7407f0b274f4b926a043 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期四, 12 六月 2025 09:53:57 +0800
Subject: [PATCH] Merge branch 'clh'

---
 SourceCode/Bond/Servo/SystemLogManager.cpp |  222 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 222 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/Servo/SystemLogManager.cpp b/SourceCode/Bond/Servo/SystemLogManager.cpp
new file mode 100644
index 0000000..87a324c
--- /dev/null
+++ b/SourceCode/Bond/Servo/SystemLogManager.cpp
@@ -0,0 +1,222 @@
+#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::initializeLogTable() {
+    // 获取可执行文件路径
+    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 + "\\SystemLog.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);
+}
+
+// 添加日志(使用当前用户)
+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 "未知";
+    }
+}

--
Gitblit v1.9.3