EffectResultService.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "EffectResultService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. EffectResultService::EffectResultService(QObject *parent) { }
  5. int EffectResultService::AddEffectResult(const EffectResult &effectResult)
  6. {
  7. int ret = -1;
  8. try {
  9. Transaction t(SqlDBHelper::getDatabase());
  10. InsertQuery query = t.insertInto("t_effect_result(`project_id`, `effect_old`, `effect_new`, "
  11. "`effect_result`, `create_time`)");
  12. NonQueryResult result = query.values(effectResult.projectId).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 EffectResultService::QueryAllEffectResultByProjectId(QList<EffectResult *> *effectResult, int projectId)
  21. {
  22. QSqlDatabase db = SqlDBHelper::getDatabase();
  23. QSqlQuery query(db);
  24. bool ret = false;
  25. QString selectSql = QString("SELECT id, project_id, effect_old,effect_new,effect_result,create_time FROM "
  26. "t_effect_result where project_id = %1 order by create_time desc")
  27. .arg(projectId);
  28. if (query.exec(selectSql)) {
  29. while (query.next()) {
  30. if (query.isNull(0) == false) {
  31. EffectResult *projectInfo = new EffectResult();
  32. projectInfo->id = query.value(0).toInt();
  33. effectResult->append(projectInfo);
  34. }
  35. ret = true;
  36. }
  37. } else {
  38. qDebug() << query.lastError();
  39. }
  40. return ret;
  41. }
  42. bool EffectResultService::addDataList(QList<EffectResult> &dataList)
  43. {
  44. if (dataList.size() <= 0) {
  45. return true;
  46. }
  47. bool deleteRet = deleteData(dataList.first().projectId);
  48. if (deleteRet == false) {
  49. return false;
  50. }
  51. bool ret = false;
  52. try {
  53. Transaction t(SqlDBHelper::getDatabase());
  54. for (int i = 0; i < dataList.size(); i++) {
  55. EffectResult effect = dataList[i];
  56. InsertQuery query =
  57. t.insertInto("t_effect_result (project_id, scheme_id, scheme_name, alg_type, index_name, "
  58. "value)");
  59. query.values(effect.projectId, effect.schemeId, effect.schemeName, effect.algType, effect.indexName,
  60. effect.value)
  61. .exec();
  62. t.commit();
  63. }
  64. ret = true;
  65. } catch (const DBException &ex) {
  66. qDebug() << ex.lastError.text();
  67. }
  68. return ret;
  69. }
  70. bool EffectResultService::queryDataList(QList<EffectResult> &dataList, int projectId)
  71. {
  72. QSqlDatabase db = SqlDBHelper::getDatabase();
  73. QSqlQuery query(db);
  74. bool ret = false;
  75. QString selectSql = QString("SELECT id, project_id, scheme_id, scheme_name, index_name, value FROM "
  76. "t_effect_result where project_id = %1 order by id")
  77. .arg(projectId);
  78. if (query.exec(selectSql)) {
  79. while (query.next()) {
  80. if (query.isNull(0) == false) {
  81. EffectResult effect;
  82. effect.id = query.value(0).toInt();
  83. effect.projectId = query.value(1).toInt();
  84. effect.schemeId = query.value(2).toInt();
  85. effect.schemeName = query.value(3).toString();
  86. effect.indexName = query.value(4).toString();
  87. effect.value = query.value(5).toString();
  88. dataList.append(effect);
  89. }
  90. ret = true;
  91. }
  92. } else {
  93. qDebug() << query.lastError();
  94. }
  95. return ret;
  96. }
  97. bool EffectResultService::deleteData(int projectId)
  98. {
  99. bool ret = false;
  100. try {
  101. Transaction t(SqlDBHelper::getDatabase());
  102. t.deleteFrom("t_effect_result").where("project_id = ?", projectId);
  103. t.commit();
  104. ret = true;
  105. } catch (const DBException &ex) {
  106. qDebug() << ex.lastError.text();
  107. }
  108. return ret;
  109. }