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

void SPowercafe::setInitiaizer(SInitializer &_initializer)
{
    m_pSInitializer = &_initializer;
    m_SBodyPowercafeMap.setInitiaizer(_initializer);
    m_SReplyPowercafeMap.setInitiaizer(_initializer);
    m_STPowercafe.setInitiaizer(_initializer);
    m_SStatsPowCafeRank.setInitiaizer(_initializer);
}

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

    QSqlDatabase dbWeb = m_pSInitializer->getDatabase(SDBManager::E_DATABASE_WEB);
    m_pSInitializer->updateWebDBInfoState(dbWeb, _companyNum, "Powercafe (Start)");
    if (!m_SBodyPowercafeMap.uploadData(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to upload Powercafe body map");
        return false;
    }
    if (!m_SReplyPowercafeMap.uploadData(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to upload Powercafe reply map");
        return false;
    }
    if (!m_SStatsPowCafeRank.uploadData(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to upload stats_pow_cafe_rank");
        return false;
    }
    if (!m_STPowercafe.uploadData(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to upload Powercafe");
        return false;
    }
    m_pSInitializer->insertLog("End upload Powercafe");
    m_pSInitializer->updateWebDBInfoState(dbWeb, _companyNum, "Powercafe (Finish)");

    return true;
}

bool SPowercafe::changeTable(int _companyNum)
{
    m_pSInitializer->insertLog("Start table change powercafe");
    QSqlDatabase dbWeb = m_pSInitializer->getDatabase(SDBManager::E_DATABASE_WEB);
    if (!m_SBodyPowercafeMap.changeTable(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to change powercafe body map");
        return false;
    }
    if (!m_SReplyPowercafeMap.changeTable(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to change powercafe reply map");
        return false;
    }
    if (!m_STPowercafe.changeTable(dbWeb, _companyNum))
    {
        m_pSInitializer->insertLog("Fail to change powercafe");
        return false;
    }

    return true;
}

bool SPowercafe::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])
        {
            if (strlist[anColumn[SInitializer::E_DATA_platform_form]].trimmed() != "cafe")
                continue;
            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])
        {
            if (strlist[anColumn[SInitializer::E_DATA_platform_form]].trimmed() != "cafe")
                continue;
            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 SPowercafe::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 SPowercafe::makeTable(SDatagroup &_datagroup)
{
    if (m_mapCategory.size() > 1)
    {
        m_pSInitializer->insertLog("Make powercafe tables");
        m_pSInitializer->insertLog("Make powercafe body tables");
        m_SBodyPowercafeMap.makeTable(_datagroup, m_mapCategory);
        m_pSInitializer->insertLog("Make powercafe reply tables");
        m_SReplyPowercafeMap.makeTable(_datagroup, m_mapCategory);
        m_pSInitializer->insertLog("Make powercafe stats_pow_cafe_rank tables");
        m_SStatsPowCafeRank.makeTable(_datagroup, m_mapCategory);
        m_pSInitializer->insertLog("Make powercafe powercafe table");
        m_STPowercafe.makeTable(_datagroup, m_mapCategory);
    }
    return true;

}

void SPowercafe::STPowercafe::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();
    QMap<QString, stPowercafe> mapPowercafeAll;

    QMap<QString, QSet<QString> > mapUserAll;
    QMap<QString, QSet<QString> > mapUserReal;


    for (QMap<QString, QStringList>::const_iterator iterPos = mapBody->constBegin(); iterPos != mapBody->constEnd(); iterPos++)
    {
        if (iterPos.value()[anColumn[SInitializer::E_DATA_platform_form]].trimmed() != "cafe")
            continue;

        QString strKey = iterPos.value()[anColumn[SInitializer::E_DATA_platform_id]].trimmed();
        if (!mapPowercafeAll.contains(strKey))
        {
            mapPowercafeAll.insert(strKey, stPowercafe());
            mapUserAll.insert(strKey, QSet<QString>());
        }
        mapPowercafeAll[strKey].bodycountall++;
        mapUserAll[strKey].insert(iterPos.value().at(anColumn[SInitializer::E_DATA_article_id]).trimmed());
    }

    for (QMap<QString, QMap<int, QStringList> >::const_iterator iterPos = mapReply->constBegin(); iterPos != mapReply->constEnd(); iterPos++)
    {
        for (QMap<int, QStringList>::const_iterator iterPos1 = iterPos.value().constBegin(); iterPos1 != iterPos.value().constEnd(); iterPos1++)
        {
            if (iterPos1.value()[anColumn[SInitializer::E_DATA_platform_form]].trimmed() != "cafe")
                continue;

            QString strKey = iterPos1.value().at(anColumn[SInitializer::E_DATA_platform_id]).trimmed();
            if (!mapPowercafeAll.contains(strKey))
            {
                mapPowercafeAll.insert(strKey, stPowercafe());
                mapUserAll.insert(strKey, QSet<QString>());
            }
            mapPowercafeAll[strKey].replycountall++;
            mapUserAll[strKey].insert(iterPos1.value().at(anColumn[SInitializer::E_DATA_article_id]).trimmed());
        }
    }

    for (QMap<int, stReal>::const_iterator iterPos = _mapCategory.constBegin(); iterPos != _mapCategory.constEnd(); iterPos++)
    {
        QMap<QString, stPowercafe> mapPowercafeReal;
        const stReal& streal = iterPos.value();

        foreach (const QString& url, streal.m_setRealBodyUrl)
        {
            QString strKey = (*mapBody)[url][anColumn[SInitializer::E_DATA_platform_id]].trimmed();
            if (!mapPowercafeReal.contains(strKey))
            {
                mapPowercafeReal.insert(strKey, stPowercafe());
                mapPowercafeReal[strKey].community_id = _datagroup.getstCommunity((*mapBody)[url])->id;
                mapPowercafeReal[strKey].community_num = _datagroup.getstCommunity((*mapBody)[url])->num;
                mapPowercafeReal[strKey].bodycountall = mapPowercafeAll[strKey].bodycountall;
                mapPowercafeReal[strKey].replycountall = mapPowercafeAll[strKey].replycountall;
                mapPowercafeReal[strKey].allcountall = mapPowercafeAll[strKey].bodycountall + mapPowercafeAll[strKey].replycountall;
                mapPowercafeReal[strKey].usercountall = mapUserAll[strKey].size();
                mapPowercafeReal[strKey].category_num = iterPos.key();
                mapPowercafeReal[strKey].community_title = _datagroup.getstCommunity((*mapBody)[url])->title;
                mapPowercafeReal[strKey].community_url = _datagroup.getstCommunity((*mapBody)[url])->url;
                mapPowercafeReal[strKey].platformname_name = (*mapBody)[url][anColumn[SInitializer::E_DATA_platform_name]].trimmed();
                mapPowercafeReal[strKey].platformname_num = m_pSInitializer->getPlatformName(mapPowercafeReal[strKey].platformname_name);

                mapUserReal.insert(strKey, QSet<QString>());
            }
            mapPowercafeReal[strKey].bodycountreal++;
            mapUserReal[strKey].insert((*mapBody)[url][anColumn[SInitializer::E_DATA_article_id]].trimmed());
        }

        for (QMap<QString, int>::const_iterator iterPos1 = streal.m_mapRealReplyUniqueUrl.begin(); iterPos1 != streal.m_mapRealReplyUniqueUrl.constEnd(); iterPos1++)
        {
            QString strKey = (*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_platform_id]].trimmed();
            if (!mapPowercafeReal.contains(strKey))
            {
                mapPowercafeReal.insert(strKey, stPowercafe());
                mapPowercafeReal[strKey].community_id = _datagroup.getstCommunity((*mapReply)[iterPos1.key()][iterPos1.value()])->id;
                mapPowercafeReal[strKey].community_num = _datagroup.getstCommunity((*mapReply)[iterPos1.key()][iterPos1.value()])->num;
                mapPowercafeReal[strKey].bodycountall = mapPowercafeAll[strKey].bodycountall;
                mapPowercafeReal[strKey].replycountall = mapPowercafeAll[strKey].replycountall;
                mapPowercafeReal[strKey].allcountall = mapPowercafeAll[strKey].bodycountall + mapPowercafeAll[strKey].replycountall;
                mapPowercafeReal[strKey].usercountall = mapUserAll[strKey].size();
                mapPowercafeReal[strKey].category_num = iterPos.key();
                mapPowercafeReal[strKey].community_title = _datagroup.getstCommunity((*mapReply)[iterPos1.key()][iterPos1.value()])->title;
                mapPowercafeReal[strKey].community_url = _datagroup.getstCommunity((*mapReply)[iterPos1.key()][iterPos1.value()])->url;
                mapPowercafeReal[strKey].platformname_name = (*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_platform_name]].trimmed();
                mapPowercafeReal[strKey].platformname_num = m_pSInitializer->getPlatformName(mapPowercafeReal[strKey].platformname_name);

                mapUserReal.insert(strKey, QSet<QString>());
            }
            mapPowercafeReal[strKey].replycountreal++;
            mapUserReal[strKey].insert((*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_article_id]].trimmed());
        }

        for (QMap<QString, stPowercafe>::iterator iterPos1 = mapPowercafeReal.begin(); iterPos1 != mapPowercafeReal.end(); iterPos1++)
        {
            iterPos1.value().usercountreal = mapUserReal[iterPos1.key()].size();
            iterPos1.value().allcountreal = iterPos1.value().bodycountreal + iterPos1.value().replycountreal;
        }
        QList<stPowercafe> listPowercafe = mapPowercafeReal.values();

        qSort(listPowercafe.begin(), listPowercafe.end(), isGreaterCountReal());
        int rank = 1;
        foreach (stPowercafe stpowercafe, listPowercafe)
        {
            stpowercafe.rank = rank++;
            m_listMap.push_back(stpowercafe);
        }
    }
}

/*
bool SPowercafe::STPowercafe::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "delete from powercafe 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;
    }
    strQuery = "insert into powercafe (company_num,rank,community_num,community_id,bodycountall,replycountall,allcountall,usercountall,bodycountreal,replycountreal,allcountreal,usercountreal)"
               " VALUES (:COMPANY_NUM,:RANK,:COMMUNITY_NUM,:COMMUNITY_ID,:BODY_ALL,:REPLY_ALL,:COUNT_ALL,:USER_ALL,:BODY_REAL,:REPLY_REAL,:ALL_REAL,:USER_REAL)";

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

    foreach (const stPowercafe& stpowercafe, m_listMap)
    {
        query.bindValue(":COMPANY_NUM", _nCompany);
        query.bindValue(":COMMUNITY_NUM", stpowercafe.community_num);
        query.bindValue(":RANK", stpowercafe.rank);
        query.bindValue(":COMMUNITY_ID", stpowercafe.community_id);
        query.bindValue(":BODY_ALL", stpowercafe.bodycountall);
        query.bindValue(":REPLY_ALL", stpowercafe.replycountall);
        query.bindValue(":COUNT_ALL", stpowercafe.allcountall);
        query.bindValue(":USER_ALL", stpowercafe.usercountall);
        query.bindValue(":BODY_REAL", stpowercafe.bodycountreal);
        query.bindValue(":REPLY_REAL", stpowercafe.replycountreal);
        query.bindValue(":ALL_REAL", stpowercafe.allcountreal);
        query.bindValue(":USER_REAL", stpowercafe.usercountreal);
        if (query.exec() == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    strQuery = "update dbinfo set powercafecount = " + QString::number(m_listMap.size()) + " where company_num = "
            + QString::number(_nCompany);
    query.exec(strQuery);
    return true;
}
*/

bool SPowercafe::STPowercafe::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "delete from powercafe_" + 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 powercafe_" + QString::number(_nCompany) + "_update (category_num,rank,community_num,community_id,bodycountall,replycountall,allcountall,usercountall,"
               "bodycountreal,replycountreal,allcountreal,usercountreal,community_title,community_url,platformname_num,platformname_name)"
               " VALUES (";

    query.clear();

    foreach (const stPowercafe& stpowercafe, m_listMap)
    {
        QString str = strQuery;
        str += QString::number(stpowercafe.category_num) + ",";
        str += QString::number(stpowercafe.rank) + ",";
        str += QString::number(stpowercafe.community_num) + ",";
        str += "'" + sqlString(stpowercafe.community_id) + "',";
        str += QString::number(stpowercafe.bodycountall) + ",";
        str += QString::number(stpowercafe.replycountall) + ",";
        str += QString::number(stpowercafe.allcountall) + ",";
        str += QString::number(stpowercafe.usercountall) + ",";
        str += QString::number(stpowercafe.bodycountreal) + ",";
        str += QString::number(stpowercafe.replycountreal) + ",";
        str += QString::number(stpowercafe.allcountreal) + ",";
        str += QString::number(stpowercafe.usercountreal) + ",";
        str += "'" + sqlString(stpowercafe.community_title) + "',";
        str += "'" + stpowercafe.community_url + "',";
        str += QString::number(stpowercafe.platformname_num) + ",";
        str += "'" + stpowercafe.platformname_name + "')";

        if (query.exec(str.toUtf8()) == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    strQuery = "update dbinfo set powercafecount = " + QString::number(m_listMap.size()) + " where company_num = "
            + QString::number(_nCompany);
    query.exec(strQuery);
    return true;
}


void SPowercafe::SStatsPowCafeRank::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();
    QMap<QString, stStatsPowCafeRank> mapPowercafeAll;
    for (QMap<QString, QStringList>::const_iterator iterPos = mapBody->constBegin(); iterPos != mapBody->constEnd(); iterPos++)
    {
        if (iterPos.value()[anColumn[SInitializer::E_DATA_platform_form]].trimmed() != "cafe")
            continue;

        QString strKey = iterPos.value()[anColumn[SInitializer::E_DATA_platform_id]].trimmed();
        if (!mapPowercafeAll.contains(strKey))
        {
            mapPowercafeAll.insert(strKey, stStatsPowCafeRank());
            mapPowercafeAll[strKey].community_id = iterPos.value()[anColumn[SInitializer::E_DATA_platform_id]].trimmed();
            mapPowercafeAll[strKey].community_title = iterPos.value()[anColumn[SInitializer::E_DATA_platform_title]].trimmed();
            mapPowercafeAll[strKey].subject = "articlecountall";
        }
        mapPowercafeAll[strKey].articlecountall++;
    }

    for (QMap<QString, QMap<int, QStringList> >::const_iterator iterPos = mapReply->constBegin(); iterPos != mapReply->constEnd(); iterPos++)
    {
        for (QMap<int, QStringList>::const_iterator iterPos1 = iterPos.value().constBegin(); iterPos1 != iterPos.value().constEnd(); iterPos1++)
        {
            if (iterPos1.value()[anColumn[SInitializer::E_DATA_platform_form]].trimmed() != "cafe")
                continue;

            QString strKey = iterPos1.value()[anColumn[SInitializer::E_DATA_platform_id]].trimmed();
            if (!mapPowercafeAll.contains(strKey))
            {
                mapPowercafeAll.insert(strKey, stStatsPowCafeRank());
                mapPowercafeAll[strKey].community_id = iterPos1.value()[anColumn[SInitializer::E_DATA_platform_id]].trimmed();
                mapPowercafeAll[strKey].community_title = iterPos1.value()[anColumn[SInitializer::E_DATA_platform_title]].trimmed();
                mapPowercafeAll[strKey].subject = "articlecountall";
            }
            mapPowercafeAll[strKey].articlecountall++;
        }
    }

    {
        QList<stStatsPowCafeRank> listPowercafe = mapPowercafeAll.values();

        qSort(listPowercafe.begin(), listPowercafe.end(), isGreaterCountAll());
        int rank = 1;
        foreach (stStatsPowCafeRank ststats, listPowercafe)
        {
            ststats.rank = rank++;
            m_listMap.push_back(ststats);
            if (rank > 10)
                break;
        }
    }


    for (QMap<int, stReal>::const_iterator iterPos = _mapCategory.constBegin(); iterPos != _mapCategory.constEnd(); iterPos++)
    {
        QMap<QString, stStatsPowCafeRank> mapPowercafeReal;
        const stReal& streal = iterPos.value();

        foreach (const QString& url, streal.m_setRealBodyUrl)
        {
            QString strKey = (*mapBody)[url][anColumn[SInitializer::E_DATA_platform_id]].trimmed();
            if (!mapPowercafeReal.contains(strKey))
            {
                mapPowercafeReal.insert(strKey, stStatsPowCafeRank());
                mapPowercafeReal[strKey].community_id = _datagroup.getstCommunity((*mapBody)[url])->id;
                mapPowercafeReal[strKey].community_title = _datagroup.getstCommunity((*mapBody)[url])->title;
                mapPowercafeReal[strKey].subject = "articlecountreal";
            }
            mapPowercafeReal[strKey].articlecountreal++;
        }

        for (QMap<QString, int>::const_iterator iterPos1 = streal.m_mapRealReplyUniqueUrl.begin(); iterPos1 != streal.m_mapRealReplyUniqueUrl.constEnd(); iterPos1++)
        {
            QString strKey = (*mapReply)[iterPos1.key()][iterPos1.value()][anColumn[SInitializer::E_DATA_platform_id]].trimmed();
            if (!mapPowercafeReal.contains(strKey))
            {
                mapPowercafeReal.insert(strKey, stStatsPowCafeRank());
                mapPowercafeReal[strKey].community_id = _datagroup.getstCommunity((*mapReply)[iterPos1.key()][iterPos1.value()])->id;
                mapPowercafeReal[strKey].community_title = _datagroup.getstCommunity((*mapReply)[iterPos1.key()][iterPos1.value()])->title;
                mapPowercafeReal[strKey].subject = "aritclecountreal";
            }
            mapPowercafeReal[strKey].articlecountreal++;
        }

        QList<stStatsPowCafeRank> listPowercafe = mapPowercafeReal.values();
        qSort(listPowercafe.begin(), listPowercafe.end(), isGreaterCountReal());
        int rank = 1;
        foreach (stStatsPowCafeRank ststats, listPowercafe)
        {
            ststats.rank = rank++;
            m_listMap.push_back(ststats);
            if (rank > 10)
                break;
        }
    }
}

bool SPowercafe::STPowercafe::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "powercafe_" + QString::number(_nCompany) + " TO " + "powercafe_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "powercafe_" + QString::number(_nCompany) + "_update" + " TO " + "powercafe_" + QString::number(_nCompany) + ",";
    strQuery += "powercafe_" + QString::number(_nCompany) + "_temp" + " TO " + "powercafe_" + 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 SPowercafe::SStatsPowCafeRank::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "delete from stats_pow_cafe_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;
    }
    strQuery = "insert into stats_pow_cafe_rank (community_id,community_title,articlecountall,articlecountreal,usercountreal,subject,rank,company_num)"
            "VALUES (:community_id,:community_title,:articlecountall,:articlecountreal,:usercountreal,:subject,:rank,:company_num)";
    query.clear();
    query.prepare(strQuery.toUtf8());
    foreach (const stStatsPowCafeRank& ststats, m_listMap)
    {
        query.bindValue(":community_id", ststats.community_id);
        query.bindValue(":community_title", sqlString(ststats.community_title));
        query.bindValue(":articlecountall", ststats.articlecountall);
        query.bindValue(":articlecountreal", ststats.articlecountreal);
        query.bindValue(":usercountreal", ststats.usercountreal);
        query.bindValue(":subject", ststats.subject);
        query.bindValue(":rank", ststats.rank);
        query.bindValue(":company_num", _nCompany);
        if (query.exec() == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    return true;
}
*/


bool SPowercafe::SStatsPowCafeRank::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "delete from stats_pow_cafe_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;
    }
    strQuery = "insert into stats_pow_cafe_rank (community_id,community_title,articlecountall,articlecountreal,"
               "usercountreal,subject,rank,company_num)"
            "VALUES (";
    query.clear();

    foreach (const stStatsPowCafeRank& ststats, m_listMap)
    {
        QString str = strQuery;
        str += "'" + sqlString(ststats.community_id) + "',";
        str += "'" + sqlString(ststats.community_title) + "',";
        str += QString::number(ststats.articlecountall) + ",";
        str += QString::number(ststats.articlecountreal) + ",";
        str += QString::number(ststats.usercountreal) + ",";
        str += "'" + ststats.subject + "',";
        str += QString::number(ststats.rank) + ",";
        str += QString::number(_nCompany) + ")";

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



void SPowercafe::SBodyPowercafeMap::makeTable(SDatagroup &_datagroup, const QMap<int, stReal> &_mapCategory)
{
    int num = 0;
    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))
            {
                stBodyPowercafeMap stbody;
                stbody.num = num++;
                stbody.body_num = _datagroup.getstBodyNum(strUrl);
                stbody.community_num = _datagroup.getstBody(strUrl)->community_num;
                stbody.category_num = iterPos.key();
                stbody.realin |= REAL_BODY;
                stbody.body_date = _datagroup.getstBody(strUrl)->date;
                stbody.platformname_num = _datagroup.getstBody(strUrl)->platformname_num;
                m_listMap.append(stbody);
                _datagroup.addRealBodyCategory(strUrl, iterPos.key());
                if (streal.m_mapRealReplyUniqueUrl.contains(strUrl))
                {
                    stbody.realin |= REAL_REPLY;

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

                }
            }
        }
    }
}


void SPowercafe::SReplyPowercafeMap::makeTable(SDatagroup &_datagroup, const QMap<int, stReal> &_mapCategory)
{
    int num = 0;
    for (QMap<int, stReal>::const_iterator iterPos = _mapCategory.constBegin(); iterPos != _mapCategory.constEnd(); iterPos++)
    {
        const stReal& streal = iterPos.value();
        for (QMap<QString, int>::const_iterator iterPos1 = streal.m_mapRealReplyUniqueUrl.constBegin(); iterPos1 != streal.m_mapRealReplyUniqueUrl.end(); iterPos1++)
        {
            stReplyPowercafeMap streply;
            streply.num = num++;
            streply.body_num = _datagroup.getstBodyNum(iterPos1.key());
            streply.category_num = iterPos.key();
            streply.realin |= REAL_REPLY;
            streply.community_num = _datagroup.getstReply(iterPos1.key(), iterPos1.value())->community_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());
        }
    }
}
/*
bool SPowercafe::SBodyPowercafeMap::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "truncate body_powercafe_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_powercafe_map_" + QString::number(_nCompany) + "_update (num,category_num,community_num,body_num,realin)"
            " values (:num,:category_num,:community_num,:body_num,:realin)";
    query.clear();
    query.prepare(strQuery.toUtf8());

    foreach (const stBodyPowercafeMap& stmap, m_listMap)
    {
        query.bindValue(":num", stmap.num);
        query.bindValue(":category_num", stmap.category_num);
        query.bindValue(":community_num", stmap.community_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 SPowercafe::SBodyPowercafeMap::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery = "truncate body_powercafe_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_powercafe_map_" + QString::number(_nCompany)
            + "_update (num,category_num,community_num,body_num,realin,body_date,platformname_num)"
            " values (";
    query.clear();

    foreach (const stBodyPowercafeMap& stmap, m_listMap)
    {
        QString str = strQuery;
        str += QString::number(stmap.num) + ",";
        str += QString::number(stmap.category_num) + ",";
        str += QString::number(stmap.community_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;
}

/*
bool SPowercafe::SReplyPowercafeMap::uploadData(QSqlDatabase &_db, const int &_nCompany)
{

    QString strQuery = "truncate reply_powercafe_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_powercafe_map_" + QString::number(_nCompany) + "_update (num,category_num,community_num,body_num,reply_num,realin)"
            " values (:num,:category_num,:community_num,:body_num,:reply_num,:realin)";
    query.clear();
    query.prepare(strQuery.toUtf8());

    foreach (const stReplyPowercafeMap& stmap, m_listMap)
    {
        query.bindValue(":num", stmap.num);
        query.bindValue(":category_num", stmap.category_num);
        query.bindValue(":community_num", stmap.community_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 SPowercafe::SReplyPowercafeMap::uploadData(QSqlDatabase &_db, const int &_nCompany)
{

    QString strQuery = "truncate reply_powercafe_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_powercafe_map_" + QString::number(_nCompany)
            + "_update (num,category_num,community_num,body_num,reply_num,realin,body_date)"
            " values (";
    query.clear();

    foreach (const stReplyPowercafeMap& stmap, m_listMap)
    {
        QString str = strQuery;
        str += QString::number(stmap.num) + ",";
        str += QString::number(stmap.category_num) + ",";
        str += QString::number(stmap.community_num) + ",";
        str += QString::number(stmap.body_num) + ",";
        str += QString::number(stmap.reply_num) + ",";
        str += QString::number(stmap.realin) + ",";
        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 SPowercafe::SBodyPowercafeMap::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "body_powercafe_map_" + QString::number(_nCompany) + " TO " + "body_powercafe_map_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "body_powercafe_map_" + QString::number(_nCompany) + "_update" + " TO " + "body_powercafe_map_" + QString::number(_nCompany) + ",";
    strQuery += "body_powercafe_map_" + QString::number(_nCompany) + "_temp" + " TO " + "body_powercafe_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 SPowercafe::SReplyPowercafeMap::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "reply_powercafe_map_" + QString::number(_nCompany) + " TO " + "reply_powercafe_map_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "reply_powercafe_map_" + QString::number(_nCompany) + "_update" + " TO " + "reply_powercafe_map_" + QString::number(_nCompany) + ",";
    strQuery += "reply_powercafe_map_" + QString::number(_nCompany) + "_temp" + " TO " + "reply_powercafe_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;
}
