GradeInfoService.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. .set("type", gradeInfo.type)
  29. .where("id = ?", gradeInfo.id);
  30. t.commit();
  31. ret = true;
  32. } catch (const DBException &ex) {
  33. qDebug() << ex.lastError.text();
  34. }
  35. return ret;
  36. }
  37. bool GradeInfoService::QueryGradeByProjectIdAndType(QList<GradeInfo *> *gradeInfoList, int projectId, int type)
  38. {
  39. QSqlDatabase db = SqlDBHelper::getDatabase();
  40. QSqlQuery query(db);
  41. bool ret = false;
  42. QString selectSql = QString("SELECT id, project_id,grade_name,grade_value,type FROM "
  43. "t_grade_info WHERE project_id = %1 and type = %2")
  44. .arg(projectId)
  45. .arg(type);
  46. if (query.exec(selectSql)) {
  47. while (query.next()) {
  48. if (query.isNull(0) == false) {
  49. GradeInfo *gradeInfo = new GradeInfo();
  50. gradeInfo->id = query.value(0).toInt();
  51. gradeInfo->projectId = query.value(1).toInt();
  52. gradeInfo->gradeName = query.value(2).toString();
  53. gradeInfo->gradeValue = query.value(3).toString();
  54. gradeInfo->type = query.value(4).toInt();
  55. gradeInfoList->append(gradeInfo);
  56. }
  57. }
  58. ret = true;
  59. } else {
  60. qDebug() << query.lastError();
  61. }
  62. return ret;
  63. }