#include "GradeInfoService.h" #include "SqlDBHelper.h" #include GradeInfoService::GradeInfoService(QObject *parent) { } int GradeInfoService::AddGradeInfo(const GradeInfo &gradeInfo) { int ret = -1; try { Transaction t(SqlDBHelper::getDatabase()); InsertQuery query = t.insertInto("t_grade_info(`project_id`, `grade_name`, `grade_value`, `type`)"); NonQueryResult result = query.values(gradeInfo.projectId, gradeInfo.gradeName, gradeInfo.gradeValue, gradeInfo.type).exec(); t.commit(); ret = result.lastInsertId().toInt(); } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool GradeInfoService::UpdateGradeInfo(const GradeInfo &gradeInfo) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); t.update("t_grade_info") .set("grade_name", gradeInfo.gradeName) .set("grade_value", gradeInfo.gradeValue) .set("type", gradeInfo.type) .where("id = ?", gradeInfo.id); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool GradeInfoService::QueryGradeByProjectIdAndType(QList *gradeInfoList, int projectId, int type) { QSqlDatabase db = SqlDBHelper::getDatabase(); QSqlQuery query(db); bool ret = false; QString selectSql = QString("SELECT id, project_id,grade_name,grade_value,type FROM " "t_grade_info WHERE project_id = %1 and type = %2") .arg(projectId) .arg(type); if (query.exec(selectSql)) { while (query.next()) { if (query.isNull(0) == false) { GradeInfo *gradeInfo = new GradeInfo(); gradeInfo->id = query.value(0).toInt(); gradeInfo->projectId = query.value(1).toInt(); gradeInfo->gradeName = query.value(2).toString(); gradeInfo->gradeValue = query.value(3).toString(); gradeInfo->type = query.value(4).toInt(); gradeInfoList->append(gradeInfo); } } ret = true; } else { qDebug() << query.lastError(); } return ret; }