From 302b2045dcde4687485c28eaa3bde3aaa2203bc7 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期五, 30 五月 2025 16:44:36 +0800
Subject: [PATCH] 1.修复合并后的RC文件问题;
---
SourceCode/Bond/Servo/SECSRuntimeManager.cpp | 1725 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 1,724 insertions(+), 1 deletions(-)
diff --git a/SourceCode/Bond/Servo/SECSRuntimeManager.cpp b/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
index 8838144..ccbcf77 100644
--- a/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
+++ b/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
@@ -2,7 +2,7 @@
#include "SECSRuntimeManager.h"
// 常量
-const std::string DATABASE_FILE = R"(SECSRuntimeManager.db)";
+const std::string DATABASE_FILE = R"(SECSDataManager.db)";
// 静态成员初始化
std::mutex SECSRuntimeManager::m_mutex;
@@ -28,6 +28,126 @@
}
}
+// 从数据库中获取整数
+int SECSRuntimeManager::getIntFromDB(const std::string& query) {
+ auto results = m_pDB->fetchResults(query);
+ if (!results.empty() && !results[0].empty()) {
+ // 转换第一个查询结果为整数
+ return std::stoi(results[0][0]);
+ }
+
+ return 0;
+}
+
+// 判断VID是否重复
+bool SECSRuntimeManager::isIDDuplicate(int nID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return false;
+ }
+
+ // 定义要检查的表
+ std::vector<std::string> tables = { "SystemSV", "EqpSV", "SystemDV", "EqpDV", "SystemEC", "EqpEC" };
+
+ // 遍历表,检查是否有重复的 ID
+ for (const auto& table : tables) {
+ // 创建 SQL 查询
+ std::string checkSQL = "SELECT COUNT(*) FROM " + table + " WHERE ID = " + std::to_string(nID) + ";";
+
+ // 执行查询
+ auto results = m_pDB->fetchResults(checkSQL);
+ int count = (!results.empty() && !results[0].empty()) ? std::stoi(results[0][0]) : 0;
+
+ // 如果找到了重复的 ID,则返回 true
+ if (count > 0) {
+ return true;
+ }
+ }
+
+ // 如果没有重复,返回 false
+ return false;
+}
+
+// 判断名称是否重复
+bool SECSRuntimeManager::isNameDuplicate(const std::string& sName) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return false;
+ }
+
+ // 定义要检查的表
+ std::vector<std::string> tables = { "SystemSV", "EqpSV", "SystemDV", "EqpDV", "SystemEC", "EqpEC" };
+
+ // 遍历表,检查是否有重复的 Name
+ for (const auto& table : tables) {
+ // 创建 SQL 查询
+ std::string checkSQL = "SELECT COUNT(*) FROM " + table + " WHERE Name = '" + sName + "';";
+
+ // 执行查询
+ auto results = m_pDB->fetchResults(checkSQL);
+ int count = (!results.empty() && !results[0].empty()) ? std::stoi(results[0][0]) : 0;
+
+ // 如果找到了重复的 Name,则返回 true
+ if (count > 0) {
+ return true;
+ }
+ }
+
+ // 如果没有重复,返回 false
+ return false;
+}
+
+// 删除指定表的所有数据
+int SECSRuntimeManager::deleteAllDataFromTable(const std::string& tableName) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 构建删除所有数据的 SQL 语句, 重置自增 ID
+ std::string deleteSQL = "TRUNCATE TABLE " + tableName + ";";
+ if (!m_pDB->executeQuery(deleteSQL)) {
+ return 2;
+ }
+
+ return 0; // 删除成功,返回 0 表示操作成功完成。
+}
+
+// 查询指定表所有数据
+bool SECSRuntimeManager::getAllDataFromTable(const std::string& tableName, std::vector<std::vector<std::string>>& outData) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return false; // 数据库连接失败
+ }
+
+ // 查询 SQL 语句,获取指定表的所有数据
+ std::string querySQL = "SELECT * FROM " + tableName + ";";
+ outData = m_pDB->fetchResults(querySQL); // 使用输出参数赋值结果
+
+ return true; // 查询成功
+}
+
+// 删除指定表中指定 ID 的数据
+int SECSRuntimeManager::deleteDataByID(const std::string& tableName, int nID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接,返回 1
+ }
+
+ // 检查 ID 是否存在
+ if (!isIDDuplicate(nID)) {
+ return 2; // ID 不存在,返回 2
+ }
+
+ // 构建删除 SQL 语句
+ std::string deleteSQL = "DELETE FROM " + tableName + " WHERE ID = " + std::to_string(nID) + ";";
+ if (!m_pDB->executeQuery(deleteSQL)) {
+ return 3; // 删除失败,返回 3
+ }
+
+ return 0; // 删除成功,返回 0
+}
+
// 设置数据库连接
void SECSRuntimeManager::setDatabase(BL::Database* db) {
std::lock_guard<std::mutex> lock(m_mutex);
@@ -49,6 +169,39 @@
return false;
}
+ // 初始化 SystemSV 表
+ initSystemSVTable();
+
+ // 初始化 EqpSV 表
+ initEqpSVTable();
+
+ // 初始化 SystemDV 表
+ initSystemDVTable();
+
+ // 初始化 EqpDV 表
+ initEqpDVTable();
+
+ // 初始化 SystemEC 表
+ initSystemECTable();
+
+ // 初始化 EqpEC 表
+ initEqpECTable();
+
+ // 初始化 SystemECID 表
+ initSystemEventTable();
+
+ // 初始化 EqpECID 表
+ initEqpEventTable();
+
+ // 初始化 SystemEventLink 表
+ initEventLinkTable();
+
+ // 初始化 PPID 表
+ initPPIDTable();
+
+ // 初始化 RPTID 表
+ initRPTIDTable();
+
return true;
}
@@ -57,4 +210,1574 @@
if (m_pDB != nullptr) {
m_pDB->disconnect();
}
+}
+
+// 初始化指定的 SystemV 表
+void SECSRuntimeManager::initSystemVTable(const std::string& tableName) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 创建表的 SQL 语句
+ std::string createTableSQL =
+ "CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ "ID INTEGER PRIMARY KEY, "
+ "Name TEXT UNIQUE NOT NULL, "
+ "DataType TEXT NOT NULL, "
+ "Length INTEGER NULL, "
+ "Unit TEXT NULL, "
+ "Remark TEXT, "
+ "SystemID INTEGER);";
+
+ // 执行创建表操作
+ if (!m_pDB->executeQuery(createTableSQL)) {
+ throw std::runtime_error("Failed to create " + tableName + " table.");
+ }
+}
+
+// 添加指定的 SystemV 表
+int SECSRuntimeManager::addSystemVData(const std::string& tableName, int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接
+ }
+
+ // 检查 ID 和 Name 是否重复
+ if (isIDDuplicate(nID)) {
+ return 2; // ID 重复
+ }
+
+ if (isNameDuplicate(sName)) {
+ return 3; // Name 重复
+ }
+
+ // 构建插入语句
+ std::string insertSQL = "INSERT INTO " + tableName + " (ID, Name, DataType, Length, Unit, Remark, SystemID) VALUES ("
+ + std::to_string(nID) + ", '"
+ + sName + "', '"
+ + sDataType + "', "
+ + (nLength > 0 ? std::to_string(nLength) : "NULL") + ", "
+ + ((sUnit == "NULL" || sUnit.empty()) ? "NULL" : "'" + sUnit + "'") + ", '"
+ + sRemark + "', "
+ + std::to_string(nSystemID) + ");";
+
+ // 执行插入操作
+ if (!m_pDB->executeQuery(insertSQL)) {
+ return 4; // 插入失败
+ }
+
+ return 0; // 插入成功
+}
+
+// 获取指定 ID 的 SystemV 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getSystemVByID(const std::string& tableName, int nID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {}; // 如果数据库未连接,返回空结果
+ }
+
+ // 构建查询 SQL 语句
+ std::string query = "SELECT * FROM " + tableName + " WHERE ID = " + std::to_string(nID) + ";";
+
+ // 执行查询并返回结果
+ return m_pDB->fetchResults(query);
+}
+
+// 获取 SystemV 的所有数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getAllSystemV(const std::string& tableName) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {}; // 如果数据库未连接,返回空结果
+ }
+
+ // 构建查询 SQL 语句
+ std::string query = "SELECT * FROM " + tableName + ";";
+
+ // 执行查询并返回结果
+ return m_pDB->fetchResults(query);
+}
+
+// 更新 SystemV 的 ID 数据
+int SECSRuntimeManager::updateIDSystemV(const std::string& tableName, int nID, int sNewID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 检查是否存在该 ID
+ if (!isIDDuplicate(nID)) {
+ return 2;
+ }
+
+ // 检查新 ID 是否重复
+ if (isIDDuplicate(sNewID)) {
+ return 3;
+ }
+
+ // 构建更新的 SQL 语句
+ std::string updateSQL = "UPDATE " + tableName + " SET ID = " + std::to_string(sNewID) + " WHERE ID = " + std::to_string(nID) + ";";
+ if (!m_pDB->executeQuery(updateSQL)) {
+ return 4;
+ }
+
+ return 0;
+}
+
+// 更新 SystemV 的所有字段
+int SECSRuntimeManager::updateAllSystemV(const std::string& tableName, int nID, int sNewID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 检查是否存在该 ID
+ if (!isIDDuplicate(nID)) {
+ return 2;
+ }
+
+ // 检查新的 ID 是否已存在,如果已存在,则返回错误代码 3。
+ if (isIDDuplicate(sNewID)) {
+ return 3;
+ }
+
+ // 构建更新的 SQL 语句
+ std::string updateSQL = "UPDATE " + tableName + " SET ";
+
+ bool firstField = true;
+
+ // 如果新的 ID 被提供,更新 ID
+ if (sNewID > 0) {
+ if (!firstField) {
+ updateSQL += ", ";
+ }
+ updateSQL += "ID = " + std::to_string(sNewID);
+ firstField = false;
+ }
+
+ // 更新 Name
+ if (!sName.empty()) {
+ if (!firstField) {
+ updateSQL += ", ";
+ }
+ updateSQL += "Name = '" + sName + "'";
+ firstField = false;
+ }
+
+ // 更新 DataType
+ if (!sDataType.empty()) {
+ if (!firstField) {
+ updateSQL += ", ";
+ }
+ updateSQL += "DataType = '" + sDataType + "'";
+ firstField = false;
+ }
+
+ // 更新 Length
+ if (nLength > 0) {
+ if (!firstField) {
+ updateSQL += ", ";
+ }
+ updateSQL += "Length = " + std::to_string(nLength);
+ firstField = false;
+ }
+
+ // 更新 Unit
+ if (sUnit != "NULL" && !sUnit.empty()) {
+ if (!firstField) {
+ updateSQL += ", ";
+ }
+ updateSQL += "Unit = '" + sUnit + "'";
+ firstField = false;
+ }
+ else if (sUnit == "NULL") {
+ if (!firstField) {
+ updateSQL += ", ";
+ }
+ updateSQL += "Unit = NULL";
+ firstField = false;
+ }
+
+ // 更新 Remark
+ if (!sRemark.empty()) {
+ if (!firstField) {
+ updateSQL += ", ";
+ }
+ updateSQL += "Remark = '" + sRemark + "'";
+ firstField = false;
+ }
+
+ // 更新 SystemID
+ if (nSystemID > 0) {
+ if (!firstField) {
+ updateSQL += ", ";
+ }
+ updateSQL += "SystemID = " + std::to_string(nSystemID);
+ }
+
+ // 添加 WHERE 子句来指定更新哪个记录
+ updateSQL += " WHERE ID = " + std::to_string(nID) + ";";
+
+ // 执行更新操作
+ if (!m_pDB->executeQuery(updateSQL)) {
+ return 4;
+ }
+
+ return 0;
+}
+
+// 初始化指定的 EqpV 表
+void SECSRuntimeManager::initEqpVTable(const std::string& tableName) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 创建设备表的 SQL 语句
+ std::string createTableSQL =
+ "CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ "ID INTEGER PRIMARY KEY, "
+ "Name TEXT UNIQUE NOT NULL, "
+ "DataType TEXT NOT NULL, "
+ "Length INTEGER NULL, "
+ "Unit TEXT NULL, "
+ "Remark TEXT, "
+ "SeqNo INTEGER);";
+
+ if (!m_pDB->executeQuery(createTableSQL)) {
+ throw std::runtime_error("Failed to create " + tableName + " table.");
+ }
+}
+
+// 添加指定 EqpV 表的数据
+int SECSRuntimeManager::addEqpVData(const std::string& tableName, int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSeqNo) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 检查 ID 是否重复
+ if (isIDDuplicate(nID)) {
+ return 2;
+ }
+
+ // 检查 Name 是否重复
+ if (isNameDuplicate(sName)) {
+ return 3;
+ }
+
+ // 构建 SQL 插入语句
+ std::string insertSQL = "INSERT INTO " + tableName + " (ID, Name, DataType, Length, Unit, Remark, SeqNo) VALUES ("
+ + std::to_string(nID) + ", '"
+ + sName + "', '"
+ + sDataType + "', "
+ + ((nLength <= 0) ? "NULL" : std::to_string(nLength)) + ", "
+ + ((sUnit == "NULL" || sUnit.empty()) ? "NULL" : "'" + sUnit + "'") + ", '"
+ + sRemark + "', "
+ + std::to_string(nSeqNo) + ");";
+
+ // 执行插入操作
+ if (!m_pDB->executeQuery(insertSQL)) {
+ return 4;
+ }
+
+ return 0; // 插入成功,返回 0 表示操作成功完成。
+}
+
+// 查询指定 ID 的 EqpV 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getEqpVDataByID(const std::string& tableName, int nID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ // 查询 SQL 语句
+ std::string querySQL = "SELECT * FROM " + tableName + " WHERE ID = " + std::to_string(nID) + ";";
+ return m_pDB->fetchResults(querySQL); // 直接返回查询结果
+}
+
+// 更新指定的 EqpV 数据
+int SECSRuntimeManager::updateEqpV(const std::string& tableName, int nID, int nNewID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSeqNo) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 如果数据库未连接,返回 1
+ }
+
+ // 检查是否存在该 ID
+ if (!isIDDuplicate(nID)) {
+ return 2; // 如果 ID 不存在,返回错误代码 2
+ }
+
+ // 检查新的 ID 是否已存在
+ if (isIDDuplicate(nNewID) && nNewID != nID) {
+ return 3; // 如果新 ID 已存在,返回错误代码 3
+ }
+
+ // 构建更新 SQL 语句
+ std::string updateSQL = "UPDATE " + tableName + " SET ";
+
+ bool firstField = true;
+
+ // 如果新的 ID 被提供,更新 ID
+ if (nNewID > 0 && nNewID != nID) {
+ updateSQL += "ID = " + std::to_string(nNewID);
+ firstField = false;
+ }
+
+ // 更新 Name
+ if (!sName.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Name = '" + sName + "'";
+ firstField = false;
+ }
+
+ // 更新 DataType
+ if (!sDataType.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "DataType = '" + sDataType + "'";
+ firstField = false;
+ }
+
+ // 更新 Length
+ if (nLength > 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Length = " + std::to_string(nLength);
+ firstField = false;
+ }
+
+ // 更新 Unit
+ if (sUnit != "NULL" && !sUnit.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Unit = '" + sUnit + "'";
+ firstField = false;
+ }
+ else if (sUnit == "NULL") {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Unit = NULL";
+ firstField = false;
+ }
+
+ // 更新 Remark
+ if (!sRemark.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Remark = '" + sRemark + "'";
+ firstField = false;
+ }
+
+ // 更新 SeqNo
+ if (nSeqNo > 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "SeqNo = " + std::to_string(nSeqNo);
+ }
+
+ // 添加 WHERE 子句,更新指定 ID 的数据
+ updateSQL += " WHERE ID = " + std::to_string(nID) + ";";
+
+ // 执行更新操作
+ if (!m_pDB->executeQuery(updateSQL)) {
+ return 4; // 如果更新失败,返回错误代码 4
+ }
+
+ return 0; // 更新成功,返回 0
+}
+
+// 初始化 SystemSV 表
+void SECSRuntimeManager::initSystemSVTable() {
+ initSystemVTable("SystemSV");
+
+ // 预定义的 SV 数据
+ std::vector<std::tuple<int, std::string, std::string, int, std::string, std::string, int>> svData = {
+ {1, "SYS_LICENSE_CODE", "ASCII", 0, "NULL", "License code (Formal; Evaluation; NoLicense)", 1},
+ {2, "SYS_LICENSE_STATUS", "UINT_1", 0, "NULL", "License status(0:Unauthorized; 1:Authorized; 2:Evaluation; 3:Evaluation Expiring; 4:Trial; 5:Trial End)", 2},
+ {3, "GEM_CLOCK", "ASCII", 0, "NULL", "System Clock", 3},
+ {4, "SYS_SECS_COMM_MODE", "UINT_1", 0, "NULL", "SECS Communication Mode(0:HSMS Mode; 1:SECSI Mode)", 4},
+ {5, "SYS_SECS_DRIVER_CONNECT_STATE", "UINT_1", 0, "NULL", "Initial SECS Driver Connect State(0:Stop; 1:Start)", 5}
+ };
+
+ for (const auto& entry : svData) {
+ int nID, nLength, nSystemID;
+ std::string sName, sDataType, sRemark, sUnit;
+ std::tie(nID, sName, sDataType, nLength, sUnit, sRemark, nSystemID) = entry;
+
+ // 检查 Name 是否已存在
+ int count = getIntFromDB("SELECT COUNT(*) FROM SystemSV WHERE Name = '" + sName + "';");
+ if (count == 0) {
+ // 插入数据
+ std::string insertSQL = "INSERT INTO SystemSV (ID, Name, DataType, Length, Unit, Remark, SystemID) VALUES ("
+ + std::to_string(nID) + ", '"
+ + sName + "', '"
+ + sDataType + "', "
+ + (nLength > 0 ? std::to_string(nLength) : "NULL") + ", "
+ + ((sUnit == "NULL") ? "NULL" : "'" + sUnit + "'") + ", '"
+ + sRemark + "', "
+ + std::to_string(nSystemID) + ");";
+
+ if (!m_pDB->executeQuery(insertSQL)) {
+ throw std::runtime_error("Failed to insert SystemSV data.");
+ }
+ }
+ }
+}
+
+// 添加 SystemSV 数据
+int SECSRuntimeManager::addSystemSV(int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
+ return addSystemVData("SystemSV", nID, sName, sDataType, nLength, sUnit, sRemark, nSystemID);
+}
+
+// 获取指定 ID 的 SystemSV 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getSystemSVByID(int nID) {
+ return getSystemVByID("SystemSV", nID);
+}
+
+// 获取所有 SystemSV 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getAllSystemSV() {
+ return getAllSystemV("SystemSV");
+}
+
+// 更新 SystemSV 表中的 ID
+int SECSRuntimeManager::updateIDSystemSV(int nID, int sNewID) {
+ return updateIDSystemV("SystemSV", nID, sNewID);
+}
+
+// 更新所有 SystemSV 数据
+int SECSRuntimeManager::updateAllSystemSV(int nID, int sNewID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
+ return updateAllSystemV("SystemSV", nID, sNewID, sName, sDataType, nLength, sUnit, sRemark, nSystemID);
+}
+
+// 删除指定 ID 的 SystemSV 数据
+int SECSRuntimeManager::deleteSystemSVByID(int nID) {
+ return deleteDataByID("SystemSV", nID);
+}
+
+// 删除所有 SystemSV 数据
+int SECSRuntimeManager::deleteAllSystemSV() {
+ return deleteAllDataFromTable("SystemSV");
+}
+
+// 初始化 EqpSV 表
+void SECSRuntimeManager::initEqpSVTable() {
+ initEqpVTable("EqpSV");
+}
+
+// 添加 EqpSV 数据
+int SECSRuntimeManager::addEqpSV(int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSeqNo) {
+ return addEqpVData("EqpSV", nID, sName, sDataType, nLength, sUnit, sRemark, nSeqNo);
+}
+
+// 查找指定 ID 的 EqpSV 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getEqpSVByID(int nID) {
+ return getEqpVDataByID("EqpSV", nID);
+}
+
+// 查找所有 EqpSV 数据
+bool SECSRuntimeManager::getAllEqpSV(std::vector<std::vector<std::string>>& outEqpSV) {
+ return getAllDataFromTable("EqpSV", outEqpSV);
+}
+
+// 更新 EqpSV 表指定 ID 的数据
+int SECSRuntimeManager::updateEqpSV(int nID, int nNewID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSeqNo) {
+ return updateEqpV("EqpSV", nID, nNewID, sName, sDataType, nLength, sUnit, sRemark, nSeqNo);
+}
+
+// 删除 EqpSV 表指定 ID 的数据
+int SECSRuntimeManager::deleteEqpSVByID(int nID) {
+ return deleteDataByID("EqpSV", nID);
+}
+
+// 删除 EqpSV 表中的所有数据
+int SECSRuntimeManager::deleteAllEqpSV() {
+ return deleteAllDataFromTable("EqpSV");
+}
+
+// 初始化 SystemDV 表
+void SECSRuntimeManager::initSystemDVTable() {
+ initSystemVTable("SystemDV");
+}
+
+// 添加 SystemDV 数据
+int SECSRuntimeManager::addSystemDV(int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
+ return addSystemVData("SystemDV", nID, sName, sDataType, nLength, sUnit, sRemark, nSystemID);
+}
+
+// 获取指定 ID 的 SystemDV 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getSystemDVByID(int nID) {
+ return getSystemVByID("SystemDV", nID);
+}
+
+// 获取所有 SystemDV 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getAllSystemDV() {
+ return getAllSystemV("SystemDV");
+}
+
+// 更新 SystemDV 表中的 ID
+int SECSRuntimeManager::updateIDSystemDV(int nID, int sNewID) {
+ return updateIDSystemV("SystemDV", nID, sNewID);
+}
+
+// 更新所有 SystemDV 数据
+int SECSRuntimeManager::updateAllSystemDV(int nID, int sNewID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
+ return updateAllSystemV("SystemDV", nID, sNewID, sName, sDataType, nLength, sUnit, sRemark, nSystemID);
+}
+
+// 删除指定 ID 的 SystemDV 数据
+int SECSRuntimeManager::deleteSystemDVByID(int nID) {
+ return deleteDataByID("SystemDV", nID);
+}
+
+// 删除所有 SystemDV 数据
+int SECSRuntimeManager::deleteAllSystemDV() {
+ return deleteAllDataFromTable("SystemDV");
+}
+
+// 初始化 EqpDV 表
+void SECSRuntimeManager::initEqpDVTable() {
+ initEqpVTable("EqpDV");
+}
+
+// 添加 EqpDV 数据
+int SECSRuntimeManager::addEqpDV(int nID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSeqNo) {
+ return addEqpVData("EqpDV", nID, sName, sDataType, nLength, sUnit, sRemark, nSeqNo);
+}
+
+// 查找指定 ID 的 EqpDV 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getEqpDVByID(int nID) {
+ return getEqpVDataByID("EqpDV", nID);
+}
+
+// 获取所有 EqpDV 数据
+bool SECSRuntimeManager::getAllEqpDV(std::vector<std::vector<std::string>>& outEqpDV) {
+ return getAllDataFromTable("EqpDV", outEqpDV);
+}
+
+// 更新 EqpDV 表指定 ID 的数据
+int SECSRuntimeManager::updateEqpDV(int nID, int nNewID, const std::string& sName, const std::string& sDataType, int nLength, const std::string& sUnit, const std::string& sRemark, int nSeqNo) {
+ return updateEqpV("EqpDV", nID, nNewID, sName, sDataType, nLength, sUnit, sRemark, nSeqNo);
+}
+
+// 删除 EqpDV 表指定 ID 的数据
+int SECSRuntimeManager::deleteEqpDVByID(int nID) {
+ return deleteDataByID("EqpDV", nID);
+}
+
+// 删除 EqpDV 表中的所有数据
+int SECSRuntimeManager::deleteAllEqpDV() {
+ return deleteAllDataFromTable("EqpDV");
+}
+
+// 初始化 SystemEC 表
+void SECSRuntimeManager::initSystemECTable() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 创建 SystemEC 表
+ std::string createTableSQL =
+ "CREATE TABLE IF NOT EXISTS SystemEC ("
+ "ID INTEGER PRIMARY KEY, "
+ "Name TEXT UNIQUE NOT NULL, "
+ "DataType TEXT NOT NULL, "
+ "MinValue INTEGER NULL, "
+ "MaxValue INTEGER NULL, "
+ "DefaultVal INTEGER NULL, "
+ "Unit TEXT NULL, "
+ "Remark TEXT, "
+ "SystemID INTEGER);";
+
+ if (!m_pDB->executeQuery(createTableSQL)) {
+ throw std::runtime_error("Failed to create SystemEC table.");
+ }
+}
+
+// 添加 SystemEC 数据
+int SECSRuntimeManager::addSystemEC(int nID, const std::string& sName, const std::string& sDataType, int nMinValue, int nMaxValue, int nDefaultVal, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接
+ }
+
+ if (isIDDuplicate(nID)) {
+ return 2; // ID 重复
+ }
+
+ if (isNameDuplicate(sName)) {
+ return 3; // Name 重复
+ }
+
+ // 构建插入 SQL 语句
+ std::string insertSQL = "INSERT INTO SystemEC (ID, Name, DataType, MinValue, MaxValue, DefaultVal, Unit, Remark, SystemID) VALUES ("
+ + std::to_string(nID) + ", '" + sName + "', '" + sDataType + "', "
+ + (nMinValue >= 0 ? std::to_string(nMinValue) : "NULL") + ", "
+ + (nMaxValue >= 0 ? std::to_string(nMaxValue) : "NULL") + ", "
+ + (nDefaultVal >= 0 ? std::to_string(nDefaultVal) : "NULL") + ", "
+ + ((sUnit == "NULL" || sUnit.empty()) ? "NULL" : "'" + sUnit + "'") + ", '"
+ + sRemark + "', " + std::to_string(nSystemID) + ");";
+
+ if (!m_pDB->executeQuery(insertSQL)) {
+ return 4; // 插入失败
+ }
+
+ return 0; // 插入成功
+}
+
+// 查询指定 ID 的 SystemEC 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getSystemECByID(int nID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ std::string querySQL = "SELECT * FROM SystemEC WHERE ID = " + std::to_string(nID) + ";";
+ return m_pDB->fetchResults(querySQL);
+}
+
+// 查询所有 SystemEC 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getAllSystemEC() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ std::string querySQL = "SELECT * FROM SystemEC;";
+ return m_pDB->fetchResults(querySQL);
+}
+
+// 更新指定 ID 的 SystemEC 数据
+int SECSRuntimeManager::updateSystemEC(int nID, int nNewID, const std::string& sName, const std::string& sDataType, int nMinValue, int nMaxValue, int nDefaultVal, const std::string& sUnit, const std::string& sRemark, int nSystemID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 检查是否存在该 ID
+ if (!isIDDuplicate(nID)) {
+ return 2;
+ }
+
+ // 检查新的 ID 是否已存在
+ if (isIDDuplicate(nNewID) && nNewID != nID) {
+ return 3;
+ }
+
+ // 构建更新 SQL 语句
+ std::string updateSQL = "UPDATE SystemEC SET ";
+
+ bool firstField = true;
+
+ if (nNewID > 0 && nNewID != nID) {
+ updateSQL += "ID = " + std::to_string(nNewID);
+ firstField = false;
+ }
+
+ if (!sName.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Name = '" + sName + "'";
+ firstField = false;
+ }
+
+ if (!sDataType.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "DataType = '" + sDataType + "'";
+ firstField = false;
+ }
+
+ if (nMinValue >= 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "MinValue = " + std::to_string(nMinValue);
+ firstField = false;
+ }
+
+ if (nMaxValue >= 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "MaxValue = " + std::to_string(nMaxValue);
+ firstField = false;
+ }
+
+ if (nDefaultVal >= 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "DefaultVal = " + std::to_string(nDefaultVal);
+ firstField = false;
+ }
+
+ if (sUnit != "NULL" && !sUnit.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Unit = '" + sUnit + "'";
+ firstField = false;
+ }
+ else if (sUnit == "NULL") {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Unit = NULL";
+ firstField = false;
+ }
+
+ if (!sRemark.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Remark = '" + sRemark + "'";
+ firstField = false;
+ }
+
+ if (nSystemID > 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "SystemID = " + std::to_string(nSystemID);
+ }
+
+ updateSQL += " WHERE ID = " + std::to_string(nID) + ";";
+
+ if (!m_pDB->executeQuery(updateSQL)) {
+ return 4;
+ }
+
+ return 0;
+}
+
+// 删除指定 ID 的 SystemEC 数据
+int SECSRuntimeManager::deleteSystemECByID(int nID) {
+ return deleteDataByID("SystemEC", nID);
+}
+
+// 删除所有 SystemEC 数据
+int SECSRuntimeManager::deleteAllSystemEC() {
+ return deleteAllDataFromTable("SystemEC");
+}
+
+// 初始化 EqpEC 表
+void SECSRuntimeManager::initEqpECTable() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 创建 EqpEC 表
+ std::string createTableSQL =
+ "CREATE TABLE IF NOT EXISTS EqpEC ("
+ "ID INTEGER PRIMARY KEY, "
+ "Name TEXT UNIQUE NOT NULL, "
+ "DataType TEXT NOT NULL, "
+ "MinValue INTEGER NULL, "
+ "MaxValue INTEGER NULL, "
+ "DefaultValue INTEGER NULL, "
+ "Unit TEXT NULL, "
+ "Remark TEXT, "
+ "SeqNo INTEGER NOT NULL, "
+ "Length INTEGER NOT NULL, "
+ "CanUpdateByHost INTEGER NOT NULL);";
+
+ if (!m_pDB->executeQuery(createTableSQL)) {
+ throw std::runtime_error("Failed to create EqpEC table.");
+ }
+}
+
+// 添加 EqpEC 数据
+int SECSRuntimeManager::addEqpEC(int nID, const std::string& sName, const std::string& sDataType, int nMinValue, int nMaxValue, int nDefaultValue, const std::string& sUnit, const std::string& sRemark, int nSeqNo, int nLength, int bCanUpdateByHost) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接
+ }
+
+ if (isIDDuplicate(nID)) {
+ return 2; // ID 重复
+ }
+
+ if (isNameDuplicate(sName)) {
+ return 3; // Name 重复
+ }
+
+ // 构建插入 SQL 语句
+ std::string insertSQL = "INSERT INTO EqpEC (ID, Name, DataType, MinValue, MaxValue, DefaultValue, Unit, Remark, SeqNo, Length, CanUpdateByHost) VALUES ("
+ + std::to_string(nID) + ", '" + sName + "', '" + sDataType + "', "
+ + (nMinValue >= 0 ? std::to_string(nMinValue) : "NULL") + ", "
+ + (nMaxValue >= 0 ? std::to_string(nMaxValue) : "NULL") + ", "
+ + (nDefaultValue >= 0 ? std::to_string(nDefaultValue) : "NULL") + ", "
+ + ((sUnit == "NULL" || sUnit.empty()) ? "NULL" : "'" + sUnit + "'") + ", '"
+ + sRemark + "', " + std::to_string(nSeqNo) + ", "
+ + std::to_string(nLength) + ", " + std::to_string(bCanUpdateByHost) + ");";
+
+ if (!m_pDB->executeQuery(insertSQL)) {
+ return 4; // 插入失败
+ }
+
+ return 0; // 插入成功
+}
+
+// 查询指定 ID 的 EqpEC 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getEqpECByID(int nID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ std::string querySQL = "SELECT * FROM EqpEC WHERE ID = " + std::to_string(nID) + ";";
+ return m_pDB->fetchResults(querySQL);
+}
+
+// 查询所有 EqpEC 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getAllEqpEC() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ std::string querySQL = "SELECT * FROM EqpEC;";
+ return m_pDB->fetchResults(querySQL);
+}
+
+// 更新指定 ID 的 EqpEC 数据
+int SECSRuntimeManager::updateEqpEC(int nID, int nNewID, const std::string& sName, const std::string& sDataType, int nMinValue, int nMaxValue, int nDefaultValue, const std::string& sUnit, const std::string& sRemark, int nSeqNo, int nLength, int bCanUpdateByHost) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 检查是否存在该 ID
+ if (!isIDDuplicate(nID)) {
+ return 2;
+ }
+
+ // 检查新的 ID 是否已存在
+ if (isIDDuplicate(nNewID) && nNewID != nID) {
+ return 3;
+ }
+
+ // 构建更新 SQL 语句
+ std::string updateSQL = "UPDATE EqpEC SET ";
+
+ bool firstField = true;
+
+ if (nNewID > 0 && nNewID != nID) {
+ updateSQL += "ID = " + std::to_string(nNewID);
+ firstField = false;
+ }
+
+ if (!sName.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Name = '" + sName + "'";
+ firstField = false;
+ }
+
+ if (!sDataType.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "DataType = '" + sDataType + "'";
+ firstField = false;
+ }
+
+ if (nMinValue >= 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "MinValue = " + std::to_string(nMinValue);
+ firstField = false;
+ }
+
+ if (nMaxValue >= 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "MaxValue = " + std::to_string(nMaxValue);
+ firstField = false;
+ }
+
+ if (nDefaultValue >= 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "DefaultValue = " + std::to_string(nDefaultValue);
+ firstField = false;
+ }
+
+ if (sUnit != "NULL" && !sUnit.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Unit = '" + sUnit + "'";
+ firstField = false;
+ }
+ else if (sUnit == "NULL") {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Unit = NULL";
+ firstField = false;
+ }
+
+ if (!sRemark.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Remark = '" + sRemark + "'";
+ firstField = false;
+ }
+
+ if (nSeqNo > 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "SeqNo = " + std::to_string(nSeqNo);
+ }
+
+ if (nLength > 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Length = " + std::to_string(nLength);
+ }
+
+ if (bCanUpdateByHost >= 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "CanUpdateByHost = " + std::to_string(bCanUpdateByHost);
+ }
+
+ updateSQL += " WHERE ID = " + std::to_string(nID) + ";";
+
+ if (!m_pDB->executeQuery(updateSQL)) {
+ return 4;
+ }
+
+ return 0;
+}
+
+// 删除指定 ID 的 EqpEC 数据
+int SECSRuntimeManager::deleteEqpECByID(int nID) {
+ return deleteDataByID("EqpEC", nID);
+}
+
+// 删除所有 EqpEC 数据
+int SECSRuntimeManager::deleteAllEqpEC() {
+ return deleteAllDataFromTable("EqpEC");
+}
+
+// 初始化 SystemEvent 表
+void SECSRuntimeManager::initSystemEventTable() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 创建 SystemEvent 表
+ std::string createTableSQL =
+ "CREATE TABLE IF NOT EXISTS SystemEvent ("
+ "CEID INTEGER PRIMARY KEY, "
+ "Name TEXT UNIQUE NOT NULL, "
+ "Remark TEXT, "
+ "SystemID INTEGER);";
+
+ if (!m_pDB->executeQuery(createTableSQL)) {
+ throw std::runtime_error("Failed to create SystemEvent table.");
+ }
+}
+
+// 添加 SystemEvent 数据
+int SECSRuntimeManager::addSystemEvent(int nCEID, const std::string& sName, const std::string& sRemark, int nSystemID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接
+ }
+
+ // 构建 SQL 插入语句
+ std::string insertSQL = "INSERT INTO SystemEvent (CEID, Name, Remark, SystemID) VALUES ("
+ + std::to_string(nCEID) + ", '" + sName + "', '" + sRemark + "', " + std::to_string(nSystemID) + ");";
+
+ if (!m_pDB->executeQuery(insertSQL)) {
+ return 2; // 插入失败
+ }
+
+ return 0; // 插入成功
+}
+
+// 查询指定 CEID 的 SystemEvent 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getSystemEventByID(int nCEID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ std::string querySQL = "SELECT * FROM SystemEvent WHERE CEID = " + std::to_string(nCEID) + ";";
+ return m_pDB->fetchResults(querySQL);
+}
+
+// 查询所有 SystemEvent 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getAllSystemEvents() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ std::string querySQL = "SELECT * FROM SystemEvent;";
+ return m_pDB->fetchResults(querySQL);
+}
+
+// 更新指定 CEID 的 SystemEvent 数据
+int SECSRuntimeManager::updateSystemEvent(int nCEID, int nNewCEID, const std::string& sName, const std::string& sRemark, int nSystemID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 构建更新 SQL 语句
+ std::string updateSQL = "UPDATE SystemEvent SET ";
+
+ bool firstField = true;
+
+ if (nNewCEID > 0 && nNewCEID != nCEID) {
+ updateSQL += "CEID = " + std::to_string(nNewCEID);
+ firstField = false;
+ }
+
+ if (!sName.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Name = '" + sName + "'";
+ firstField = false;
+ }
+
+ if (!sRemark.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Remark = '" + sRemark + "'";
+ firstField = false;
+ }
+
+ if (nSystemID > 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "SystemID = " + std::to_string(nSystemID);
+ }
+
+ updateSQL += " WHERE CEID = " + std::to_string(nCEID) + ";";
+
+ if (!m_pDB->executeQuery(updateSQL)) {
+ return 2;
+ }
+
+ return 0;
+}
+
+// 删除指定 CEID 的 SystemEvent 数据
+int SECSRuntimeManager::deleteSystemEventByID(int nCEID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接,返回 1
+ }
+
+ // 构建删除 SQL 语句
+ std::string deleteSQL = "DELETE FROM SystemEvent WHERE CEID = " + std::to_string(nCEID) + ";";
+ if (!m_pDB->executeQuery(deleteSQL)) {
+ return 2; // 删除失败,返回 2
+ }
+
+ return 0; // 删除成功,返回 0
+}
+
+// 删除所有 SystemEvent 数据
+int SECSRuntimeManager::deleteAllSystemEvents() {
+ return deleteAllDataFromTable("SystemEvent");
+}
+
+// 初始化 EqpEvent 表
+void SECSRuntimeManager::initEqpEventTable() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 创建 EqpEvent 表
+ std::string createTableSQL =
+ "CREATE TABLE IF NOT EXISTS EqpEvent ("
+ "CEID INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "Name TEXT UNIQUE NOT NULL, "
+ "Remark TEXT, "
+ "BitNo INTEGER);";
+
+ if (!m_pDB->executeQuery(createTableSQL)) {
+ throw std::runtime_error("Failed to create EqpEvent table.");
+ }
+}
+
+// 添加 EqpEvent 数据
+int SECSRuntimeManager::addEqpEvent(const std::string& sName, const std::string& sRemark, int nBitNo) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接
+ }
+
+ // 构建 SQL 插入语句
+ std::string insertSQL = "INSERT INTO EqpEvent (Name, Remark, BitNo) VALUES ('"
+ + sName + "', '" + sRemark + "', " + std::to_string(nBitNo) + ");";
+
+ if (!m_pDB->executeQuery(insertSQL)) {
+ return 2; // 插入失败
+ }
+
+ return 0; // 插入成功
+}
+
+// 查询指定 CEID 的 EqpEvent 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getEqpEventByID(int nCEID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ std::string querySQL = "SELECT * FROM EqpEvent WHERE CEID = " + std::to_string(nCEID) + ";";
+ return m_pDB->fetchResults(querySQL);
+}
+
+// 查询所有 EqpEvent 数据
+std::vector<std::vector<std::string>> SECSRuntimeManager::getAllEqpEvents() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ std::string querySQL = "SELECT * FROM EqpEvent;";
+ return m_pDB->fetchResults(querySQL);
+}
+
+// 更新指定 CEID 的 EqpEvent 数据
+int SECSRuntimeManager::updateEqpEvent(int nCEID, const std::string& sName, const std::string& sRemark, int nBitNo) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 构建更新 SQL 语句
+ std::string updateSQL = "UPDATE EqpEvent SET ";
+
+ bool firstField = true;
+
+ if (!sName.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Name = '" + sName + "'";
+ firstField = false;
+ }
+
+ if (!sRemark.empty()) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "Remark = '" + sRemark + "'";
+ firstField = false;
+ }
+
+ if (nBitNo >= 0) {
+ if (!firstField) updateSQL += ", ";
+ updateSQL += "BitNo = " + std::to_string(nBitNo);
+ }
+
+ updateSQL += " WHERE CEID = " + std::to_string(nCEID) + ";";
+
+ if (!m_pDB->executeQuery(updateSQL)) {
+ return 2;
+ }
+
+ return 0;
+}
+
+// 删除指定 CEID 的 EqpEvent 数据
+int SECSRuntimeManager::deleteEqpEventByID(int nCEID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接,返回 1
+ }
+
+ // 构建删除 SQL 语句
+ std::string deleteSQL = "DELETE FROM EqpEvent WHERE CEID = " + std::to_string(nCEID) + ";";
+ if (!m_pDB->executeQuery(deleteSQL)) {
+ return 2; // 删除失败,返回 2
+ }
+
+ return 0; // 删除成功,返回 0
+}
+
+// 删除所有 EqpEvent 数据
+int SECSRuntimeManager::deleteAllEqpEvents() {
+ return deleteAllDataFromTable("EqpEvent");
+}
+
+// 初始化 EventLink 表
+void SECSRuntimeManager::initEventLinkTable() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 创建 EventLink 表
+ std::string createEventLinkSQL =
+ "CREATE TABLE IF NOT EXISTS EventLink ("
+ "CEID INTEGER, "
+ "RPTID INTEGER, "
+ "FOREIGN KEY (CEID) REFERENCES EqpEvent(CEID));";
+
+ if (!m_pDB->executeQuery(createEventLinkSQL)) {
+ throw std::runtime_error("Failed to create EventLink table.");
+ }
+}
+
+// 初始化 PPID 表
+void SECSRuntimeManager::initPPIDTable() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 创建 EqpPPID 表
+ std::string createTableSQL =
+ "CREATE TABLE IF NOT EXISTS EqpPPID ("
+ "RecipeNo INTEGER PRIMARY KEY, "
+ "PPID TEXT NULL);";
+ if (!m_pDB->executeQuery(createTableSQL)) {
+ throw std::runtime_error("Failed to create EqpPPID table.");
+ }
+
+ // 先检查表是否为空
+ int nCount = getIntFromDB("SELECT COUNT(*) FROM EqpPPID;");
+ if (nCount == 0) {
+ // 插入初始数据(512 行)
+ for (int nBitNo = 0; nBitNo < 512; ++nBitNo) {
+ std::string insertSQL = "INSERT INTO EqpPPID (RecipeNo) VALUES (" + std::to_string(nBitNo) + ");";
+ if (!m_pDB->executeQuery(insertSQL)) {
+ throw std::runtime_error("Failed to insert data into EqpPPID table.");
+ }
+ }
+ }
+}
+
+std::vector<std::string> SECSRuntimeManager::getAllPPID() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ std::string querySQL = "SELECT PPID FROM EqpPPID;";
+ auto rows = m_pDB->fetchResults(querySQL);
+
+ std::vector<std::string> vecResult;
+ for (const auto& row : rows) {
+ vecResult.push_back(row[0]);
+ }
+ return vecResult;
+}
+
+void SECSRuntimeManager::setAllPPID(const std::vector<std::string>& vecPPIDList) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) return;
+
+ // 开启事务
+ m_pDB->executeQuery("BEGIN TRANSACTION;");
+
+ for (size_t i = 0; i < vecPPIDList.size(); ++i) {
+ std::string safePPID = vecPPIDList[i];
+ size_t pos = 0;
+ while ((pos = safePPID.find('\'', pos)) != std::string::npos) {
+ safePPID.insert(pos, 1, '\'');
+ pos += 2;
+ }
+ std::string sql = "UPDATE EqpPPID SET PPID = '" + safePPID + "' WHERE RecipeNo = " + std::to_string(i) + ";";
+ m_pDB->executeQuery(sql);
+ }
+
+ // 提交事务
+ m_pDB->executeQuery("COMMIT;");
+}
+
+bool SECSRuntimeManager::updatePPIDForRecipe(int nRecipeNo, const std::string& strPPID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return false;
+ }
+
+ // 转义单引号,防止 SQL 注入
+ std::string safePPID = strPPID;
+ size_t pos = 0;
+ while ((pos = safePPID.find('\'', pos)) != std::string::npos) {
+ safePPID.insert(pos, 1, '\'');
+ pos += 2;
+ }
+
+ std::string updateSQL = "UPDATE EqpPPID SET PPID = '" + safePPID + "' WHERE RecipeNo = " + std::to_string(nRecipeNo) + ";";
+ return m_pDB->executeQuery(updateSQL);
+}
+
+std::string SECSRuntimeManager::getPPIDForRecipe(int nRecipeNo) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return "";
+ }
+
+ std::string querySQL = "SELECT PPID FROM EqpPPID WHERE RecipeNo = " + std::to_string(nRecipeNo) + ";";
+ std::vector<std::vector<std::string>> results = m_pDB->fetchResults(querySQL);
+ if (!results.empty() && !results[0].empty()) {
+ return results[0][0];
+ }
+
+ return "";
+}
+
+int SECSRuntimeManager::getRecipeForPPID(std::string strPPID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return -1;
+ }
+
+ std::string querySQL = "SELECT RecipeNo FROM EqpPPID WHERE PPID = '" + strPPID + "';";
+ std::vector<std::vector<std::string>> results = m_pDB->fetchResults(querySQL);
+ if (!results.empty() && !results[0].empty()) {
+ return std::stoi(results[0][0]);
+ }
+
+ return -1;
+}
+
+bool SECSRuntimeManager::deletePPIDForRecipe(int nRecipeNo) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return false;
+ }
+
+ std::string deleteSQL = "UPDATE EqpPPID SET PPID = NULL WHERE RecipeNo = " + std::to_string(nRecipeNo) + ";";
+ return m_pDB->executeQuery(deleteSQL);
+}
+
+bool SECSRuntimeManager::deletePPIDForAllRecipes() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return false;
+ }
+
+ std::string deleteSQL = "UPDATE EqpPPID SET PPID = NULL WHERE RecipeNo BETWEEN 0 AND 511;";
+ return m_pDB->executeQuery(deleteSQL);
+}
+
+// 初始化 RPTID 表
+void SECSRuntimeManager::initRPTIDTable() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 创建 Report 表
+ std::string createReportTableSQL =
+ "CREATE TABLE IF NOT EXISTS Report ("
+ "RPTID INTEGER PRIMARY KEY);";
+
+ // 创建 ReportVIDs 表,保证同一个 RPTID 下的 VID 唯一
+ std::string createReportVIDsTableSQL =
+ "CREATE TABLE IF NOT EXISTS ReportVIDs ("
+ "RPTID INTEGER NOT NULL, "
+ "VID INTEGER NOT NULL, "
+ "FOREIGN KEY (RPTID) REFERENCES Report(RPTID), "
+ "UNIQUE(RPTID, VID));";
+
+ // 执行创建表操作
+ if (!m_pDB->executeQuery(createReportTableSQL)) {
+ throw std::runtime_error("Failed to create Report table.");
+ }
+ if (!m_pDB->executeQuery(createReportVIDsTableSQL)) {
+ throw std::runtime_error("Failed to create ReportVIDs table.");
+ }
+}
+
+// 添加 RPTID 数据
+int SECSRuntimeManager::addRPTIDVID(int nRPTID, int nVID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 检查 RPTID 和 VID 的组合是否已经存在
+ std::string checkExistSQL = "SELECT COUNT(*) FROM ReportVIDs WHERE RPTID = " + std::to_string(nRPTID) + " AND VID = " + std::to_string(nVID) + ";";
+ int count = getIntFromDB(checkExistSQL);
+ if (count > 0) {
+ return 2; // 如果关系已存在,返回错误代码 2
+ }
+
+ // 检查 Report 表中是否存在该 RPTID
+ std::string checkReportExistSQL = "SELECT COUNT(*) FROM Report WHERE RPTID = " + std::to_string(nRPTID) + ";";
+ int reportCount = getIntFromDB(checkReportExistSQL);
+
+ if (reportCount == 0) {
+ // 如果 RPTID 不存在,插入 RPTID
+ std::string insertReportSQL = "INSERT INTO Report (RPTID) VALUES (" + std::to_string(nRPTID) + ");";
+ if (!m_pDB->executeQuery(insertReportSQL)) {
+ return 3; // 插入失败,返回错误代码 3
+ }
+ }
+
+ // 构造插入 SQL 语句
+ std::string insertSQL = "INSERT INTO ReportVIDs (RPTID, VID) VALUES ("
+ + std::to_string(nRPTID) + ", "
+ + std::to_string(nVID) + ");";
+
+ // 执行插入操作
+ if (!m_pDB->executeQuery(insertSQL)) {
+ return 4; // 插入失败,返回错误代码 4
+ }
+
+ return 0; // 插入成功,返回 0
+}
+
+// 获取指定 RPTID 的所有 VID
+int SECSRuntimeManager::addRPTIDVIDs(int nRPTID, const std::vector<int>& vecVID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1;
+ }
+
+ // 检查 Report 表中是否存在该 RPTID
+ std::string checkReportExistSQL = "SELECT COUNT(*) FROM Report WHERE RPTID = " + std::to_string(nRPTID) + ";";
+ int reportCount = getIntFromDB(checkReportExistSQL);
+
+ if (reportCount == 0) {
+ // 如果 RPTID 不存在,插入 RPTID
+ std::string insertReportSQL = "INSERT INTO Report (RPTID) VALUES (" + std::to_string(nRPTID) + ");";
+ if (!m_pDB->executeQuery(insertReportSQL)) {
+ return 2; // 插入失败,返回错误代码 3
+ }
+ }
+
+ // 批量插入 VID 和 RPTID 关系
+ for (int nVID : vecVID) {
+ // 检查当前 RPTID 和 VID 的组合是否已经存在
+ std::string checkExistSQL = "SELECT COUNT(*) FROM ReportVIDs WHERE RPTID = " + std::to_string(nRPTID) + " AND VID = " + std::to_string(nVID) + ";";
+ int count = getIntFromDB(checkExistSQL);
+ if (count == 0) {
+ // 如果关系不存在,插入
+ std::string insertSQL = "INSERT INTO ReportVIDs (RPTID, VID) VALUES ("
+ + std::to_string(nRPTID) + ", "
+ + std::to_string(nVID) + ");";
+
+ if (!m_pDB->executeQuery(insertSQL)) {
+ return 3; // 插入失败,返回错误代码 3
+ }
+ }
+ }
+
+ return 0; // 插入成功,返回 0
+}
+
+// 获取指定 RPTID 的所有 VID
+int SECSRuntimeManager::deleteRPTIDVID(int nRPTID, int nVID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接
+ }
+
+ // 检查 ReportVIDs 表中是否存在该 RPTID 和 VID 组合
+ std::string checkExistSQL = "SELECT COUNT(*) FROM ReportVIDs WHERE RPTID = " + std::to_string(nRPTID) + " AND VID = " + std::to_string(nVID) + ";";
+ int count = getIntFromDB(checkExistSQL);
+ if (count == 0) {
+ return 2; // 记录不存在
+ }
+
+ // 删除 ReportVIDs 表中指定 RPTID 和 VID 的关系
+ std::string deleteSQL = "DELETE FROM ReportVIDs WHERE RPTID = " + std::to_string(nRPTID) + " AND VID = " + std::to_string(nVID) + ";";
+ if (!m_pDB->executeQuery(deleteSQL)) {
+ return 3; // 删除失败
+ }
+
+ return 0; // 删除成功
+}
+
+// 删除指定 RPTID 的所有 VID6
+int SECSRuntimeManager::deleteRPTID(int nRPTID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接
+ }
+
+ // 删除 ReportVIDs 表中所有与该 RPTID 相关的记录
+ std::string deleteReportVIDsSQL = "DELETE FROM ReportVIDs WHERE RPTID = " + std::to_string(nRPTID) + ";";
+ if (!m_pDB->executeQuery(deleteReportVIDsSQL)) {
+ return 2; // 删除失败
+ }
+
+ // 可选择是否删除 Report 表中的 RPTID,以下是删除 Report 表中的 RPTID 的 SQL
+ std::string deleteReportSQL = "DELETE FROM Report WHERE RPTID = " + std::to_string(nRPTID) + ";";
+ if (!m_pDB->executeQuery(deleteReportSQL)) {
+ return 3; // 删除失败
+ }
+
+ return 0; // 删除成功
+}
+
+// 更新指定 RPTID 的 VID
+int SECSRuntimeManager::updateRPTIDVID(int nRPTID, int nOldVID, int nNewVID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接
+ }
+
+ // 检查指定的 RPTID 和 OldVID 是否存在
+ std::string checkExistSQL = "SELECT COUNT(*) FROM ReportVIDs WHERE RPTID = " + std::to_string(nRPTID) + " AND VID = " + std::to_string(nOldVID) + ";";
+ int count = getIntFromDB(checkExistSQL);
+ if (count == 0) {
+ return 2; // 记录不存在,不能更新
+ }
+
+ // 检查新的 VID 是否已经存在于相同的 RPTID 下,防止重复
+ std::string checkNewVIDExistSQL = "SELECT COUNT(*) FROM ReportVIDs WHERE RPTID = " + std::to_string(nRPTID) + " AND VID = " + std::to_string(nNewVID) + ";";
+ int newVIDCount = getIntFromDB(checkNewVIDExistSQL);
+ if (newVIDCount > 0) {
+ return 3; // 新的 VID 已经存在,不能重复
+ }
+
+ // 执行更新操作,替换 OldVID 为 NewVID
+ std::string updateSQL = "UPDATE ReportVIDs SET VID = " + std::to_string(nNewVID) + " WHERE RPTID = " + std::to_string(nRPTID) + " AND VID = " + std::to_string(nOldVID) + ";";
+ if (!m_pDB->executeQuery(updateSQL)) {
+ return 4; // 更新失败
+ }
+
+ return 0; // 更新成功
+}
+
+// 更新指定 RPTID 的所有 VID
+int SECSRuntimeManager::updateRPTIDVIDs(int nRPTID, const std::vector<int>& vecVID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ if (m_pDB == nullptr) {
+ return 1; // 数据库未连接
+ }
+
+ // 1. 先删除 RPTID 下所有的 VID 关联记录
+ int deleteResult = deleteRPTID(nRPTID);
+ if (deleteResult != 0) {
+ return deleteResult; // 删除失败,返回删除的错误码
+ }
+
+ // 2. 添加新的 VID 关联记录
+ int addResult = addRPTIDVIDs(nRPTID, vecVID);
+ if (addResult != 0) {
+ return 3; // 插入失败
+ }
+
+ return 0; // 删除并添加成功
+}
+
+// 获取指定 RPTID 的所有 VID
+std::vector<int> SECSRuntimeManager::getVIDsByRPTID(int nRPTID) {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ std::vector<int> vecVIDs;
+
+ if (m_pDB == nullptr) {
+ return {};
+ }
+
+ // 构建查询 SQL,查询指定 RPTID 下的所有 VID
+ std::string querySQL = "SELECT VID FROM ReportVIDs WHERE RPTID = " + std::to_string(nRPTID) + ";";
+
+ // 执行查询
+ std::vector<std::vector<std::string>> results = m_pDB->fetchResults(querySQL);
+
+ // 处理查询结果并填充到 vecVIDs 中
+ for (const auto& row : results) {
+ if (!row.empty()) {
+ int nVID = std::stoi(row[0]); // 将字符串类型的 VID 转换为 int
+ vecVIDs.push_back(nVID); // 将 VID 加入结果向量
+ }
+ }
+
+ return vecVIDs;
+}
+
+// 获取所有 RPTID
+std::vector<int> SECSRuntimeManager::getAllRPTIDs() {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ std::vector<int> vecRPTIDs;
+
+ if (m_pDB == nullptr) {
+ throw std::runtime_error("Database not connected.");
+ }
+
+ // 构建查询 SQL,查询 Report 表中的所有 RPTID
+ std::string querySQL = "SELECT RPTID FROM Report;";
+
+ // 执行查询
+ std::vector<std::vector<std::string>> results = m_pDB->fetchResults(querySQL);
+
+ // 处理查询结果并填充到 vecRPTIDs 中
+ for (const auto& row : results) {
+ if (!row.empty()) {
+ int nRPTID = std::stoi(row[0]); // 将字符串类型的 RPTID 转换为 int
+ vecRPTIDs.push_back(nRPTID); // 将 RPTID 加入结果向量
+ }
+ }
+
+ return vecRPTIDs;
}
\ No newline at end of file
--
Gitblit v1.9.3