From 1ea724d777948a9e70ba30a06d3db28641d61d0c Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期五, 21 二月 2025 17:43:57 +0800
Subject: [PATCH] 1.添加警告CEqAlarmStep

---
 SourceCode/Bond/Servo/SECSRuntimeManager.cpp |  235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 232 insertions(+), 3 deletions(-)

diff --git a/SourceCode/Bond/Servo/SECSRuntimeManager.cpp b/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
index bb382a2..0af3a89 100644
--- a/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
+++ b/SourceCode/Bond/Servo/SECSRuntimeManager.cpp
@@ -829,22 +829,251 @@
         throw std::runtime_error("Database not connected.");
     }
 
-    // 创建 RPTID 相关表
+    // 创建 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 ("
-        "ID INTEGER PRIMARY KEY AUTOINCREMENT, "
         "RPTID INTEGER NOT NULL, "
         "VID INTEGER NOT NULL, "
-        "FOREIGN KEY (RPTID) REFERENCES Report(RPTID));";
+        "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