SourceCode/Bond/Servo/SECSRuntimeManager.cpp
@@ -39,6 +39,64 @@
    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;
}
// 设置数据库连接
void SECSRuntimeManager::setDatabase(BL::Database* db) {
    std::lock_guard<std::mutex> lock(m_mutex);
@@ -159,6 +217,201 @@
    }
}
// 添加 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) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    if (isIDDuplicate(nID)) {
      return 2;
    }
    if (isNameDuplicate(sName)) {
        return 3;
    }
    // 如果 Unit 是 "NULL" 字符串或者为空,则插入 NULL 值
    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" || sUnit.empty()) ? "NULL" : "'" + sUnit + "'") + ", '"
        + sRemark + "', "
        + std::to_string(nSystemID) + ");";
    if (!m_pDB->executeQuery(insertSQL)) {
        return 4;
    }
    return 0;
}
// 更新指定 ID 的 SystemSV 数据
int SECSRuntimeManager::updateIDSystemSV(int nID, int sNewID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 检查是否存在该 ID
    if (!isIDDuplicate(nID)) {
        return 2;
    }
    if (isIDDuplicate(sNewID)) {
        return 3;
    }
    // 构建更新的 SQL 语句
    std::string updateSQL = "UPDATE SystemSV SET ID = " + std::to_string(sNewID) + " WHERE ID = " + std::to_string(nID) + ";";
    if (!m_pDB->executeQuery(updateSQL)) {
        return 4;
    }
    return 0;
}
// 更新所有 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) {
    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 SystemSV 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;
}
// 删除指定 ID 的 SystemSV 数据
int SECSRuntimeManager::deleteSystemSVByID(int nID) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 检查是否存在该 ID
    if (!isIDDuplicate(nID)) {
        return 2;
    }
    // 构建删除的 SQL 语句
    std::string deleteSQL = "DELETE FROM SystemSV WHERE ID = " + std::to_string(nID) + ";";
    if (!m_pDB->executeQuery(deleteSQL)) {
        return 3;
    }
    return 0;
}
int SECSRuntimeManager::deleteAllSystemSV() {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    // 构建删除所有数据的 SQL 语句
    std::string deleteSQL = "DELETE FROM SystemSV;";
    if (!m_pDB->executeQuery(deleteSQL)) {
        return 2;
    }
    return 0; // 删除成功,返回 0 表示操作成功完成。
}
// 初始化 EqpSV 表
void SECSRuntimeManager::initEqpSVTable() {
    std::lock_guard<std::mutex> lock(m_mutex);
@@ -182,6 +435,39 @@
    }
}
// 添加 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) {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_pDB == nullptr) {
        return 1;
    }
    if (isIDDuplicate(nID)) {
        return 2;
    }
    if (isNameDuplicate(sName)) {
        return 3;
    }
    // 构建 SQL 插入语句,插入数据到 EqpSV 表中。
    std::string insertSQL = "INSERT INTO EqpSV (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 表示操作成功完成。
}
// 初始化 SystemDV 表
void SECSRuntimeManager::initSystemDVTable() {
    std::lock_guard<std::mutex> lock(m_mutex);