#include "databaseeventthread.h"

//#define HostName "172.17.35.51"
//#define HostName "124.71.140.20"
//#define HostName "172.16.120.165"
#define HostName "uskycloud000.mysql.rds.aliyuncs.com"
#define HostPort 3306
#define UserName "usky_admin"
//"usky"
#define PassWord "Usky@2023!@#$_"
//"Yt#75Usky"
#define DatabaseName "usky-data"

DatabaseEventThread::DatabaseEventThread(QObject *parent) : QThread(parent)
{
    hour = 255;
    keep=false;
    sqlList.clear();

    db = QSqlDatabase::addDatabase("QMYSQL","write_db1");
    db.setHostName(QString(HostName));
    db.setPort(HostPort);
    db.setUserName(QString(UserName));
    db.setPassword(QString(PassWord));
    db.setDatabaseName(QString(DatabaseName));



}

void DatabaseEventThread::appendSql(QString sql)
{
    sqlList.append(sql);
}


void DatabaseEventThread::stop()
{
    keep = false;
}

void DatabaseEventThread::run()
{

    keep = true;
    while (keep) {
        if(!db.open()){
            db.open();
        }else{
            if(sqlList.length()>0){
                while (sqlList.length()>0) {
                    db.exec(sqlList.first());

                    emit dbdata_log(QString("[%1] DatabaseEventThread sqlList.length() %2 writeProcess sql %3").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).arg(sqlList.length()).arg(sqlList.first()));

                    sqlList.removeFirst();
                    usleep(1000);
                }
            }
        }
        usleep(50000);
    }


}











