#include "SchemeInfoService.h" #include "SqlDBHelper.h" #include SchemeInfoService::SchemeInfoService(QObject *parent) { } /////////////////////方案持久化-start//////////////// bool SchemeInfoService::AddSchemeInfoList(const QList &schemeList) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); for (int i = 0; i < schemeList.length(); i++) { SchemaEval *scheme = schemeList.at(i); InsertQuery q = t.insertInto("t_scheme_info (project_id,name,remark,value_str,score,file_path,type)"); q.values(scheme->engineerId, scheme->name, scheme->remark, scheme->valueStr, scheme->score, scheme->filePath, scheme->type) .exec(); t.commit(); } ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool SchemeInfoService::DeleteSchemeByEngineerId(int engineerId) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); t.deleteFrom("t_scheme_info").where("project_id = ?", engineerId); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool SchemeInfoService::QuerySchemeInfoByEngineerId(QList *schemeList, int engineerId, int type) { QSqlDatabase db = SqlDBHelper::getDatabase(); QSqlQuery query(db); bool ret = false; QString selectSql = QString("select id,project_id,name,remark,value_str " ",score,file_path,type from t_scheme_info where " " project_id =%1 and type = %2 ") .arg(QString::number(engineerId)) .arg(type); // qDebug() << "sql=" << selectSql; if (query.exec(selectSql)) { while (query.next()) { if (query.isNull(0) == false) { SchemaEval *scheme = new SchemaEval(); scheme->id = query.value(0).toInt(); scheme->engineerId = query.value(1).toInt(); scheme->name = query.value(2).toString(); scheme->remark = query.value(3).toString(); scheme->valueStr = query.value(4).toString(); scheme->score = query.value(5).toDouble(); scheme->filePath = query.value(6).toString(); scheme->type = query.value(7).toInt(); schemeList->append(scheme); } } ret = true; } else { qDebug() << query.lastError(); } return ret; } bool SchemeInfoService::UpdateValueStrById(int id, QString valueStr) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); t.update("t_scheme_info").set("value_str", valueStr).where("id = ?", id); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool SchemeInfoService::addScheme(const SchemaEval &scheme) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); InsertQuery q = t.insertInto("t_scheme_info (project_id,name,remark,value_str,score,file_path,type)"); q.values(scheme.engineerId, scheme.name, scheme.remark, scheme.valueStr, scheme.score, scheme.filePath, scheme.type) .exec(); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool SchemeInfoService::updateSchemeScore(const int &id, const double &score) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); t.update("t_scheme_info").set("score", score).where("id = ?", id); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool SchemeInfoService::deleteScheme(int id) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); t.deleteFrom("t_scheme_info").where("id = ?", id); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool SchemeInfoService::addUniqueGCEData(const SchemaEval &scheme) { SchemaEval s; bool qRet = queryGCEData(s, scheme.engineerId, scheme.name); if (qRet == false) { return false; } if (s.id < 0) { return addScheme(scheme); } else { return UpdateValueStrById(s.id, scheme.valueStr); } } bool SchemeInfoService::queryGCEData(SchemaEval &scheme, int projId, QString name) { QSqlDatabase db = SqlDBHelper::getDatabase(); QSqlQuery query(db); bool ret = false; QString selectSql = QString("select id,project_id,name,remark,value_str " ",score,file_path,type from t_scheme_info where " " project_id =%1 and name = '%2' and type = 2") .arg(QString::number(projId)) .arg(name); qDebug() << "sql=" << selectSql; if (query.exec(selectSql)) { while (query.next()) { scheme.id = query.value(0).toInt(); scheme.engineerId = query.value(1).toInt(); scheme.name = query.value(2).toString(); scheme.remark = query.value(3).toString(); scheme.valueStr = query.value(4).toString(); scheme.score = query.value(5).toDouble(); scheme.filePath = query.value(6).toString(); scheme.type = query.value(7).toInt(); } ret = true; } else { qDebug() << query.lastError(); } return ret; } ///////////////////////common-end/////////////////////