SchemeInfoService.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "SchemeInfoService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. SchemeInfoService::SchemeInfoService(QObject *parent) { }
  5. /////////////////////方案持久化-start////////////////
  6. bool SchemeInfoService::AddSchemeInfoList(const QList<SchemaEval *> &schemeList)
  7. {
  8. bool ret = false;
  9. try {
  10. Transaction t(SqlDBHelper::getDatabase());
  11. for (int i = 0; i < schemeList.length(); i++) {
  12. SchemaEval *scheme = schemeList.at(i);
  13. InsertQuery q = t.insertInto("t_scheme_info (project_id,name,remark,value_str,score,file_path,type)");
  14. q.values(scheme->engineerId, scheme->name, scheme->remark, scheme->valueStr, scheme->score,
  15. scheme->filePath, scheme->type)
  16. .exec();
  17. t.commit();
  18. }
  19. ret = true;
  20. } catch (const DBException &ex) {
  21. qDebug() << ex.lastError.text();
  22. }
  23. return ret;
  24. }
  25. bool SchemeInfoService::DeleteSchemeByEngineerId(int engineerId)
  26. {
  27. bool ret = false;
  28. try {
  29. Transaction t(SqlDBHelper::getDatabase());
  30. t.deleteFrom("t_scheme_info").where("project_id = ?", engineerId);
  31. t.commit();
  32. ret = true;
  33. } catch (const DBException &ex) {
  34. qDebug() << ex.lastError.text();
  35. }
  36. return ret;
  37. }
  38. bool SchemeInfoService::QuerySchemeInfoByEngineerId(QList<SchemaEval *> *schemeList, int engineerId, int type)
  39. {
  40. QSqlDatabase db = SqlDBHelper::getDatabase();
  41. QSqlQuery query(db);
  42. bool ret = false;
  43. QString selectSql = QString("select id,project_id,name,remark,value_str "
  44. ",score,file_path,type from t_scheme_info where "
  45. " project_id =%1 and type = %2 ")
  46. .arg(QString::number(engineerId))
  47. .arg(type);
  48. // qDebug() << "sql=" << selectSql;
  49. if (query.exec(selectSql)) {
  50. while (query.next()) {
  51. if (query.isNull(0) == false) {
  52. SchemaEval *scheme = new SchemaEval();
  53. scheme->id = query.value(0).toInt();
  54. scheme->engineerId = query.value(1).toInt();
  55. scheme->name = query.value(2).toString();
  56. scheme->remark = query.value(3).toString();
  57. scheme->valueStr = query.value(4).toString();
  58. scheme->score = query.value(5).toDouble();
  59. scheme->filePath = query.value(6).toString();
  60. scheme->type = query.value(7).toInt();
  61. schemeList->append(scheme);
  62. }
  63. }
  64. ret = true;
  65. } else {
  66. qDebug() << query.lastError();
  67. }
  68. return ret;
  69. }
  70. bool SchemeInfoService::UpdateValueStrById(int id, QString valueStr)
  71. {
  72. bool ret = false;
  73. try {
  74. Transaction t(SqlDBHelper::getDatabase());
  75. t.update("t_scheme_info").set("value_str", valueStr).where("id = ?", id);
  76. t.commit();
  77. ret = true;
  78. } catch (const DBException &ex) {
  79. qDebug() << ex.lastError.text();
  80. }
  81. return ret;
  82. }
  83. bool SchemeInfoService::addScheme(const SchemaEval &scheme)
  84. {
  85. bool ret = false;
  86. try {
  87. Transaction t(SqlDBHelper::getDatabase());
  88. InsertQuery q = t.insertInto("t_scheme_info (project_id,name,remark,value_str,score,file_path,type)");
  89. q.values(scheme.engineerId, scheme.name, scheme.remark, scheme.valueStr, scheme.score, scheme.filePath,
  90. scheme.type)
  91. .exec();
  92. t.commit();
  93. ret = true;
  94. } catch (const DBException &ex) {
  95. qDebug() << ex.lastError.text();
  96. }
  97. return ret;
  98. }
  99. bool SchemeInfoService::updateSchemeScore(const int &id, const double &score)
  100. {
  101. bool ret = false;
  102. try {
  103. Transaction t(SqlDBHelper::getDatabase());
  104. t.update("t_scheme_info").set("score", score).where("id = ?", id);
  105. t.commit();
  106. ret = true;
  107. } catch (const DBException &ex) {
  108. qDebug() << ex.lastError.text();
  109. }
  110. return ret;
  111. }
  112. bool SchemeInfoService::deleteScheme(int id)
  113. {
  114. bool ret = false;
  115. try {
  116. Transaction t(SqlDBHelper::getDatabase());
  117. t.deleteFrom("t_scheme_info").where("id = ?", id);
  118. t.commit();
  119. ret = true;
  120. } catch (const DBException &ex) {
  121. qDebug() << ex.lastError.text();
  122. }
  123. return ret;
  124. }
  125. bool SchemeInfoService::addUniqueGCEData(const SchemaEval &scheme)
  126. {
  127. SchemaEval s;
  128. bool qRet = queryGCEData(s, scheme.engineerId, scheme.name);
  129. if (qRet == false) {
  130. return false;
  131. }
  132. if (s.id < 0) {
  133. return addScheme(scheme);
  134. } else {
  135. return UpdateValueStrById(s.id, scheme.valueStr);
  136. }
  137. }
  138. bool SchemeInfoService::queryGCEData(SchemaEval &scheme, int projId, QString name)
  139. {
  140. QSqlDatabase db = SqlDBHelper::getDatabase();
  141. QSqlQuery query(db);
  142. bool ret = false;
  143. QString selectSql = QString("select id,project_id,name,remark,value_str "
  144. ",score,file_path,type from t_scheme_info where "
  145. " project_id =%1 and name = '%2' and type = 2")
  146. .arg(QString::number(projId))
  147. .arg(name);
  148. qDebug() << "sql=" << selectSql;
  149. if (query.exec(selectSql)) {
  150. while (query.next()) {
  151. scheme.id = query.value(0).toInt();
  152. scheme.engineerId = query.value(1).toInt();
  153. scheme.name = query.value(2).toString();
  154. scheme.remark = query.value(3).toString();
  155. scheme.valueStr = query.value(4).toString();
  156. scheme.score = query.value(5).toDouble();
  157. scheme.filePath = query.value(6).toString();
  158. scheme.type = query.value(7).toInt();
  159. }
  160. ret = true;
  161. } else {
  162. qDebug() << query.lastError();
  163. }
  164. return ret;
  165. }
  166. ///////////////////////common-end/////////////////////