AlgorithmService.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "AlgorithmService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. AlgorithmService::AlgorithmService(QObject *parent) { }
  5. int AlgorithmService::AddAlgorithm(const AlgorithmInfo &info)
  6. {
  7. int ret = -1;
  8. try {
  9. Transaction t(SqlDBHelper::getDatabase());
  10. InsertQuery query = t.insertInto("t_algorithm_info( `code`, `name`, `desc`, `type`, `status`, `create_time`)");
  11. NonQueryResult result =
  12. query.values(info.code, info.name, info.desc, info.type, info.status, info.createTime).exec();
  13. t.commit();
  14. ret = result.lastInsertId().toInt();
  15. } catch (const DBException &ex) {
  16. qDebug() << ex.lastError.text();
  17. }
  18. return ret;
  19. }
  20. bool AlgorithmService::UpdateAlgorithm(const AlgorithmInfo &info)
  21. {
  22. bool ret = false;
  23. try {
  24. Transaction t(SqlDBHelper::getDatabase());
  25. t.update("t_algorithm_info")
  26. .set("code", info.code)
  27. .set("name", info.name)
  28. .set("desc", info.desc)
  29. .set("type", info.type)
  30. .set("updateTime", info.updateTime)
  31. .where("id = ?", info.id);
  32. t.commit();
  33. ret = true;
  34. } catch (const DBException &ex) {
  35. qDebug() << ex.lastError.text();
  36. }
  37. return ret;
  38. }
  39. bool AlgorithmService::QueryByCode(QString code)
  40. {
  41. QSqlDatabase db = SqlDBHelper::getDatabase();
  42. QSqlQuery query(db);
  43. bool ret = false;
  44. QString selectSql = QString("SELECT * FROM t_algorithm_info WHERE code = %1 and status=1").arg(code);
  45. if (query.exec(selectSql)) {
  46. if (query.next()) {
  47. ret = true;
  48. }
  49. } else {
  50. qDebug() << query.lastError();
  51. }
  52. return ret;
  53. }
  54. bool AlgorithmService::QueryByType(QList<AlgorithmInfo *> *infos, int type)
  55. {
  56. QSqlDatabase db = SqlDBHelper::getDatabase();
  57. QSqlQuery query(db);
  58. bool ret = false;
  59. QString selectSql =
  60. QString("SELECT id, code,name,desc,type FROM t_algorithm_info WHERE type = %1 and status=1").arg(type);
  61. if (query.exec(selectSql)) {
  62. while (query.next()) {
  63. if (query.isNull(0) == false) {
  64. AlgorithmInfo *info = new AlgorithmInfo();
  65. info->id = query.value(0).toInt();
  66. info->code = query.value(1).toString();
  67. info->name = query.value(2).toString();
  68. info->desc = query.value(3).toString();
  69. info->type = query.value(5).toInt();
  70. infos->append(info);
  71. }
  72. }
  73. ret = true;
  74. } else {
  75. qDebug() << query.lastError();
  76. }
  77. return ret;
  78. }
  79. bool AlgorithmService::QueryAll(QList<AlgorithmInfo *> *infos)
  80. {
  81. QSqlDatabase db = SqlDBHelper::getDatabase();
  82. QSqlQuery query(db);
  83. bool ret = false;
  84. QString selectSql = QString(
  85. "SELECT id, code,name,desc,type,status,create_time,update_time FROM t_algorithm_info order by type,status");
  86. if (query.exec(selectSql)) {
  87. while (query.next()) {
  88. if (query.isNull(0) == false) {
  89. AlgorithmInfo *info = new AlgorithmInfo();
  90. info->id = query.value(0).toInt();
  91. info->code = query.value(1).toString();
  92. info->name = query.value(2).toString();
  93. info->desc = query.value(3).toString();
  94. info->type = query.value(5).toInt();
  95. info->status = query.value(6).toInt();
  96. info->createTime = query.value(7).toString();
  97. info->updateTime = query.value(8).toString();
  98. infos->append(info);
  99. }
  100. }
  101. ret = true;
  102. } else {
  103. qDebug() << query.lastError();
  104. }
  105. return ret;
  106. }
  107. bool AlgorithmService::DeleteById(int id)
  108. {
  109. bool ret = false;
  110. try {
  111. Transaction t(SqlDBHelper::getDatabase());
  112. t.update("t_algorithm_info")
  113. .set("status", 1)
  114. .set("update_time", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
  115. .where("id = ?", id);
  116. t.commit();
  117. ret = true;
  118. } catch (const DBException &ex) {
  119. qDebug() << ex.lastError.text();
  120. }
  121. return ret;
  122. }