From 1318cc77e20a82b3328aa82ea6b8d8ca600de44f Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期二, 10 六月 2025 15:51:55 +0800
Subject: [PATCH] 1. 拆分数据库,每一个管理类是单独的数据库文件 2. 修复复制产生的ID错误

---
 SourceCode/Bond/Servo/SystemLogManager.cpp |   35 ++++++--
 SourceCode/Bond/Servo/Servo.cpp            |   33 ++++++++
 SourceCode/Bond/Servo/Servo.rc             |    0 
 SourceCode/Bond/Servo/SystemLogManager.h   |    3 
 SourceCode/Bond/Servo/ServoDlg.cpp         |   97 ++++++++++++++++++++++++
 SourceCode/Bond/Servo/UserManager.cpp      |   14 +-
 SourceCode/Bond/Servo/ServoDlg.h           |    1 
 7 files changed, 163 insertions(+), 20 deletions(-)

diff --git a/SourceCode/Bond/Servo/Servo.cpp b/SourceCode/Bond/Servo/Servo.cpp
index 657947a..74298c4 100644
--- a/SourceCode/Bond/Servo/Servo.cpp
+++ b/SourceCode/Bond/Servo/Servo.cpp
@@ -9,6 +9,8 @@
 #include "AlarmManager.h"
 #include "SECSRuntimeManager.h"
 #include "TransferManager.h"
+#include "SystemLogManager.h"
+#include "UserManager.h"
 #include "VerticalLine.h"
 #include "HorizontalLine.h"
 #include "EqsGraphWnd.h"
@@ -162,6 +164,37 @@
 		return FALSE;
 	}
 
+	// 初始化运行日志管理库
+	try {
+		if (!SystemLogManager::getInstance().initializeLogTable()) {
+			AfxMessageBox("初始化运行日志管理库失败!");
+			return FALSE;
+		}
+	}
+	catch (const std::exception& ex) {
+		CString errorMsg;
+		errorMsg.Format(_T("初始化运行日志管理库失败:%s"), CString(ex.what()));
+		AfxMessageBox(errorMsg, MB_ICONERROR);
+		return FALSE;
+	}
+
+	// 初始化用户管理库
+	try {
+		UserManager& userManager = UserManager::getInstance();
+#if !defined(_DEBUG)
+		userManager.initializeIdleDetection(AfxGetMainWnd()->m_hWnd);
+		SetTimer(1, 60000, nullptr);
+#endif
+		userManager.loadSession();
+	}
+	catch (const std::exception& ex) {
+		CString errorMsg;
+		errorMsg.Format(_T("初始化用户管理库失败:%s"), CString(ex.what()));
+		AfxMessageBox(errorMsg, MB_ICONERROR);
+		return FALSE;
+	}
+
+
 
 	CServoDlg dlg;
 	m_pMainWnd = &dlg;
diff --git a/SourceCode/Bond/Servo/Servo.rc b/SourceCode/Bond/Servo/Servo.rc
index d94e990..07190ea 100644
--- a/SourceCode/Bond/Servo/Servo.rc
+++ b/SourceCode/Bond/Servo/Servo.rc
Binary files differ
diff --git a/SourceCode/Bond/Servo/ServoDlg.cpp b/SourceCode/Bond/Servo/ServoDlg.cpp
index c99cced..b8c863c 100644
--- a/SourceCode/Bond/Servo/ServoDlg.cpp
+++ b/SourceCode/Bond/Servo/ServoDlg.cpp
@@ -15,6 +15,12 @@
 #include "HmTab.h"
 #include "CRobotCmdContainerDlg.h"
 #include "CRobotCmdTestDlg.h"
+#include "LoginDlg.h"
+#include "ChangePasswordDlg.h"
+#include "UserManagerDlg.h"
+#include "SystemLogManagerDlg.h"
+#include "UserManager.h"
+#include "SystemLogManager.h"
 
 
 #ifdef _DEBUG
@@ -347,6 +353,12 @@
 
 	// 初始化master以后需要控件绑定数据
 	m_pPageGraph1->BindEquipmentToGraph();
+
+
+	// 更新登录状态
+	UpdateLoginStatus();
+	//SystemLogManager::getInstance.log(SystemLogManager::LogType::Info, _T("BondEq启动..."));
+	//SystemLogManager::getInstance.
 
 
 	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
@@ -806,6 +818,39 @@
 	}
 }
 
+void CServoDlg::UpdateLoginStatus()
+{
+	HMENU hMenu = m_pTopToolbar->GetOperatorMenu();
+	UserManager& userManager = UserManager::getInstance();
+	if (userManager.isLoggedIn())
+	{
+		::EnableMenuItem(hMenu, ID_OPEATOR_LOGIN, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
+		::EnableMenuItem(hMenu, ID_OPERATOR_CHANGE_PASSWORD, MF_BYCOMMAND | MF_ENABLED);
+		::EnableMenuItem(hMenu, ID_OPERATOR_SYSTEM_LOG, MF_BYCOMMAND | MF_ENABLED);
+		::EnableMenuItem(hMenu, ID_OPEATOR_SWITCH, MF_BYCOMMAND | MF_ENABLED);
+		::EnableMenuItem(hMenu, ID_OPERATOR_LOGOUT, MF_BYCOMMAND | MF_ENABLED);
+
+		if (userManager.getCurrentUserRole() == UserRole::SuperAdmin) {
+			::EnableMenuItem(hMenu, ID_OPEATOR_USER_MANAGER, MF_BYCOMMAND | MF_ENABLED);
+		}
+		else {
+			::EnableMenuItem(hMenu, ID_OPEATOR_USER_MANAGER, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
+		}
+
+		m_pTopToolbar->SetOperatorBtnText(userManager.getCurrentUser().c_str());
+	}
+	else {
+		::EnableMenuItem(hMenu, ID_OPEATOR_LOGIN, MF_BYCOMMAND | MF_ENABLED);
+		::EnableMenuItem(hMenu, ID_OPERATOR_CHANGE_PASSWORD, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
+		::EnableMenuItem(hMenu, ID_OPEATOR_USER_MANAGER, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
+		::EnableMenuItem(hMenu, ID_OPERATOR_SYSTEM_LOG, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
+		::EnableMenuItem(hMenu, ID_OPEATOR_SWITCH, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
+		::EnableMenuItem(hMenu, ID_OPERATOR_LOGOUT, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
+
+		m_pTopToolbar->SetOperatorBtnText(_T("未登录"));
+	}
+}
+
 LRESULT CServoDlg::OnToolbarBtnClicked(WPARAM wParam, LPARAM lParam)
 {
 	int id = (int)lParam;
@@ -823,6 +868,58 @@
 		dlg.SetEFEM(pEFEM);
 		dlg.DoModal();
 	}
+	else if (id == IDC_BUTTON_OPERATOR) {
+		int menuId = (int)wParam;
+		SystemLogManager& logManager = SystemLogManager::getInstance();
+		UserManager& userManager = UserManager::getInstance();
+		if (menuId == 0) {
+			CLoginDlg loginDlg;
+			loginDlg.DoModal();
+		}
+		else if (1 == menuId) {
+			CChangePasswordDlg changePasswordDlg;
+			changePasswordDlg.DoModal();
+		}
+		else if (2 == menuId) {
+			CUserManagerDlg dlg;
+			if (dlg.DoModal() != IDOK) {
+				logManager.log(SystemLogManager::LogType::Operation, _T("用户管理的预操作被取消!"));
+			}
+		}
+		else if (3 == menuId) {
+			CSystemLogManagerDlg dlg;
+			dlg.DoModal();
+		}
+		else if (4 == menuId) {
+			int ret = AfxMessageBox(_T("是否切换用户?切换用户会退出当前账号!"), MB_OKCANCEL | MB_ICONEXCLAMATION);
+			if (ret != IDOK) {
+				return 0;
+			}
+
+			logManager.log(SystemLogManager::LogType::Operation, _T("确认切换角色!"));
+			if (userManager.isLoggedIn()) {
+				logManager.log(SystemLogManager::LogType::Info, _T("退出登录!"));
+				userManager.logout();
+			}
+
+			CLoginDlg loginDlg;
+			loginDlg.DoModal();
+		}
+		else {
+			CString cstrMessage;
+			cstrMessage.Format(_T("是否退出用户 [%s]?"), userManager.getCurrentUser().c_str());
+			int ret = AfxMessageBox(_T(cstrMessage), MB_OKCANCEL | MB_ICONEXCLAMATION);
+			if (ret != IDOK) {
+				return 0;
+			}
+
+			logManager.log(SystemLogManager::LogType::Info, _T("退出登录!"));
+			userManager.logout();
+		}
+
+		UpdateLoginStatus();
+	}
+
 	return 0;
 }
 
diff --git a/SourceCode/Bond/Servo/ServoDlg.h b/SourceCode/Bond/Servo/ServoDlg.h
index fbf4f05..454d778 100644
--- a/SourceCode/Bond/Servo/ServoDlg.h
+++ b/SourceCode/Bond/Servo/ServoDlg.h
@@ -34,6 +34,7 @@
 	void InitRxWindows();
 	void Resize();
 	void ShowChildPage(int index);
+	void UpdateLoginStatus();
 	CString& GetRuntimeFormatText(CString& strText, const char* pszSuffix);
 
 private:
diff --git a/SourceCode/Bond/Servo/SystemLogManager.cpp b/SourceCode/Bond/Servo/SystemLogManager.cpp
index 7117af7..87a324c 100644
--- a/SourceCode/Bond/Servo/SystemLogManager.cpp
+++ b/SourceCode/Bond/Servo/SystemLogManager.cpp
@@ -16,25 +16,40 @@
 }
 
 // 构造函数
-SystemLogManager::SystemLogManager() : m_pDB(nullptr) {}
+SystemLogManager::SystemLogManager() : m_pDB(nullptr) {
+    m_pDB = new BL::SQLiteDatabase();
+}
 
 // 析构函数
 SystemLogManager::~SystemLogManager() {
-    m_pDB = nullptr; // 清除指针引用
-}
-
-// 设置数据库连接
-void SystemLogManager::setDatabase(BL::Database* db) {
-    std::lock_guard<std::mutex> lock(m_mutex);
-    m_pDB = db;
+    if (m_pDB) {
+        delete m_pDB;
+        m_pDB = nullptr;
+    }
 }
 
 // 初始化日志表
 bool SystemLogManager::initializeLogTable() {
-    if (!m_pDB) {
-        throw std::runtime_error("Database connection is not set.");
+    // 获取可执行文件路径
+    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,
diff --git a/SourceCode/Bond/Servo/SystemLogManager.h b/SourceCode/Bond/Servo/SystemLogManager.h
index 764450c..5ae9ab0 100644
--- a/SourceCode/Bond/Servo/SystemLogManager.h
+++ b/SourceCode/Bond/Servo/SystemLogManager.h
@@ -20,9 +20,6 @@
     // 获取单例实例
     static SystemLogManager& getInstance();
 
-    // 设置数据库连接
-    void setDatabase(BL::Database* db);
-
     // 初始化日志表
     bool initializeLogTable();
 
diff --git a/SourceCode/Bond/Servo/UserManager.cpp b/SourceCode/Bond/Servo/UserManager.cpp
index db9f1e5..cceff0c 100644
--- a/SourceCode/Bond/Servo/UserManager.cpp
+++ b/SourceCode/Bond/Servo/UserManager.cpp
@@ -157,18 +157,18 @@
 
 // 获取程序路径下的config文件夹路径
 std::string UserManager::getConfigFolderPath() {
-    char path[MAX_PATH];
-    GetModuleFileName(NULL, path, MAX_PATH);
-    std::string exePath = std::string(path).substr(0, std::string(path).find_last_of("\\/"));
-    std::string configPath = exePath + "\\Config\\";
+    char szPath[MAX_PATH];
+    GetModuleFileName(NULL, szPath, MAX_PATH);
+    std::string exePath(szPath);
+    std::string dbDir = exePath.substr(0, exePath.find_last_of("\\/")) + "\\DB";
 
     // 检查并创建config文件夹
-    DWORD fileAttr = GetFileAttributes(configPath.c_str());
+    DWORD fileAttr = GetFileAttributes(dbDir.c_str());
     if (fileAttr == INVALID_FILE_ATTRIBUTES) {
-        CreateDirectory(configPath.c_str(), NULL);
+        CreateDirectory(dbDir.c_str(), NULL);
     }
 
-    return configPath;
+    return dbDir;
 }
 
 // 获取session.dat文件路径

--
Gitblit v1.9.3