123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #include "JUserAccountsData.h"
- #include <QSqlError>
- #include <QSqlQuery>
- #include <qDebug>
- JUserAccountsData* JUserAccountsData::GetInstance()
- {
- static JUserAccountsData pInstance1;
- return &pInstance1;
- }
- JUserAccountsData::JUserAccountsData()
- {
- GetData();
- }
- JUserAccountsData::~JUserAccountsData()
- {
- }
- void JUserAccountsData::GetData()
- {
- QSqlDatabase db = QSqlDatabase::database();
- if (!db.isOpen())
- {
- //qWarning() << "数据库未打开";
- return ;
- }
- // 查询 systemConfig 表
- QSqlQuery queryBond(db);
- queryBond.prepare(R"(
- SELECT UserName, DisplayName,UserId,PassWord,Privilege,WaitTime
- FROM User
- )");
- if (queryBond.exec())
- {
- while (queryBond.next())
- {
- UserAccounts _userInfo;
- _userInfo.UserName = queryBond.value("UserName").toString();
- _userInfo.DisplayName = queryBond.value("DisplayName").toString();
- _userInfo.UserId = queryBond.value("UserId").toInt();
- _userInfo.passWord = queryBond.value("PassWord").toString();
- _userInfo.Privilege = queryBond.value("Privilege").toString();
- _userInfo.WaitTime = queryBond.value("WaitTime").toInt();
- m_veUserAccounts.push_back(_userInfo);
- }
- // qDebug() << "Fetched fields:" << fieldsArray;
- }
- else
- {
- qWarning() << "查询 systemConfig 失败:" << queryBond.lastError().text();
- }
- }
- void JUserAccountsData::Updata()
- {
- m_db = QSqlDatabase::database();
- if (!m_db.isOpen())
- {
- //qWarning() << "数据库未打开";
- return;
- }
- for (auto a: m_veUserAccounts)
- {
- AddOrUpdateUserAccount(a);
- }
- }
- void JUserAccountsData::AddOrUpdateUserAccount(UserAccounts& userInfo)
- {
- QSqlQuery queryCheck(m_db);
- queryCheck.prepare(R"(
- SELECT COUNT(*)
- FROM User
- WHERE UserId = :userId
- )");
- queryCheck.bindValue(":userId", userInfo.UserId);
- if (queryCheck.exec() && queryCheck.next()) {
- int count = queryCheck.value(0).toInt();
- if (count > 0) {
- // 用户已存在,所以更新该记录
- QSqlQuery queryUpdate(m_db);
- queryUpdate.prepare(R"(
- UPDATE User
- SET UserName = :userName,
- DisplayName = :displayName,
- PassWord = :passWord,
- Privilege = :privilege,
- WaitTime = :waitTime
- WHERE UserId = :userId
- )");
- queryUpdate.bindValue(":userName", userInfo.UserName);
- queryUpdate.bindValue(":displayName", userInfo.DisplayName);
- queryUpdate.bindValue(":passWord", userInfo.passWord);
- queryUpdate.bindValue(":privilege", userInfo.Privilege);
- queryUpdate.bindValue(":waitTime", userInfo.WaitTime);
- queryUpdate.bindValue(":userId", userInfo.UserId);
- if (queryUpdate.exec()) {
- qDebug() << "User updated successfully";
- }
- else {
- qDebug() << "Update failed:" << queryUpdate.lastError().text();
- }
- }
- else
- {
- // 用户不存在,所以插入新记录
- QSqlQuery queryInsert(m_db);
- queryInsert.prepare(R"(
- INSERT INTO User (UserName, DisplayName, UserId, PassWord, Privilege, WaitTime)
- VALUES (:userName, :displayName, :userId, :passWord, :privilege, :waitTime)
- )");
- queryInsert.bindValue(":userName", userInfo.UserName);
- queryInsert.bindValue(":displayName", userInfo.DisplayName);
- queryInsert.bindValue(":userId", userInfo.UserId);
- queryInsert.bindValue(":passWord", userInfo.passWord);
- queryInsert.bindValue(":privilege", userInfo.Privilege);
- queryInsert.bindValue(":waitTime", userInfo.WaitTime);
- if (queryInsert.exec()) {
- qDebug() << "User inserted successfully";
- }
- else {
- qDebug() << "Insert failed:" << queryInsert.lastError().text();
- }
- }
- }
- else {
- qDebug() << "Check user existence failed:" << queryCheck.lastError().text();
- }
- }
|