MindScoreService.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "MindScoreService.h"
  2. #include "ClassSet.h"
  3. #include "SqlDBHelper.h"
  4. #include <QDebug>
  5. MindScoreService::MindScoreService(QObject *parent) : QObject(parent) { }
  6. bool MindScoreService::saveUniqueScoreData(int projId, const QString &score)
  7. {
  8. MindScoreInfo info;
  9. bool qRet = queryScoreData(&info, projId);
  10. if (qRet == false) {
  11. return false;
  12. }
  13. if (info.id < 0) {
  14. info.projectId = projId;
  15. info.score = score;
  16. return saveScoreData(&info);
  17. } else {
  18. return updateScoreData(info.id, score);
  19. }
  20. }
  21. bool MindScoreService::queryScoreData(MindScoreInfo *info, int projId)
  22. {
  23. bool ret = false;
  24. try {
  25. Transaction t(SqlDBHelper::getDatabase());
  26. QString selectSql = QString("SELECT id, project_id, score "
  27. "from t_mind_score WHERE project_id = %1")
  28. .arg(projId);
  29. QueryResult queryResult = t.execQuery(selectSql);
  30. if (queryResult.next()) {
  31. info->id = queryResult.value(0).toInt();
  32. info->projectId = queryResult.value(1).toInt();
  33. info->score = queryResult.value(2).toString();
  34. }
  35. ret = true;
  36. } catch (const DBException &ex) {
  37. qDebug() << ex.lastError.text();
  38. }
  39. return ret;
  40. }
  41. bool MindScoreService::updateScoreData(int id, const QString &score)
  42. {
  43. bool ret = false;
  44. try {
  45. Transaction t(SqlDBHelper::getDatabase());
  46. t.update("t_mind_score").set("score", score).where("id = ?", id);
  47. t.commit();
  48. ret = true;
  49. } catch (const DBException &ex) {
  50. qDebug() << ex.lastError.text();
  51. }
  52. return ret;
  53. }
  54. bool MindScoreService::saveScoreData(MindScoreInfo *info)
  55. {
  56. bool ret = false;
  57. try {
  58. Transaction t(SqlDBHelper::getDatabase());
  59. InsertQuery q = t.insertInto("t_mind_score (project_id,score)");
  60. NonQueryResult result = q.values(info->projectId, info->score).exec();
  61. t.commit();
  62. info->id = result.lastInsertId().toInt();
  63. ret = true;
  64. } catch (const DBException &ex) {
  65. qDebug() << ex.lastError.text();
  66. }
  67. return ret;
  68. }