123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include "EffectResultService.h"
- #include "SqlDBHelper.h"
- #include <QDebug>
- EffectResultService::EffectResultService(QObject *parent) { }
- int EffectResultService::AddEffectResult(const EffectResult &effectResult)
- {
- int ret = -1;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- InsertQuery query = t.insertInto("t_effect_result(`project_id`, `effect_old`, `effect_new`, "
- "`effect_result`, `create_time`)");
- NonQueryResult result = query.values(effectResult.projectId).exec();
- t.commit();
- ret = result.lastInsertId().toInt();
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- bool EffectResultService::QueryAllEffectResultByProjectId(QList<EffectResult *> *effectResult, int projectId)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, project_id, effect_old,effect_new,effect_result,create_time FROM "
- "t_effect_result where project_id = %1 order by create_time desc")
- .arg(projectId);
- if (query.exec(selectSql)) {
- while (query.next()) {
- if (query.isNull(0) == false) {
- EffectResult *projectInfo = new EffectResult();
- projectInfo->id = query.value(0).toInt();
- effectResult->append(projectInfo);
- }
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool EffectResultService::addDataList(QList<EffectResult> &dataList)
- {
- if (dataList.size() <= 0) {
- return true;
- }
- bool deleteRet = deleteData(dataList.first().projectId);
- if (deleteRet == false) {
- return false;
- }
- bool ret = false;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- for (int i = 0; i < dataList.size(); i++) {
- EffectResult effect = dataList[i];
- InsertQuery query =
- t.insertInto("t_effect_result (project_id, scheme_id, scheme_name, alg_type, index_name, "
- "value)");
- query.values(effect.projectId, effect.schemeId, effect.schemeName, effect.algType, effect.indexName,
- effect.value)
- .exec();
- t.commit();
- }
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- bool EffectResultService::queryDataList(QList<EffectResult> &dataList, int projectId)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT id, project_id, scheme_id, scheme_name, index_name, value FROM "
- "t_effect_result where project_id = %1 order by id")
- .arg(projectId);
- if (query.exec(selectSql)) {
- while (query.next()) {
- if (query.isNull(0) == false) {
- EffectResult effect;
- effect.id = query.value(0).toInt();
- effect.projectId = query.value(1).toInt();
- effect.schemeId = query.value(2).toInt();
- effect.schemeName = query.value(3).toString();
- effect.indexName = query.value(4).toString();
- effect.value = query.value(5).toString();
- dataList.append(effect);
- }
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool EffectResultService::deleteData(int projectId)
- {
- bool ret = false;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- t.deleteFrom("t_effect_result").where("project_id = ?", projectId);
- t.commit();
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
|