GradeIndexInfoService.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "GradeIndexInfoService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. GradeIndexInfoService::GradeIndexInfoService(QObject *parent) { }
  5. bool GradeIndexInfoService::QueryGradeIndexInfoByProjectId(QList<GradeIndexInfo *> *gradeIndexInfoList, int projectId)
  6. {
  7. QSqlDatabase db = SqlDBHelper::getDatabase();
  8. QSqlQuery query(db);
  9. bool ret = false;
  10. QString selectSql =
  11. QString("SELECT id, project_id,grade_index_name,grade_index_value,grade_parent_name,grade_level FROM "
  12. "t_grade_index_info WHERE project_id = %1")
  13. .arg(projectId);
  14. if (query.exec(selectSql)) {
  15. while (query.next()) {
  16. if (query.isNull(0) == false) {
  17. GradeIndexInfo *gradeIndexInfo = new GradeIndexInfo;
  18. gradeIndexInfo->id = query.value(0).toInt();
  19. gradeIndexInfo->projectId = query.value(1).toInt();
  20. gradeIndexInfo->gradeIndexName = query.value(2).toString();
  21. gradeIndexInfo->gradeIndexValue = query.value(3).toString();
  22. gradeIndexInfo->gradeParentName = query.value(4).toString();
  23. gradeIndexInfo->gradeLevel = query.value(5).toInt();
  24. gradeIndexInfoList->append(gradeIndexInfo);
  25. }
  26. }
  27. ret = true;
  28. } else {
  29. qDebug() << query.lastError();
  30. }
  31. return ret;
  32. }
  33. int GradeIndexInfoService::AddGradeIndexInfo(const GradeIndexInfo &gradeIndexInfo)
  34. {
  35. int ret = -1;
  36. try {
  37. Transaction t(SqlDBHelper::getDatabase());
  38. InsertQuery query = t.insertInto("t_grade_index_info(`project_id`, `grade_index_name`, `grade_index_value`, "
  39. "`grade_parent_name`, `grade_level`)");
  40. NonQueryResult result =
  41. query.values(gradeIndexInfo.projectId, gradeIndexInfo.gradeIndexName, gradeIndexInfo.gradeIndexValue,
  42. gradeIndexInfo.gradeParentName, gradeIndexInfo.gradeLevel)
  43. .exec();
  44. t.commit();
  45. ret = result.lastInsertId().toInt();
  46. } catch (const DBException &ex) {
  47. qDebug() << ex.lastError.text();
  48. }
  49. return ret;
  50. }
  51. bool GradeIndexInfoService::UpdateGradeIndexInfo(const GradeIndexInfo &gradeIndexInfo)
  52. {
  53. bool ret = false;
  54. try {
  55. Transaction t(SqlDBHelper::getDatabase());
  56. t.update("t_grade_index_info")
  57. .set("grade_index_name", gradeIndexInfo.gradeIndexName)
  58. .set("grade_index_value", gradeIndexInfo.gradeIndexValue)
  59. .set("grade_parent_name", gradeIndexInfo.gradeParentName)
  60. .set("grade_level", gradeIndexInfo.gradeLevel)
  61. .where("id = ?", gradeIndexInfo.id);
  62. t.commit();
  63. ret = true;
  64. } catch (const DBException &ex) {
  65. qDebug() << ex.lastError.text();
  66. }
  67. return ret;
  68. }