#include "dbthread.h"
#include "yt_unit_shm.h"

extern YT_UNIT_SHM *ytShm;


DBThread::DBThread(QObject *parent, QSqlDatabase *db, QSqlDatabase *db3) :
    QThread(parent)
{
    Db = db;
    Db3 = db3;
    sqlList.clear();
    sqlList3.clear();
}

void DBThread::appendSQL(quint8 sqlid, QString sql)
{
    if(sqlid==1)
        sqlList.append(sql);
    else if(sqlid==3)
        sqlList3.append(sql);
}

void DBThread::run()
{
    ytShm->dogTimeList.dogTime[DB_THREAD].IDX = DB_THREAD;
    ytShm->dogTimeList.dogTime[DB_THREAD].WAITSEC = 30;
    ytShm->dogTimeList.dogTime[DB_THREAD].ENABLED = 0x01;
    emit log(QString("[%1] db thread start\r\n")
             .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
    while(1)
    {
        ytShm->dogTimeList.dogTime[DB_THREAD].LASTFEED = QDateTime::currentDateTime().toTime_t();
        for(int comm=0; comm<4; comm++)
        {
            if(ytShm->commList.comm[comm].UNSAVED==0x01)
            {
                appendSQL(1,QString("update `yt_t_comm` set `CommType`=%1, `CommPath`='%2', Enabled=%3 where Id=%4")
                          .arg(ytShm->commList.comm[comm].CommType)
                          .arg(QString(ytShm->commList.comm[comm].PATH))
                          .arg(ytShm->commList.comm[comm].ENABLED)
                          .arg(ytShm->commList.comm[comm].IDX));
                ytShm->commList.comm[comm].UNSAVED = 0x00;
            }
            for(int addr=0; addr<256; addr++)
            {
                if(ytShm->sPointList.sPoint[comm][addr].UNSAVED==0x01)
                {
                    appendSQL(1,QString("update `yt_t_point` set `Enabled`=%1, `Name`='%2', `PnoType`=%3, `PnoAlarm`=%4, `PnoValue`=%5, `PnoDly`=%6, `PnoAd`=%7, `Offset`=%8, `OffsetFlag`=%9, `PnoMax`=%10, `PnoMin`=%11, `AlarmH`=%12, `AlarmL`=%13, `SubType`=%14 where `CommId`=%15 and `BusAdd`=%16")
                              .arg(ytShm->sPointList.sPoint[comm][addr].ENABLED)
                              .arg(QString(ytShm->sPointList.sPoint[comm][addr].NAME))
                              .arg(ytShm->sPointList.sPoint[comm][addr].PNO_TYPE)
                              .arg(ytShm->sPointList.sPoint[comm][addr].PNO_ALARM)
                              .arg(ytShm->sPointList.sPoint[comm][addr].PNO_VALUE)
                              .arg(ytShm->sPointList.sPoint[comm][addr].PNO_DLY)
                              .arg(ytShm->sPointList.sPoint[comm][addr].PNO_AD)
                              .arg(ytShm->sPointList.sPoint[comm][addr].OFFSET)
                              .arg(ytShm->sPointList.sPoint[comm][addr].OFFSET_FLAG)
                              .arg(ytShm->sPointList.sPoint[comm][addr].PNO_MAX)
                              .arg(ytShm->sPointList.sPoint[comm][addr].PNO_MIN)
                              .arg(ytShm->sPointList.sPoint[comm][addr].ALARM_H)
                              .arg(ytShm->sPointList.sPoint[comm][addr].ALARM_L)
                              .arg(ytShm->sPointList.sPoint[comm][addr].SUB_TYPE)
                              .arg(ytShm->sPointList.sPoint[comm][addr].COMM_IDX)
                              .arg(ytShm->sPointList.sPoint[comm][addr].BUS_ADD));
                    ytShm->sPointList.sPoint[comm][addr].UNSAVED = 0x00;
                }
            }
        }
        if(sqlList.length()>0)
        {
            if(Db->transaction())
            {
                while(sqlList.length()>0)
                {
                    emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
                             .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
                             .arg(sqlList.first()));
                    Db->exec(sqlList.first());
                    sqlList.removeFirst();
                    usleep(10000);
                }
                Db->commit();
            }
        }
        else if(sqlList3.length()>0)
        {
            if(Db3->transaction())
            {
                while(sqlList3.length()>0)
                {
                    emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
                             .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
                             .arg(sqlList3.first()));
                    printf("%s\n",sqlList3.first().toUtf8().data());
                    Db3->exec(sqlList3.first());
                    sqlList3.removeFirst();
                    usleep(10000);
                }
                Db3->commit();
            }
        }
        usleep(500000);
    }
}
