ProjectMindRelationService.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "ProjectMindRelationService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. ProjectMindRelationService::ProjectMindRelationService(QObject *parent) { }
  5. bool ProjectMindRelationService::AddOrUpdateRelation(const ProjectMindRelation &relation)
  6. {
  7. bool ret = false;
  8. try {
  9. QString nowDate = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
  10. Transaction t(SqlDBHelper::getDatabase());
  11. int id = relation.id;
  12. if (id == -1) {
  13. t.update("t_project_mind_ralation")
  14. .set("mind_id", relation.mindId)
  15. .set("type", relation.type)
  16. .set("projectId", relation.projectId)
  17. .set("updateTime", nowDate)
  18. .where("id = ?", id);
  19. } else {
  20. InsertQuery query = t.insertInto("t_project_mind_ralation( `mind_id`, `project_id`, `type` `create_time`)");
  21. NonQueryResult result = query.values(relation.mindId, relation.projectId, relation.type, nowDate).exec();
  22. }
  23. t.commit();
  24. ret = true;
  25. } catch (const DBException &ex) {
  26. qDebug() << ex.lastError.text();
  27. }
  28. return ret;
  29. }
  30. bool ProjectMindRelationService::QueryByProjectIdAndType(ProjectMindRelation *relation, int projectId, int type)
  31. {
  32. QSqlDatabase db = SqlDBHelper::getDatabase();
  33. QSqlQuery query(db);
  34. bool ret = false;
  35. QString selectSql = QString("SELECT id, mind_id,project_id,type FROM t_project_mind_ralation WHERE type = %1 and "
  36. "project_id = %2 ")
  37. .arg(type)
  38. .arg(projectId);
  39. if (query.exec(selectSql)) {
  40. if (query.isNull(0) == false) {
  41. relation->id = query.value(0).toInt();
  42. relation->mindId = query.value(1).toInt();
  43. relation->projectId = query.value(0).toInt();
  44. relation->type = query.value(0).toInt();
  45. }
  46. ret = true;
  47. } else {
  48. qDebug() << query.lastError();
  49. }
  50. return ret;
  51. }
  52. bool ProjectMindRelationService::QueryByProjectId(QList<ProjectMindRelation *> *relationList, int projectId)
  53. {
  54. QSqlDatabase db = SqlDBHelper::getDatabase();
  55. QSqlQuery query(db);
  56. bool ret = false;
  57. QString selectSql = QString("SELECT id, mind_id,project_id,type FROM t_project_mind_ralation WHERE "
  58. "project_id = %1 ")
  59. .arg(projectId);
  60. if (query.exec(selectSql)) {
  61. while (query.next()) {
  62. if (query.isNull(0) == false) {
  63. ProjectMindRelation *relation = new ProjectMindRelation();
  64. relation->id = query.value(0).toInt();
  65. relation->mindId = query.value(1).toInt();
  66. relation->projectId = query.value(0).toInt();
  67. relation->type = query.value(0).toInt();
  68. relationList->append(relation);
  69. }
  70. }
  71. ret = true;
  72. } else {
  73. qDebug() << query.lastError();
  74. }
  75. return ret;
  76. }