ProjectAlgorithmRelationService.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "ProjectAlgorithmRelationService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. ProjectAlgorithmRelationService::ProjectAlgorithmRelationService(QObject *parent) { }
  5. bool ProjectAlgorithmRelationService::AddOrUpdateRelation(const ProjectAlgorithmRelation &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_algorithm_ralation")
  14. .set("code", relation.code)
  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(
  21. "t_project_algorithm_ralation( `code`, `project_id`, `type`, `status`, `create_time`)");
  22. NonQueryResult result =
  23. query.values(relation.code, relation.projectId, relation.type, relation.status, nowDate).exec();
  24. }
  25. t.commit();
  26. ret = true;
  27. } catch (const DBException &ex) {
  28. qDebug() << ex.lastError.text();
  29. }
  30. return ret;
  31. }
  32. bool ProjectAlgorithmRelationService::QueryByProjectIdAndType(ProjectAlgorithmRelation *relation, int projectId,
  33. int type)
  34. {
  35. QSqlDatabase db = SqlDBHelper::getDatabase();
  36. QSqlQuery query(db);
  37. bool ret = false;
  38. QString selectSql = QString("SELECT id, code,project_id,type FROM t_project_algorithm_ralation WHERE type = %1 and "
  39. "project_id = %2 and status=1")
  40. .arg(type)
  41. .arg(projectId);
  42. if (query.exec(selectSql)) {
  43. if (query.isNull(0) == false) {
  44. relation->id = query.value(0).toInt();
  45. relation->code = query.value(1).toString();
  46. relation->projectId = query.value(0).toInt();
  47. relation->type = query.value(0).toInt();
  48. }
  49. ret = true;
  50. } else {
  51. qDebug() << query.lastError();
  52. }
  53. return ret;
  54. }