#include "mainwindow.h"
#include "myhelper.h"
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QSqlDatabase>
#include <QSqlQuery>
#include "modeller_applog/applog.h" //日志
#include "sqliteassistant/sqliteassistant.h"    //数据库助手
#include "logindlg.h"   //登陆
int LogKeepDay = 0; //日志保存天数,

//读取数据库信息
QString DB_ServerIP;  //服务器IP
QString DB_ServerProt;

QString DB_HkIP;  //抓拍机IP
QString DB_HkProt;
QString DB_HkUser;
QString DB_HkPwd;

QString DB_RFIDComName;   //读卡器
QString DB_RFIDComBund;

QString DB_ScanComName;   //扫码器
QString DB_ScanComBund;

QString DB_HTrunkComName;   //道闸
QString DB_HTrunkComBund;

QString DB_HkVideoIP;  //监控机IP
QString DB_HkVideoProt;
QString DB_HkVideoUser;
QString DB_HkVideoPwd;

int nFiles = 0;
int rmFiles = 0;

SqliteAssistant *m_SqliteAssistant;


int GetRSSCount(QString path)
{
int count=0;
QDir myDir(path);

//QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
for(unsigned int i=0;i<myDir.count();i++)
{
QString sext=myDir[i].toLower();
if(-1!=sext.indexOf(".jpg"))
count++;
}
return count;
}
void removefilesindir(const QString& path){
    QDir dir(path);
    QFileInfoList info_list = dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::AllDirs);

    if(dir.exists())
    {
        dir.setFilter(QDir::Files | QDir::NoSymLinks);
        QFileInfoList list = dir.entryInfoList();

        //qDebug()<<list.count();
    }

    foreach(QFileInfo file_info, info_list)
    {
        if (file_info.isDir())
        {
            removefilesindir(file_info.absoluteFilePath());
        }
        else if (file_info.isFile())
        {
            QFile file(file_info.absoluteFilePath());

            qDebug() << "remove file  : " << file_info.absoluteFilePath();
            file.remove();
            rmFiles ++;
            qDebug() << "%" << int((double)rmFiles / nFiles * 100) << rmFiles;

        }
    }

}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    myHelper::SetStyle("navy");
    Singleton<AppLog>::getInstance().start();

    m_SqliteAssistant = new SqliteAssistant();
    if(0!=m_SqliteAssistant->connectSql())
    {
        qDebug()<<"connectSql error";
        return 0;
    }
    else
    {
        qDebug()<<"connectSql ok";
    }
    ///读取配置信息
    DB_ServerIP = m_SqliteAssistant->Select_SystemConfigInfo("ServerIP");
    DB_ServerProt = m_SqliteAssistant->Select_SystemConfigInfo("ServerProt");

    DB_RFIDComName = m_SqliteAssistant->Select_SystemConfigInfo("RFIDComName");
    DB_RFIDComBund = m_SqliteAssistant->Select_SystemConfigInfo("RFIDComBund");

    DB_ScanComName = m_SqliteAssistant->Select_SystemConfigInfo("ScanComName");
    DB_ScanComBund = m_SqliteAssistant->Select_SystemConfigInfo("ScanComBund");

    DB_HkIP = m_SqliteAssistant->Select_SystemConfigInfo("HkIP");
    DB_HkProt = m_SqliteAssistant->Select_SystemConfigInfo("HkProt");
    DB_HkUser = m_SqliteAssistant->Select_SystemConfigInfo("HkUser");
    DB_HkPwd = m_SqliteAssistant->Select_SystemConfigInfo("HkPwd");

    DB_HTrunkComName = m_SqliteAssistant->Select_SystemConfigInfo("HTrunkComName");
    DB_HTrunkComBund = m_SqliteAssistant->Select_SystemConfigInfo("HTrunkComBund");

    DB_HkVideoIP = m_SqliteAssistant->Select_SystemConfigInfo("HkVideoIP");
    DB_HkVideoProt = m_SqliteAssistant->Select_SystemConfigInfo("HkVideoProt");
    DB_HkVideoUser = m_SqliteAssistant->Select_SystemConfigInfo("HkVideoUser");
    DB_HkVideoPwd = m_SqliteAssistant->Select_SystemConfigInfo("HkVideoPwd");

    qDebug()<<"DB_ServerIP"<<DB_ServerIP;
    qDebug()<<"DB_ServerProt"<<DB_ServerProt;
    qDebug()<<"DB_RFIDComName"<<DB_RFIDComName;
    qDebug()<<"DB_RFIDComBund"<<DB_RFIDComBund;
    qDebug()<<"DB_ScanComName"<<DB_ScanComName;
    qDebug()<<"DB_ScanComBund"<<DB_ScanComBund;
    qDebug()<<"DB_ScanComBund"<<DB_HkIP;
    qDebug()<<"DB_HkProt"<<DB_HkProt;
    qDebug()<<"DB_HkUser"<<DB_HkUser;
    qDebug()<<"DB_HkPwd"<<DB_HkPwd;
    qDebug()<<"DB_HTrunkComName"<<DB_HTrunkComName;
    qDebug()<<"DB_HTrunkComBund"<<DB_HTrunkComBund;

    qDebug()<<"DB_HkVideoIP"<<DB_HkVideoIP;
    qDebug()<<"DB_HkVideoProt"<<DB_HkVideoProt;
    qDebug()<<"DB_HkVideoUser"<<DB_HkVideoUser;
    qDebug()<<"DB_HkVideoPwd"<<DB_HkVideoPwd;


    qDebug()<<GetRSSCount("C:\\AFrontPicture");
    if(GetRSSCount("C:\\AFrontPicture")>300){
        removefilesindir("C:\\AFrontPicture");
    }
    //LogKeepDay
    Singleton<AppLog>::getInstance().onClearLogFile("./log",LogKeepDay);
    LoginDlg login;
    login.show();
    return a.exec();
}
