#include "databaselms.h"

#include <QtSql>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QMessageBox>

int DataBaseLMS::insertTaskAMM(TaskAmmFim task, int id_trainee)
{
    task.ammProcedure.title = task.ammProcedure.title.replace("'", "''"); //Задваиваем одинарные кавычки

    QString queryStr = QString("INSERT INTO public.tasks_amm (title, dm_code, fk_trainee_id) "
                                                 "VALUES ('%1', '%2', %3) "
                                                 "RETURNING tasks_amm.task_id").arg(
                                                        task.ammProcedure.title,
                                                        task.ammProcedure.dmCode,
                                                        QString::number(id_trainee));

    return queryExecInt(queryStr);
}

int DataBaseLMS::updateTaskAMM(TaskAmmFim task)
{
    task.ammProcedure.title = task.ammProcedure.title.replace("'", "''"); //Задваиваем одинарные кавычки

    QString queryStr = QString("UPDATE public.tasks_amm SET title = '%1', dm_code = '%2', status = '%3' "
                                                 "WHERE task_id = %4 "
                                                 "RETURNING tasks_amm.task_id").arg(
                                                        task.ammProcedure.title,
                                                        task.ammProcedure.dmCode,
                                                        task.status,
                                                        QString::number(task.getID()) );

    return queryExecInt(queryStr);
}

int DataBaseLMS::updateStatusTaskAMM(int task_id, QString status)
{
    QString queryStr;
    bool resBool = false;
    int id_trainee = 0;

    resBool = db->transaction();

    queryStr = QString("SELECT users.user_id "
                                                 "FROM public.users JOIN public.tasks_amm ON users.user_id = tasks_amm.fk_trainee_id "
                                                 "WHERE tasks_amm.task_id = %1 "
                                                 "ORDER BY users.user_id ASC").arg(
                                                        QString::number(task_id));

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if (query.first())
        {//Обучаемый
            id_trainee = query.value(0).toInt();
        }
    }
    if(!id_trainee)
    {
        resBool = db->rollback();
        return 0;
    }


    queryStr = QString("UPDATE public.tasks_amm SET status = '%1' "
                                                 "WHERE task_id = %2 "
                                                 "RETURNING tasks_amm.task_id").arg(
                                                        status,
                                                        QString::number(task_id) );


    if(!queryExecInt(queryStr))
    {
        resBool = db->rollback();
        return 0;
    }

    resBool = db->commit();
    return id_trainee;
}

int DataBaseLMS::deleteTaskAMM(int id_task)
{
    QString queryStr;
    bool resBool = false;
    int id_trainee = 0;

    resBool = db->transaction();

    queryStr = QString("SELECT users.user_id "
                                                 "FROM public.users JOIN public.tasks_amm ON users.user_id = tasks_amm.fk_trainee_id "
                                                 "WHERE tasks_amm.task_id = %1 "
                                                 "ORDER BY users.user_id ASC").arg(
                                                        QString::number(id_task));

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if (query.first())
        {//Обучаемый
            id_trainee = query.value(0).toInt();
        }
    }
    if(!id_trainee)
    {
        resBool = db->rollback();
        return 0;
    }

    queryStr = QString("DELETE FROM public.tasks_amm "
                                                 "WHERE task_id = %1 "
                                                 "RETURNING tasks_amm.task_id").arg(
                                                        QString::number(id_task));

    if(!queryExecInt(queryStr))
    {
        resBool = db->rollback();
        return 0;
    }

    resBool = db->commit();
    return id_trainee;
}

QList<TaskAmmFim> DataBaseLMS::selectTasksAMMofTrainee(int id_trainee)
{
    QList<TaskAmmFim> listTasks;

    QString queryStr = QString("SELECT tasks_amm.task_id, tasks_amm.title, tasks_amm.dm_code, tasks_amm.status, "
                                                 "users.user_id "
                                                 "FROM public.tasks_amm JOIN public.users ON users.user_id = tasks_amm.fk_trainee_id "
                                                 "WHERE tasks_amm.fk_trainee_id = %1 "
                                                 "ORDER BY tasks_amm.task_id ASC").arg(
                                                        id_trainee);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        while (query.next())
        {//Задача
            TaskAmmFim task;

            task.setID(query.value(0).toInt());
            task.ammProcedure.title = query.value(1).toString();
            task.ammProcedure.dmCode = query.value(2).toString();
            task.status = query.value(3).toString();

            listTasks.append(task);
        }
    }

    return listTasks;
}

TaskAmmFim DataBaseLMS::selectTaskAMMbyID(int id_task)
{
    TaskAmmFim task;

    QString queryStr = QString("SELECT tasks_amm.task_id, tasks_amm.title, tasks_amm.dm_code, tasks_amm.status "
                                                 "FROM public.tasks_amm "
                                                 "WHERE tasks_amm.task_id = %1 "
                                                 "ORDER BY tasks_amm.task_id ASC").arg(
                                                        id_task);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if (query.first())
        {//Задача
            task.setID(query.value(0).toInt());
            task.ammProcedure.title = query.value(1).toString();
            task.ammProcedure.dmCode = query.value(2).toString();
            task.status = query.value(3).toString();
        }
    }

    return task;
}


int DataBaseLMS::insertTaskFIM(TaskAmmFim task, int id_trainee)
{
    QString queryStr;
    bool resBool = false;

    resBool = db->transaction();

    task.title = task.title.replace("'", "''"); //Задваиваем одинарные кавычки

    queryStr = QString("INSERT INTO public.tasks_fim (title, fk_trainee_id) "
                                                 "VALUES ('%1', %2) "
                                                 "RETURNING tasks_fim.task_id").arg(
                                                        task.title,
                                                        QString::number(id_trainee));

    int task_id = queryExecInt(queryStr);
    if(!task_id)
    {
        resBool = db->rollback();
        return 0;
    }

    for(Malfunction malfanction : task.malfunctionList)
    {
        malfanction.description = malfanction.description.replace("'", "''"); //Задваиваем одинарные кавычки

        queryStr = QString("INSERT INTO public.malfunctions (num, dm_code, description, fk_task_fim_id) "
                                                     "VALUES ('%1', '%2', '%3', %4) "
                                                     "RETURNING malfunctions.malfunction_id").arg(
                                                            malfanction.num,
                                                            malfanction.dmCode,
                                                            malfanction.description,
                                                            QString::number(task_id));

        int malfunction_id = queryExecInt(queryStr);
        if(!malfunction_id)
        {
            resBool = db->rollback();
            return 0;
        }

        //Sign
        for(MalfunctionSign sign : malfanction.malfunctionSigns)
        {
            sign.description = sign.description.replace("'", "''"); //Задваиваем одинарные кавычки

            queryStr = QString("INSERT INTO public.malf_signs (type, description, fk_malfunction_id) "
                                                         "VALUES ('%1', '%2', %3) "
                                                         "RETURNING malf_signs.sign_id").arg(
                                                                QString::number(sign.type),
                                                                sign.description,
                                                                QString::number(malfunction_id));

            if(!queryExecInt(queryStr))
            {
                resBool = db->rollback();
                return 0;
            }
        }
    }

    resBool = db->commit();
    return task_id;
}

int DataBaseLMS::updateTaskFIM(TaskAmmFim task)
{
    task.title = task.title.replace("'", "''"); //Задваиваем одинарные кавычки

    QString queryStr = QString("UPDATE public.tasks_fim SET title = '%1', status = '%2' "
                                                 "WHERE task_id = %3 "
                                                 "RETURNING tasks_fim.task_id").arg(
                                                        task.title,
                                                        task.status,
                                                        QString::number(task.getID()) );

    return queryExecInt(queryStr);
}

int DataBaseLMS::updateStatusTaskFIM(int task_id, QString status)
{
    QString queryStr;
    bool resBool = false;
    int id_trainee = 0;

    resBool = db->transaction();

    queryStr = QString("SELECT users.user_id "
                                                 "FROM public.users JOIN public.tasks_fim ON users.user_id = tasks_fim.fk_trainee_id "
                                                 "WHERE tasks_fim.task_id = %1 "
                                                 "ORDER BY users.user_id ASC").arg(
                                                        QString::number(task_id));

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if (query.first())
        {//Обучаемый
            id_trainee = query.value(0).toInt();
        }
    }
    if(!id_trainee)
    {
        resBool = db->rollback();
        return 0;
    }

    queryStr = QString("UPDATE public.tasks_fim SET status = '%1' "
                                                 "WHERE task_id = %2 "
                                                 "RETURNING tasks_fim.task_id").arg(
                                                        status,
                                                        QString::number(task_id) );

    QSqlQuery query1 = QSqlQuery(*db);
    if(!queryExec(queryStr, &query1))
    {
        resBool = db->rollback();
        return 0;
    }

    resBool = db->commit();
    return id_trainee;
}

int DataBaseLMS::deleteTaskFIM(int id_task)
{
    QString queryStr;
    bool resBool = false;
    int id_trainee = 0;

    resBool = db->transaction();

    queryStr = QString("SELECT users.user_id "
                                                 "FROM public.users JOIN public.tasks_fim ON users.user_id = tasks_fim.fk_trainee_id "
                                                 "WHERE tasks_fim.task_id = %1 "
                                                 "ORDER BY users.user_id ASC").arg(
                                                        QString::number(id_task));

    QSqlQuery queryUserSEL = QSqlQuery(*db);

    if(queryExec(queryStr, &queryUserSEL))
    {
        if (queryUserSEL.first())
        {//Обучаемый
            id_trainee = queryUserSEL.value(0).toInt();
        }
    }
    if(!id_trainee)
    {
        resBool = db->rollback();
        return 0;
    }

    //Выгребаем все malfunction для этой задачи
    queryStr = QString("SELECT malfunctions.malfunction_id "
                                                 "FROM public.malfunctions JOIN public.tasks_fim ON tasks_fim.task_id = malfunctions.fk_task_fim_id "
                                                 "WHERE malfunctions.fk_task_fim_id = %1 "
                                                 "ORDER BY malfunctions.malfunction_id ASC").arg(
                                                        id_task);
    QSqlQuery queryMalfSEL = QSqlQuery(*db);

    if(queryExec(queryStr, &queryMalfSEL))
    {
        while (queryMalfSEL.next())
        {//Неисправность
            int malfunction_id = 0;

            malfunction_id = queryMalfSEL.value(0).toString().toInt();

            if(malfunction_id)
            {
                queryStr = QString("DELETE FROM public.malf_signs "
                                                             "WHERE fk_malfunction_id = %1 ").arg(
                                                                    malfunction_id);

                QSqlQuery querySignDEL = QSqlQuery(*db);
                if(!queryExec(queryStr, &querySignDEL))
                {
                    resBool = db->rollback();
                    return 0;
                }
            }
        };
    }
    else
    {
        resBool = db->rollback();
        return 0;
    }


    queryStr = QString("DELETE FROM public.malfunctions "
                                                 "WHERE fk_task_fim_id = %1 ").arg(
                                                        QString::number(id_task));

    QSqlQuery queryMulfDEL = QSqlQuery(*db);
    if(!queryExec(queryStr, &queryMulfDEL))
    {
        resBool = db->rollback();
        return 0;
    }



    queryStr = QString("SELECT reports.report_id "
                                                 "FROM public.reports "
                                                 "WHERE fk_task_fim_id = %1 "
                                                 "ORDER BY reports.report_id ASC").arg(
                                                        QString::number(id_task));

    int report_id = 0;

    QSqlQuery queryReportsSEL = QSqlQuery(*db);
    if(queryExec(queryStr, &queryReportsSEL))
    {
        if (queryReportsSEL.first())
        {//Отчет
            report_id = queryReportsSEL.value(0).toInt();
        }
    }
    else
    {
        resBool = db->rollback();
        return 0;
    }


    queryStr = QString("DELETE FROM public.report_items "
                                                 "WHERE fk_report_id = %1 ").arg(
                                                        QString::number(report_id));

    QSqlQuery queryItemsDEL = QSqlQuery(*db);
    if(!queryExec(queryStr, &queryItemsDEL))
    {
        resBool = db->rollback();
        return 0;
    }

    queryStr = QString("DELETE FROM public.reports "
                                                 "WHERE report_id = %1 ").arg(
                                                        QString::number(report_id));

    QSqlQuery queryReportsDEL = QSqlQuery(*db);
    if(!queryExec(queryStr, &queryReportsDEL))
    {
        resBool = db->rollback();
        return 0;
    }



    queryStr = QString("DELETE FROM public.tasks_fim "
                                                 "WHERE task_id = %1 "
                                                 "RETURNING tasks_fim.task_id").arg(
                                                        QString::number(id_task));

    if(!queryExecInt(queryStr))
    {
        resBool = db->rollback();
        return 0;
    }

    resBool = db->commit();
    return id_trainee;
}

QList<TaskAmmFim> DataBaseLMS::selectTasksFIMofTrainee(int id_trainee)
{
    QList<TaskAmmFim> listTasks;
    QString queryStr;
    bool resBool = false;

    resBool = db->transaction();

    queryStr = QString("SELECT tasks_fim.task_id, tasks_fim.title, tasks_fim.status, "
                                                 "users.user_id "
                                                 "FROM public.tasks_fim JOIN public.users ON users.user_id = tasks_fim.fk_trainee_id "
                                                 "WHERE tasks_fim.fk_trainee_id = %1 "
                                                 "ORDER BY tasks_fim.task_id ASC").arg(
                                                        id_trainee);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        while (query.next())
        {//Задача
            TaskAmmFim task;

            task.setID(query.value(0).toInt());
            task.title = query.value(1).toString();
            task.status = query.value(2).toString();

            //Выгребаем все malfunction для этой задачи
            queryStr = QString("SELECT malfunctions.malfunction_id, malfunctions.num, malfunctions.dm_code, malfunctions.description, "
                                                         "tasks_fim.task_id "
                                                         "FROM public.malfunctions JOIN public.tasks_fim ON tasks_fim.task_id = malfunctions.fk_task_fim_id "
                                                         "WHERE malfunctions.fk_task_fim_id = %1 "
                                                         "ORDER BY malfunctions.num ASC").arg(
                                                                task.getID());

            QSqlQuery queryMalf = QSqlQuery(*db);

            if(queryExec(queryStr, &queryMalf))
            {
                while (queryMalf.next())
                {//Неисправность
                    Malfunction malfanction;
                    int malfunction_id = 0;

                    malfunction_id = queryMalf.value(0).toString().toInt();
                    malfanction.num = queryMalf.value(1).toString();
                    malfanction.dmCode = queryMalf.value(2).toString();
                    malfanction.description = queryMalf.value(3).toString();

                    //Выгребаем сигналы для этой неисправности
                    queryStr = QString("SELECT malf_signs.sign_id, malf_signs.type, malf_signs.description "
                                                                 "FROM public.malf_signs "
                                                                 "WHERE fk_malfunction_id = %1 "
                                                                 "ORDER BY malf_signs.sign_id ASC").arg(
                                                                        QString::number(malfunction_id));

                    QSqlQuery querySign = QSqlQuery(*db);
                    if(queryExec(queryStr, &querySign))
                    {
                        while (querySign.next())
                        {//Сигналы
                            MalfunctionSign sign;
                            sign.type = querySign.value(1).toString().toInt();
                            sign.description = querySign.value(2).toString();

                            malfanction.malfunctionSigns.append(sign);
                        };
                    }

                    task.addMalfunction(malfanction);
                };
            }
            else
            {
                resBool = db->rollback();
                return QList<TaskAmmFim>();
            }


            //Выгребаем отчет для этой задачи
            int report_id = 0;
            queryStr = QString("SELECT reports.report_id "
                                                         "FROM public.reports "
                                                         "WHERE fk_task_fim_id = %1 "
                                                         "ORDER BY reports.report_id ASC").arg(
                                                                QString::number(task.getID()));

            QSqlQuery queryReport = QSqlQuery(*db);

            if(queryExec(queryStr, &queryReport))
            {
                if (queryReport.first())
                {//Отчет
                    report_id = queryReport.value(0).toInt();
                    task.report.id = report_id;
                }
            }
            else
            {
                resBool = db->rollback();
                return QList<TaskAmmFim>();
            }

            if(report_id)
            {//Отчет есть
                //Выгребаем все item отчета

                queryStr = QString("SELECT report_items.item_id, report_items.text, report_items.doc, report_items.dm_code, report_items.title, report_items.result, report_items.fk_report_id, report_items.number "
                                                             "FROM public.report_items "
                                                             "WHERE fk_report_id = %1 "
                                                             "ORDER BY report_items.number ASC").arg(
                                                                      QString::number(report_id));

                QSqlQuery queryItems = QSqlQuery(*db);

                if(queryExec(queryStr, &queryItems))
                {
                    while (queryItems.next())
                    {//report_item
                        FIMReportItem reportItem;
                        reportItem.id = queryItems.value(0).toInt();
                        reportItem.text = queryItems.value(1).toString();
                        reportItem.procedure.doc = queryItems.value(2).toString();
                        reportItem.procedure.dmCode = queryItems.value(3).toString();
                        reportItem.procedure.title = queryItems.value(4).toString();
                        reportItem.procedure.result = queryItems.value(5).toString();
                        //item_report
                        //number

                        task.report.itemList.append(reportItem);
                    }
                }
                else
                {
                    resBool = db->rollback();
                    return QList<TaskAmmFim>();
                }
            }

            listTasks.append(task);
        };
    }
    else
    {
        resBool = db->rollback();
        return QList<TaskAmmFim>();
    }

    resBool = db->commit();
    return listTasks;
}

TaskAmmFim DataBaseLMS::selectTaskFIMbyID(int id_task)
{
    TaskAmmFim task;

    QString queryStr;
    bool resBool = false;

    resBool = db->transaction();

    queryStr = QString("SELECT tasks_fim.task_id, tasks_fim.title, tasks_fim.status "
                                                 "FROM public.tasks_fim "
                                                 "WHERE tasks_fim.task_id = %1 "
                                                 "ORDER BY tasks_fim.task_id ASC").arg(
                                                        id_task);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if(query.first())
        {//Задача

            task.setID(query.value(0).toInt());
            task.title = query.value(1).toString();
            task.status = query.value(2).toString();

            //Выгребаем все malfunction для этой задачи
            queryStr = QString("SELECT malfunctions.malfunction_id, malfunctions.num, malfunctions.dm_code, malfunctions.description, "
                                                         "tasks_fim.task_id "
                                                         "FROM public.malfunctions JOIN public.tasks_fim ON tasks_fim.task_id = malfunctions.fk_task_fim_id "
                                                         "WHERE malfunctions.fk_task_fim_id = %1 "
                                                         "ORDER BY malfunctions.num ASC").arg(
                                                                task.getID());

            QSqlQuery queryMalf = QSqlQuery(*db);

            if(queryExec(queryStr, &queryMalf))
            {
                while (queryMalf.next())
                {//Неисправность
                    Malfunction malfanction;
                    int malfunction_id = 0;

                    malfunction_id = queryMalf.value(0).toString().toInt();
                    malfanction.num = queryMalf.value(1).toString();
                    malfanction.dmCode = queryMalf.value(2).toString();
                    malfanction.description = queryMalf.value(3).toString();

                    //Выгребаем сигналы для этой неисправности
                    queryStr = QString("SELECT malf_signs.sign_id, malf_signs.type, malf_signs.description "
                                                                 "FROM public.malf_signs "
                                                                 "WHERE fk_malfunction_id = %1 "
                                                                 "ORDER BY malf_signs.sign_id ASC").arg(
                                                                        QString::number(malfunction_id));

                    QSqlQuery querySign = QSqlQuery(*db);
                    if(queryExec(queryStr, &querySign))
                    {
                        while (querySign.next())
                        {//Сигналы
                            MalfunctionSign sign;
                            sign.type = querySign.value(1).toString().toInt();
                            sign.description = querySign.value(2).toString();

                            malfanction.malfunctionSigns.append(sign);
                        };
                    }

                    task.addMalfunction(malfanction);
                };
            }
            else
            {
                resBool = db->rollback();
                return task;
            }


            //Выгребаем отчет для этой задачи
            int report_id = 0;
            queryStr = QString("SELECT reports.report_id "
                                                         "FROM public.reports "
                                                         "WHERE fk_task_fim_id = %1 "
                                                         "ORDER BY reports.report_id ASC").arg(
                                                                QString::number(task.getID()));

            QSqlQuery queryReport = QSqlQuery(*db);

            if(queryExec(queryStr, &queryReport))
            {
                if (queryReport.first())
                {//Отчет
                    report_id = queryReport.value(0).toInt();
                    task.report.id = report_id;
                }
            }
            else
            {
                resBool = db->rollback();
                return task;
            }

            if(report_id)
            {//Отчет есть
                //Выгребаем все item отчета

                queryStr = QString("SELECT report_items.item_id, report_items.text, report_items.doc, report_items.dm_code, report_items.title, report_items.result, report_items.fk_report_id, report_items.number "
                                                             "FROM public.report_items "
                                                             "WHERE fk_report_id = %1 "
                                                             "ORDER BY report_items.number ASC").arg(
                                                                      QString::number(report_id));

                QSqlQuery queryItems = QSqlQuery(*db);

                if(queryExec(queryStr, &queryItems))
                {
                    while (queryItems.next())
                    {//report_item
                        FIMReportItem reportItem;
                        reportItem.id = queryItems.value(0).toInt();
                        reportItem.text = queryItems.value(1).toString();
                        reportItem.procedure.doc = queryItems.value(2).toString();
                        reportItem.procedure.dmCode = queryItems.value(3).toString();
                        reportItem.procedure.title = queryItems.value(4).toString();
                        reportItem.procedure.result = queryItems.value(5).toString();
                        //item_report
                        //number

                        task.report.itemList.append(reportItem);
                    }
                }
                else
                {
                    resBool = db->rollback();
                    return task;
                }
            }
        };
    }
    else
    {
        resBool = db->rollback();
        return task;
    }

    resBool = db->commit();
    return task;
}

int DataBaseLMS::updateReportFIMforTask(TaskAmmFim task)
{
    QString queryStr;
    bool resBool = false;
    int report_id = 0;
    int task_id = task.getID();

    resBool = db->transaction();

    queryStr = QString("SELECT reports.report_id "
                                                 "FROM public.reports "
                                                 "WHERE fk_task_fim_id = %1 "
                                                 "ORDER BY reports.report_id ASC").arg(
                                                        QString::number(task_id));

    QSqlQuery queryReportsSEL = QSqlQuery(*db);

    if(queryExec(queryStr, &queryReportsSEL))
    {
        if (queryReportsSEL.first())
        {//Отчет
            report_id = queryReportsSEL.value(0).toInt();
        }
    }
    else
    {
        resBool = db->rollback();
        return 0;
    }

    if(!report_id)
    {
        queryStr = QString("INSERT INTO public.reports (fk_task_fim_id) "
                                                     "VALUES (%1) "
                                                     "RETURNING reports.report_id").arg(
                                                            task.getID());

        report_id = queryExecInt(queryStr);
        if(!report_id)
        {
            resBool = db->rollback();
            return 0;
        }
    }
    else
    {
        queryStr = QString("DELETE FROM public.report_items "
                                                     "WHERE fk_report_id = %1 ").arg(
                                                            QString::number(report_id));

        QSqlQuery queryItemsDEL = QSqlQuery(*db);
        if(!queryExec(queryStr, &queryItemsDEL))
        {
            resBool = db->rollback();
            return 0;
        }
    }

    int number = 0;
    for(FIMReportItem reportItem : task.report.itemList)
    {
        queryStr = QString("INSERT INTO public.report_items (text, doc, dm_code, title, result, fk_report_id, number) "
                                                     "VALUES ('%1', '%2', '%3', '%4', '%5', %6, %7) "
                                                     "RETURNING report_items.item_id").arg(
                                                            reportItem.text,
                                                            reportItem.procedure.doc,
                                                            reportItem.procedure.dmCode,
                                                            reportItem.procedure.title,
                                                            reportItem.procedure.result,
                                                            QString::number(report_id),
                                                            QString::number(++number));

        if(!queryExecInt(queryStr))
        {
            resBool = db->rollback();
            return 0;
        }
    }

    queryStr = QString("UPDATE public.tasks_fim SET status = '%1' "
                                                 "WHERE task_id = %2 ").arg(
                                                        "checkup",
                                                        QString::number(task_id) );

    QSqlQuery queryTaskUPD = QSqlQuery(*db);
    if(!queryExec(queryStr, &queryTaskUPD))
    {
        resBool = db->rollback();
        return 0;
    }

    resBool = db->commit();
    return report_id;
}
