JUserAccountsData.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "JUserAccountsData.h"
  2. #include <QSqlError>
  3. #include <QSqlQuery>
  4. #include <qDebug>
  5. JUserAccountsData* JUserAccountsData::GetInstance()
  6. {
  7. static JUserAccountsData pInstance1;
  8. return &pInstance1;
  9. }
  10. JUserAccountsData::JUserAccountsData()
  11. {
  12. GetData();
  13. }
  14. JUserAccountsData::~JUserAccountsData()
  15. {
  16. }
  17. void JUserAccountsData::GetData()
  18. {
  19. QSqlDatabase db = QSqlDatabase::database();
  20. if (!db.isOpen())
  21. {
  22. //qWarning() << "数据库未打开";
  23. return ;
  24. }
  25. // 查询 systemConfig 表
  26. QSqlQuery queryBond(db);
  27. queryBond.prepare(R"(
  28. SELECT UserName, DisplayName,UserId,PassWord,Privilege,WaitTime
  29. FROM User
  30. )");
  31. if (queryBond.exec())
  32. {
  33. while (queryBond.next())
  34. {
  35. UserAccounts _userInfo;
  36. _userInfo.UserName = queryBond.value("UserName").toString();
  37. _userInfo.DisplayName = queryBond.value("DisplayName").toString();
  38. _userInfo.UserId = queryBond.value("UserId").toInt();
  39. _userInfo.passWord = queryBond.value("PassWord").toString();
  40. _userInfo.Privilege = queryBond.value("Privilege").toString();
  41. _userInfo.WaitTime = queryBond.value("WaitTime").toInt();
  42. m_veUserAccounts.push_back(_userInfo);
  43. }
  44. // qDebug() << "Fetched fields:" << fieldsArray;
  45. }
  46. else
  47. {
  48. qWarning() << "查询 systemConfig 失败:" << queryBond.lastError().text();
  49. }
  50. }
  51. void JUserAccountsData::Updata()
  52. {
  53. m_db = QSqlDatabase::database();
  54. if (!m_db.isOpen())
  55. {
  56. //qWarning() << "数据库未打开";
  57. return;
  58. }
  59. for (auto a: m_veUserAccounts)
  60. {
  61. AddOrUpdateUserAccount(a);
  62. }
  63. }
  64. void JUserAccountsData::AddOrUpdateUserAccount(UserAccounts& userInfo)
  65. {
  66. QSqlQuery queryCheck(m_db);
  67. queryCheck.prepare(R"(
  68. SELECT COUNT(*)
  69. FROM User
  70. WHERE UserId = :userId
  71. )");
  72. queryCheck.bindValue(":userId", userInfo.UserId);
  73. if (queryCheck.exec() && queryCheck.next()) {
  74. int count = queryCheck.value(0).toInt();
  75. if (count > 0) {
  76. // 用户已存在,所以更新该记录
  77. QSqlQuery queryUpdate(m_db);
  78. queryUpdate.prepare(R"(
  79. UPDATE User
  80. SET UserName = :userName,
  81. DisplayName = :displayName,
  82. PassWord = :passWord,
  83. Privilege = :privilege,
  84. WaitTime = :waitTime
  85. WHERE UserId = :userId
  86. )");
  87. queryUpdate.bindValue(":userName", userInfo.UserName);
  88. queryUpdate.bindValue(":displayName", userInfo.DisplayName);
  89. queryUpdate.bindValue(":passWord", userInfo.passWord);
  90. queryUpdate.bindValue(":privilege", userInfo.Privilege);
  91. queryUpdate.bindValue(":waitTime", userInfo.WaitTime);
  92. queryUpdate.bindValue(":userId", userInfo.UserId);
  93. if (queryUpdate.exec()) {
  94. qDebug() << "User updated successfully";
  95. }
  96. else {
  97. qDebug() << "Update failed:" << queryUpdate.lastError().text();
  98. }
  99. }
  100. else
  101. {
  102. // 用户不存在,所以插入新记录
  103. QSqlQuery queryInsert(m_db);
  104. queryInsert.prepare(R"(
  105. INSERT INTO User (UserName, DisplayName, UserId, PassWord, Privilege, WaitTime)
  106. VALUES (:userName, :displayName, :userId, :passWord, :privilege, :waitTime)
  107. )");
  108. queryInsert.bindValue(":userName", userInfo.UserName);
  109. queryInsert.bindValue(":displayName", userInfo.DisplayName);
  110. queryInsert.bindValue(":userId", userInfo.UserId);
  111. queryInsert.bindValue(":passWord", userInfo.passWord);
  112. queryInsert.bindValue(":privilege", userInfo.Privilege);
  113. queryInsert.bindValue(":waitTime", userInfo.WaitTime);
  114. if (queryInsert.exec()) {
  115. qDebug() << "User inserted successfully";
  116. }
  117. else {
  118. qDebug() << "Insert failed:" << queryInsert.lastError().text();
  119. }
  120. }
  121. }
  122. else {
  123. qDebug() << "Check user existence failed:" << queryCheck.lastError().text();
  124. }
  125. }