SchemeProcessService.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "SchemeProcessService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. SchemeProcessService::SchemeProcessService(QObject *parent) { }
  5. int SchemeProcessService::AddSchemeProcess(const SchemePlanManager::SchemeProcessInfo &schemeProcessInfo)
  6. {
  7. int ret = -1;
  8. try {
  9. Transaction t(SqlDBHelper::getDatabase());
  10. InsertQuery query =
  11. t.insertInto("t_scheme_process_info(`project_id`, `index_type`, `step`, `type`, "
  12. "`data_source`, `algorithm`,`efficiency_grades`, `is_checked`, `create_time`)");
  13. NonQueryResult result =
  14. query.values(schemeProcessInfo.projectId, schemeProcessInfo.indexType, schemeProcessInfo.step,
  15. schemeProcessInfo.type, schemeProcessInfo.dSource, schemeProcessInfo.algorithm,
  16. schemeProcessInfo.efficiencyGrades, schemeProcessInfo.isChecked,
  17. QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
  18. .exec();
  19. t.commit();
  20. ret = result.lastInsertId().toInt();
  21. } catch (const DBException &ex) {
  22. qDebug() << ex.lastError.text();
  23. }
  24. return ret;
  25. }
  26. int SchemeProcessService::SaveSchemeProcess(const SchemePlanManager::SchemeProcessInfo &schemeProcessInfo)
  27. {
  28. int ret = -1;
  29. try {
  30. Transaction t(SqlDBHelper::getDatabase());
  31. InsertQuery query = t.insertInto("t_scheme_process_info(`id`,`project_id`, `index_type`, `step`, `type`, "
  32. "`data_source`, `algorithm`,`efficiency_grades`, `is_checked`)");
  33. NonQueryResult result =
  34. query.values(schemeProcessInfo.id, schemeProcessInfo.projectId, schemeProcessInfo.indexType,
  35. schemeProcessInfo.step, schemeProcessInfo.type, schemeProcessInfo.dSource,
  36. schemeProcessInfo.algorithm, schemeProcessInfo.efficiencyGrades,
  37. schemeProcessInfo.isChecked)
  38. .exec();
  39. t.commit();
  40. ret = result.lastInsertId().toInt();
  41. } catch (const DBException &ex) {
  42. qDebug() << ex.lastError.text();
  43. }
  44. return ret;
  45. }
  46. bool SchemeProcessService::UpdateSchemeProcess(const SchemePlanManager::SchemeProcessInfo &schemeProcessInfo)
  47. {
  48. bool ret = false;
  49. try {
  50. Transaction t(SqlDBHelper::getDatabase());
  51. t.update("t_scheme_process_info")
  52. .set("project_id", schemeProcessInfo.projectId)
  53. .set("index_type", schemeProcessInfo.indexType)
  54. .set("step", schemeProcessInfo.step)
  55. .set("type", schemeProcessInfo.type)
  56. .set("data_source", schemeProcessInfo.dSource)
  57. .set("algorithm", schemeProcessInfo.algorithm)
  58. .set("efficiency_grades", schemeProcessInfo.efficiencyGrades)
  59. .set("is_checked", schemeProcessInfo.isChecked)
  60. .set("update_time", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
  61. .where("id = ?", schemeProcessInfo.id);
  62. t.commit();
  63. ret = true;
  64. } catch (const DBException &ex) {
  65. qDebug() << ex.lastError.text();
  66. }
  67. return ret;
  68. }
  69. bool SchemeProcessService::QueryAllByProjectIdAndIndexType(
  70. QList<SchemePlanManager::SchemeProcessInfo> &schemeProcessInfo, int projectId, int indexType)
  71. {
  72. QSqlDatabase db = SqlDBHelper::getDatabase();
  73. QSqlQuery query(db);
  74. bool ret = false;
  75. QString selectSql = QString("SELECT `id`, `project_id`, `index_type`, `step`, `type`, `data_source`, "
  76. "`algorithm`, `efficiency_grades`, `is_checked` FROM "
  77. "t_scheme_process_info where project_id = %1 and index_type =%2 order by step ")
  78. .arg(projectId)
  79. .arg(indexType);
  80. if (query.exec(selectSql)) {
  81. while (query.next()) {
  82. if (query.isNull(0) == false) {
  83. SchemePlanManager::SchemeProcessInfo info;
  84. info.id = query.value(0).toInt();
  85. info.projectId = query.value(1).toInt();
  86. info.indexType = query.value(2).toInt();
  87. info.step = query.value(3).toInt();
  88. info.type = (SchemePlanManager::SchemeProcessType)query.value(4).toInt();
  89. info.dSource = (SchemePlanManager::SchemeDataSource)query.value(5).toInt();
  90. info.algorithm = (SchemePlanManager::Algorithm)query.value(6).toInt();
  91. info.efficiencyGrades = query.value(7).toInt();
  92. info.isChecked = query.value(8).toInt();
  93. schemeProcessInfo.append(info);
  94. }
  95. }
  96. ret = true;
  97. } else {
  98. qDebug() << query.lastError();
  99. }
  100. return ret;
  101. }
  102. bool SchemeProcessService::QueryAllByProjectId(QList<SchemePlanManager::SchemeProcessInfo> &schemeProcessInfo,
  103. int projectId)
  104. {
  105. QSqlDatabase db = SqlDBHelper::getDatabase();
  106. QSqlQuery query(db);
  107. bool ret = false;
  108. QString selectSql = QString("SELECT `id`, `project_id`, `index_type`, `step`, `type`, `data_source`, "
  109. "`algorithm`, `efficiency_grades`, `is_checked` FROM "
  110. "t_scheme_process_info where project_id = %1 order by step ")
  111. .arg(projectId);
  112. if (query.exec(selectSql)) {
  113. while (query.next()) {
  114. if (query.isNull(0) == false) {
  115. SchemePlanManager::SchemeProcessInfo info;
  116. info.id = query.value(0).toInt();
  117. info.projectId = query.value(1).toInt();
  118. info.indexType = query.value(2).toInt();
  119. info.step = query.value(3).toInt();
  120. info.type = (SchemePlanManager::SchemeProcessType)query.value(4).toInt();
  121. info.dSource = (SchemePlanManager::SchemeDataSource)query.value(5).toInt();
  122. info.algorithm = (SchemePlanManager::Algorithm)query.value(6).toInt();
  123. info.efficiencyGrades = query.value(7).toInt();
  124. info.isChecked = query.value(8).toInt();
  125. schemeProcessInfo.append(info);
  126. }
  127. }
  128. ret = true;
  129. } else {
  130. qDebug() << query.lastError();
  131. }
  132. return ret;
  133. }
  134. bool SchemeProcessService::DeleteById(int id)
  135. {
  136. bool ret = false;
  137. try {
  138. qDebug() << __FUNCTION__ << __LINE__
  139. << SqlDBHelper::getDatabase().driver()->hasFeature(QSqlDriver::Transactions) << endl;
  140. Transaction t(SqlDBHelper::getDatabase());
  141. t.deleteFrom("t_scheme_process_info").where("id = ?", id);
  142. t.commit();
  143. ret = true;
  144. } catch (const DBException &ex) {
  145. qDebug() << ex.lastError.text();
  146. }
  147. return ret;
  148. }
  149. bool SchemeProcessService::DeleteAll()
  150. {
  151. QSqlDatabase db = SqlDBHelper::getDatabase();
  152. QSqlQuery query(db);
  153. bool ret = false;
  154. QString deleteSql = QString("DELETE FROM t_scheme_process_info ");
  155. if (query.exec(deleteSql)) {
  156. ret = true;
  157. qDebug() << "deleteSql success!";
  158. } else {
  159. qDebug() << query.lastError();
  160. }
  161. return ret;
  162. }
  163. bool SchemeProcessService::AddAllSchemeProcess(QList<SchemePlanManager::SchemeProcessInfo> &schemeProcessInfos)
  164. {
  165. int ret = false;
  166. try {
  167. for (int i = 0; i < schemeProcessInfos.size(); i++) {
  168. SchemePlanManager::SchemeProcessInfo schemeProcessInfo = schemeProcessInfos.at(i);
  169. Transaction t(SqlDBHelper::getDatabase());
  170. InsertQuery query =
  171. t.insertInto("t_scheme_process_info(`project_id`, `index_type`, `step`, `type`, "
  172. "`data_source`, `algorithm`,`efficiency_grades`, `is_checked`, `create_time`)");
  173. NonQueryResult result =
  174. query.values(schemeProcessInfo.projectId, schemeProcessInfo.indexType, schemeProcessInfo.step,
  175. schemeProcessInfo.type, schemeProcessInfo.dSource, schemeProcessInfo.algorithm,
  176. schemeProcessInfo.efficiencyGrades, schemeProcessInfo.isChecked,
  177. QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
  178. .exec();
  179. t.commit();
  180. int id = result.lastInsertId().toInt();
  181. qDebug() << id;
  182. schemeProcessInfo.id = id;
  183. schemeProcessInfos.replace(i, schemeProcessInfo);
  184. }
  185. ret = true;
  186. } catch (const DBException &ex) {
  187. qDebug() << ex.lastError.text();
  188. }
  189. return ret;
  190. }