#include <QString>
#include <iostream>
#include "sfilteralgorithm.h"
#include "sfilterprocess.h"
#include "../Json/sjson.h"
#include <QDebug>
#include <QSqlDatabase>
using namespace std;

bool SFilterProcess::init(QString _strFilterProcessGroupID, QString _strFilterProcessGroupName)
{
    if (!m_SInitializer.init(_strFilterProcessGroupID, _strFilterProcessGroupName))
    {
        cout << "initializer init error" << endl;
        return false;
    }

    m_SDatagroup.setInitializer(m_SInitializer);
    m_SConsumer.setInitiaizer(m_SInitializer);
    m_SInfluencer.setInitiaizer(m_SInitializer);
    m_SSpammer.setInitiaizer(m_SInitializer);
    m_SPowercafe.setInitiaizer(m_SInitializer);
    m_SInitializer.closeConnection(SDBManager::E_DATABASE_WEB);
    return true;
}

bool SFilterProcess::run()
{
    m_SInitializer.insertLog("New Filter start");
    QList<SInitializer::stFilterProcess> listFilterProcess = m_SInitializer.getFilterProcess();

    foreach (SInitializer::stFilterProcess process, listFilterProcess)
    {
        switch(process.m_nFilterProcessType)
        {
        case E_PROCSS_TYPE_LOAD:
        {
            m_SDatagroup.loadData(process.m_nDatagroupID);
            break;
        }
        case E_PROCSS_TYPE_CATEGORY_MAKE:
        {
            switch(process.m_nServiceNum)
            {
            case E_SERVICE_INFLUENCER:
            {
                qDebug() << "Influencer:" << process.m_nCategoryNum;
                m_SInfluencer.makeCategory(m_SDatagroup, process.m_nCategoryNum, m_SInitializer.getFilterGroup(process.m_nFilterGroupID));
                break;
            }
            case E_SERVICE_CONSUMER:
            {
                qDebug() << "Consumer:" << process.m_nCategoryNum;
                m_SConsumer.makeCategory(m_SDatagroup, process.m_nCategoryNum, m_SInitializer.getFilterGroup(process.m_nFilterGroupID));
                break;
            }
            case E_SERVICE_SPAMMER:
            {
                qDebug() << "Spammer:" << process.m_nCategoryNum;
                m_SSpammer.makeCategory(m_SDatagroup, process.m_nCategoryNum, m_SInitializer.getFilterGroup(process.m_nFilterGroupID));
                break;
            }
            case E_SERVICE_POWERCAFE:
            {
                qDebug() << "Powercafe:" << process.m_nCategoryNum;
                m_SPowercafe.makeCategory(m_SDatagroup, process.m_nCategoryNum, m_SInitializer.getFilterGroup(process.m_nFilterGroupID));
                break;
            }
            }

            break;
        }
        case E_PROCSS_TYPE_SEND:
        {
            m_SDatagroup.makeTable();

            m_SConsumer.makeOverallCategory(m_SInitializer.getAllCategoryNum(E_SERVICE_CONSUMER));
            m_SInfluencer.makeOverallCategory(m_SInitializer.getAllCategoryNum(E_SERVICE_INFLUENCER));
            m_SSpammer.makeOverallCategory(m_SInitializer.getAllCategoryNum(E_SERVICE_SPAMMER));
            m_SPowercafe.makeOverallCategory(m_SInitializer.getAllCategoryNum(E_SERVICE_POWERCAFE));

            m_SConsumer.makeTable(m_SDatagroup);
            m_SInfluencer.makeTable(m_SDatagroup);
            m_SSpammer.makeTable(m_SDatagroup);
            m_SPowercafe.makeTable(m_SDatagroup);

            m_SInitializer.openConnection(SDBManager::E_DATABASE_WEB);
            m_SDatagroup.uploadData(process.m_nCompanyNum);
            m_SConsumer.uploadData(process.m_nCompanyNum);
            m_SInfluencer.uploadData(process.m_nCompanyNum);
            m_SSpammer.uploadData(process.m_nCompanyNum);
            m_SPowercafe.uploadData(process.m_nCompanyNum);

            m_SDatagroup.changeTable(process.m_nCompanyNum);
            m_SConsumer.changeTable(process.m_nCompanyNum);
            m_SInfluencer.changeTable(process.m_nCompanyNum);
            m_SSpammer.changeTable(process.m_nCompanyNum);
            m_SPowercafe.changeTable(process.m_nCompanyNum);

            m_SInitializer.updateWebDBInfoComplete(m_SInitializer.getDatabase(SDBManager::E_DATABASE_WEB), m_SDatagroup.getstDate()->m_strMindate,
                                                   m_SDatagroup.getstDate()->m_strMaxdate, m_SDatagroup.getstDate()->m_strLastupdate,
                                                   m_SDatagroup.getCounts()->m_nFilterBodyCount, m_SDatagroup.getCounts()->m_nFilterReplyCount,
                                                   process.m_nCompanyNum);
            break;
        }
    }
    }
    m_SInitializer.insertLog("New Filter END");

    return true;
}

