123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #include "SchemeProcessService.h"
- #include "SqlDBHelper.h"
- #include <QDebug>
- 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<SchemePlanManager::SchemeProcessInfo> &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<SchemePlanManager::SchemeProcessInfo> &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<SchemePlanManager::SchemeProcessInfo> &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;
- }
|