#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include <QString>
#include <QStringList>
#include "sspammer.h"


void SSpammer::setInitiaizer(SInitializer &_initializer)
{
    m_pSInitializer = &_initializer;
    m_SBodySpammerMap.setInitiaizer(_initializer);
    m_SReplySpammerMap.setInitiaizer(_initializer);
    m_SStatsSpaSpammerRank.setInitiaizer(_initializer);
    m_STSpammer.setInitiaizer(_initializer);
}

bool SSpammer::uploadData(int _companyNum)
{
    m_pSInitializer->insertLog("Start upload Spammer");

    QSqlDatabase dbWeb = m_pSInitializer->getDatabase(SDBManager::E_DATABASE_WEB);
    m_pSInitializer->updateWebDBInfoState(dbWeb, _companyNum, "Spammer (Start)");
    if (!m_SBodySpammerMap.uploadData(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to upload Spammer body map");
        return false;
    }
    if (!m_SReplySpammerMap.uploadData(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to upload Spammer reply map");
        return false;
    }
    if (!m_SStatsSpaSpammerRank.uploadData(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to upload stats_spa_spammer_rank");
        return false;
    }
    if (!m_STSpammer.uploadData(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to upload spammer");
        return false;
    }
    m_pSInitializer->insertLog("End upload Spammer");
    m_pSInitializer->updateWebDBInfoState(dbWeb, _companyNum, "Spammer (Finish)");

    return true;
}

bool SSpammer::changeTable(int _companyNum)
{
    m_pSInitializer->insertLog("Start table change spammer");
    QSqlDatabase dbWeb = m_pSInitializer->getDatabase(SDBManager::E_DATABASE_WEB);
    if (!m_SBodySpammerMap.changeTable(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to change spammer body map");
        return false;
    }
    if (!m_SReplySpammerMap.changeTable(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to change spammer reply map");
        return false;
    }
    if (!m_STSpammer.changeTable(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to change spammer");
        return false;
    }
    return true;

}

bool SSpammer::makeCategory(SDatagroup &_datagroup, int _nCategory, const QList<SInitializer::stFilter> &_listFilter)
{
    QVector<QStringList> vecData[SInitializer::E_ARTICLE_MAX];
    bool bFiltered[SInitializer::E_ARTICLE_MAX];

    for (int i = 0; i < SInitializer::E_ARTICLE_MAX; i++)
        bFiltered[i] = false;
    _datagroup.copyData(vecData);
    foreach (const SInitializer::stFilter& filter, _listFilter)
    {
        switch(filter.m_nType)
        {
        case E_FILTER_TYPE_DATE://Date
            m_SFilterAlgorithm.FilterDate(vecData, bFiltered, filter.m_vData.toString(),
                                          m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_article_date]);

            break;
        case E_FILTER_TYPE_SEARCH:
            m_SFilterAlgorithm.FilterSearch(vecData, bFiltered, filter.m_vData.toString());

            break;
        case E_FILTER_TYPE_LENGTH:
            m_SFilterAlgorithm.FilterLength(vecData, bFiltered, filter.m_vData.toString());
            break;
        case E_FILTER_TYPE_REPLACE:
            m_SFilterAlgorithm.FilterReplace(vecData, bFiltered, filter.m_vData.toString());
            break;
        case E_FILTER_TYPE_COUNTER:
            m_SFilterAlgorithm.FilterCounter(vecData, bFiltered, filter.m_vData.toString());
            break;
        }
    }
    stReal streal;
    m_pSInitializer->insertLog(QString("CategoryNum:") + QString::number(_nCategory) + ",(" +
                               QString::number(vecData[SInitializer::E_ARTICLE_BODY].size()) + '/' + QString::number(vecData[SInitializer::E_ARTICLE_REPLY].size()) + ")" + "(Body/Reply)" );
    const int *anColumn = m_pSInitializer->getColumnIntArray();
    if (bFiltered[SInitializer::E_ARTICLE_BODY])
    {
        foreach (const QStringList& strlist, vecData[SInitializer::E_ARTICLE_BODY])
        {
            streal.m_setRealAllUrl.insert(strlist[anColumn[SInitializer::E_DATA_article_url]].trimmed());
            streal.m_setRealBodyUrl.insert(strlist[anColumn[SInitializer::E_DATA_article_url]].trimmed());
            _datagroup.addRealUrl(strlist[anColumn[SInitializer::E_DATA_article_url]].trimmed());
        }
    }

    if (bFiltered[SInitializer::E_ARTICLE_REPLY])
    {
        foreach (const QStringList& strlist, vecData[SInitializer::E_ARTICLE_REPLY])
        {
            streal.m_setRealAllUrl.insert(strlist[anColumn[SInitializer::E_DATA_article_url]].trimmed());
            streal.m_mapRealReplyUniqueUrl.insertMulti(strlist[anColumn[SInitializer::E_DATA_article_url]].trimmed(),
                    strlist[anColumn[SInitializer::E_DATA_article_order]].trimmed().toInt());
            _datagroup.addRealUrl(strlist[anColumn[SInitializer::E_DATA_article_url]].trimmed());
        }
    }

    m_mapCategory.insert(_nCategory, streal);
    return true;
}

bool SSpammer::makeOverallCategory(int _categoryNum)
{
    if (m_mapCategory.size() < 1)
        return true;

    stReal streal;
    for (QMap<int, stReal>::iterator iterPos = m_mapCategory.begin(); iterPos != m_mapCategory.end(); iterPos++ )
    {
        stReal& val = iterPos.value();
        foreach (const QString& _strurl, val.m_setRealBodyUrl )
            streal.m_setRealBodyUrl.insert(_strurl);
        foreach (const QString& _strurl, val.m_setRealAllUrl )
            streal.m_setRealAllUrl.insert(_strurl);

        for (QMap<QString, int>::iterator iterPos1 = val.m_mapRealReplyUniqueUrl.begin(); iterPos1 != val.m_mapRealReplyUniqueUrl.end();
             iterPos1++)
        {
            streal.m_mapRealReplyUniqueUrl.insertMulti(iterPos1.key(), iterPos1.value());
        }
    }

    m_mapCategory.insert(_categoryNum, streal);
    return true;
}

bool SSpammer::makeTable(SDatagroup &_datagroup)
{
    if (m_mapCategory.size() > 1)
    {
        m_pSInitializer->insertLog("Make spammer tables");
        m_pSInitializer->insertLog("Make spammer spammer");
        m_STSpammer.makeTable(_datagroup, m_mapCategory, m_setSpammer);
        m_pSInitializer->insertLog("Make spammer stats_spa_spammer_rank tables");
        m_SStatsSpaSpammerRank.makeTable(_datagroup, m_mapCategory);
        m_pSInitializer->insertLog("Make spammer body tables");
        m_SBodySpammerMap.makeTable(_datagroup, m_mapCategory, m_setSpammer);
        m_pSInitializer->insertLog("Make spammer reply tables");
        m_SReplySpammerMap.makeTable(_datagroup, m_mapCategory, m_setSpammer);
    }
    return true;
}

void SSpammer::STSpammer::makeTable(SDatagroup &_datagroup, const QMap<int, stReal> &_mapCategory, QSet<int> &setSpammer)
{
    const QMap<QString, QStringList>* mapBody = _datagroup.getmapBody();
    const QMap<QString, QMap<int, QStringList> >* mapReply = _datagroup.getmapReply();
    const int* anColumn = m_pSInitializer->getColumnIntArray();


    for (QMap<int, stReal>::const_iterator iterPos = _mapCategory.constBegin(); iterPos != _mapCategory.constEnd(); iterPos++)
    {
        QMap<QString, stSpammerScore> mapScore;
        const stReal& streal = iterPos.value();
        foreach (const QString& _url, streal.m_setRealBodyUrl)
        {
            QString strKey = (*mapBody)[_url][anColumn[SInitializer::E_DATA_article_id]].trimmed() + ",";
            strKey += (*mapBody)[_url][anColumn[SInitializer::E_DATA_platform_name]].trimmed() + ",";
            strKey += (*mapBody)[_url][anColumn[SInitializer::E_DATA_platform_form]].trimmed();
            if (!mapScore.contains(strKey))
            {
                mapScore.insert(strKey, stSpammerScore());
                mapScore[strKey].id_num = _datagroup.getstBody(_url)->id_num;
            }
            mapScore[strKey].body++;
            mapScore[strKey].nickname.insert((*mapBody)[_url][anColumn[SInitializer::E_DATA_article_nickname]].trimmed());
        }

        for (QMap<QString, int>::const_iterator iterPos1 = streal.m_mapRealReplyUniqueUrl.constBegin(); iterPos1 != streal.m_mapRealReplyUniqueUrl.constEnd(); iterPos1++)
        {
            QString strKey = (*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_article_id]].trimmed() + ",";
            strKey += (*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_platform_name]].trimmed() + ",";
            strKey += (*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_platform_form]].trimmed();
            if (!mapScore.contains(strKey))
            {
                mapScore.insert(strKey, stSpammerScore());
                mapScore[strKey].id_num = _datagroup.getstReply(iterPos1.key(), iterPos1.value())->id_num;
            }
            mapScore[strKey].reply++;
            mapScore[strKey].nickname.insert((*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_article_nickname]].trimmed());
        }


        QList<stSpammer> listSpammer;

        for (QMap<QString, stSpammerScore>::iterator iterPos1 = mapScore.begin(); iterPos1 != mapScore.end(); iterPos1++)
        {
            stSpammer stspammer;
            stspammer.bodycount = iterPos1->body;
            stspammer.replycount = iterPos1->reply;
            stspammer.nicknamecount = iterPos1->nickname.size();
            stspammer.id_num = iterPos1->id_num;
            stspammer.id_id = iterPos1.key().split(",").at(0);
            stspammer.category_num = iterPos.key();
            stspammer.platformname_name = iterPos1.key().split(",").at(1);
            stspammer.platformname_num = m_pSInitializer->getPlatformName(stspammer.platformname_name);
            if (stspammer.bodycount > m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_BODY_COUNT_CUT] ||
                    stspammer.replycount > m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_NICK_COUNT_CUT] ||
                    iterPos1->nickname.size() > m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_NICK_COUNT_CUT])
            {

                stspammer.spammervalue = stspammer.bodycount * m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_BODY_COUNT_RATIO]
                        + stspammer.replycount * m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_REPLY_COUNT_RATIO]
                        + iterPos1->nickname.size() * m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_NICK_COUNT_RATIO];
                listSpammer.push_back(stspammer);
            }
        }
        qSort(listSpammer.begin(), listSpammer.end(), isGreaterSpammerValue());

        int i = 1;

        for (QList<stSpammer>::iterator iterPos1 = listSpammer.begin(); iterPos1 != listSpammer.end(); iterPos1++)
        {
            iterPos1->rank = i++;
            setSpammer.insert(iterPos1->id_num);
            m_listMap.push_back(*iterPos1);

        }
    }
}

/*
bool SSpammer::STSpammer::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    //_db.transaction();
    QString strQuery = "delete from spammer where company_num = " + QString::number(_nCompany);
    QSqlQuery query(_db);
    if (query.exec(strQuery.toUtf8()) == false)
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    //_db.commit();
    //_db.transaction();
    strQuery = "insert into spammer (rank,id_num,id_id,nicknamecount,bodycount,replycount,spammervalue,company_num) VALUES ("
            ":rank,:id_num,:id_id,:nicknamecount,:bodycount,:replycount,:spammervalue,:company_num)";


    query.clear();
    query.prepare(strQuery.toUtf8());

    foreach (const stSpammer& stspammer, m_listMap)
    {
        query.bindValue(":rank", stspammer.rank);
        query.bindValue(":id_num", stspammer.id_num);
        query.bindValue(":id_id", stspammer.id_id);
        query.bindValue(":nicknamecount", stspammer.nicknamecount);
        query.bindValue(":bodycount", stspammer.bodycount);
        query.bindValue(":replycount", stspammer.replycount);
        query.bindValue(":spammervalue", QString::number(stspammer.spammervalue, 'g', 3));
        query.bindValue(":company_num", _nCompany);
        if (query.exec() == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    //_db.commit();


    strQuery = "update dbinfo set spammercount = " + QString::number(m_listMap.size()) + " where company_num = "
            + QString::number(_nCompany);
    query.exec(strQuery);


    return true;
}
*/


bool SSpammer::STSpammer::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    //_db.transaction();
    QString strQuery = "delete from spammer_" + QString::number(_nCompany) + "_update";
    QSqlQuery query(_db);
    if (query.exec(strQuery.toUtf8()) == false)
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    //_db.commit();
    //_db.transaction();
    strQuery = "insert into spammer_" + QString::number(_nCompany) + "_update (rank,id_num,id_id,nicknamecount,bodycount,replycount,spammervalue,category_num,platformname_name,platformname_num) VALUES (";

    query.clear();

    foreach (const stSpammer& stspammer, m_listMap)
    {
        QString str = strQuery;
        str += QString::number(stspammer.rank) + ",";
        str += QString::number(stspammer.id_num) + ",";
        str += "'" + sqlString(stspammer.id_id) + "',";
        str += QString::number(stspammer.nicknamecount) + ",";
        str += QString::number(stspammer.bodycount) + ",";
        str += QString::number(stspammer.replycount) + ",";
        str += QString::number(stspammer.spammervalue, 'g', 3) + ",";
        str += QString::number(stspammer.category_num) + ",";
        str += "'" + stspammer.platformname_name + "',";
        str += QString::number(stspammer.platformname_num) + ")";

        if (query.exec(str.toUtf8()) == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    //_db.commit();

    strQuery = "update dbinfo set spammercount = " + QString::number(m_listMap.size()) + " where company_num = "
            + QString::number(_nCompany);
    query.exec(strQuery);

    return true;
}


bool SSpammer::STSpammer::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "spammer_" + QString::number(_nCompany) + " TO " + "spammer_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "spammer_" + QString::number(_nCompany) + "_update" + " TO " + "spammer_" + QString::number(_nCompany) + ",";
    strQuery += "spammer_" + QString::number(_nCompany) + "_temp" + " TO " + "spammer_" + QString::number(_nCompany) + "_update";
    if (!query.exec(strQuery.toUtf8()))
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    return true;
}




void SSpammer::SStatsSpaSpammerRank::makeTable(SDatagroup &_datagroup, const QMap<int, stReal> &_mapCategory)
{
    const QMap<QString, QStringList>* mapBody = _datagroup.getmapBody();
    const QMap<QString, QMap<int, QStringList> >* mapReply = _datagroup.getmapReply();
    const int* anColumn = m_pSInitializer->getColumnIntArray();

    for (QMap<int, stReal>::const_iterator iterPos = _mapCategory.constBegin(); iterPos != _mapCategory.constEnd(); iterPos++)
    {
        QMap<QString, stSpammerScore> mapScore;
        const stReal& streal = iterPos.value();
        foreach (const QString& _url, streal.m_setRealBodyUrl)
        {
            QString strKey = (*mapBody)[_url][anColumn[SInitializer::E_DATA_article_id]].trimmed() + ",";
            strKey += ((*mapBody)[_url][anColumn[SInitializer::E_DATA_platform_name]].trimmed() + ",");
            strKey += (*mapBody)[_url][anColumn[SInitializer::E_DATA_platform_form]].trimmed();
            if (!mapScore.contains(strKey))
            {
                mapScore.insert(strKey, stSpammerScore());
                mapScore[strKey].id_num = _datagroup.getstBody(_url)->id_num;
            }
            mapScore[strKey].body++;
            mapScore[strKey].nickname.insert((*mapBody)[_url][anColumn[SInitializer::E_DATA_article_nickname]].trimmed());
        }

        for (QMap<QString, int>::const_iterator iterPos1 = streal.m_mapRealReplyUniqueUrl.constBegin(); iterPos1 != streal.m_mapRealReplyUniqueUrl.constEnd(); iterPos1++)
        {
            QString strKey = (*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_article_id]].trimmed() + ",";
            strKey += ((*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_platform_name]].trimmed() + ",");
            strKey += (*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_platform_form]].trimmed();
            if (!mapScore.contains(strKey))
            {
                mapScore.insert(strKey, stSpammerScore());
                mapScore[strKey].id_num = _datagroup.getstBody(iterPos1.key())->id_num;
            }
            mapScore[strKey].reply++;
            mapScore[strKey].nickname.insert((*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_article_nickname]].trimmed());
        }


        QList<stStatsSpaSpammerRank> listSpammer;

        for (QMap<QString, stSpammerScore>::iterator iterPos1 = mapScore.begin(); iterPos1 != mapScore.end(); iterPos1++)
        {
            stStatsSpaSpammerRank ststats;
            ststats.bodycount = iterPos1->body;
            ststats.replycount = iterPos1->reply;
            ststats.id_num = iterPos1->id_num;
            ststats.nicknamecount = iterPos1->nickname.size();
            ststats.id_id = iterPos1.key().split(",").at(0);
            ststats.platformname_name = iterPos1.key().split(",").at(1);
            ststats.subject = "spammervalue";
            if (ststats.bodycount > m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_BODY_COUNT_CUT] ||
                    ststats.replycount > m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_NICK_COUNT_CUT] ||
                    iterPos1->nickname.size() > m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_NICK_COUNT_CUT])
            {

                ststats.spammervalue = ststats.bodycount * m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_BODY_COUNT_RATIO]
                        + ststats.replycount * m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_REPLY_COUNT_RATIO]
                        + iterPos1->nickname.size() * m_pSInitializer->getSpammerParam()[SInitializer::E_SPAMER_NICK_COUNT_RATIO];
                listSpammer.push_back(ststats);
            }
        }

        qSort(listSpammer.begin(), listSpammer.end(), isGreaterSpammerValue());
        QMap<QString, int> mapRank;

        for (QList<stStatsSpaSpammerRank>::iterator iterPos1 = listSpammer.begin(); iterPos1 != listSpammer.end(); iterPos1++)
        {
            if (!mapRank.contains(iterPos1->platformname_name))
                mapRank.insert(iterPos1->platformname_name, 1);

            if (mapRank[iterPos1->platformname_name] <= 10)
            {
                iterPos1->rank = mapRank[iterPos1->platformname_name]++;
                m_listMap.push_back(*iterPos1);
            }
        }
    }
}

/*
bool SSpammer::SStatsSpaSpammerRank::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    //_db.transaction();
    QString strQuery = "delete from stats_spa_spammer_rank where company_num = " + QString::number(_nCompany);
    QSqlQuery query(_db);
    if (query.exec(strQuery.toUtf8()) == false)
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    //_db.commit();
    //_db.transaction();
    strQuery = "insert into stats_spa_spammer_rank (rank,id_num,id_id,nicknamecount,bodycount,replycount,spammervalue,company_num,platformname_name,subject) VALUES ("
            ":rank,:id_num,:id_id,:nicknamecount,:bodycount,:replycount,:spammervalue,:company_num,:platformname_name,:subject)";

    query.clear();
    query.prepare(strQuery.toUtf8());

    foreach (const stStatsSpaSpammerRank& ststats, m_listMap)
    {
        query.bindValue(":rank", ststats.rank);
        query.bindValue(":id_num", ststats.id_num);
        query.bindValue(":id_id", ststats.id_id);
        query.bindValue(":nicknamecount", ststats.nicknamecount);
        query.bindValue(":bodycount", ststats.bodycount);
        query.bindValue(":replycount", ststats.replycount);
        query.bindValue(":spammervalue", QString::number(ststats.spammervalue, 'g', 3));
        query.bindValue(":company_num", _nCompany);
        query.bindValue(":platformname_name", ststats.platformname_name);
        query.bindValue(":subject", ststats.subject);
        if (query.exec() == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    //_db.commit();
    return true;
}
*/

bool SSpammer::SStatsSpaSpammerRank::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    //_db.transaction();
    QString strQuery = "delete from stats_spa_spammer_rank where company_num = " + QString::number(_nCompany);
    QSqlQuery query(_db);
    if (query.exec(strQuery.toUtf8()) == false)
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    //_db.commit();
    //_db.transaction();
    strQuery = "insert into stats_spa_spammer_rank (rank,id_num,id_id,nicknamecount,bodycount,replycount,"
               "spammervalue,company_num,platformname_name,subject) VALUES (";
    query.clear();

    foreach (const stStatsSpaSpammerRank& ststats, m_listMap)
    {
        QString str = strQuery;
        str += QString::number(ststats.rank) + ",";
        str += QString::number(ststats.id_num) + ",";
        str += "'" + sqlString(ststats.id_id) + "',";
        str += QString::number(ststats.nicknamecount) + ",";
        str += QString::number(ststats.bodycount) + ",";
        str += QString::number(ststats.replycount) + ",";
        str += QString::number(ststats.spammervalue, 'g', 3) + ",";
        str += QString::number(_nCompany) + ",";
        str += "'" + ststats.platformname_name + "',";
        str += "'" + ststats.subject + "')";

        if (query.exec(str.toUtf8()) == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    //_db.commit();
    return true;
}


void SSpammer::SBodySpammerMap::makeTable(SDatagroup &_datagroup, const QMap<int, stReal> &_mapCategory, QSet<int>& setSpammer)
{
    int num = 1;
    const QStringList* orderedUrl = _datagroup.getOrderedUrl();
    for (QMap<int, stReal>::const_iterator iterPos = _mapCategory.constBegin(); iterPos != _mapCategory.constEnd(); iterPos++)
    {
        const stReal& streal = iterPos.value();
        foreach (const QString& strUrl, *orderedUrl)
        {
            if (streal.m_setRealBodyUrl.contains(strUrl))
            {
                if (!setSpammer.contains(_datagroup.getstBody(strUrl)->id_num))
                    continue;

                stBodySpammerMap stbody;
                stbody.num = num++;
                stbody.body_num = _datagroup.getstBodyNum(strUrl);
                stbody.id_num = _datagroup.getstBody(strUrl)->id_num;
                stbody.category_num = iterPos.key();
                stbody.realin |= REAL_BODY;
                stbody.platformname_num = _datagroup.getstBody(strUrl)->platformname_num;
                stbody.body_date = _datagroup.getstBody(strUrl)->date;
                if (streal.m_mapRealReplyUniqueUrl.contains(strUrl))
                {
                    stbody.realin |= REAL_REPLY;

                    foreach (int order, streal.m_mapRealReplyUniqueUrl.values(strUrl))
                    {
                        _datagroup.addRealReplyCategory(strUrl, order, iterPos.key());
                    }

                }
                m_listMap.append(stbody);
                _datagroup.addRealBodyCategory(strUrl, iterPos.key());
            }

            else if (streal.m_mapRealReplyUniqueUrl.contains(strUrl))
            {
                stBodySpammerMap stbody;
                stbody.num = num++;
                stbody.body_num = _datagroup.getstBodyNum(strUrl);
                stbody.category_num = iterPos.key();
                stbody.id_num = _datagroup.getstBody(strUrl)->id_num;
                stbody.realin |= REAL_REPLY;
                stbody.platformname_num = _datagroup.getstBody(strUrl)->platformname_num;
                stbody.body_date = _datagroup.getstBody(strUrl)->date;

                foreach (int order, streal.m_mapRealReplyUniqueUrl.values(strUrl))
                {
                    _datagroup.addRealReplyCategory(strUrl, order, iterPos.key());
                }
                m_listMap.append(stbody);
            }

        }
    }
}

/*
bool SSpammer::SBodySpammerMap::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "truncate body_spammer_map_" + QString::number(_nCompany) + "_update";

    QSqlQuery query(_db);
    if (query.exec(strQuery.toUtf8()) == false)
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    strQuery = "insert into body_spammer_map_" + QString::number(_nCompany) + "_update (num,category_num,id_num,body_num,realin)"
            " values (:num,:category_num,:id_num,:body_num,:realin)";
    query.clear();
    query.prepare(strQuery.toUtf8());

    foreach (const stBodySpammerMap& stmap, m_listMap)
    {
        query.bindValue(":num", stmap.num);
        query.bindValue(":category_num", stmap.category_num);
        query.bindValue(":id_num", stmap.id_num);
        query.bindValue(":body_num", stmap.body_num);
        query.bindValue(":realin", stmap.realin);
        if (query.exec() == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    return true;
}
*/

bool SSpammer::SBodySpammerMap::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "truncate body_spammer_map_" + QString::number(_nCompany) + "_update";

    QSqlQuery query(_db);
    if (query.exec(strQuery.toUtf8()) == false)
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    strQuery = "insert into body_spammer_map_" + QString::number(_nCompany) + "_update (num,category_num,id_num,body_num,realin,body_date,platformname_num)"
            " values (";
    query.clear();

    foreach (const stBodySpammerMap& stmap, m_listMap)
    {
        QString str = strQuery;
        str += QString::number(stmap.num) + ",";
        str += QString::number(stmap.category_num) + ",";
        str += QString::number(stmap.id_num) + ",";
        str += QString::number(stmap.body_num) + ",";
        str += QString::number(stmap.realin) + ",";
        str += "'" + stmap.body_date + "',";
        str += QString::number(stmap.platformname_num) + ")";
        if (query.exec(str.toUtf8()) == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    return true;
}


void SSpammer::SReplySpammerMap::makeTable(SDatagroup &_datagroup, const QMap<int, stReal> &_mapCategory, QSet<int>& setSpammer)
{
    int num = 1;
    //const QStringList* orderedUrl = _datagroup.getOrderedUrl();
    for (QMap<int, stReal>::const_iterator iterPos = _mapCategory.constBegin(); iterPos != _mapCategory.constEnd(); iterPos++)
    {
        const stReal& streal = iterPos.value();
        //foreach (const QString& strUrl, *orderedUrl)
        {
            for (QMap<QString, int>::const_iterator iterPos1 = streal.m_mapRealReplyUniqueUrl.constBegin(); iterPos1 != streal.m_mapRealReplyUniqueUrl.constEnd(); iterPos1++)
            {
                if (!setSpammer.contains(_datagroup.getstReply(iterPos1.key(), iterPos1.value())->id_num))
                    continue;

                stReplySpammerMap streply;
                streply.num = num++;
                streply.category_num = iterPos.key();
                streply.body_num = _datagroup.getstReply(iterPos1.key(), iterPos1.value())->body_num;
                streply.id_num = _datagroup.getstReply(iterPos1.key(), iterPos1.value())->id_num;
                streply.reply_num = _datagroup.getstReply(iterPos1.key(), iterPos1.value())->num;
                streply.body_date = _datagroup.getstBody(iterPos1.key())->date;
                m_listMap.append(streply);


                _datagroup.addRealReplyCategory(iterPos1.key(), iterPos1.value(), iterPos.key());

            }

            /*
            if (streal.m_setRealAllUrl.contains(strUrl))
            {
                if (_datagroup.getstBody(strUrl)->reply_count == 0)
                    continue;

                const QMap<int, SDatagroup::stReply>* mapReply = _datagroup.getstReply(strUrl);

                for(QMap<int, SDatagroup::stReply>::const_iterator iterPos1 = mapReply->constBegin(); iterPos1 != mapReply->constEnd(); iterPos1++)
                {
                    stReplySpammerMap streply;
                    streply.num = num++;
                    streply.category_num = iterPos.key();
                    streply.body_num = iterPos1.value().body_num;
                    streply.id_num = iterPos1.value().id_num;
                    streply.reply_num = iterPos1.value().num;
                    m_listMap.append(streply);
                }
            }
            */
        }
    }
}
/*
bool SSpammer::SReplySpammerMap::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "truncate reply_spammer_map_" + QString::number(_nCompany) + "_update";
    QSqlQuery query(_db);
    if (query.exec(strQuery.toUtf8()) == false)
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    strQuery = "insert into reply_spammer_map_" + QString::number(_nCompany) + "_update (num,category_num,id_num,body_num,reply_num)"
            " values (:num,:category_num,:id_num,:body_num,:reply_num)";
    query.clear();
    query.prepare(strQuery.toUtf8());

    foreach (const stReplySpammerMap& stmap, m_listMap)
    {
        query.bindValue(":num", stmap.num);
        query.bindValue(":category_num", stmap.category_num);
        query.bindValue(":id_num", stmap.id_num);
        query.bindValue(":body_num", stmap.body_num);
        //query.bindValue(":realin", stmap.realin);
        query.bindValue(":reply_num", stmap.reply_num);
        if (query.exec() == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    return true;
}
*/

bool SSpammer::SReplySpammerMap::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "truncate reply_spammer_map_" + QString::number(_nCompany) + "_update";
    QSqlQuery query(_db);
    if (query.exec(strQuery.toUtf8()) == false)
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    strQuery = "insert into reply_spammer_map_" + QString::number(_nCompany) + "_update (num,category_num,id_num,body_num,reply_num,body_date)"
            " values (";
    query.clear();

    foreach (const stReplySpammerMap& stmap, m_listMap)
    {
        QString str = strQuery;
        str += QString::number(stmap.num) + ",";
        str += QString::number(stmap.category_num) + ",";
        str += QString::number(stmap.id_num) + ",";
        str += QString::number(stmap.body_num) + ",";
        str += QString::number(stmap.reply_num) + ",";
        str += "'" + stmap.body_date + "')";
        if (query.exec(str.toUtf8()) == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    return true;
}

bool SSpammer::SBodySpammerMap::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "body_spammer_map_" + QString::number(_nCompany) + " TO " + "body_spammer_map_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "body_spammer_map_" + QString::number(_nCompany) + "_update" + " TO " + "body_spammer_map_" + QString::number(_nCompany) + ",";
    strQuery += "body_spammer_map_" + QString::number(_nCompany) + "_temp" + " TO " + "body_spammer_map_" + QString::number(_nCompany) + "_update";
    if (!query.exec(strQuery.toUtf8()))
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    return true;
}


bool SSpammer::SReplySpammerMap::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "reply_spammer_map_" + QString::number(_nCompany) + " TO " + "reply_spammer_map_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "reply_spammer_map_" + QString::number(_nCompany) + "_update" + " TO " + "reply_spammer_map_" + QString::number(_nCompany) + ",";
    strQuery += "reply_spammer_map_" + QString::number(_nCompany) + "_temp" + " TO " + "reply_spammer_map_" + QString::number(_nCompany) + "_update";
    if (!query.exec(strQuery.toUtf8()))
    {
        m_pSInitializer->insertLog(query.lastQuery());
        m_pSInitializer->insertLog(query.lastError().text());
        return false;
    }
    return true;
}
