GradeInfoService.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "GradeInfoService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. GradeInfoService::GradeInfoService(QObject *parent) { }
  5. int GradeInfoService::AddGradeInfo(const GradeInfo &gradeInfo)
  6. {
  7. int ret = -1;
  8. try {
  9. Transaction t(SqlDBHelper::getDatabase());
  10. InsertQuery query = t.insertInto("t_grade_info(`project_id`, `grade_name`, `grade_value`, `type`)");
  11. NonQueryResult result =
  12. query.values(gradeInfo.projectId, gradeInfo.gradeName, gradeInfo.gradeValue, gradeInfo.type).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 GradeInfoService::UpdateGradeInfo(const GradeInfo &gradeInfo)
  21. {
  22. bool ret = false;
  23. try {
  24. Transaction t(SqlDBHelper::getDatabase());
  25. t.update("t_grade_info")
  26. .set("grade_name", gradeInfo.gradeName)
  27. .set("grade_value", gradeInfo.gradeValue)
  28. .where("id = ?", gradeInfo.id);
  29. t.commit();
  30. ret = true;
  31. } catch (const DBException &ex) {
  32. qDebug() << ex.lastError.text();
  33. }
  34. return ret;
  35. }
  36. bool GradeInfoService::QueryGradeByProjectIdAndType(GradeInfo *gradeInfo, int projectId, int type)
  37. {
  38. QSqlDatabase db = SqlDBHelper::getDatabase();
  39. QSqlQuery query(db);
  40. bool ret = false;
  41. QString selectSql = QString("SELECT id, project_id,grade_name,grade_value FROM "
  42. "t_grade_info WHERE project_id = %1 and type = %2")
  43. .arg(projectId)
  44. .arg(type);
  45. if (query.exec(selectSql)) {
  46. if (query.next()) {
  47. if (query.isNull(0) == false) {
  48. gradeInfo->id = query.value(0).toInt();
  49. gradeInfo->projectId = query.value(1).toInt();
  50. gradeInfo->gradeName = query.value(2).toString();
  51. gradeInfo->gradeValue = query.value(3).toString();
  52. gradeInfo->type = query.value(4).toInt();
  53. }
  54. ret = true;
  55. }
  56. } else {
  57. qDebug() << query.lastError();
  58. }
  59. return ret;
  60. }