#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;
}