#include "MindWeightService.h"

#include "ClassSet.h"

#include "SqlDBHelper.h"

#include <QDebug>

MindWeightService::MindWeightService(QObject *parent) : QObject(parent) { }

bool MindWeightService::saveUniqueWeightData(int projId, int indexType, int dataSource, int algorithm,
                                             const QString &weight)
{
    MindWeightInfo info;
    bool qRet = queryWeightData(&info, projId, indexType);
    if (qRet == false) {
        return false;
    }

    if (info.id < 0) {
        info.projectId  = projId;
        info.indexType  = indexType;
        info.dataSource = dataSource;
        info.algorithm  = algorithm;
        info.weight     = weight;
        return saveWeightData(&info);
    } else {
        return updateWeightData(info.id, weight);
    }
}

bool MindWeightService::queryWeightData(MindWeightInfo *info, int projId, int indexType, int dataSource, int algorithm)
{
    bool ret = false;
    try {
        Transaction t(SqlDBHelper::getDatabase());
        QString selectSql = QString("SELECT id, project_id, index_type, data_source, algorithm, weight "
                                    "from t_mind_weight WHERE project_id = %1 and index_type = %2 and "
                                    "data_source = %3 and algorithm = %4")
                                    .arg(projId)
                                    .arg(indexType)
                                    .arg(dataSource)
                                    .arg(algorithm);
        QueryResult queryResult = t.execQuery(selectSql);
        if (queryResult.next()) {
            info->id         = queryResult.value(0).toInt();
            info->projectId  = queryResult.value(1).toInt();
            info->indexType  = queryResult.value(2).toInt();
            info->dataSource = queryResult.value(3).toInt();
            info->algorithm  = queryResult.value(4).toInt();
            info->weight     = queryResult.value(5).toString();
        }
        ret = true;
    } catch (const DBException &ex) {
        qDebug() << ex.lastError.text();
    }
    return ret;
}

bool MindWeightService::queryWeightData(MindWeightInfo *info, int projId, int indexType)
{
    bool ret = false;
    try {
        Transaction t(SqlDBHelper::getDatabase());
        QString selectSql = QString("SELECT id, project_id, index_type, weight "
                                    "from t_mind_weight WHERE project_id = %1 and index_type = %2")
                                    .arg(projId)
                                    .arg(indexType);
        QueryResult queryResult = t.execQuery(selectSql);
        if (queryResult.next()) {
            info->id        = queryResult.value(0).toInt();
            info->projectId = queryResult.value(1).toInt();
            info->indexType = queryResult.value(2).toInt();
            info->weight    = queryResult.value(3).toString();
        }
        ret = true;
    } catch (const DBException &ex) {
        qDebug() << ex.lastError.text();
    }
    return ret;
}

bool MindWeightService::updateWeightData(int id, const QString &weight)
{
    bool ret = false;
    try {
        Transaction t(SqlDBHelper::getDatabase());
        t.update("t_mind_weight").set("weight", weight).where("id = ?", id);
        t.commit();
        ret = true;
    } catch (const DBException &ex) {
        qDebug() << ex.lastError.text();
    }
    return ret;
}

bool MindWeightService::deleteWeightData(int id)
{
    bool ret = false;
    try {
        Transaction t(SqlDBHelper::getDatabase());
        t.deleteFrom("t_mind_weight").where("id = ?", id);
        t.commit();
        ret = true;
    } catch (const DBException &ex) {
        qDebug() << ex.lastError.text();
    }
    return ret;
}

bool MindWeightService::saveWeightData(MindWeightInfo *info)
{
    bool ret = false;
    try {
        Transaction t(SqlDBHelper::getDatabase());
        InsertQuery q = t.insertInto("t_mind_weight (project_id,index_type,data_source,algorithm,weight)");
        NonQueryResult result =
                q.values(info->projectId, info->indexType, info->dataSource, info->algorithm, info->weight).exec();
        t.commit();
        info->id = result.lastInsertId().toInt();
        ret      = true;
    } catch (const DBException &ex) {
        qDebug() << ex.lastError.text();
    }
    return ret;
}