#include "sfilterprocess.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QVariant>
#include <QDebug>
#include <QFile>
#include <QSqlError>
#include <QSettings>

#include "../Json/sjson.h"
#include "../common.h"

SFilterProcess::SFilterProcess() : C_MAP_REAL_BODY (0x01) , C_MAP_REAL_REPLY (0x02)
{
    m_db = QSqlDatabase::addDatabase("QMYSQL");
}

SFilterProcess::~SFilterProcess()
{
}

bool SFilterProcess::ReloadSetup()
{
    QSettings settings(QString("setup.ini"), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.beginGroup(QString("database"));
    if (settings.childKeys().size() == 0) return false;
    m_strWebDataBaseHost = settings.value("host").toString();
    m_strWebDataBaseUser = settings.value("user").toString();
    m_strWebDataBasePass = settings.value("pass").toString();
    m_strWebDataBaseName = settings.value("name").toString();
    settings.endGroup();

    settings.beginGroup(QString("settings"));
    if (settings.childKeys().size() == 0) return false;
    m_nCutDays = settings.value("days").toInt();
    QString strFrom = settings.value("from").toString().trimmed();
    QString strTo = settings.value("to").toString().trimmed();
    if(!strFrom.isEmpty())
        m_strStartDay = "'" + strFrom + "'";
    if(!strTo.isEmpty())
        m_strEndDay = "'" + strTo + "'";
    settings.endGroup();

    settings.beginGroup(QString("spammers"));
    if (settings.childKeys().size() == 0) return false;
    m_adSpammer[E_SPAMER_BODY_COUNT_CUT] = settings.value("body_cut").toDouble();
    m_adSpammer[E_SPAMER_BODY_COUNT_RATIO] = settings.value("body_ratio").toDouble();
    m_adSpammer[E_SPAMER_REPLY_COUNT_CUT] = settings.value("reply_cut").toDouble();
    m_adSpammer[E_SPAMER_REPLY_COUNT_RATIO] = settings.value("reply_ratio").toDouble();
    m_adSpammer[E_SPAMER_NICK_COUNT_CUT] = settings.value("nick_cut").toDouble();
    m_adSpammer[E_SPAMER_NICK_COUNT_RATIO] = settings.value("nick_ratio").toDouble();
    settings.endGroup();
    return true;
}

bool SFilterProcess::ReloadColumn()
{
    QVector <QStringList> vecColumn;
    QFile file("column.txt");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text) == false) return false;
    {
        while (!file.atEnd())
        {
            QString str = QString(file.readLine());
            if (str.at(0) == QChar('#')) continue;
            if (str.trimmed().isEmpty()) continue;
            vecColumn.push_back(str.split(","));
        }
    }
    if (vecColumn.size() <= 0) return false;

    foreach(QStringList strList,vecColumn)
        m_listDataBaseColumn.push_back(strList.at(E_COLUMN_DATABASE));

    for(int i = 0; i < m_listDataBaseColumn.size(); i++)
    {
        if (m_listDataBaseColumn[i].trimmed() == QString("platform_title")) m_anData[E_DATA_platform_title] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_form"))  m_anData[E_DATA_article_form] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_parent"))  m_anData[E_DATA_article_parent] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_id"))  m_anData[E_DATA_article_id] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_nickname"))  m_anData[E_DATA_article_nickname] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_title"))  m_anData[E_DATA_article_title] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_data"))  m_anData[E_DATA_article_data] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_url"))  m_anData[E_DATA_article_url] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_hit"))  m_anData[E_DATA_article_hit] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_date"))  m_anData[E_DATA_article_date] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_order"))  m_anData[E_DATA_article_order] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("platform_id"))  m_anData[E_DATA_platform_id] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("platform_name"))  m_anData[E_DATA_platform_name] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("platform_form"))  m_anData[E_DATA_platform_form] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("reply_url"))  m_anData[E_DATA_reply_url] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("keyword_id"))  m_anData[E_DATA_keyword_id] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_profileurl")) m_anData[E_DATA_article_profileurl] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("article_profile")) m_anData[E_DATA_article_profile] = i;
        if (m_listDataBaseColumn[i].trimmed() == QString("etc")) m_anData[E_DATA_etc] = i;
    }

    m_nUrlColumn = m_anData[E_DATA_article_url];
    m_nDateColumn = m_anData[E_DATA_article_date];
    m_nOrderColumn = m_anData[E_DATA_article_order];
    return true;
}

void SFilterProcess::InsertLog(QString _strMsg)
{
    cout << "[";
    cout << m_strFilterGroupName.toStdString();
    cout << "] ";
    cout << _strMsg.toStdString() << endl;

    QFile file(m_strFilterGroupName + "." + QDate::currentDate().toString(Qt::ISODate)+".log");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
        return;

    QTextStream out(&file);
    out << QTime::currentTime().toString("[hh:mm:ss] ") << _strMsg << "\n";
    file.close();
}

QString SFilterProcess::SeqDelString(QString _strData,QChar _chChar,int _nMax)
{
    static const int C_SEQ_FIND = 0;
    static const int C_SEQ_DEL = 1;

    QString strOut;
    int nMode = C_SEQ_FIND,nCount=0;
    for (int i = 0 ; i < _strData.length(); i++)
    {
        switch(nMode)
        {
            case C_SEQ_FIND:
                if (_strData[i] == _chChar)
                    nCount++;
                else
                    nCount = 0;
                strOut += _strData[i];
                if (nCount >= _nMax)
                    nMode = C_SEQ_DEL;
                break;
            case C_SEQ_DEL:
                nCount = 0;
                if (_strData[i] != _chChar)
                {
                    strOut += _strData[i];
                    nMode = C_SEQ_FIND;
                }
                break;
        }
    }
    return strOut;
}

void SFilterProcess::run(QString _strFindID,QString _strFilterGroupName)
{
    m_strFilterGroupName =  _strFilterGroupName;
    if (ReloadColumn() == false)
    {
        InsertLog("Error : column.txt do not find");
        exit(0);
        return;
    }
    m_db.setHostName(m_strWebDataBaseHost);
    m_db.setUserName(m_strWebDataBaseUser);
    m_db.setPassword(m_strWebDataBasePass);
    m_db.setDatabaseName(m_strWebDataBaseName);
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    m_mapPlatformform.clear();
    m_mapPlatformname.clear();
    {
        QSqlQuery query("select num,form FROM platformform");
        while(query.next())
            m_mapPlatformform.insert(query.value(1).toString(),query.value(0).toInt());
    }
    {
        QSqlQuery query("select num,name FROM platformname");
        while(query.next())
            m_mapPlatformname.insert(query.value(1).toString(),query.value(0).toInt());
    }
    m_db.close();
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    QString strQuery;
    QSqlQuery query;

    strQuery = QString("select filterprocess_type,datagroup_id,filtergroup_id,company_num,category_num,filterprocess_order from filterprocess where filterprocessgroup_id = ") + _strFindID;

    if(query.exec(strQuery.toUtf8()) == false)
    {
        InsertLog(query.lastQuery());
        InsertLog(query.lastError().text());
        m_db.close();
        return;
    }

    QList <QVariantList> listArgument;
    while(query.next())
    {
        QVariantList list;
        for (int i = 0; i < 6 ; i++ )
            list.push_back(query.value(i));
        listArgument.push_back(list);
    }
    m_db.close();
    /*filterprocess_type, 0
     * datagroup_id, 1
     * filtergroup_id, 2
     * company_num, 3
     * category_num 4
     */
    {
        QString strCompany;
        InsertLog("<Start>");
        foreach (QVariantList data,listArgument)
        {            
            switch(data.at(0).toInt())
            {
            case E_PROCSS_TYPE_LOAD:
                DataLoad(data.at(1).toString());
                break;
            case E_PROCSS_TYPE_CATEGORY_MAKE:
                DataCategoryMake(data.at(2).toString(),data.at(4).toInt());
                break;
            case E_PROCSS_TYPE_SEND:
                strCompany = data.at(3).toString();
                DataCategoryAllMake(strCompany);
                DataPut(strCompany);
                break;
            case E_PROCSS_TYPE_INFLUENCE_COUNT:
                DataInfluenceCount(data.at(2).toString(),strCompany);
                break;
            case E_PROCSS_TYPE_SPAMMERS_COUNT:
                DataSpammersCount(data.at(2).toString(),strCompany);
                break;
            case E_PROCSS_TYPE_POWER_CAFE_COUNT:
                DataPowerCafeCount(data.at(2).toString(),strCompany);
                break;
            }
        }
        m_db.setHostName(m_strWebDataBaseHost);
        m_db.setUserName(m_strWebDataBaseUser);
        m_db.setPassword(m_strWebDataBasePass);
        m_db.setDatabaseName(m_strWebDataBaseName);
        if (m_db.open() == false)
        {
            InsertLog(m_db.lastError().text());
            return;
        }
        strQuery = QString("UPDATE dbinfo SET lastupdate = sysdate() WHERE company_num = ") + strCompany;
        m_db.exec(strQuery.toUtf8());
        m_db.close();
        InsertLog("<Finish>");
    }
}

void SFilterProcess::DataCategoryMake(QString _strFilter,int _nCategory)
{
    InsertLog("Category Make Start");
    ReloadData();
    InsertLog("Data Filter");
    DataFilter(_strFilter);
    InsertLog("Data Add");
    DataAdd();
    InsertLog("Make Buzzy");
    MakeBuzzy(_nCategory,&m_data[0],&m_bDataFlag[0]);
    InsertLog("Data Make");
    DataMake(_nCategory,&m_data[0],&m_bDataFlag[0]);
    InsertLog("Category Make End");
}

void SFilterProcess::DataAdd()
{
    for (int nArticle = 0; nArticle < E_ARTICLE_MAX; nArticle++ )
    {
        if (m_bDataFlag[nArticle] == true)
        {
            foreach(QStringList strList , m_data[nArticle])
            {
                QString strURL = strList[m_nUrlColumn].trimmed();
                strURL += "\\";
                strURL += strList[m_nOrderColumn].trimmed();
                if (m_setDataAll[nArticle].contains(strURL) == false)
                {
                   m_dataAll[nArticle].push_back(strList);
                   m_bDataFlagAll[nArticle] = true;
                   m_setDataAll[nArticle].insert(strURL);
                }
            }
        }
    }
}

void SFilterProcess::DataCategoryAllMake(QString _strCompany)
{    
    int nCategory = GetWebData("category","num", "company_num = '" + _strCompany + "' and statsColumnNum = '1'").toInt();
    InsertLog("Make Buzzy Category All");
    MakeBuzzy(nCategory,&m_dataAll[0],&m_bDataFlagAll[0]);
    InsertLog("Data Make Category All");
    DataMake(nCategory,&m_dataAll[0],&m_bDataFlagAll[0]);
    for (int nArticle = 0; nArticle < E_ARTICLE_MAX; nArticle++ )
    {
        m_dataAll[nArticle].clear();
        m_setDataAll[nArticle].clear();
    }
}

/*
void SFilterProcess::DataCategoryMakeAllMerge(int _nCategory)
{
    InsertLog("Category Make Start");
    MakeBuzzy(_nCategory);
    InsertLog("Data Make");
    DataMake(_nCategory);
    InsertLog("Category Make End");
}
*/

QString SFilterProcess::SqlString(QString _str)
{
    _str.replace("\\","\\\\");
    _str.replace("'","\\'");
    _str.replace("\"","\\\"");
    _str.replace(",","\\,");
    return _str;
}

void SFilterProcess::FilterDate(QString _strJson)
{
    SJson json;
    int nSelect = json.Get(_strJson,"Article").toInt() - 1;
    if (nSelect < 0) return;
    m_bDataFlag[nSelect] = true;
    QVector <QStringList> copyData;
    QDate dateStart = QDate::fromString(json.Get(_strJson,"Start"),"yyyy-MM-dd");
    QDate dateEnd = QDate::fromString(json.Get(_strJson,"End"),"yyyy-MM-dd");
    foreach(QStringList strList , m_data[nSelect])
    {
        bool bFlag = false;
        QString strTime = strList.at(m_nDateColumn).trimmed();
        if (strTime.size() >= 10)
        {
            QChar ch = strTime.at(4);
            QString strFormat = QString("yyyy")+ch+QString("MM")+ch+QString("dd");
            QDate date = QDate::fromString(strTime.left(10),strFormat);
            if (dateStart <= date && dateEnd >= date)
                bFlag = true;
        }
        if (bFlag)
            copyData.push_back(strList);
    }
    m_data[nSelect].clear();
    m_data[nSelect] = copyData;
}

void SFilterProcess::FilterLength(QString _strJson)
{
    SJson json;
    int nSelect = json.Get(_strJson,"Article").toInt() - 1;
    if (nSelect < 0) return;
    m_bDataFlag[nSelect] = true;

    int nCatalog = json.GetNumber(_strJson,"Category");
    int nComp = json.GetNumber(_strJson,"Comp");
    int nFlag = json.GetNumber(_strJson,"InsDel");
    int nLength = json.Get(_strJson,"String").toInt();

    QVector <QStringList> copyData;
    foreach(QStringList strList , m_data[nSelect])
    {
        bool bFlag = false;
        int nCurrentLength = strList.at(nCatalog).trimmed().length();
        switch(nComp)
        {
        case E_LENGTH_COMP_GREATER:
            if (nCurrentLength > nLength)
                bFlag = true;
            break;
        case E_LENGTH_COMP_LESS:
            if (nCurrentLength < nLength)
                bFlag = true;
            break;
        case E_LENGTH_COMP_EQUAL:
            if (nCurrentLength == nLength)
                bFlag = true;
            break;
        }
        if (nFlag == 1) bFlag = !bFlag;
        if (bFlag)
            copyData.push_back(strList);
    }
    m_data[nSelect].clear();
    m_data[nSelect] = copyData;
}


void SFilterProcess::FilterCounter(QString _strJson)
{
    SJson json;
    int nSelect = json.Get(_strJson,"Article").toInt() - 1;
    if (nSelect < 0) return;
    m_bDataFlag[nSelect] = true;

    int nCatalog = json.GetNumber(_strJson,"Category");
    int nComp = json.GetNumber(_strJson,"Comp");
    int nFlag = json.GetNumber(_strJson,"InsDel");
    int nCounter = json.Get(_strJson,"String").toInt();

    QMap <QString, int> mapData, mapDataresult;
    foreach(QStringList strList , m_data[nSelect])
    {
        QString str = strList.at(nCatalog).trimmed();
        if(mapData.contains(str))
            mapData[str]++;
        else
            mapData.insert(str, 1);
    }

    for(QMap <QString, int>::iterator iterPos = mapData.begin(); iterPos != mapData.end(); iterPos++)
    {
        bool bFlag = false;
        switch(nComp)
        {
        case E_LENGTH_COMP_GREATER:
            if (iterPos.value() > nCounter)
                bFlag = true;
            break;
        case E_LENGTH_COMP_LESS:
            if (iterPos.value() < nCounter)
                bFlag = true;
            break;
        case E_LENGTH_COMP_EQUAL:
            if (iterPos.value() == nCounter)
                bFlag = true;
            break;
        }
        if (nFlag == 1) bFlag = !bFlag;
        if (bFlag)
            mapDataresult.insert(iterPos.key(), iterPos.value());
    }

    mapData.clear();

    QVector <QStringList> copyData;
    foreach(QStringList strList , m_data[nSelect])
    {
        QString str = strList.at(nCatalog).trimmed();
        if(mapDataresult.contains(str))
            copyData.push_back(strList);
    }
    m_data[nSelect].clear();
    m_data[nSelect] = copyData;
}

void SFilterProcess::FilterReplace(QString _strJson)
{
    SJson json;
    int nSelect = json.Get(_strJson,"Article").toInt() - 1;
    if (nSelect < 0) return;
    m_bDataFlag[nSelect] = true;

    int nCatalog = json.GetNumber(_strJson,"Category");
    QString strFind = json.Get(_strJson,"String_Find");
    QString strReplace = json.Get(_strJson,"String_Replace");

    bool bFlag = false;
    QStringList strListKeyword;
    if (json.GetNumber(_strJson,"Find") == E_REPLACE_SPACE)
    {
        bFlag = true;
        strListKeyword = strFind.split(" ");
    }

    for (int i = 0 ; i < m_data[nSelect].size(); i++)
    {
        if (bFlag)
        {
            foreach(QString str,strListKeyword)
            {
                m_data[nSelect][i][nCatalog] = m_data[nSelect][i][nCatalog].replace(str,strReplace);
            }
        }
        else
            m_data[nSelect][i][nCatalog] = m_data[nSelect][i][nCatalog].replace(strFind,strReplace);
    }
}

void SFilterProcess::FilterSerach(QString _strJson)
{
    SJson json;
    int nSelect = json.Get(_strJson,"Article").toInt() - 1;
    if (nSelect < 0) return;
    m_bDataFlag[nSelect] = true;
    {
        SKeyword stKeyword;
        stKeyword.m_nCatalog = json.GetNumber(_strJson,"Category");
        stKeyword.m_nKeyword = json.GetNumber(_strJson,"Keyword");
        stKeyword.m_strListKeyword = json.Get(_strJson,"String").split(" ");
        if (json.GetNumber(_strJson,"Method") == 1)
        {
            for (int i = 0 ; i < stKeyword.m_strListKeyword.size(); i++ )
                stKeyword.m_strListKeyword[i] = " " + stKeyword.m_strListKeyword[i] + " ";
        }
        m_vecKeyword.push_back(stKeyword);
    }
    if (json.GetBool(_strJson,"Last")) //
    {
        QVector <QStringList> copyData;

        foreach(QStringList strList , m_data[nSelect])
        {
            bool bFlag = false;
            foreach(SKeyword stKeyword,m_vecKeyword)
            {
                QString strData = strList.at(stKeyword.m_nCatalog);
                switch(stKeyword.m_nKeyword)
                {
                    case E_SEARCH_KEYWORD_OR:
                    {
                        foreach(QString strKey , stKeyword.m_strListKeyword)
                            if (strData.contains(strKey)){bFlag = true;break;}
                        break;
                    }
                    case E_SEARCH_KEYWORD_AND:
                    {
                        int nKeyCount = 0;
                        foreach(QString strKey , stKeyword.m_strListKeyword)
                            if (strData.contains(strKey)) nKeyCount++;
                        if (nKeyCount == stKeyword.m_strListKeyword.size())
                            bFlag = true;
                        break;
                    }
                    case E_SEARCH_KEYWORD_CELL_DELETE_OR:
                    {
                        bFlag = true;
                        foreach(QString strKey , stKeyword.m_strListKeyword)
                            if (strData.contains(strKey)){bFlag = false;break;}
                        break;
                    }
                    case E_SEARCH_KEYWORD_CELL_DELETE_AND:
                    {
                        bFlag = true;
                        int nCount = 0;
                        foreach(QString strKey , stKeyword.m_strListKeyword)
                            if (strData.contains(strKey)){nCount++;}
                        if (nCount == stKeyword.m_strListKeyword.size())
                            bFlag = false;
                        break;
                    }
                }
            }
            if (bFlag) copyData.push_back(strList);
        }
        m_data[nSelect].clear();
        m_data[nSelect] = copyData;
        m_vecKeyword.clear();
    }
}

void SFilterProcess::ReloadData()
{    
    for (int i = 0; i < E_ARTICLE_MAX ; i++)
    {
        m_data[i].clear();
        m_bDataFlag[i] = false;
    }

    foreach(SBody stBody, m_mapData.values())
    {
        m_data[E_ARTICLE_BODY].push_back(stBody.m_strlist);
        foreach(SReply stReply , stBody.m_vecReply.values())
        {
            m_data[E_ARTICLE_REPLY].push_back(stReply.m_strlist);
        }
    }
}

void SFilterProcess::DataFilter(QString _strFilterGroupID)
{
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");

    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    QString sql = "select type,data from filter where filtergroup_id = " + _strFilterGroupID;
    QSqlQuery query;
    if(query.exec(sql) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
    while (query.next())
    {
        switch(query.value(0).toInt())
        {
        case E_FILTER_TYPE_DATE://Date
            FilterDate(query.value(1).toString());
            break;
        case E_FILTER_TYPE_SEARCH:
            FilterSerach(query.value(1).toString());
            break;
        case E_FILTER_TYPE_LENGTH:
            FilterLength(query.value(1).toString());
            break;
        case E_FILTER_TYPE_REPLACE:
            FilterReplace(query.value(1).toString());
            break;            
        case E_FILTER_TYPE_COUNTER:
            FilterCounter(query.value(1).toString());
            break;
        }
    }
    m_db.close();
}

QString SFilterProcess::GetCommunityKey(const QStringList _strList)
{
    QString str;
    str.append(_strList[m_anData[E_DATA_platform_form]].trimmed());
    str.append(".");
    str.append(_strList[m_anData[E_DATA_platform_name]].trimmed());
    str.append(".com/");
    str.append(_strList[m_anData[E_DATA_platform_id]].trimmed());
    return str.trimmed();
}

void SFilterProcess::MakeBuzzyAll()
{
    m_vecBuzzy.clear();
    SBuzzy stBuzzy;
    stBuzzy.m_nCategory = 0;
    foreach(SBody stBody, m_mapData.values())
    {
        if (stBuzzy.m_astArticle[E_ARTICLE_BODY].m_mapCount.contains(stBody.m_date))
            stBuzzy.m_astArticle[E_ARTICLE_BODY].m_mapCount[stBody.m_date]++;
        else
            stBuzzy.m_astArticle[E_ARTICLE_BODY].m_mapCount.insert(stBody.m_date,1);
        foreach(SReply stReply , stBody.m_vecReply.values())
        {
            if (stBuzzy.m_astArticle[E_ARTICLE_REPLY].m_mapCount.contains(stReply.m_date))
                stBuzzy.m_astArticle[E_ARTICLE_REPLY].m_mapCount[stReply.m_date]++;
            else
                stBuzzy.m_astArticle[E_ARTICLE_REPLY].m_mapCount.insert(stReply.m_date,1);
        }
    }
    m_vecBuzzy.push_back(stBuzzy);
}

QVariant SFilterProcess::GetWebData(QString _strTable,QString _strColumn,int _nNum)
{
    QVariant value;
    m_db.setHostName(m_strWebDataBaseHost);
    m_db.setUserName(m_strWebDataBaseUser);
    m_db.setPassword(m_strWebDataBasePass);
    m_db.setDatabaseName(m_strWebDataBaseName);
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return value;
    }
    QSqlQuery query("select " + _strColumn + " FROM " + _strTable + " where num = " + QString::number(_nNum));
    if (query.next())
      value = query.value(0);
    m_db.close();
    return value;
}

QVariant SFilterProcess::GetWebData(QString _strTable,QString _strColumn,QString _strWhere)
{
    QVariant value;
    m_db.setHostName(m_strWebDataBaseHost);
    m_db.setUserName(m_strWebDataBaseUser);
    m_db.setPassword(m_strWebDataBasePass);
    m_db.setDatabaseName(m_strWebDataBaseName);
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return value;
    }
    QSqlQuery query("select " + _strColumn + " FROM " + _strTable + " where " + _strWhere);
    if (query.next())
      value = query.value(0);
    m_db.close();
    return value;
}


void SFilterProcess::MakeBuzzy(int _nCategory,QVector <QStringList> *_data, bool *_bDataFlag)
{
    SBuzzy stBuzzy;
    QVariant value = GetWebData("category","statsColumnNum",_nCategory);
    if (value.isNull())
    {
        InsertLog("Make Buzzy Error : statsColumnNum is null ...");
        return;
    }
    stBuzzy.m_nCategory = value.toInt();
    if (stBuzzy.m_nCategory == -1) return;
    QMap <QString,bool> map;
    if (_bDataFlag[E_ARTICLE_BODY] == true)
    {
        foreach(QStringList strList,_data[E_ARTICLE_BODY])
        {
            QString strURL = strList[m_nUrlColumn].trimmed();
            if(map.contains(strURL) == false)
            {
                QDate date = QDateTime::fromString(strList[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss").date();
                if (stBuzzy.m_astArticle[E_ARTICLE_BODY].m_mapCount.contains(date))
                    stBuzzy.m_astArticle[E_ARTICLE_BODY].m_mapCount[date]++;
                else
                    stBuzzy.m_astArticle[E_ARTICLE_BODY].m_mapCount.insert(date,1);
                map.insert(strURL,true);
            }
        }
    }
    map.clear();
    if (_bDataFlag[E_ARTICLE_REPLY] == true)
    {
        foreach(QStringList strList,_data[E_ARTICLE_REPLY])
        {
            QString strURL = strList[m_nUrlColumn].trimmed();
            strURL += "\\";
            strURL += strList[m_nOrderColumn].trimmed();
            if(map.contains(strURL) == false)
            {
                QDate date = QDateTime::fromString(strList[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss").date();
                if (stBuzzy.m_astArticle[E_ARTICLE_REPLY].m_mapCount.contains(date))
                    stBuzzy.m_astArticle[E_ARTICLE_REPLY].m_mapCount[date]++;
                else
                    stBuzzy.m_astArticle[E_ARTICLE_REPLY].m_mapCount.insert(date,1);
                map.insert(strURL,true);
            }
        }
    }
    m_vecBuzzy.push_back(stBuzzy);
}

void SFilterProcess::DataMake(int _nCategory,QVector <QStringList> *_data, bool *_bDataFlag)
{
    QMap <QString,int> mapData;

    if (_bDataFlag[E_ARTICLE_BODY] == true)
    {
        foreach(QStringList strList,_data[E_ARTICLE_BODY])
        {
            QString strURL = strList[m_nUrlColumn].trimmed();
            if (m_mapData.contains(strURL))
            {
                m_mapData[strURL].m_bSelect = true;
                m_mapData[strURL].m_vecCategory.push_back(_nCategory);
                if (mapData.contains(strURL))
                    mapData[strURL] |= C_MAP_REAL_BODY;
                else
                    mapData.insert(strURL,C_MAP_REAL_BODY);
            }
        }
    }

    if (_bDataFlag[E_ARTICLE_REPLY] == true)
    {
        foreach(QStringList strList,_data[E_ARTICLE_REPLY])
        {
            QString strURL = strList[m_nUrlColumn].trimmed();
            if (m_mapData.contains(strURL))
            {
                m_mapData[strURL].m_bReplySelect = true;
                int nSelect = strList[m_nOrderColumn].trimmed().toInt();
                if (m_mapData[strURL].m_vecReply.contains(nSelect))
                {
                    m_mapData[strURL].m_vecReply[nSelect].m_bSelect = true;
                    m_mapData[strURL].m_vecReply[nSelect].m_vecCategory.push_back(_nCategory);
                    if (mapData.contains(strURL))
                        mapData[strURL] |= C_MAP_REAL_REPLY;
                    else
                        mapData.insert(strURL,C_MAP_REAL_REPLY);
                }
            }
        }
    }

    foreach(QString strUrl , m_strlistOrder)
    {
        if (mapData.contains(strUrl))
        {
            if (mapData.value(strUrl) != 0)
            {
                SMap stMap;
                stMap.m_nBody = m_mapData[strUrl].m_nNum;
                stMap.m_nCategory = _nCategory;
                stMap.m_nRealIn = mapData.value(strUrl);
                stMap.m_strURL = strUrl;
                m_vecMap.push_back(stMap);
            }
        }
    }
}

QString SFilterProcess::GetVectorToString(QVector <int> _vecCategory,QString _strDelimiter)
{
    QString str;
    if (_vecCategory.isEmpty()) return QString(" ");
    foreach (int n , _vecCategory )
        str += QString::number(n) + _strDelimiter;
    return str.left(str.length() - _strDelimiter.length());
}

QString SFilterProcess::MakeInfluencerrankQuery(const SInfluenceCount &_stIn,int _nRank,QString _strSubject,QString _strCompanyNum)
{
    QString strOut;
    strOut = "insert into stats_inf_blogger_rank(rank,id_id,nickname_nickname,bodycount,replycount,influencevalue,subject,company_num) VALUES (";
    strOut += QString::number(_nRank) + ",";
    strOut += "'" + _stIn.m_strlistData.at(m_anData[E_DATA_article_id]).trimmed() + "',";
    strOut += "'" + _stIn.m_strlistData.at(m_anData[E_DATA_article_nickname]).trimmed() + "',";
    strOut += QString::number(_stIn.m_ncBody) + ",";
    strOut += QString::number(_stIn.m_ncReply) + ",";
    strOut += QString::number(_stIn.m_dinfulrence,'g',3)+ ",";
    strOut += "'" + _strSubject + "',";
    strOut += _strCompanyNum + ")";
    return strOut;
}

void SFilterProcess::DataLoad(QString _strDataNum)
{
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");

    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }

    QSqlQuery query;
    QString strSelect;
    strSelect = "select ";
    foreach (QString strColumn,m_listDataBaseColumn)
        strSelect += "CONVERT(" + strColumn + " USING utf8),";
    strSelect = strSelect.left(strSelect.size() - 1);
    strSelect += " from ";
    strSelect += "data_" + _strDataNum;

    InsertLog("Start Loading data_" + _strDataNum);
    m_strlistOrder.clear();
    m_mapData.clear();
    m_vecMap.clear();

    m_mapID.clear();
    m_mapCommunity.clear();
    m_mapNickname.clear();

    SId stID;
    m_mapID.insert("(none)",stID);

    SCommunity stCom;
    m_mapCommunity.insert(" ",stCom);

    QString astrWhere[E_ARTICLE_MAX] = {" where article_form = 'body'"," where article_form = 'reply'"};
    astrWhere[E_ARTICLE_BODY] += " and date(article_date) >= ";
    if(m_strStartDay.isEmpty())
    {
        astrWhere[E_ARTICLE_BODY] += "date(subdate(now(), INTERVAL ";
        astrWhere[E_ARTICLE_BODY] += QString::number(abs(m_nCutDays));
        astrWhere[E_ARTICLE_BODY] += " DAY))";
    }
    else
    {
        astrWhere[E_ARTICLE_BODY] += "date(" + m_strStartDay + ") ";
    }
    if(m_strEndDay.isEmpty())
    {
        astrWhere[E_ARTICLE_BODY] += " and date(article_date) <= date(now())";
    }
    else
    {
        astrWhere[E_ARTICLE_BODY] += " and date(article_date) <= date(" + m_strEndDay + ")";
    }

    astrWhere[E_ARTICLE_BODY] += " Order by article_date DESC";
    for (int i = 0; i < E_ARTICLE_MAX ; i++)
    {
        if(query.exec(QString(strSelect + astrWhere[i]).toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
        int nCount = 0;
        while(query.next())
        {
            QStringList strList;
            for(int j = 0; j < m_listDataBaseColumn.size(); j++)
                strList << " " + query.value(j).toString() + " ";

            if(strList[m_anData[E_DATA_platform_form]].trimmed() == "post" || strList[m_anData[E_DATA_platform_form]].trimmed() == "story" || strList[m_anData[E_DATA_platform_form]].trimmed() == "channel")
                strList[m_anData[E_DATA_platform_form]] = " sns ";

            if(strList[m_anData[E_DATA_platform_form]].trimmed() == "group")
                strList[m_anData[E_DATA_platform_form]] = " community ";

            QString strURL = strList[m_nUrlColumn].trimmed();
            switch(i)
            {
            case E_ARTICLE_BODY:
                {
                    if (m_mapData.contains(strURL) == false)
                    {
                        if (strList[m_anData[E_DATA_article_id]].trimmed().isEmpty() && strList[m_anData[E_DATA_platform_form]].trimmed() == "news")
                        {
                            strList[m_anData[E_DATA_article_id]] = strList[m_anData[E_DATA_platform_title]];
                            strList[m_anData[E_DATA_article_nickname]] = strList[m_anData[E_DATA_platform_title]];
                        }
                        if (strList[m_anData[E_DATA_article_date]].trimmed().isEmpty()) continue;                        
                        if (strList[m_anData[E_DATA_article_date]].trimmed() == "0000-00-00 00:00:00")
                            strList[m_anData[E_DATA_article_date]] = "1990-01-01 00:00:00";
                        if (strList[m_anData[E_DATA_article_id]].trimmed().isEmpty()) continue;
                        if (strList[m_anData[E_DATA_article_nickname]].trimmed().isEmpty()) continue;
                        if (strList[m_anData[E_DATA_platform_id]].trimmed().isEmpty()) continue;
                        if (strList[m_anData[E_DATA_platform_id]].trimmed().isEmpty()) continue;
                        if (strList[m_anData[E_DATA_platform_name]].trimmed() == QString("naver") && strList[m_anData[E_DATA_platform_form]].trimmed() == QString("blog"))
                        {
                            QString strID = strList[m_anData[E_DATA_platform_id]].trimmed();
                            strList[m_anData[E_DATA_article_id]] = strList[m_anData[E_DATA_article_url]].trimmed().split("/").at(3);
                            if (m_mapNaverBlogProfile.contains(strID) == false )
                            {
                                SNaverBlogProfile stProfile;
                                stProfile.m_strURL = strList[m_anData[E_DATA_article_profileurl]].trimmed();
                                stProfile.m_date = QDateTime::fromString(strList[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss");
                                m_mapNaverBlogProfile.insert(strID,stProfile);
                            }
                            else
                            {
                                QString strUrl = strList[m_anData[E_DATA_article_profileurl]].trimmed();
                                if (strUrl.isEmpty() == false)
                                {
                                    if (m_mapNaverBlogProfile[strID].m_strURL.isEmpty())
                                        m_mapNaverBlogProfile[strID].m_strURL = strUrl;
                                    else
                                    {
                                        QDateTime dtComp = QDateTime::fromString(strList[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss");
                                        if (dtComp > m_mapNaverBlogProfile[strID].m_date)
                                            m_mapNaverBlogProfile[strID].m_strURL = strUrl;
                                    }
                                }
                            }
                        }
                        SBody stBody;
                        stBody.m_date = QDateTime::fromString(strList[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss").date();                        
                        stBody.m_dtLast = QDateTime::fromString(strList[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss");                                                
                        stBody.m_bSelect = false;
                        stBody.m_bReplySelect = false;
                        stBody.m_strlist = strList;
                        stBody.m_nNum = nCount++;
                        m_mapData.insert(strURL,stBody);
                        m_strlistOrder.push_back(strURL);
                        if (m_mapID.contains(strList[m_anData[E_DATA_article_id]].trimmed()) == false)
                        {
                            SId stID;
                            stID.m_nNum = m_mapID.size();
                            stID.m_strID = strList[m_anData[E_DATA_article_id]].trimmed();
                            if(m_mapPlatformname.contains(strList[m_anData[E_DATA_platform_name]].trimmed()))
                                stID.m_nPlatfromName = m_mapPlatformname[strList[m_anData[E_DATA_platform_name]].trimmed()];
                            m_mapID.insert(stID.m_strID,stID);
                        }
                        if (m_mapNickname.contains(strList[m_anData[E_DATA_article_nickname]].trimmed()) == false)
                        {
                            SNickname stNick;
                            stNick.m_nNum = m_mapNickname.size();
                            stNick.m_strNickname = strList[m_anData[E_DATA_article_nickname]].trimmed();
                            m_mapNickname.insert(stNick.m_strNickname,stNick);
                        }
                        QString strKey = GetCommunityKey(strList);
                        if (m_mapCommunity.contains(strKey) == false)
                        {
                            SCommunity stCom;
                            stCom.m_nNum = m_mapCommunity.size();
                            stCom.m_strID = strList[m_anData[E_DATA_platform_id]].trimmed();
                            stCom.m_strTitle = strList[m_anData[E_DATA_platform_title]].trimmed();
                            if(m_mapPlatformname.contains(strList[m_anData[E_DATA_platform_name]].trimmed()))
                                stCom.m_nPlatformname = m_mapPlatformname[strList[m_anData[E_DATA_platform_name]].trimmed()];
                            else
                                continue;
                            if(m_mapPlatformform.contains(strList[m_anData[E_DATA_platform_form]].trimmed()))
                                stCom.m_nPlatformform = m_mapPlatformform[strList[m_anData[E_DATA_platform_form]].trimmed()];
                            else
                                continue;
                            //if (strList[m_anData[E_DATA_platform_name]].trimmed().compare(QString("naver")) == false)
                            //{
                            stCom.m_strUrl = strKey;

                            if (strList[m_anData[E_DATA_platform_name]].trimmed() == QString("daum") && strList[m_anData[E_DATA_platform_form]].trimmed() == QString("cafe"))
                                stCom.m_strExid = strList[m_anData[E_DATA_etc]].trimmed();
                            //}
                            m_mapCommunity.insert(strKey,stCom);
                        }                        
                    }
                }
                break;
            case E_ARTICLE_REPLY:
                {
                    if (m_mapData.contains(strURL))
                    {
                        if (strList[m_anData[E_DATA_article_id]].trimmed().isEmpty()) continue;
                        if (strList[m_anData[E_DATA_article_nickname]].trimmed().isEmpty()) continue;
                        if (strList[m_anData[E_DATA_article_date]].trimmed() == "0000-00-00 00:00:00" || strList[m_anData[E_DATA_article_date]].trimmed().isEmpty())
                            strList[m_anData[E_DATA_article_date]] = "1990-01-01 00:00:00";

                        SReply stReply;
                        stReply.m_bSelect = false;
                        stReply.m_nOrder = strList[m_nOrderColumn].toInt();
                        stReply.m_strlist = strList;
                        stReply.m_date = QDateTime::fromString(strList[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss").date();
                        QDateTime dtComp = QDateTime::fromString(strList[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss");
                        if (m_mapData[strURL].m_dtLast < dtComp)
                            m_mapData[strURL].m_dtLast = dtComp;
                        m_mapData[strURL].m_vecReply.insert(stReply.m_nOrder,stReply);
                        if (m_mapID.contains(strList[m_anData[E_DATA_article_id]].trimmed()) == false)
                        {
                            SId stID;
                            stID.m_nNum = m_mapID.size();
                            stID.m_strID = strList[m_anData[E_DATA_article_id]].trimmed();
                            if(m_mapPlatformname.contains(strList[m_anData[E_DATA_platform_name]].trimmed()))
                                stID.m_nPlatfromName = m_mapPlatformname[strList[m_anData[E_DATA_platform_name]].trimmed()];
                            m_mapID.insert(stID.m_strID,stID);
                        }
                        if (m_mapNickname.contains(strList[m_anData[E_DATA_article_nickname]].trimmed()) == false)
                        {
                            SNickname stNick;
                            stNick.m_nNum = m_mapNickname.size();
                            stNick.m_strNickname = strList[m_anData[E_DATA_article_nickname]].trimmed();
                            m_mapNickname.insert(stNick.m_strNickname,stNick);
                        }
                    }
                }
                break;
            }
        }
    }
    foreach(SId stID,m_mapID)
    {
        QString strCommunityKey = "blog.naver.com/" + stID.m_strID.trimmed();
        if (m_mapCommunity.contains(strCommunityKey))
            stID.m_nCommunity = m_mapCommunity[strCommunityKey].m_nNum;
    }

    foreach(SBody stBody, m_mapData.values())
    {
        QString strNick = stBody.m_strlist[m_anData[E_DATA_article_nickname]].trimmed();
        QString strID = stBody.m_strlist[m_anData[E_DATA_article_id]].trimmed();
        if (m_mapNickname.contains(strNick) && m_mapID.contains(strID))
            m_mapNickname[strNick].m_nID = m_mapID[strID].m_nNum;
        foreach(SReply stReply , stBody.m_vecReply.values())
        {
            QString strNick = stReply.m_strlist[m_anData[E_DATA_article_nickname]].trimmed();
            QString strID = stReply.m_strlist[m_anData[E_DATA_article_id]].trimmed();
            if (m_mapNickname.contains(strNick) && m_mapID.contains(strID))
                m_mapNickname[strNick].m_nID = m_mapID[strID].m_nNum;
        }
    }
    MakeBuzzyAll();
    InsertLog("End Loading");
    m_db.close();
}

struct SInfluenceBuzz
{
    int m_ncId;
    int m_ncBody;
    int m_ncReply;
    QSet <QString> m_setName;
    SInfluenceBuzz()
    {
        m_ncId = m_ncBody = m_ncReply = 0;
    }
};


void SFilterProcess::DataInfluenceBuzzCount(QString _strCompanyNum)
{
    QMap <QDate,SInfluenceBuzz> map;
    QDate dateStart(2100,1,1),dateEnd(2000,1,1);
    foreach(QStringList strlist,m_data[E_ARTICLE_BODY])
    {
        if (strlist[m_anData[E_DATA_platform_form]].trimmed() != "blog") continue;
        QDate date = QDateTime::fromString(strlist[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss").date();
        dateStart = (date < dateStart ) ? date : dateStart;
        dateEnd = (date > dateEnd ) ? date : dateEnd;
        if (!map.contains(date))
        {
            SInfluenceBuzz stBuzz;
            map.insert(date,stBuzz);
        }
        map[date].m_ncBody++;
        QString strID = strlist[m_anData[E_DATA_article_id]].trimmed();
        if (!map[date].m_setName.contains(strID))
        {
            map[date].m_setName.insert(strID);
            map[date].m_ncId++;
        }
    }
    foreach(QStringList strlist,m_data[E_ARTICLE_REPLY])
    {
        if (strlist[m_anData[E_DATA_platform_form]].trimmed() != "blog") continue;
        QDate date = QDateTime::fromString(strlist[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss").date();
        dateStart = (date < dateStart ) ? date : dateStart;
        dateEnd = (date > dateEnd ) ? date : dateEnd;
        if (!map.contains(date))
        {
            SInfluenceBuzz stBuzz;
            map.insert(date,stBuzz);
        }
        map[date].m_ncReply++;
    }

    m_db.setHostName(m_strWebDataBaseHost);
    m_db.setUserName(m_strWebDataBaseUser);
    m_db.setPassword(m_strWebDataBasePass);
    m_db.setDatabaseName(m_strWebDataBaseName);
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    QSqlQuery query;
    QString strQuery;
    strQuery = "delete from stats_inf_blogger_buzzbyday where company_num = " + _strCompanyNum;
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastError().text());return;}

    for(QDate date = dateStart; date < dateEnd; date=date.addDays(1))
    {
        strQuery = "insert into stats_inf_blogger_buzzbyday (company_num,date,formateddate,idcount,bodycount,replycount) VALUES ( " + _strCompanyNum ;
        strQuery += ",'" + date.toString("yyyy-MM-dd") + "'";
        strQuery += ",'" + date.toString("yyyyMMdd")+ "'";
        if (map.contains(date))
        {
            strQuery += "," + QString::number(map[date].m_ncId);
            strQuery += "," + QString::number(map[date].m_ncBody);
            strQuery += "," + QString::number(map[date].m_ncReply) + ")";
        }
        else
            strQuery += ",0,0,0)";
        if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());qDebug() << strQuery;return;}
    }
    m_db.close();
}

bool QSortBody(const SInfluenceCount &stInflunce1, const SInfluenceCount &stInflunce2)
{
    return stInflunce1.m_ncBody > stInflunce2.m_ncBody;
}

bool QSortReply(const SInfluenceCount &stInflunce1, const SInfluenceCount &stInflunce2)
{
    return stInflunce1.m_ncReply > stInflunce2.m_ncReply;
}

bool QSortInfulrence(const SInfluenceCount &stInflunce1, const SInfluenceCount &stInflunce2)
{
    return stInflunce1.m_dinfulrence > stInflunce2.m_dinfulrence;
}

struct SInfluenceMap
{
    int m_nBody;
    int m_nReply;
    int m_nId;
};

void SFilterProcess::DataInfluenceCount(QString _strFilter,QString _strCompanyNum)
{
    ReloadData();

    InsertLog("Influence Data Buzz Start ");
    DataInfluenceBuzzCount(_strCompanyNum);
    InsertLog("Influence Data Buzz End ");


    InsertLog("Influence Data Filter Run Start ");
    DataFilter(_strFilter);
    InsertLog("Influence Data Filter Run End ");

    if (m_bDataFlag[E_ARTICLE_BODY] == false){InsertLog("Error : Influence Body Data don't have"); return;}

    QVector <SInfluenceMap> vecInfluenceMap[E_ARTICLE_MAX];
    QMap <QString,SInfluenceCount> mapCount;
    {
        InsertLog("Influence Rank Count Make Start ");
        QString strCompanyNum = _strCompanyNum;        
        foreach(QStringList strlist,m_data[E_ARTICLE_BODY])
        {            
            QString strUrl = strlist[m_nUrlColumn].trimmed();
            if (m_mapData.contains(strUrl) == false) continue;
            SBody stBody = m_mapData.value(strUrl);
            if (stBody.m_strlist[m_anData[E_DATA_platform_form]].trimmed().compare(QString("blog")) != 0) continue;
            QString strKey = stBody.m_strlist[m_anData[E_DATA_article_id]].trimmed();
            {
                SInfluenceMap stInfluenceMap;
                stInfluenceMap.m_nBody = m_mapData[strUrl].m_nNum;
                stInfluenceMap.m_nId = m_mapID[strlist[m_anData[E_DATA_article_id]].trimmed()].m_nNum;
                vecInfluenceMap[E_ARTICLE_BODY].push_back(stInfluenceMap);
            }
            if (mapCount.contains(strKey)==false)
            {
                SInfluenceCount stInfluence;
                stInfluence.m_nID = m_mapID[strKey].m_nNum;
                QString strCommunityKey = GetCommunityKey(stBody.m_strlist);
                if (m_mapCommunity.contains(strCommunityKey) == false) continue;
                stInfluence.m_nCommunity = m_mapCommunity[strCommunityKey].m_nNum;
                QString strNickname = stBody.m_strlist[m_anData[E_DATA_article_nickname]].trimmed();
                if (m_mapNickname.contains(strNickname) == false)
                    stInfluence.m_nNickname = 0;
                else
                    stInfluence.m_nNickname = m_mapNickname[strNickname].m_nNum;
                stInfluence.m_ncBody = 0;
                stInfluence.m_ncReply = 0;
                stInfluence.m_strID = strKey;
                stInfluence.m_strlistData = stBody.m_strlist;
                mapCount.insert(strKey,stInfluence);
            }
            mapCount[strKey].m_ncBody++;
            foreach(SReply stReply , stBody.m_vecReply.values())
            {
                if (stReply.m_strlist[m_anData[E_DATA_article_id]].compare(strKey) != 0)
                    mapCount[strKey].m_ncReply++;
            }
        }

        foreach (SInfluenceCount stInfluence,mapCount.values())
        {
            mapCount[stInfluence.m_strID].m_dinfulrence = ((double)stInfluence.m_ncBody * 0.4) + ((double)stInfluence.m_ncReply * 0.6);
        }


        foreach(QStringList strlist,m_data[E_ARTICLE_REPLY])
        {
            if (strlist[m_anData[E_DATA_platform_form]].trimmed().compare(QString("blog")) != 0) continue;
            QString strUrl = strlist[m_nUrlColumn].trimmed();            
            SInfluenceMap stInfluenceMap;
            stInfluenceMap.m_nBody = m_mapData[strUrl].m_nNum;
            stInfluenceMap.m_nId = m_mapID[strlist[m_anData[E_DATA_article_id]].trimmed()].m_nNum;
            QString strInfluenceReplyKey = QString::number(stInfluenceMap.m_nBody) + "_" + strlist[m_anData[E_DATA_article_order]].trimmed();
            stInfluenceMap.m_nReply = m_mapReply[strInfluenceReplyKey];
            vecInfluenceMap[E_ARTICLE_REPLY].push_back(stInfluenceMap);
        }

        InsertLog("Influence Rank Count Make End");
        {
            m_db.setHostName(m_strWebDataBaseHost);
            m_db.setUserName(m_strWebDataBaseUser);
            m_db.setPassword(m_strWebDataBasePass);
            m_db.setDatabaseName(m_strWebDataBaseName);
            if (m_db.open() == false)
            {
                InsertLog(m_db.lastError().text());
                return;
            }

            UpdateDBInfoState(m_db,_strCompanyNum,"Influence (Delete)");
            InsertLog("Influence Rank Count Send Start");
            QSqlQuery query;
            QString strQuery;
            strQuery = "delete from stats_inf_blogger_rank where company_num = " + strCompanyNum;
            if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            strQuery = "delete from influencer where company_num = " + strCompanyNum;
            if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            strQuery = "truncate body_influencer_map_" + _strCompanyNum + "_update";
            if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            strQuery = "truncate reply_influencer_map_" + _strCompanyNum + "_update";
            if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

            QList<SInfluenceCount> list = mapCount.values();
            qSort(list.begin(),list.end(),QSortInfulrence);

            //id_id,rank,
            strQuery = "insert into influencer (rank,company_num,id_num,nickname_num,community_num,bodycount,replycount,influencervalue,profile,profileurl,id_id)"
                       " VALUES (:RANK," + strCompanyNum + ",:ID_NUM,:NICKNAME_NUM,:COMMUNITY_NUM,:BODY_COUNT,:REPLY_COUNT,:INPL,:PROFILE,:PROFILE_URL,:ID_ID)";

            UpdateDBInfoState(m_db,_strCompanyNum,"Influence (Insert)");

            query.prepare(strQuery.toUtf8());
            int ncRank = 1;
            foreach(SInfluenceCount stIn,list)
            {
                if (stIn.m_strID.length() > 42) continue;
                if (m_mapID.contains(stIn.m_strID) == false ) continue;
                query.bindValue(":ID_NUM",stIn.m_nID);
                query.bindValue(":NICKNAME_NUM",stIn.m_nNickname);
                query.bindValue(":COMMUNITY_NUM",stIn.m_nCommunity);
                query.bindValue(":BODY_COUNT",stIn.m_ncBody);
                query.bindValue(":REPLY_COUNT",stIn.m_ncReply);
                query.bindValue(":INPL",QString::number(stIn.m_dinfulrence,'g',3));
                query.bindValue(":PROFILE",stIn.m_strlistData.at(m_anData[E_DATA_article_profile]).trimmed().toUtf8());

                {
                    QString strID = stIn.m_strlistData.at(m_anData[E_DATA_platform_id]).trimmed();
                    if (m_mapNaverBlogProfile.contains(strID))
                        query.bindValue(":PROFILE_URL",m_mapNaverBlogProfile[strID].m_strURL);
                    else
                        query.bindValue(":PROFILE_URL",stIn.m_strlistData.at(m_anData[E_DATA_article_profileurl]).trimmed().toUtf8());
                }

                query.bindValue(":ID_ID",stIn.m_strID.trimmed().toUtf8());
                query.bindValue(":RANK",ncRank++);
                if(query.exec() == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            }

            UpdateDBInfoState(m_db,_strCompanyNum,"Influence (rank influence value Insert)");

            int nCount = 0;
            foreach(SInfluenceCount stIn,list)
            {
                if (nCount >= 10) break; nCount++;
                strQuery = MakeInfluencerrankQuery(stIn,nCount,"influencevalue",strCompanyNum);
                if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            }
            nCount = 0;
            qSort(list.begin(),list.end(),QSortBody);

            UpdateDBInfoState(m_db,_strCompanyNum,"Influence (rank body count Insert)");
            foreach(SInfluenceCount stIn,list)
            {
                if (nCount >= 10) break; nCount++;
                strQuery = MakeInfluencerrankQuery(stIn,nCount,"bodycount",strCompanyNum);
                if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            }

            qSort(list.begin(),list.end(),QSortReply);
            UpdateDBInfoState(m_db,_strCompanyNum,"Influence (rank reply count Insert)");
            nCount = 0;
            foreach(SInfluenceCount stIn,list)
            {
                if (nCount >= 10) break; nCount++;
                strQuery = MakeInfluencerrankQuery(stIn,nCount,"replycount",strCompanyNum);
                if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            }

            UpdateDBInfoState(m_db,_strCompanyNum,"Influence (Map body Insert)");
            nCount = 1;

            foreach(SInfluenceMap stMap,vecInfluenceMap[E_ARTICLE_BODY])
            {
                strQuery = "insert into body_influencer_map_";
                strQuery += _strCompanyNum + "_update";
                strQuery += " (num,body_num,id_num) VALUES (";
                strQuery += QString::number(nCount++) + ",";
                strQuery += QString::number(stMap.m_nBody) + ",";
                strQuery += QString::number(stMap.m_nId) + ")";
                if(query.exec(strQuery.toUtf8()) == false)
                {
                    qDebug() << strQuery;InsertLog(query.lastQuery()); InsertLog(query.lastError().text());
                    InsertLog(strQuery);
                }
            }


            nCount = 1;
            foreach(SInfluenceMap stMap,vecInfluenceMap[E_ARTICLE_REPLY])
            {
                strQuery = "insert into reply_influencer_map_";
                strQuery += _strCompanyNum + "_update";
                strQuery += " (num,body_num,reply_num,id_num) VALUES (";
                strQuery += QString::number(nCount++) + ",";
                strQuery += QString::number(stMap.m_nBody) + ",";
                strQuery += QString::number(stMap.m_nReply) + ",";
                strQuery += QString::number(stMap.m_nId) + ")";
                if(query.exec(strQuery.toUtf8()) == false)
                {
                    qDebug() << strQuery;
                    InsertLog(query.lastQuery());
                    InsertLog(query.lastError().text());
                    InsertLog(strQuery);
                }
            }


            RenameTable("body_influencer_map_" + _strCompanyNum,"body_influencer_map_" + _strCompanyNum + "_update","body_influencer_map_" + _strCompanyNum +"_temp");
            RenameTable("reply_influencer_map_" + _strCompanyNum,"reply_influencer_map_" + _strCompanyNum + "_update","reply_influencer_map_" + _strCompanyNum +"_temp");

            //
            strQuery = "update dbinfo set "
                    "influencercount = " + QString::number(list.size()) + " "
                    "where company_num = " + strCompanyNum;
            if(query.exec(strQuery.toUtf8()) == false) { InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            UpdateDBInfoState(m_db,_strCompanyNum,"Influence (Finish)");
            m_db.close();            
            InsertLog("Influence Rank Count Send End");
        }
    }
}

struct SSpamer
{
    QString m_strKey;
    QString m_strTitle;
    QString m_strNickname;
    double m_dBodycount;
    double m_dReplycount;
    double m_dSpammerValue;
    SSpamer()
    {
        m_dBodycount = 0.0;
        m_dReplycount = 0.0;
    }
    QSet <QString> m_setNick;
};

bool QSortSpamer(const SSpamer &st1, const SSpamer &st2)
{
    return st1.m_dSpammerValue > st2.m_dSpammerValue;
}

struct SSpammerMap
{
    int m_nReply_num;
    int m_nBody_num;
    int m_nId_num;
};

void SFilterProcess::DataSpammersCount(QString _strFilter,QString _strCompanyNum)
{
    QMap<QString,SSpamer> mapSpamer;

    ReloadData();
    InsertLog("Spamer Data Filter Run Start ");
    DataFilter(_strFilter);
    InsertLog("Spamer Data Filter Run End ");
    QVector <SSpammerMap> vecSpammerMap[E_ARTICLE_MAX];
    {
        InsertLog("Spamer Rank Count Make Start ");
        if (m_bDataFlag[E_ARTICLE_BODY])
        {
            foreach(QStringList strlist,m_data[E_ARTICLE_BODY])
            {
                if (strlist[m_anData[E_DATA_article_id]].trimmed().isEmpty()) continue;
                QString strKey = strlist[m_anData[E_DATA_platform_name]].trimmed() + QString(",");
                strKey += strlist[m_anData[E_DATA_platform_form]].trimmed() + QString(",");
                strKey += strlist[m_anData[E_DATA_article_id]].trimmed();
                if (mapSpamer.contains(strKey) == false)
                {
                    SSpamer stSpamer;
                    stSpamer.m_strKey = strKey;
                    stSpamer.m_strNickname = strlist[m_anData[E_DATA_article_nickname]].trimmed();
                    mapSpamer.insert(strKey,stSpamer);
                }
                mapSpamer[strKey].m_dBodycount+=1.0;

                QString strReKey = strlist[m_anData[E_DATA_article_nickname]].trimmed();
                if (mapSpamer[strKey].m_setNick.contains(strReKey) == false)
                    mapSpamer[strKey].m_setNick.insert(strReKey);

                SSpammerMap stSpammerMap;
                QString strURL = strlist[m_nUrlColumn].trimmed();
                stSpammerMap.m_nBody_num = m_mapData[strURL].m_nNum;
                stSpammerMap.m_nId_num = m_mapID[strlist[m_anData[E_DATA_article_id]].trimmed()].m_nNum;
                vecSpammerMap[E_ARTICLE_BODY].push_back(stSpammerMap);
            }
        }

        if (m_bDataFlag[E_ARTICLE_REPLY])
        {
            foreach(QStringList strlist,m_data[E_ARTICLE_REPLY])
            {
                if (strlist[m_anData[E_DATA_article_id]].trimmed().isEmpty()) continue;
                QString strKey = strlist[m_anData[E_DATA_platform_name]].trimmed() + QString(",");
                strKey += strlist[m_anData[E_DATA_platform_form]].trimmed() + QString(",");
                strKey += strlist[m_anData[E_DATA_article_id]].trimmed();
                if (mapSpamer.contains(strKey) == false)
                {
                    SSpamer stSpamer;
                    stSpamer.m_strKey = strKey;
                    stSpamer.m_strNickname = strlist[m_anData[E_DATA_article_nickname]].trimmed();
                    mapSpamer.insert(strKey,stSpamer);
                }
                mapSpamer[strKey].m_dReplycount+=1.0;

                QString strReKey = strlist[m_anData[E_DATA_article_nickname]].trimmed();
                if (mapSpamer[strKey].m_setNick.contains(strReKey) == false)
                    mapSpamer[strKey].m_setNick.insert(strReKey);

                SSpammerMap stSpammerMap;
                QString strURL = strlist[m_nUrlColumn].trimmed();
                stSpammerMap.m_nBody_num = m_mapData[strURL].m_nNum;
                QString strSpammerReplyKey = QString::number(stSpammerMap.m_nBody_num) + "_" + strlist[m_anData[E_DATA_article_order]].trimmed();
                stSpammerMap.m_nReply_num = m_mapReply[strSpammerReplyKey];
                stSpammerMap.m_nId_num = m_mapID[strlist[m_anData[E_DATA_article_id]].trimmed()].m_nNum;
                vecSpammerMap[E_ARTICLE_REPLY].push_back(stSpammerMap);
            }
        }

        if (!m_bDataFlag[E_ARTICLE_BODY] && !m_bDataFlag[E_ARTICLE_REPLY])
        {
            foreach(SBody stBody, m_mapData.values())
            {
                if (stBody.m_strlist[m_anData[E_DATA_article_id]].trimmed().isEmpty()) continue;
                QString strKey = stBody.m_strlist[m_anData[E_DATA_platform_name]].trimmed() + QString(",");
                strKey += stBody.m_strlist[m_anData[E_DATA_platform_form]].trimmed() + QString(",");
                strKey += stBody.m_strlist[m_anData[E_DATA_article_id]].trimmed();
                if (mapSpamer.contains(strKey) == false)
                {
                    SSpamer stSpamer;
                    stSpamer.m_strKey = strKey;
                    stSpamer.m_strNickname = stBody.m_strlist[m_anData[E_DATA_article_nickname]].trimmed();
                    mapSpamer.insert(strKey,stSpamer);
                }

                SSpammerMap stSpammerMap;
                stSpammerMap.m_nBody_num = stBody.m_nNum;
                stSpammerMap.m_nId_num = m_mapID[stBody.m_strlist[m_anData[E_DATA_article_id]].trimmed()].m_nNum;
                vecSpammerMap[E_ARTICLE_BODY].push_back(stSpammerMap);

                {
                    //mapSpamer[strKey].m_setNick.insert(stBody.m_strlist[m_anData[E_DATA_article_nickname]].trimmed());
                    QString strReKey = stBody.m_strlist[m_anData[E_DATA_article_nickname]].trimmed();
                    if (mapSpamer[strKey].m_setNick.contains(strReKey) == false)
                        mapSpamer[strKey].m_setNick.insert(strReKey);
                }
                foreach(SReply stReply , stBody.m_vecReply.values())
                {
                    QString strKey = stReply.m_strlist[m_anData[E_DATA_platform_name]].trimmed() + QString(",");
                    strKey += stReply.m_strlist[m_anData[E_DATA_platform_form]].trimmed() + QString(",");
                    strKey += stReply.m_strlist[m_anData[E_DATA_article_id]].trimmed();
                    if (mapSpamer.contains(strKey) == false)
                    {
                        SSpamer stSpamer;
                        stSpamer.m_strKey = strKey;
                        stSpamer.m_strNickname = stReply.m_strlist[m_anData[E_DATA_article_nickname]].trimmed();
                        mapSpamer.insert(strKey,stSpamer);
                    }

                    SSpammerMap stSpammerMap;
                    stSpammerMap.m_nBody_num = stBody.m_nNum;
                    stSpammerMap.m_nId_num = m_mapID[stReply.m_strlist[m_anData[E_DATA_article_id]].trimmed()].m_nNum;
                    QString strSpammerReplyKey = QString::number(stSpammerMap.m_nBody_num) + "_" + stReply.m_strlist[m_anData[E_DATA_article_order]].trimmed();
                    stSpammerMap.m_nReply_num = m_mapReply[strSpammerReplyKey];
                    vecSpammerMap[E_ARTICLE_REPLY].push_back(stSpammerMap);

                    QString strReKey = stReply.m_strlist[m_anData[E_DATA_article_nickname]].trimmed();
                    if (mapSpamer[strKey].m_setNick.contains(strReKey) == false)
                        mapSpamer[strKey].m_setNick.insert(strReKey);
                }
            }
        }

        QList <SSpamer> list;
        foreach(SSpamer stSpamer,mapSpamer.values())
        {
            bool bFlag = false;
            if (stSpamer.m_dBodycount > m_adSpammer[E_SPAMER_BODY_COUNT_CUT])
                bFlag = true;
            if (stSpamer.m_dReplycount > m_adSpammer[E_SPAMER_REPLY_COUNT_CUT])
                bFlag = true;
            if ((double)stSpamer.m_setNick.size() > m_adSpammer[E_SPAMER_NICK_COUNT_CUT])
                bFlag = true;
            if (bFlag)
            {
                stSpamer.m_dSpammerValue = (m_adSpammer[E_SPAMER_BODY_COUNT_RATIO]*stSpamer.m_dBodycount)+
                        (m_adSpammer[E_SPAMER_REPLY_COUNT_RATIO]*stSpamer.m_dReplycount)+
                        (m_adSpammer[E_SPAMER_NICK_COUNT_RATIO]*(double)stSpamer.m_setNick.size());
                list.push_back(stSpamer);
            }
        }
        qSort(list.begin(),list.end(),QSortSpamer);
        InsertLog("Spamer Rank Count Make End ");

        InsertLog("Spamer Rank Count Send Start ");
        m_db.setHostName(m_strWebDataBaseHost);
        m_db.setUserName(m_strWebDataBaseUser);
        m_db.setPassword(m_strWebDataBasePass);
        m_db.setDatabaseName(m_strWebDataBaseName);
        if (m_db.open() == false)
        {
            InsertLog(m_db.lastError().text());
            return;
        }

        UpdateDBInfoState(m_db,_strCompanyNum,"Spammer (Delete)");
        QSqlQuery query;
        QString strQuery;
        QString strCompanyNum = _strCompanyNum;
        strQuery = "delete from stats_spa_spammer_rank where company_num = " + strCompanyNum;
        if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
        strQuery = "delete from spammer where company_num = " + strCompanyNum;
        if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
        strQuery = "truncate body_spammer_map_" + _strCompanyNum + "_update";
        if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
        strQuery = "truncate reply_spammer_map_" + _strCompanyNum + "_update";
        if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

        QMap <QString,int> mapPlatformnameCount;

        foreach(QString str,m_mapPlatformname.keys())
            mapPlatformnameCount.insert(str,1);

        UpdateDBInfoState(m_db,_strCompanyNum,"Spammer (Insert)");

        int nCount = 1;
        QSet <int> setId;
        foreach(SSpamer stIn,list)
        {
            QStringList strList = stIn.m_strKey.split(",");
            QString strID = strList[2].trimmed();
            QString strPlatformname = strList[0].trimmed();
            if (strID.isEmpty()) continue;
            if (strPlatformname.isEmpty()) continue;            
            int nID = m_mapID[strID].m_nNum;
            QString strValue;
            if (setId.contains(nID)==false)
                setId.insert(nID);
            else
                continue;
            strValue += QString::number(nID) + ",'";
            strValue += strID + "',";
            strValue += QString::number(stIn.m_setNick.size()) + ",";
            strValue += QString::number((int)stIn.m_dBodycount) + ",";
            strValue += QString::number((int)stIn.m_dReplycount) + ",";
            strValue += QString::number(stIn.m_dSpammerValue,'g',3) + ",";
            strValue += strCompanyNum;
            strQuery = "insert into spammer (rank,id_num,id_id,nicknamecount,bodycount,replycount,spammervalue,company_num) VALUES (";
            strQuery += QString::number(nCount) + ",";
            strQuery += strValue + ")";
            if(query.exec(strQuery.toUtf8()) == false) {
                qDebug() << strQuery;
                InsertLog(query.lastQuery());
                InsertLog(query.lastError().text());
                InsertLog(strQuery);
                continue;
            }
            if (mapPlatformnameCount.contains(strPlatformname))
            {
                if (mapPlatformnameCount[strPlatformname] <= 10)
                {
                    strQuery = "insert into stats_spa_spammer_rank (rank,id_num,id_id,nicknamecount,bodycount,replycount,spammervalue,company_num,platformname_name,subject) VALUES (";
                    strQuery += QString::number(mapPlatformnameCount[strPlatformname]) + ",";
                    strQuery += strValue + ",'" + strPlatformname + "','spammervalue')";
                    if(query.exec(strQuery.toUtf8()) == false) {
                        qDebug() << strQuery;
                        InsertLog(query.lastQuery());
                        InsertLog(query.lastError().text());
                        InsertLog(strQuery);
                        continue;
                    }                    
                    mapPlatformnameCount[strPlatformname]++;
                }                
            }
            nCount++;
        }
        {
            nCount=1;            
            foreach(SSpammerMap stMap,vecSpammerMap[E_ARTICLE_BODY])
            {
                if(setId.contains(stMap.m_nId_num)==false) continue;
                strQuery = "insert into body_spammer_map_";
                strQuery += _strCompanyNum + "_update";
                strQuery += " (num,body_num,id_num) VALUES (";
                strQuery += QString::number(nCount++) + ",";
                strQuery += QString::number(stMap.m_nBody_num) + ",";
                strQuery += QString::number(stMap.m_nId_num) + ")";
                if(query.exec(strQuery.toUtf8()) == false)
                {
                    qDebug() << strQuery;
                    InsertLog(query.lastQuery());
                    InsertLog(query.lastError().text());
                    InsertLog(strQuery);
                }
            }
        }

        {
            nCount=1;
            foreach(SSpammerMap stMap,vecSpammerMap[E_ARTICLE_REPLY])
            {
                if(setId.contains(stMap.m_nId_num)==false) continue;
                strQuery = "insert into reply_spammer_map_";
                strQuery += _strCompanyNum + "_update";
                strQuery += " (num,body_num,reply_num,id_num) VALUES (";
                strQuery += QString::number(nCount++) + ",";
                strQuery += QString::number(stMap.m_nBody_num) + ",";
                strQuery += QString::number(stMap.m_nReply_num) + ",";
                strQuery += QString::number(stMap.m_nId_num) + ")";
                if(query.exec(strQuery.toUtf8()) == false)
                {
                    qDebug() << strQuery;
                    InsertLog(query.lastQuery());
                    InsertLog(query.lastError().text());
                    InsertLog(strQuery);
                }
            }
        }

        RenameTable("body_spammer_map_" + _strCompanyNum,"body_spammer_map_" + _strCompanyNum + "_update","body_spammer_map_" + _strCompanyNum +"_temp");
        RenameTable("reply_spammer_map_" + _strCompanyNum,"reply_spammer_map_" + _strCompanyNum + "_update","reply_spammer_map_" + _strCompanyNum +"_temp");

        //influencercount
        strQuery = "update dbinfo set "
                "spammercount = " + QString::number(list.size()) + " "
                "where company_num = " + strCompanyNum;
        if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

        UpdateDBInfoState(m_db,_strCompanyNum,"Spammer (Finish)");

        m_db.close();
        InsertLog("Spamer Rank Count Send End ");
    }
}

bool QSortPowerCafe(const SPowerCafeCount &st1, const SPowerCafeCount &st2)
{
    return st1.m_ncAll > st2.m_ncAll;
}

bool QSortPowerCafeReal(const SPowerCafeCount &st1, const SPowerCafeCount &st2)
{
    return st1.m_ncAllReal > st2.m_ncAllReal;
}

struct SPowerCafeMap
{
    int m_nBody;
    int m_nCommunity;
    int m_nReply;
};


void SFilterProcess::DataPowerCafeCount(QString _strFilter,QString _strCompanyNum)
{
    ReloadData();    
    InsertLog("Power Cafe Data Filter Run Start ");
    DataFilter(_strFilter);
    InsertLog("Power Cafe Data Filter Run End ");    
    QMap <QString,SPowerCafeCount> mapRealCount;
    QMap <QString,SPowerCafeCount> mapAllCount;    
    QVector <SPowerCafeMap> vecPowerCafeMap[E_ARTICLE_MAX];
    {        
        InsertLog("Power Cafe Rank Count Make Start ");
        // real make
        if (m_bDataFlag[E_ARTICLE_BODY] == true)
        {
            foreach(QStringList strlist,m_data[E_ARTICLE_BODY])
            {
                if (strlist[m_anData[E_DATA_platform_form]].trimmed().compare(QString("cafe")) != 0) continue;
                QString strKey = strlist[m_anData[E_DATA_platform_id]].trimmed();

                if (mapRealCount.contains(strKey)==false)
                {
                    SPowerCafeCount stPowerCafe;
                    stPowerCafe.m_strCafeID = strKey;
                    QString strUrl = GetCommunityKey(strlist);
                    if (m_mapCommunity.contains(strUrl) == false ) continue;
                    stPowerCafe.m_strCommunityURL = strUrl;
                    stPowerCafe.m_nCommunityID = m_mapCommunity[strUrl].m_nNum;
                    stPowerCafe.m_strlistData = strlist;
                    mapRealCount.insert(strKey,stPowerCafe);
                }

                {
                    QString strUrl = strlist[m_nUrlColumn].trimmed();
                    QString strCommunityKey = GetCommunityKey(strlist);

                    if(m_mapData.contains(strUrl))
                    {
                        SPowerCafeMap stPowerCafeMap;
                        stPowerCafeMap.m_nBody = m_mapData[strUrl].m_nNum;
                        stPowerCafeMap.m_nCommunity = m_mapCommunity[strCommunityKey.trimmed()].m_nNum;
                        vecPowerCafeMap[E_ARTICLE_BODY].push_back(stPowerCafeMap);
                    }
                }

                mapRealCount[strKey].m_ncBodyReal++;
                mapRealCount[strKey].m_ncAllReal++;
                if (mapRealCount[strKey].m_mapUser.contains(strlist[m_anData[E_DATA_article_id]].trimmed())==false)
                    mapRealCount[strKey].m_mapUser.insert(strlist[m_anData[E_DATA_article_id]].trimmed(),true);
            }
        }
        if (m_bDataFlag[E_ARTICLE_REPLY] == true)
        {
            foreach(QStringList strlist,m_data[E_ARTICLE_REPLY])
            {
                if (strlist[m_anData[E_DATA_platform_form]].trimmed().compare(QString("cafe")) != 0) continue;
                QString strKey = strlist[m_anData[E_DATA_platform_id]].trimmed();
                if (mapRealCount.contains(strKey)==false)
                {
                    SPowerCafeCount stPowerCafe;
                    stPowerCafe.m_strCafeID = strKey;
                    QString strUrl = GetCommunityKey(strlist);
                    if (m_mapCommunity.contains(strUrl) == false ) continue;
                    stPowerCafe.m_strCommunityURL = strUrl;
                    stPowerCafe.m_nCommunityID = m_mapCommunity[strUrl].m_nNum;
                    stPowerCafe.m_strlistData = strlist;
                    mapRealCount.insert(strKey,stPowerCafe);
                }


                {
                    QString strUrl = strlist[m_nUrlColumn].trimmed();
                    QString strCommunityKey = GetCommunityKey(strlist);

                    if(m_mapData.contains(strUrl))
                    {
                        SPowerCafeMap stPowerCafeMap;
                        stPowerCafeMap.m_nBody = m_mapData[strUrl].m_nNum;
                        stPowerCafeMap.m_nCommunity = m_mapCommunity[strCommunityKey.trimmed()].m_nNum;
                        QString strPowerCafeReplyKey = QString::number(stPowerCafeMap.m_nBody) + "_" + strlist[m_anData[E_DATA_article_order]].trimmed();
                        stPowerCafeMap.m_nReply = m_mapReply[strPowerCafeReplyKey];
                        vecPowerCafeMap[E_ARTICLE_REPLY].push_back(stPowerCafeMap);
                    }
                }

                mapRealCount[strKey].m_ncReplyReal++;
                mapRealCount[strKey].m_ncAllReal++;
                if (mapRealCount[strKey].m_mapUser.contains(strlist[m_anData[E_DATA_article_id]].trimmed())==false)
                    mapRealCount[strKey].m_mapUser.insert(strlist[m_anData[E_DATA_article_id]].trimmed(),true);
            }
        }
        // all make
        foreach(SBody stBody, m_mapData.values())
        {
            if (stBody.m_strlist[m_anData[E_DATA_platform_form]].trimmed().compare(QString("cafe")) != 0) continue;
            QString strKey = stBody.m_strlist[m_anData[E_DATA_platform_id]].trimmed();
            if (mapAllCount.contains(strKey)==false)
            {
                SPowerCafeCount stPowerCafe;
                stPowerCafe.m_strCafeID = strKey;
                QString strUrl = GetCommunityKey(stBody.m_strlist);
                if (m_mapCommunity.contains(strUrl) == false ) continue;
                stPowerCafe.m_strCommunityURL = strUrl;
                stPowerCafe.m_nCommunityID = m_mapCommunity[strUrl].m_nNum;
                stPowerCafe.m_strlistData = stBody.m_strlist;
                mapAllCount.insert(strKey,stPowerCafe);
            }
            mapAllCount[strKey].m_ncBodyAll++;
            if (mapAllCount[strKey].m_mapUser.contains(stBody.m_strlist[m_anData[E_DATA_article_id]].trimmed())==false)
                mapAllCount[strKey].m_mapUser.insert(stBody.m_strlist[m_anData[E_DATA_article_id]].trimmed(),true);

            foreach(SReply stReply , stBody.m_vecReply.values())
            {
                if (stReply.m_strlist[m_anData[E_DATA_article_id]].compare(strKey) != 0)
                    mapAllCount[strKey].m_ncReplyAll++;
                if (mapAllCount[strKey].m_mapUser.contains(stReply.m_strlist[m_anData[E_DATA_article_id]].trimmed())==false)
                    mapAllCount[strKey].m_mapUser.insert(stReply.m_strlist[m_anData[E_DATA_article_id]].trimmed(),true);
            }
        }
        foreach (SPowerCafeCount stPowerCafe,mapAllCount.values())
            mapAllCount[stPowerCafe.m_strCafeID].m_ncAll = stPowerCafe.m_ncBodyAll + stPowerCafe.m_ncReplyAll;

        foreach(SPowerCafeCount st,mapRealCount)
        {
            if (mapAllCount.contains(st.m_strCafeID))
            {
                mapRealCount[st.m_strCafeID].m_ncBodyAll = mapAllCount[st.m_strCafeID].m_ncBodyAll;
                mapRealCount[st.m_strCafeID].m_ncReplyAll = mapAllCount[st.m_strCafeID].m_ncReplyAll;
                mapRealCount[st.m_strCafeID].m_ncAll = mapAllCount[st.m_strCafeID].m_ncAll;
                mapRealCount[st.m_strCafeID].m_ncUserAll = mapAllCount[st.m_strCafeID].m_mapUser.size();
            }
        }
        InsertLog("Power Cafe Rank Count Make End");
        InsertLog("Power Cafe Rank Send Start ");
        {
            m_db.setHostName(m_strWebDataBaseHost);
            m_db.setUserName(m_strWebDataBaseUser);
            m_db.setPassword(m_strWebDataBasePass);
            m_db.setDatabaseName(m_strWebDataBaseName);
            if (m_db.open() == false)
            {

                InsertLog(m_db.lastError().text());
                return;
            }
            QSqlQuery query;
            QString strQuery;
            QString strCompanyNum = _strCompanyNum;
            UpdateDBInfoState(m_db,_strCompanyNum,"Power Cafe (Delete)");
            strQuery = "delete from powercafe where company_num = " + strCompanyNum;
            if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            strQuery = "delete from stats_pow_cafe_rank where company_num = " + strCompanyNum;
            if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            strQuery = "truncate body_powercafe_map_" + _strCompanyNum + "_update";
            if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
            strQuery = "truncate reply_powercafe_map_" + _strCompanyNum + "_update";
            if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

            QList<SPowerCafeCount> list = mapRealCount.values();
            qSort(list.begin(),list.end(),QSortPowerCafeReal);
            strQuery = "insert into powercafe (company_num,rank,community_num,community_id,bodycountall,replycountall,allcountall,usercountall,bodycountreal,replycountreal,allcountreal,usercountreal)"
                       " VALUES (" + strCompanyNum + ",:RANK,:COMMNUNITY_NUM,:COMMNUNITY_ID,:BODY_ALL,:REPLY_ALL,:COUNT_ALL,:USER_ALL,:BODY_REAL,:REPLY_REAL,:ALL_REAL,:USER_REAL)";

            int nCount = 0;
            query.prepare(strQuery.toUtf8());
            UpdateDBInfoState(m_db,_strCompanyNum,"Power Cafe (Insert)");
            foreach(SPowerCafeCount stIn,list)
            {
                query.bindValue(":COMMNUNITY_NUM",stIn.m_nCommunityID);
                query.bindValue(":COMMNUNITY_ID",stIn.m_strCafeID);
                query.bindValue(":BODY_ALL",stIn.m_ncBodyAll);
                query.bindValue(":REPLY_ALL",stIn.m_ncReplyAll);
                query.bindValue(":COUNT_ALL",stIn.m_ncAll);
                query.bindValue(":USER_ALL",stIn.m_ncUserAll);
                query.bindValue(":BODY_REAL",stIn.m_ncBodyReal);
                query.bindValue(":REPLY_REAL",stIn.m_ncReplyReal);
                query.bindValue(":ALL_REAL",stIn.m_ncAllReal);
                query.bindValue(":USER_REAL",stIn.m_mapUser.size());
                query.bindValue(":RANK",++nCount);
                if(query.exec() == false) {
                    InsertLog(query.lastQuery());
                    InsertLog(query.lastError().text());return;
                }
            }
            nCount = 0;
            UpdateDBInfoState(m_db,_strCompanyNum,"Power Cafe (Rank Insert)");
            //articlecountall/articlecountreal/usercountreal
            strQuery = "insert into stats_pow_cafe_rank (rank,articlecountall,usercountreal,subject,company_num,community_id,community_title,articlecountreal)"
                       " VALUES (:RANK,0,0,'articlecountreal'," + strCompanyNum + ",:ID,:TITLE,:COUNT_ALL)";
            query.prepare(strQuery.toUtf8());
            foreach(SPowerCafeCount stIn,list)
            {
                if (stIn.m_strCafeID.trimmed().isEmpty()) continue;
                if (stIn.m_strlistData[m_anData[E_DATA_platform_title]].trimmed().isEmpty()) continue;
                if (nCount >= 10) break; nCount++;
                query.bindValue(":ID",stIn.m_strCafeID.trimmed().toUtf8());
                query.bindValue(":TITLE",stIn.m_strlistData[m_anData[E_DATA_platform_title]].trimmed().toUtf8());
                query.bindValue(":COUNT_ALL",stIn.m_ncAllReal);
                query.bindValue(":RANK",nCount);
                if(query.exec() == false) {
                    InsertLog(query.lastQuery());
                    InsertLog(query.lastError().text());return;
                }
            }

            QList<SPowerCafeCount> listAll = mapAllCount.values();
            qSort(listAll.begin(),listAll.end(),QSortPowerCafe);

            UpdateDBInfoState(m_db,_strCompanyNum,"Power Cafe (Stats Rank Insert)");

            nCount = 0;
            strQuery = "insert into stats_pow_cafe_rank (rank,articlecountreal,usercountreal,subject,company_num,community_id,community_title,articlecountall)"
                       " VALUES (:RANK,0,0,'articlecountall'," + strCompanyNum + ",:ID,:TITLE,:COUNT_ALL)";
            query.prepare(strQuery.toUtf8());
            foreach(SPowerCafeCount stIn,listAll)
            {
                if (stIn.m_strCafeID.trimmed().isEmpty()) continue;
                if (stIn.m_strlistData[m_anData[E_DATA_platform_title]].trimmed().isEmpty()) continue;
                if (nCount >= 10) break; nCount++;
                query.bindValue(":ID",stIn.m_strCafeID.trimmed().toUtf8());
                query.bindValue(":TITLE",stIn.m_strlistData[m_anData[E_DATA_platform_title]].trimmed().toUtf8());
                query.bindValue(":COUNT_ALL",stIn.m_ncAll);
                query.bindValue(":RANK",nCount);
                if(query.exec() == false) {
                    InsertLog(query.lastQuery());
                    InsertLog(query.lastError().text());
                    return;}
            }

            nCount = 1;
            foreach(SPowerCafeMap stMap,vecPowerCafeMap[E_ARTICLE_BODY])
            {
                strQuery = "insert into body_powercafe_map_";
                strQuery += _strCompanyNum + "_update";
                strQuery += " (num,body_num,community_num) VALUES (";
                strQuery += QString::number(nCount++) + ",";
                strQuery += QString::number(stMap.m_nBody) + ",";
                strQuery += QString::number(stMap.m_nCommunity) + ")";
                if(query.exec(strQuery.toUtf8()) == false)
                {
                    qDebug() << strQuery;InsertLog(query.lastError().text());
                    InsertLog(query.lastQuery());
                    InsertLog(strQuery);
                }
            }

            nCount = 1;
            foreach(SPowerCafeMap stMap,vecPowerCafeMap[E_ARTICLE_REPLY])
            {
                strQuery = "insert into reply_powercafe_map_";
                strQuery += _strCompanyNum + "_update";
                strQuery += " (num,body_num,community_num,reply_num) VALUES (";
                strQuery += QString::number(nCount++) + ",";
                strQuery += QString::number(stMap.m_nBody) + ",";
                strQuery += QString::number(stMap.m_nCommunity) + ",";
                strQuery += QString::number(stMap.m_nReply) + ")";
                if(query.exec(strQuery.toUtf8()) == false)
                {
                    qDebug() << strQuery;
                    InsertLog(query.lastError().text());
                    InsertLog(query.lastQuery());
                    InsertLog(strQuery);
                }
            }

            RenameTable("body_powercafe_map_" + _strCompanyNum,"body_powercafe_map_" + _strCompanyNum + "_update","body_powercafe_map_" + _strCompanyNum +"_temp");
            RenameTable("reply_powercafe_map_" + _strCompanyNum,"reply_powercafe_map_" + _strCompanyNum + "_update","reply_powercafe_map_" + _strCompanyNum +"_temp");
            //influencercount,spammercount
            strQuery = "update dbinfo set "
                    "powercafecount = " + QString::number(listAll.size()) + " "
                    "where company_num = " + strCompanyNum;
            if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

            UpdateDBInfoState(m_db,_strCompanyNum,"Power Cafe (Finish)");

            m_db.close();
            InsertLog("Power Cafe Rank Send End");
        }
    }
}

struct SMaketer
{
    int m_nBodyCount;
    int m_nReplyCount;
    int m_nAllCount;
    QString m_strID;
    QString m_strPlatformName;

    SMaketer()
    {
        m_nBodyCount=0;
        m_nReplyCount=0;
    }
};

bool QSortMarketer(const SMaketer &stMaketer1, const SMaketer &stMaketer2)
{
    return stMaketer1.m_nAllCount > stMaketer2.m_nAllCount;
}

void SFilterProcess::DataConsumerSupervisionCount(QString _strCompanyNum)
{
    m_db.setHostName(m_strWebDataBaseHost);
    m_db.setUserName(m_strWebDataBaseUser);
    m_db.setPassword(m_strWebDataBasePass);
    m_db.setDatabaseName(m_strWebDataBaseName);
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    QMap <QString,SMaketer> mapMaketer;
    QMap <QString,SMaketer> mapCafe;

    {
        InsertLog("Markter Rank Count Make Start ");
        QString strCompanyNum = _strCompanyNum;

        QSqlQuery query;
        QString strQuery;
        strQuery = "delete from stats_con_client_buzzbymaketer where company_num = " + strCompanyNum;
        if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

        strQuery = "select id,platformname from marketer where company_num = " + strCompanyNum;
        if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
        while(query.next())
        {
            SMaketer stMaketer;
            stMaketer.m_strID = query.value(0).toString();
            stMaketer.m_strPlatformName = query.value(1).toString();
            mapMaketer.insert(stMaketer.m_strID,stMaketer);
        }

        foreach(SBody stBody, m_mapData.values())
        {
            QString strKey = stBody.m_strlist[m_anData[E_DATA_article_id]].trimmed();
            if (mapMaketer.contains(strKey))
            {
                if (stBody.m_strlist[m_anData[E_DATA_platform_name]].trimmed().compare(mapMaketer[strKey].m_strPlatformName) == 0)
                {
                    mapMaketer[strKey].m_nBodyCount++;
                    QString strCafe = stBody.m_strlist[m_anData[E_DATA_platform_id]].trimmed();
                    if (stBody.m_strlist[m_anData[E_DATA_platform_form]].trimmed().compare("cafe") == 0)
                    {
                        if (mapCafe.contains(strCafe) == false)
                        {
                            SMaketer stCafe;
                            stCafe.m_strID = strCafe;
                            mapCafe.insert(strCafe,stCafe);
                        }
                        mapCafe[strCafe].m_nBodyCount++;
                    }
                }
            }

            foreach(SReply stReply , stBody.m_vecReply.values())
            {
                strKey = stReply.m_strlist[m_anData[E_DATA_article_id]].trimmed();
                if (mapMaketer.contains(strKey))
                {
                    if (stReply.m_strlist[m_anData[E_DATA_platform_name]].trimmed().compare(mapMaketer[strKey].m_strPlatformName) == 0)
                    {
                        mapMaketer[strKey].m_nReplyCount++;
                        QString strCafe = stBody.m_strlist[m_anData[E_DATA_platform_id]].trimmed();
                        if (stBody.m_strlist[m_anData[E_DATA_platform_form]].trimmed().compare("cafe") == 0)
                        {
                            if (mapCafe.contains(strCafe) == false)
                            {
                                SMaketer stCafe;
                                stCafe.m_strID = strCafe;
                                mapCafe.insert(strCafe,stCafe);
                            }
                            mapCafe[strCafe].m_nReplyCount++;
                        }
                    }
                }
            }
        }

        foreach (SMaketer stMaketer,mapMaketer.values())
            mapMaketer[stMaketer.m_strID].m_nAllCount = stMaketer.m_nBodyCount + stMaketer.m_nReplyCount;

        foreach (SMaketer stCafe,mapCafe.values())
            mapCafe[stCafe.m_strID].m_nAllCount = stCafe.m_nBodyCount + stCafe.m_nReplyCount;
        {
            QList<SMaketer> list = mapMaketer.values();
            qSort(list.begin(),list.end(),QSortMarketer);
            InsertLog("Markter Rank Count Make End");
            {
                InsertLog("Markter Rank Count Send Start");
                foreach(SMaketer stMaket,list)
                {
                    strQuery = "insert into stats_con_client_buzzbymaketer (rank,platformid,subject,marketerid,marketernickname,bodycount,replycount,company_num) values (0,' ' , 'marketerid' , ";
                    strQuery += "'" + stMaket.m_strID + "',";
                    strQuery += "' ',";
                    strQuery += QString::number(stMaket.m_nBodyCount) + ",";
                    strQuery += QString::number(stMaket.m_nReplyCount) + ",";
                    strQuery += strCompanyNum + ")";
                    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
                }
                InsertLog("Markter Rank Count Send End");
            }
        }
        {
            QList<SMaketer> list = mapCafe.values();
            qSort(list.begin(),list.end(),QSortMarketer);
            {
                InsertLog("Maketer in Cafe Rank Count Send Start");
                foreach(SMaketer stMaket,list)
                {
                    if (stMaket.m_nAllCount == 0) break;
                    strQuery = "insert into stats_con_client_buzzbymaketer (rank,marketerid,subject,platformid,marketernickname,bodycount,replycount,company_num) values (0,' ' , 'platformid' , ";
                    strQuery += "'" + stMaket.m_strID + "',";
                    strQuery += "' ',";
                    strQuery += QString::number(stMaket.m_nBodyCount) + ",";
                    strQuery += QString::number(stMaket.m_nReplyCount) + ",";
                    strQuery += strCompanyNum + ")";
                    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
                }
                InsertLog("Maketer in Cafe Rank Count Send  End");
            }
        }
    }
    m_db.close();
}

bool SFilterProcess::RenameTable(QString _strOld, QString _strNew , QString _strTmp)
{
    QString strQuery;
    QSqlQuery query;
    strQuery = "RENAME TABLE ";
    strQuery += _strOld + " TO " + _strTmp + ",";
    strQuery += _strNew + " TO " + _strOld + ",";
    strQuery += _strTmp + " TO " + _strNew;
    if (query.exec(strQuery.toUtf8())==false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return false;}
    return true;
}

#include <QTextCodec>

QString UniCodeToEuckr(QString _strData)
{
    QString strOut;
    QChar *pch = _strData.data();
    QTextCodec *codec = QTextCodec::codecForName("EUC-KR");
    for (int i = 0; i < _strData.length(); i++)
    {
        if (pch[i].isDigit() || pch[i].isLower() || pch[i].isUpper())
            strOut += pch[i];
        else if (pch[i].isSpace())
            strOut += "%" + codec->fromUnicode(pch[i]).toHex().toUpper();
        else
        {
            QString str;
            str = codec->fromUnicode(pch[i]).toHex().toUpper();
            if (str.length()%2) continue;
            if (str.length() == 2) strOut += pch[i];
            else
            {
                int i = 0;
                while (i < str.length())
                {
                    strOut += "%" + str[i++];
                    strOut += str[i++];
                }
            }
        }
    }
    return strOut;
}

void SFilterProcess::DataPut(QString _strCompanyNum)
{
    m_db.setHostName(m_strWebDataBaseHost);
    m_db.setUserName(m_strWebDataBaseUser);
    m_db.setPassword(m_strWebDataBasePass);
    m_db.setDatabaseName(m_strWebDataBaseName);
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }

    InsertLog("Data Put Start");    
    QSqlQuery query;
    QString strQuery;

    UpdateDBInfoState(m_db,_strCompanyNum,"Data (Delete)");

    strQuery = "truncate body_" + _strCompanyNum + "_update";
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

    strQuery = "truncate reply_" + _strCompanyNum + "_update";
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

    strQuery = "truncate body_consumer_map_" + _strCompanyNum + "_update";
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

    strQuery = "delete from id_" + _strCompanyNum  + "_update";
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

    strQuery = "truncate nickname_" + _strCompanyNum  + "_update";
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

    strQuery = "delete from community_" + _strCompanyNum  + "_update";
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

    QString strReplyQuery = QString("insert into reply_" + _strCompanyNum + "_update (num,id_num,nickname_num,community_num,id_id,parent,data,date,body_num,realbycategorynum) VALUES (:NUM,:ID_NUM,:NICKNAME_NUM,:COMMUNITY_NUM,:ID_ID,:PARENT,:DATA,:DATE,:BODY_NUM,:REALBYCATEGORYNUM)").toUtf8();
    QString strBodyQuery = QString("insert into body_" + _strCompanyNum + "_update(num,id_num,nickname_num,community_num,id_id,platformname_num,platformform_num, title, data, url, view, date, reply_startnum, reply_endnum, reply_count, lastupdate, realbycategorynum)"
                                                                   " VALUES (:NUM,:ID_NUM,:NICKNAME_NUM,:COMMUNITY_NUM,:ID_ID,:PLATFORMNAME_NUM,:PLATFORMFORM_NUM,:TITLE,:DATA,:URL,:VIEW,:DATE,:REPLY_STARTNUM,:REPLY_ENDNUM,:REPLY_COUNT,:LASTUPDATE,:REALBYCATEGORYNUM)").toUtf8();
    QString strMapQuery = QString("insert into body_consumer_map_" + _strCompanyNum + "_update (num,body_num,category_num,realin) VALUES (:NUM,:BODY_NUM,:CATEGORY_NUM,:REALIN)").toUtf8();

    QString strCommunityQuery = QString("insert into community_" + _strCompanyNum + "_update (num,id,title,url,exid,platformname_num,platformform_num) values (");

    UpdateDBInfoState(m_db,_strCompanyNum,"Data (Coummunity Insert)");
    foreach (SCommunity stCommunity,m_mapCommunity.values())
    {
        strQuery = strCommunityQuery;
        strQuery += QString::number(stCommunity.m_nNum);
        strQuery += ",'" + SqlString(stCommunity.m_strID) + "'";
        strQuery += ",'" + SqlString(stCommunity.m_strTitle) + "'";
        strQuery += ",'" + SqlString(stCommunity.m_strUrl) + "'";
        strQuery += ",'" + SqlString(stCommunity.m_strExid) + "'";
        strQuery += "," + QString::number(stCommunity.m_nPlatformname);
        strQuery += "," + QString::number(stCommunity.m_nPlatformform);
        strQuery += ")";
        if(query.exec(strQuery.toUtf8()) == false)
        {
            InsertLog(query.lastQuery());
            InsertLog(query.lastError().text());m_db.close();qDebug() << strQuery;return;
        }
    }

    UpdateDBInfoState(m_db,_strCompanyNum,"Data (Id Insert)");
    QString strIDQuery = QString("insert into id_" + _strCompanyNum + "_update (num,id,platformname_num,community_num) values (");
    foreach (SId stID,m_mapID.values())
    {
        strQuery = strIDQuery;
        strQuery += QString::number(stID.m_nNum);
        strQuery += ",'" + SqlString(stID.m_strID) + "'";
        strQuery += "," + QString::number(stID.m_nPlatfromName);
        strQuery += "," + QString::number(stID.m_nCommunity);
        strQuery += ")";
        if(query.exec(strQuery.toUtf8()) == false) {
            InsertLog(query.lastQuery());
            InsertLog(query.lastError().text());m_db.close();qDebug() << strQuery;return;
        }
    }

    UpdateDBInfoState(m_db,_strCompanyNum,"Data (Nickname Insert)");

    QString strNickQuery = QString("insert into nickname_" + _strCompanyNum + "_update (num,nickname,id_num,exnickname) values (");
    foreach (SNickname stNick,m_mapNickname.values())
    {
        strQuery = strNickQuery;
        strQuery += QString::number(stNick.m_nNum);
        strQuery += ",'" + SqlString(stNick.m_strNickname) + "'";
        strQuery += "," + QString::number(stNick.m_nID);
        strQuery += ",'" + SqlString(UniCodeToEuckr(stNick.m_strNickname)) + "'";
        strQuery += ")";
        if(query.exec(strQuery.toUtf8()) == false)
        {
            InsertLog(query.lastQuery());
            InsertLog(query.lastError().text());m_db.close();qDebug() << strQuery;return;
        }
    }

    UpdateDBInfoState(m_db,_strCompanyNum,"Data (Body Reply Insert)");
    int nReplyCount = 0,nBodyCount = 0;
    QDateTime dtMin = QDateTime::currentDateTime().addYears(50);
    QDateTime dtMax = QDateTime::currentDateTime().addYears(-50);
    foreach(QString strUrl , m_strlistOrder )
    {
        int nReplyStart = nReplyCount;
        foreach(SReply stReply,m_mapData[strUrl].m_vecReply)
        {
            query.prepare(strReplyQuery);
            query.bindValue(":NUM",nReplyCount);            
            query.bindValue(":ID_NUM",m_mapID[stReply.m_strlist[m_anData[E_DATA_article_id]].trimmed()].m_nNum);
            query.bindValue(":NICKNAME_NUM",m_mapNickname[stReply.m_strlist[m_anData[E_DATA_article_nickname]].trimmed()].m_nNum);
            query.bindValue(":COMMUNITY_NUM",m_mapCommunity[GetCommunityKey(m_mapData[strUrl].m_strlist)].m_nNum);
            query.bindValue(":ID_ID",stReply.m_strlist[m_anData[E_DATA_article_id]].trimmed());
            query.bindValue(":PARENT",stReply.m_strlist[m_anData[E_DATA_article_parent]].trimmed().toUtf8());
            query.bindValue(":DATA",SqlString(stReply.m_strlist[m_anData[E_DATA_article_data]].trimmed()).toUtf8());
            query.bindValue(":DATE",stReply.m_strlist[m_anData[E_DATA_article_date]].trimmed().toUtf8());
            query.bindValue(":BODY_NUM",m_mapData[strUrl].m_nNum);
            query.bindValue(":REALBYCATEGORYNUM",GetVectorToString(stReply.m_vecCategory," ").trimmed());
            if (query.exec()==false){
                InsertLog(query.lastQuery());
                InsertLog(query.lastError().text());m_db.close();return;
            }
            {
                QString strReplyKey = QString::number(m_mapData[strUrl].m_nNum) + "_" + stReply.m_strlist[m_anData[E_DATA_article_order]].trimmed();
                m_mapReply.insert(strReplyKey,nReplyCount);
            }
            nReplyCount++;
        }
        query.prepare(strBodyQuery);
        query.bindValue(":NUM",m_mapData[strUrl].m_nNum);
        query.bindValue(":PLATFORMNAME_NUM",m_mapPlatformname[m_mapData[strUrl].m_strlist[m_anData[E_DATA_platform_name]].trimmed()]);
        query.bindValue(":PLATFORMFORM_NUM",m_mapPlatformform[m_mapData[strUrl].m_strlist[m_anData[E_DATA_platform_form]].trimmed()]);
        query.bindValue(":ID_NUM",m_mapID[m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_id]].trimmed()].m_nNum);
        query.bindValue(":NICKNAME_NUM",m_mapNickname[m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_nickname]].trimmed()].m_nNum);
        query.bindValue(":COMMUNITY_NUM",m_mapCommunity[GetCommunityKey(m_mapData[strUrl].m_strlist)].m_nNum);
        query.bindValue(":ID_ID",m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_id]].trimmed());
        query.bindValue(":TITLE",SqlString(m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_title]]).trimmed());
        query.bindValue(":PROFILE",SqlString(m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_profile]]).trimmed());
        QString strData = m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_data]];
        strData=strData.replace(QChar(160),"");
        bool bQuit = true;
        while(bQuit)
        {
            if (strData.contains("\n\n\n"))
                strData=strData.replace("\n\n\n","");
            else
                bQuit = false;
        }
        //query.bindValue(":DATA",SqlString(strData.left(65536).replace(QString("\n"),QString("<br>"))).trimmed());
        query.bindValue(":DATA",SqlString(strData.left(65536)).trimmed());
        QString strArticleUrl = m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_url]].trimmed();
        query.bindValue(":URL",strArticleUrl.right(strArticleUrl.length() - ( (strArticleUrl.contains("https://") ? QString("https://").length() : QString("http://").length() ) )));
        if (m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_hit]].trimmed().isEmpty())
            query.bindValue(":VIEW","-1");
        else
            query.bindValue(":VIEW",m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_hit]]);
        query.bindValue(":DATE",m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_date]].trimmed());
        QDateTime dtData = QDateTime::fromString(m_mapData[strUrl].m_strlist[m_anData[E_DATA_article_date]].trimmed(),"yyyy-MM-dd HH:mm:ss");

        if (dtData < dtMin && dtData > QDateTime::currentDateTime().addYears(-20)) dtMin = dtData;
        if (dtData > dtMax) dtMax = dtData;
        if (m_mapData[strUrl].m_vecReply.size() == 0)
        {
            query.bindValue(":REPLY_STARTNUM",-1);
            query.bindValue(":REPLY_ENDNUM",-1);
        }
        else
        {
            query.bindValue(":REPLY_STARTNUM",nReplyStart);
            query.bindValue(":REPLY_ENDNUM",nReplyCount-1);
        }
        query.bindValue(":REPLY_COUNT",m_mapData[strUrl].m_vecReply.size());
        QString strDate = m_mapData[strUrl].m_dtLast.toString("yyyy-MM-dd HH:mm:ss");
        if (strDate.trimmed().isEmpty())
            query.bindValue(":LASTUPDATE","1900-01-01 00:00:00");
        else
            query.bindValue(":LASTUPDATE",strDate);

        query.bindValue(":REALBYCATEGORYNUM",GetVectorToString(m_mapData[strUrl].m_vecCategory," ").trimmed());
        if (query.exec()==false) {
            InsertLog(query.lastQuery());
            InsertLog(query.lastError().text());m_db.close();return;
        }
        nBodyCount++;
    }

    UpdateDBInfoState(m_db,_strCompanyNum,"Data (Map Insert)");

    query.prepare(strMapQuery);
    int nCount = 0;
    foreach(SMap stMap,m_vecMap)
    {
        query.bindValue(":NUM",nCount++);
        query.bindValue(":BODY_NUM",stMap.m_nBody);
        query.bindValue(":CATEGORY_NUM",stMap.m_nCategory);
        query.bindValue(":REALIN",stMap.m_nRealIn);
        if (query.exec()==false){
            InsertLog(query.lastQuery());
            InsertLog(query.lastError().text());m_db.close();return;}
    }

    strQuery = "delete from stats_con_user_buzzbyday where company_num = " + _strCompanyNum;
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}

    QDate date = QDate::currentDate().addDays(m_nCutDays);
    if(!m_strStartDay.isEmpty())
        date = QDate::fromString(m_strStartDay.replace("'",""), "yyyy-MM-dd");

    QDate endDate = QDate::currentDate();
    if(!m_strEndDay.isEmpty())
        endDate = QDate::fromString(m_strEndDay.replace("'",""), "yyyy-MM-dd");

    QString astrOrderBy[D_ARTICLE_MAX] = {"body","reply","all"};
    while(date <= endDate)
    {
        QVector <SBuzzyCount> vecCount;
        foreach(SBuzzy stBuzzy,m_vecBuzzy)
        {
            SBuzzyCount stCount;
            memset(&stCount.m_anCount,0,sizeof(int)*D_ARTICLE_MAX);
            stCount.m_nCategory = stBuzzy.m_nCategory;
            if (stBuzzy.m_astArticle[E_ARTICLE_BODY].m_mapCount.contains(date))
                stCount.m_anCount[0] = stBuzzy.m_astArticle[E_ARTICLE_BODY].m_mapCount[date];
            if (stBuzzy.m_astArticle[E_ARTICLE_REPLY].m_mapCount.contains(date))
                stCount.m_anCount[1] = stBuzzy.m_astArticle[E_ARTICLE_REPLY].m_mapCount[date];
            stCount.m_anCount[2] = stCount.m_anCount[0] + stCount.m_anCount[1];
            vecCount.push_back(stCount);
        }
        for (int i = 0; i < D_ARTICLE_MAX ; i++)
        {
            strQuery = "insert stats_con_user_buzzbyday set ";
            foreach(SBuzzyCount stCount,vecCount)
            {
                strQuery += "category_";
                //if (stCount.m_nCategory)
                    strQuery += QString::number(stCount.m_nCategory);
                //else
                    //strQuery += "all";
                strQuery += " = ";
                strQuery += QString::number(stCount.m_anCount[i]);
                strQuery += ",";
            }
            strQuery += "date = '";
            strQuery += date.toString("yyyy-MM-dd");
            strQuery += "', subject = '" ;
            strQuery += astrOrderBy[i];
            strQuery += "',company_num = ";
            strQuery += _strCompanyNum;
            if (query.exec(strQuery.toUtf8())==false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());m_db.close();qDebug() << strQuery;return;}
        }
        date = date.addDays(1);
    }    
    UpdateDBInfoState(m_db,_strCompanyNum,"Data (Table Rename)");

    RenameTable("body_" + _strCompanyNum,"body_" + _strCompanyNum + "_update","body_" + _strCompanyNum +"_temp");
    RenameTable("reply_" + _strCompanyNum,"reply_" + _strCompanyNum + "_update","reply_" + _strCompanyNum + "_temp");
    RenameTable("body_consumer_map_" + _strCompanyNum,"body_consumer_map_" + _strCompanyNum + "_update","body_consumer_map_" + _strCompanyNum + "_temp");
    RenameTable("id_" + _strCompanyNum,"id_" + _strCompanyNum + "_update","id_" + _strCompanyNum + "_temp");
    RenameTable("nickname_" + _strCompanyNum,"nickname_" + _strCompanyNum + "_update","nickname_" + _strCompanyNum + "_temp");
    RenameTable("community_" + _strCompanyNum,"community_" + _strCompanyNum + "_update","community_" + _strCompanyNum + "_temp");
    //influencercount,spammercount,powercafecount,
    strQuery = "update dbinfo set "
            "bodycount = " + QString::number(nBodyCount) + " , "
            "replycount = " + QString::number(nReplyCount)+" , "
            "mindate = '" + dtMin.toString("yyyy-MM-dd HH:mm:ss") + "' , "
            "maxdate = '" + dtMax.toString("yyyy-MM-dd HH:mm:ss") + "' "
            "where company_num = " + _strCompanyNum;
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastQuery()); InsertLog(query.lastError().text());return;}
    UpdateDBInfoState(m_db,_strCompanyNum,"Data (Finish)");
    m_db.close();
    InsertLog("Data Put End");
}

void SFilterProcess::UpdateDBInfoState(QSqlDatabase &_db,QString _strCompanyNum,QString _strStatus)
{
    QString strQuery = "update dbinfo set "
            "status = '" + _strStatus + "' "
            "where company_num = " + _strCompanyNum;
    _db.exec(strQuery.toUtf8());
}
