#include "SchemeProcessService.h" #include "SqlDBHelper.h" #include SchemeProcessService::SchemeProcessService(QObject *parent) { } int SchemeProcessService::AddSchemeProcess(const SchemePlanManager::SchemeProcessInfo &schemeProcessInfo) { int ret = -1; try { Transaction t(SqlDBHelper::getDatabase()); InsertQuery query = t.insertInto("t_scheme_process_info(`project_id`, `index_type`, `step`, `type`, " "`data_source`, `algorithm`,`efficiency_grades`, `is_checked`, `create_time`)"); NonQueryResult result = query.values(schemeProcessInfo.projectId, schemeProcessInfo.indexType, schemeProcessInfo.step, schemeProcessInfo.type, schemeProcessInfo.dSource, schemeProcessInfo.algorithm, schemeProcessInfo.efficiencyGrades, schemeProcessInfo.isChecked, QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) .exec(); t.commit(); ret = result.lastInsertId().toInt(); } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } int SchemeProcessService::SaveSchemeProcess(const SchemePlanManager::SchemeProcessInfo &schemeProcessInfo) { int ret = -1; try { Transaction t(SqlDBHelper::getDatabase()); InsertQuery query = t.insertInto("t_scheme_process_info(`id`,`project_id`, `index_type`, `step`, `type`, " "`data_source`, `algorithm`,`efficiency_grades`, `is_checked`)"); NonQueryResult result = query.values(schemeProcessInfo.id, schemeProcessInfo.projectId, schemeProcessInfo.indexType, schemeProcessInfo.step, schemeProcessInfo.type, schemeProcessInfo.dSource, schemeProcessInfo.algorithm, schemeProcessInfo.efficiencyGrades, schemeProcessInfo.isChecked) .exec(); t.commit(); ret = result.lastInsertId().toInt(); } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool SchemeProcessService::UpdateSchemeProcess(const SchemePlanManager::SchemeProcessInfo &schemeProcessInfo) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); t.update("t_scheme_process_info") .set("project_id", schemeProcessInfo.projectId) .set("index_type", schemeProcessInfo.indexType) .set("step", schemeProcessInfo.step) .set("type", schemeProcessInfo.type) .set("data_source", schemeProcessInfo.dSource) .set("algorithm", schemeProcessInfo.algorithm) .set("efficiency_grades", schemeProcessInfo.efficiencyGrades) .set("is_checked", schemeProcessInfo.isChecked) .set("update_time", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) .where("id = ?", schemeProcessInfo.id); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool SchemeProcessService::QueryAllByProjectIdAndIndexType( QList &schemeProcessInfo, int projectId, int indexType) { QSqlDatabase db = SqlDBHelper::getDatabase(); QSqlQuery query(db); bool ret = false; QString selectSql = QString("SELECT `id`, `project_id`, `index_type`, `step`, `type`, `data_source`, " "`algorithm`, `efficiency_grades`, `is_checked` FROM " "t_scheme_process_info where project_id = %1 and index_type =%2 order by step ") .arg(projectId) .arg(indexType); if (query.exec(selectSql)) { while (query.next()) { if (query.isNull(0) == false) { SchemePlanManager::SchemeProcessInfo info; info.id = query.value(0).toInt(); info.projectId = query.value(1).toInt(); info.indexType = query.value(2).toInt(); info.step = query.value(3).toInt(); info.type = (SchemePlanManager::SchemeProcessType)query.value(4).toInt(); info.dSource = (SchemePlanManager::SchemeDataSource)query.value(5).toInt(); info.algorithm = (SchemePlanManager::Algorithm)query.value(6).toInt(); info.efficiencyGrades = query.value(7).toInt(); info.isChecked = query.value(8).toInt(); schemeProcessInfo.append(info); } } ret = true; } else { qDebug() << query.lastError(); } return ret; } bool SchemeProcessService::QueryAllByProjectId(QList &schemeProcessInfo, int projectId) { QSqlDatabase db = SqlDBHelper::getDatabase(); QSqlQuery query(db); bool ret = false; QString selectSql = QString("SELECT `id`, `project_id`, `index_type`, `step`, `type`, `data_source`, " "`algorithm`, `efficiency_grades`, `is_checked` FROM " "t_scheme_process_info where project_id = %1 order by step ") .arg(projectId); if (query.exec(selectSql)) { while (query.next()) { if (query.isNull(0) == false) { SchemePlanManager::SchemeProcessInfo info; info.id = query.value(0).toInt(); info.projectId = query.value(1).toInt(); info.indexType = query.value(2).toInt(); info.step = query.value(3).toInt(); info.type = (SchemePlanManager::SchemeProcessType)query.value(4).toInt(); info.dSource = (SchemePlanManager::SchemeDataSource)query.value(5).toInt(); info.algorithm = (SchemePlanManager::Algorithm)query.value(6).toInt(); info.efficiencyGrades = query.value(7).toInt(); info.isChecked = query.value(8).toInt(); schemeProcessInfo.append(info); } } ret = true; } else { qDebug() << query.lastError(); } return ret; } bool SchemeProcessService::DeleteById(int id) { bool ret = false; try { qDebug() << __FUNCTION__ << __LINE__ << SqlDBHelper::getDatabase().driver()->hasFeature(QSqlDriver::Transactions) << endl; Transaction t(SqlDBHelper::getDatabase()); t.deleteFrom("t_scheme_process_info").where("id = ?", id); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool SchemeProcessService::DeleteAll() { QSqlDatabase db = SqlDBHelper::getDatabase(); QSqlQuery query(db); bool ret = false; QString deleteSql = QString("DELETE FROM t_scheme_process_info "); if (query.exec(deleteSql)) { ret = true; qDebug() << "deleteSql success!"; } else { qDebug() << query.lastError(); } return ret; } bool SchemeProcessService::AddAllSchemeProcess(QList &schemeProcessInfos) { int ret = false; try { for (int i = 0; i < schemeProcessInfos.size(); i++) { SchemePlanManager::SchemeProcessInfo schemeProcessInfo = schemeProcessInfos.at(i); Transaction t(SqlDBHelper::getDatabase()); InsertQuery query = t.insertInto("t_scheme_process_info(`project_id`, `index_type`, `step`, `type`, " "`data_source`, `algorithm`,`efficiency_grades`, `is_checked`, `create_time`)"); NonQueryResult result = query.values(schemeProcessInfo.projectId, schemeProcessInfo.indexType, schemeProcessInfo.step, schemeProcessInfo.type, schemeProcessInfo.dSource, schemeProcessInfo.algorithm, schemeProcessInfo.efficiencyGrades, schemeProcessInfo.isChecked, QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) .exec(); t.commit(); int id = result.lastInsertId().toInt(); qDebug() << id; schemeProcessInfo.id = id; schemeProcessInfos.replace(i, schemeProcessInfo); } ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; }