123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #include "AlgorithmService.h"
- #include "SqlDBHelper.h"
- #include <QDebug>
- AlgorithmService::AlgorithmService(QObject *parent) { }
- int AlgorithmService::AddAlgorithm(const AlgorithmInfo &info)
- {
- int ret = -1;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- InsertQuery query = t.insertInto("t_algorithm_info( `code`, `name`, `desc`, `type`, `status`, `create_time`)");
- NonQueryResult result =
- query.values(info.code, info.name, info.desc, info.type, info.status, info.createTime).exec();
- t.commit();
- ret = result.lastInsertId().toInt();
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- bool AlgorithmService::UpdateAlgorithm(const AlgorithmInfo &info)
- {
- bool ret = false;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- t.update("t_algorithm_info")
- .set("code", info.code)
- .set("name", info.name)
- .set("desc", info.desc)
- .set("type", info.type)
- .set("updateTime", info.updateTime)
- .where("id = ?", info.id);
- t.commit();
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- bool AlgorithmService::QueryByCode(QString code)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString("SELECT * FROM t_algorithm_info WHERE code = %1 and status=1").arg(code);
- if (query.exec(selectSql)) {
- if (query.next()) {
- ret = true;
- }
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool AlgorithmService::QueryByType(QList<AlgorithmInfo *> *infos, int type)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql =
- QString("SELECT id, code,name,desc,type FROM t_algorithm_info WHERE type = %1 and status=1").arg(type);
- if (query.exec(selectSql)) {
- while (query.next()) {
- if (query.isNull(0) == false) {
- AlgorithmInfo *info = new AlgorithmInfo();
- info->id = query.value(0).toInt();
- info->code = query.value(1).toString();
- info->name = query.value(2).toString();
- info->desc = query.value(3).toString();
- info->type = query.value(5).toInt();
- infos->append(info);
- }
- }
- ret = true;
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool AlgorithmService::QueryAll(QList<AlgorithmInfo *> *infos)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql = QString(
- "SELECT id, code,name,desc,type,status,create_time,update_time FROM t_algorithm_info order by type,status");
- if (query.exec(selectSql)) {
- while (query.next()) {
- if (query.isNull(0) == false) {
- AlgorithmInfo *info = new AlgorithmInfo();
- info->id = query.value(0).toInt();
- info->code = query.value(1).toString();
- info->name = query.value(2).toString();
- info->desc = query.value(3).toString();
- info->type = query.value(5).toInt();
- info->status = query.value(6).toInt();
- info->createTime = query.value(7).toString();
- info->updateTime = query.value(8).toString();
- infos->append(info);
- }
- }
- ret = true;
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- bool AlgorithmService::DeleteById(int id)
- {
- bool ret = false;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- t.update("t_algorithm_info")
- .set("status", 1)
- .set("update_time", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
- .where("id = ?", id);
- t.commit();
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
|