#include <QString>
#include <QStringList>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlError>
#include <QVariant>
#include <QDebug>
#include <QRegExp>
#include <QTextCodec>
#include <QtAlgorithms>
#include "sdatagroup.h"
#include "sinfluencer.h"
#include "sinitializer.h"

QString sqlString(QString _str)
{
    _str.replace("\\","\\\\");
    _str.replace("'","\\'");
    _str.replace("\"","\\\"");
    _str.replace(",","\\,");
    return _str;
}

void SDatagroup::setInitializer(SInitializer &initializer)
{
    m_pSInitializer = &initializer;
    m_SBody.setInitializer(initializer);
    m_SCommunity.setInitializer(initializer);
    m_SReply.setInitializer(initializer);
    m_SId.setInitializer(initializer);
    m_SNickname.setInitializer(initializer);
}

void SDatagroup::init()
{
    m_mapBody.clear();
    m_mapReply.clear();
    m_setFilteredUrl.clear();
    m_slOrderedBodyUrl.clear();
}

void SDatagroup::setloadQuery(QString _astrQuery[], int _nDatagroupNumber)
{
    QStringList slColumn = m_pSInitializer->getColumnStringList();
    QString strQuery("select ");
    foreach (const QString& str, slColumn)
        strQuery += (str + ",");
    strQuery = strQuery.left(strQuery.length() - 1);
    strQuery += " from data_";
    strQuery += QString::number(_nDatagroupNumber);
    _astrQuery[E_ARTICLE_BODY] = _astrQuery[E_ARTICLE_REPLY] = strQuery;

    _astrQuery[E_ARTICLE_REPLY] += " where article_form = 'reply'";
    _astrQuery[E_ARTICLE_BODY] += " where article_form = 'body'";
    _astrQuery[E_ARTICLE_BODY] += " and date(article_date) >= date('" + m_pSInitializer->getStartDay() + "')";
    _astrQuery[E_ARTICLE_BODY] += " and date(article_date) <= date('" + m_pSInitializer->getEndDay() + "')";
    _astrQuery[E_ARTICLE_BODY] += " Order by article_date DESC";
}


bool SDatagroup::loadData(const int& _nDatagroupNumber)
{
    init();
    QSqlQuery query(m_pSInitializer->getDatabase(SDBManager::E_DATABASE_DATA));
    const int *anColumn = m_pSInitializer->getColumnIntArray();

    QString astrWhere[E_ARTICLE_MAX];
    setloadQuery(astrWhere, _nDatagroupNumber);

    m_pSInitializer->insertLog("Start Loding data_" + QString::number(_nDatagroupNumber));

    QSet<QString> setCheckDupUrl;
    for (int i = 0; i < E_ARTICLE_MAX; i++)
    {
        if (query.exec(astrWhere[i].toUtf8()) == false)
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }

        while (query.next())
        {
            QStringList strList;
            for (int j = 0; j < m_pSInitializer->getColumnStringList().size(); j++)
                strList.append(" " + query.value(j).toString() + " ");

            /*
            if(strList[anColumn[SInitializer::E_DATA_platform_form]].trimmed() == "post" ||
                    strList[anColumn[SInitializer::E_DATA_platform_form]].trimmed() == "story" ||
                    strList[anColumn[SInitializer::E_DATA_platform_form]].trimmed() == "channel")
                strList[anColumn[SInitializer::E_DATA_platform_form]] = " sns ";

            if(strList[anColumn[SInitializer::E_DATA_platform_form]].trimmed() == "group")
                strList[anColumn[SInitializer::E_DATA_platform_form]] = " community ";
            */
            switch(i)
            {
            case E_ARTICLE_BODY:
            {
                if (setCheckDupUrl.contains(strList[anColumn[SInitializer::E_DATA_article_url]].trimmed()))
                {
                    continue;
                }
                if (strList[anColumn[SInitializer::E_DATA_article_id]].trimmed().isEmpty() && strList[anColumn[SInitializer::E_DATA_platform_form]].trimmed() == "news")
                {
                    strList[anColumn[SInitializer::E_DATA_article_id]] = strList[anColumn[SInitializer::E_DATA_platform_title]];
                    strList[anColumn[SInitializer::E_DATA_article_nickname]] = strList[anColumn[SInitializer::E_DATA_platform_title]];
                }
                if (strList[anColumn[SInitializer::E_DATA_article_date]].trimmed().isEmpty()) continue;
                if (strList[anColumn[SInitializer::E_DATA_article_date]].trimmed() == "0000-00-00 00:00:00")
                    strList[anColumn[SInitializer::E_DATA_article_date]] = "1990-01-01 00:00:00";
                if (strList[anColumn[SInitializer::E_DATA_article_id]].trimmed().isEmpty()) continue;
                if (strList[anColumn[SInitializer::E_DATA_article_nickname]].trimmed().isEmpty()) continue;
                if (strList[anColumn[SInitializer::E_DATA_platform_id]].trimmed().isEmpty()) continue;
                if (strList[anColumn[SInitializer::E_DATA_platform_id]].trimmed().isEmpty()) continue;
                setCheckDupUrl.insert(strList[anColumn[SInitializer::E_DATA_article_url]].trimmed());
                m_slOrderedBodyUrl.append(strList[anColumn[SInitializer::E_DATA_article_url]].trimmed());
                m_mapBody.insert(strList[anColumn[SInitializer::E_DATA_article_url]].trimmed(), strList);
                //profiles
                makeProfile(strList);

            }break;

            case E_ARTICLE_REPLY:
            {
                if (!setCheckDupUrl.contains(strList[anColumn[SInitializer::E_DATA_article_url]].trimmed()))
                    continue;
                if (strList[anColumn[SInitializer::E_DATA_article_id]].trimmed().isEmpty()) continue;
                if (strList[anColumn[SInitializer::E_DATA_article_nickname]].trimmed().isEmpty()) continue;
                if (strList[anColumn[SInitializer::E_DATA_article_date]].trimmed() == "0000-00-00 00:00:00" || strList[anColumn[SInitializer::E_DATA_article_date]].trimmed().isEmpty())
                    strList[anColumn[SInitializer::E_DATA_article_date]] = "1990-01-01 00:00:00";
                QString strKey = strList[anColumn[SInitializer::E_DATA_article_url]].trimmed();
                int nKey = strList[anColumn[SInitializer::E_DATA_article_order]].trimmed().toInt();

                if (m_mapReply.contains(strKey))
                {
                    if (!m_mapReply[strKey].contains(nKey))
                    {
                        m_mapReply[strKey].insert(nKey, strList);
                    }
                }
                else
                {
                    QMap<int, QStringList> reply;
                    reply.insert(nKey, strList);
                    m_mapReply.insert(strKey, reply);
                }
            }break;
            }
        }
        query.clear();
    }

    int ncReply = 0;
    for (QMap<QString, QMap<int, QStringList> >::iterator iterPos1 = m_mapReply.begin(); iterPos1 != m_mapReply.end(); iterPos1++)
    {
        for (QMap<int, QStringList>::iterator iterPos2 = iterPos1.value().begin(); iterPos2 != iterPos1.value().end(); iterPos2++)
        {
            ncReply++;
        }
    }
    qDebug() << "body size = " << m_mapBody.size();
    qDebug() << "reply size = " << ncReply;

    m_stBodyReplyCount.m_nAllBodyCount = m_mapBody.size();
    m_stBodyReplyCount.m_nAllReplyCount = ncReply;
    m_pSInitializer->insertLog("End Loading");
    return true;
}


void SDatagroup::makeProfile(const QStringList& _strList)
{
    const int *anColumn = m_pSInitializer->getColumnIntArray();
    stProfileurlKey stprofileurlkey(m_pSInitializer->getPlatformForm(
                                        _strList[anColumn[SInitializer::E_DATA_platform_form]].trimmed()),
            _strList[anColumn[SInitializer::E_DATA_article_id]].trimmed());
    if (m_mapProfile.contains(stprofileurlkey))
    {
        if (!(*(m_mapProfile[stprofileurlkey]))[SInitializer::E_DATA_article_profileurl].trimmed().isEmpty())
        {
            if ((*(m_mapProfile[stprofileurlkey]))[SInitializer::E_DATA_article_date].trimmed() < _strList[SInitializer::E_DATA_article_date].trimmed())
            {
                m_mapProfile[stprofileurlkey] = &(m_mapBody[_strList[anColumn[SInitializer::E_DATA_article_url]].trimmed()]);
            }
        }
    }
    else
    {
        m_mapProfile.insert(stprofileurlkey, &(m_mapBody[_strList[anColumn[SInitializer::E_DATA_article_url]].trimmed()]));
    }
}

void SDatagroup::addRealBodyCategory(const QString& _strUrl, const int _nCategory)
{
    m_SBody.putCategory(_strUrl, _nCategory);
}

void SDatagroup::addRealReplyCategory(const QString& _strUrl, const int& _order, int _nCategory)
{
    m_SReply.putCategory(_strUrl, _order, _nCategory);
}


void SDatagroup::addRealUrl(const QString& _strUrl)
{
    m_setFilteredUrl.insert(_strUrl.trimmed());
}


bool SDatagroup::uploadData(const int &_nCompany)
{
    m_pSInitializer->insertLog("Start upload data");
    m_pSInitializer->insertLog("Start upload body data");

    QSqlDatabase dbWeb = m_pSInitializer->getDatabase(SDBManager::E_DATABASE_WEB);
    m_pSInitializer->updateWebDBInfoState(dbWeb, _nCompany, "Datagoup (Start)");

    if (!m_SBody.uploadData(dbWeb, _nCompany, m_slOrderedBodyUrl))
    {
        m_pSInitializer->insertLog("Change body_" + QString::number(_nCompany) + " fail");
        return false;
    }
    m_pSInitializer->insertLog("Start upload reply data");
    if (!m_SReply.uploadData(dbWeb, _nCompany, m_slOrderedBodyUrl))
    {
        m_pSInitializer->insertLog("upload reply_" + QString::number(_nCompany) + " fail");
        return false;
    }

    m_pSInitializer->insertLog("Start upload id data");

    if (!m_SId.uploadData(dbWeb, _nCompany))
    {
        m_pSInitializer->insertLog("upload id_" + QString::number(_nCompany) + " fail");
        return false;
    }
    m_pSInitializer->insertLog("Start upload nickname data");

    if (!m_SNickname.uploadData(dbWeb, _nCompany))
    {
        m_pSInitializer->insertLog("upload nickname_" + QString::number(_nCompany) + " fail");
        return false;
    }
    m_pSInitializer->insertLog("Start upload community data");

    if (!m_SCommunity.uploadData(dbWeb, _nCompany))
    {
        m_pSInitializer->insertLog("upload community_" + QString::number(_nCompany) + " fail");
        return false;
    }
    m_pSInitializer->insertLog("Success upload data");
    m_pSInitializer->updateWebDBInfoState(dbWeb, _nCompany, "Datagoup (End)");
    return true;
}

bool SDatagroup::makeTable()
{
    QRegExp rxHttp("http[s]?://");
    m_pSInitializer->insertLog("Start Datagroup makeTable");
    m_pSInitializer->insertLog("#ofALL/#ofFiltered :" + QString::number(m_mapBody.size()) + "/" + QString::number(m_setFilteredUrl.size()) );
    const int* anColumn = m_pSInitializer->getColumnIntArray();
    int nBody = 0;
    int nId = 0;
    int nNickname = 0;
    int nCommunity = 0;
    int nReply = 0;

    {
        stCommunity stcommunity;
        stcommunity.num = nCommunity++;
        m_SCommunity.put(" ", stcommunity);

        stId stid;
        stid.num = nId++;
        m_SId.put("(none)", stid);
    }

    foreach (const QString& strUrl, m_slOrderedBodyUrl)
    {
        if (m_setFilteredUrl.contains(strUrl))
        {
            QStringList &strListBody = m_mapBody[strUrl];
            QMap<int, QStringList> &mapReply = m_mapReply[strUrl];

            // community
            if (!m_SCommunity.contains(m_SCommunity.getCommunityKey(strListBody).trimmed()))
            {
                stCommunity stcommunity;
                stcommunity.num = nCommunity++;
                stcommunity.platformform_num = m_pSInitializer->getPlatformForm(strListBody[anColumn[SInitializer::E_DATA_platform_form]].trimmed());
                stcommunity.platformname_num = m_pSInitializer->getPlatformName(strListBody[anColumn[SInitializer::E_DATA_platform_name]].trimmed());
                stcommunity.title = strListBody[anColumn[SInitializer::E_DATA_platform_title]].trimmed();
                stcommunity.id = strListBody[anColumn[SInitializer::E_DATA_platform_id]].trimmed();
                stcommunity.url = m_SCommunity.getCommunityKey(strListBody).trimmed();
                if(strListBody[anColumn[SInitializer::E_DATA_platform_name]].trimmed() == QString("daum") && strListBody[anColumn[SInitializer::E_DATA_platform_form]].trimmed() == QString("cafe"))
                        stcommunity.exid = strListBody[anColumn[SInitializer::E_DATA_etc]].trimmed();
                m_SCommunity.put(m_SCommunity.getCommunityKey(strListBody).trimmed(), stcommunity);
            }

            //m_SReply

            if (!m_SId.contains(m_SId.getIdKey(strListBody)))
            {
                stId stid;
                stid.num = nId++;
                stid.community_num = 0;//m_SCommunity.get(m_SCommunity.getCommunityKey(strListBody).trimmed())->num;
                stid.platformname_num = m_pSInitializer->getPlatformName(strListBody[anColumn[SInitializer::E_DATA_platform_name]].trimmed());
                stid.id = strListBody[anColumn[SInitializer::E_DATA_article_id]].trimmed();
                m_SId.put(strListBody[anColumn[SInitializer::E_DATA_article_id]].trimmed(), stid);
            }

            if (!m_SNickname.contains(strListBody[anColumn[SInitializer::E_DATA_article_nickname]].trimmed()))
            {
                stNickname stnickname;
                stnickname.nickname = strListBody[anColumn[SInitializer::E_DATA_article_nickname]];
                stnickname.num = nNickname++;
                m_SNickname.put(strListBody[anColumn[SInitializer::E_DATA_article_nickname]].trimmed(), stnickname);
            }

            QString lastupdate = strListBody[anColumn[SInitializer::E_DATA_article_date]].trimmed();
            if (mapReply.size() > 0)
            {
                for(QMap<int, QStringList>::iterator iterPos = mapReply.begin(); iterPos != mapReply.end(); iterPos++)
                {
                    QStringList &strListReply = iterPos.value();

                    lastupdate = ((strListReply[anColumn[SInitializer::E_DATA_article_date]].trimmed() > lastupdate)?
                                strListReply[anColumn[SInitializer::E_DATA_article_date]].trimmed() : lastupdate);

                    m_stDate.m_strLastupdate = (( m_stDate.m_strLastupdate > lastupdate ) ? m_stDate.m_strLastupdate : lastupdate);

                    if (!m_SId.contains(strListReply[anColumn[SInitializer::E_DATA_article_id]].trimmed()))
                    {
                        stId stid;
                        stid.num = nId++;
                        stid.community_num = 0;//m_SCommunity.get(m_SCommunity.getCommunityKey(strListBody).trimmed())->num;
                        stid.platformname_num = m_pSInitializer->getPlatformName(strListReply[anColumn[SInitializer::E_DATA_platform_name]].trimmed());
                        stid.id = strListReply[anColumn[SInitializer::E_DATA_article_id]].trimmed();
                        m_SId.put(strListReply[anColumn[SInitializer::E_DATA_article_id]].trimmed(), stid);
                    }

                    if (!m_SNickname.contains(strListReply[anColumn[SInitializer::E_DATA_article_nickname]].trimmed()))
                    {
                        stNickname stnickname;
                        stnickname.nickname = strListReply[anColumn[SInitializer::E_DATA_article_nickname]];
                        stnickname.num = nNickname++;
                        m_SNickname.put(strListReply[anColumn[SInitializer::E_DATA_article_nickname]].trimmed(), stnickname);
                    }
                }
            }
            {
                if (m_SId.contains(m_SId.getIdKey(strListBody)) && m_SNickname.contains(strListBody[anColumn[SInitializer::E_DATA_article_nickname]].trimmed()))
                    m_SNickname.get(strListBody[anColumn[SInitializer::E_DATA_article_nickname]].trimmed())->id_num =
                            m_SId.get(m_SId.getIdKey(strListBody))->num;
            }

            // m_SBody
            {
                stBody stbody;
                stbody.num = nBody++;
                stbody.data = &(strListBody[anColumn[SInitializer::E_DATA_article_data]]);
                stbody.title = &(strListBody[anColumn[SInitializer::E_DATA_article_title]]);
                stbody.replycount = mapReply.size();
                stbody.id_id = strListBody[anColumn[SInitializer::E_DATA_article_id]].trimmed();
                stbody.date = strListBody[anColumn[SInitializer::E_DATA_article_date]].trimmed();
                stbody.lastupdate = lastupdate;
                stbody.view = ( (strListBody[anColumn[SInitializer::E_DATA_article_hit]].trimmed().isEmpty()) ? -1 :
                                 strListBody[anColumn[SInitializer::E_DATA_article_hit]].trimmed().toInt());
                stbody.platformform_num = m_pSInitializer->getPlatformForm(strListBody[anColumn[SInitializer::E_DATA_platform_form]].trimmed());
                stbody.platformname_num = m_pSInitializer->getPlatformName(strListBody[anColumn[SInitializer::E_DATA_platform_name]].trimmed());
                stbody.url = strListBody[anColumn[SInitializer::E_DATA_article_url]].trimmed().replace(rxHttp, "");
                stbody.id_num = m_SId.get(m_SId.getIdKey(strListBody))->num;
                stbody.community_num = m_SCommunity.get(m_SCommunity.getCommunityKey(strListBody))->num;
                stbody.nickname_num = m_SNickname.get(strListBody[anColumn[SInitializer::E_DATA_article_nickname]].trimmed())->num;
                stbody.replystartnum = ((mapReply.size() > 0) ? nReply : -1);
                stbody.replyendnum = ((mapReply.size() > 0) ? nReply + mapReply.size() - 1 : -1);

                stbody.community_title = m_SCommunity.get(m_SCommunity.getCommunityKey(strListBody))->title;
                stbody.community_url = m_SCommunity.get(m_SCommunity.getCommunityKey(strListBody))->url;
                stbody.community_id = m_SCommunity.get(m_SCommunity.getCommunityKey(strListBody))->id;
                stbody.community_exid = m_SCommunity.get(m_SCommunity.getCommunityKey(strListBody))->exid;
                stbody.platformform_form = strListBody[anColumn[SInitializer::E_DATA_platform_form]].trimmed();
                stbody.platformname_name = strListBody[anColumn[SInitializer::E_DATA_platform_name]].trimmed();
                stbody.nickname_nickname =  m_SNickname.get(strListBody[anColumn[SInitializer::E_DATA_article_nickname]].trimmed())->nickname;
                stbody.nickname_exnickname = UniCodeToEucKr(m_SNickname.get(strListBody[anColumn[SInitializer::E_DATA_article_nickname]].trimmed())->nickname);

                m_SBody.put(strUrl, stbody);

                m_stDate.m_strMaxdate = (( m_stDate.m_strMaxdate > stbody.date ) ? m_stDate.m_strMaxdate : stbody.date );
                m_stDate.m_strMindate = (( m_stDate.m_strMindate < stbody.date ) ? m_stDate.m_strMindate : stbody.date );
            }
            // m_SReply
            if (mapReply.size() > 0)
            {
                for(QMap<int, QStringList>::iterator iterPos = mapReply.begin(); iterPos != mapReply.end(); iterPos++)
                {
                    QStringList &strListReply = iterPos.value();
                    stReply streply;
                    streply.num = nReply++;
                    streply.body_num = m_SBody.getBodyNum(strUrl);
                    streply.id_id = strListReply[anColumn[SInitializer::E_DATA_article_id]].trimmed();
                    streply.id_num = m_SId.get(m_SId.getIdKey(strListReply))->num;
                    streply.nickname_num = m_SNickname.get(strListReply[anColumn[SInitializer::E_DATA_article_nickname]].trimmed())->num;
                    streply.community_num = m_SCommunity.get(m_SCommunity.getCommunityKey(strListBody))->num;
                    streply.data = &(strListReply[anColumn[SInitializer::E_DATA_article_data]]);
                    streply.date = strListReply[anColumn[SInitializer::E_DATA_article_date]].trimmed();
                    streply.parent = strListReply[anColumn[SInitializer::E_DATA_article_parent]].trimmed();

                    streply.nickname_nickname = m_SNickname.get(strListReply[anColumn[SInitializer::E_DATA_article_nickname]].trimmed())->nickname;
                    streply.nickname_exnickname = m_SNickname.get(strListReply[anColumn[SInitializer::E_DATA_article_nickname]].trimmed())->nickname;
                    streply.platformform_num = m_pSInitializer->getPlatformForm(strListReply[anColumn[SInitializer::E_DATA_platform_form]].trimmed());
                    streply.platformform_form = strListReply[anColumn[SInitializer::E_DATA_platform_form]].trimmed();
                    streply.platformname_num = m_pSInitializer->getPlatformName(strListReply[anColumn[SInitializer::E_DATA_platform_name]].trimmed());
                    streply.platformname_name = strListReply[anColumn[SInitializer::E_DATA_platform_name]].trimmed();
                    streply.body_date = m_SBody.get(strUrl)->date;

                    if (streply.parent.isEmpty())
                        streply.parent = " ";
                    if (m_SId.contains(m_SId.getIdKey(strListReply)) && m_SNickname.contains(strListReply[anColumn[SInitializer::E_DATA_article_nickname]].trimmed()))
                        m_SNickname.get(strListReply[anColumn[SInitializer::E_DATA_article_nickname]].trimmed())->id_num =
                                m_SId.get(m_SId.getIdKey(strListReply))->num;
                    m_SReply.put(strUrl, iterPos.key(), streply);
                }
            }
        }
    }
    m_pSInitializer->insertLog("End makeTable");
    m_stBodyReplyCount.m_nFilterBodyCount = nBody;
    m_stBodyReplyCount.m_nFilterReplyCount = nReply;
    return true;
}

const SDatagroup::stDate* SDatagroup::getstDate()
{
    return &m_stDate;
}

const SDatagroup::stBodyReplyCount* SDatagroup::getCounts()
{
    return &m_stBodyReplyCount;
}

const QStringList* SDatagroup::getmapBody(const stProfileurlKey& _stprofileurlkey)
{
    return m_mapProfile[_stprofileurlkey];
}

bool SDatagroup::changeTable(const int &_nCompany)
{
    m_pSInitializer->insertLog("Start change tables");
    QSqlDatabase dbWeb = m_pSInitializer->getDatabase(SDBManager::E_DATABASE_WEB);
    if (!m_SBody.changeTable(dbWeb, _nCompany))
    {
        m_pSInitializer->insertLog("Change body_" + QString::number(_nCompany) + " fail");
        return false;
    }

    if (!m_SReply.changeTable(dbWeb, _nCompany))
    {
        m_pSInitializer->insertLog("Change reply_" + QString::number(_nCompany) + " fail");
        return false;
    }

    if (!m_SId.changeTable(dbWeb, _nCompany))
    {
        m_pSInitializer->insertLog("Change id_" + QString::number(_nCompany) + " fail");
        return false;
    }

    if (!m_SNickname.changeTable(dbWeb, _nCompany))
    {
        m_pSInitializer->insertLog("Change nickname_" + QString::number(_nCompany) + " fail");
        return false;
    }

    if (!m_SCommunity.changeTable(dbWeb, _nCompany))
    {
        m_pSInitializer->insertLog("Change community_" + QString::number(_nCompany) + " fail");
        return false;
    }
    m_pSInitializer->insertLog("Success change tables");
    return true;
}

void SDatagroup::copyData(QVector<QStringList> *_data)
{
    for(QMap<QString, QStringList>::const_iterator iterPos = m_mapBody.begin(); iterPos != m_mapBody.end(); iterPos++)
        _data[SInitializer::E_ARTICLE_BODY].append(iterPos.value());

    for(QMap<QString, QMap<int, QStringList> >::const_iterator iterPos1 = m_mapReply.begin(); iterPos1 != m_mapReply.end(); iterPos1++)
    {
        for(QMap<int, QStringList>::const_iterator iterPos2 = iterPos1.value().begin(); iterPos2 != iterPos1.value().end(); iterPos2++)
        {
            _data[SInitializer::E_ARTICLE_REPLY].append(iterPos2.value());
        }
    }

}

const SDatagroup::stCommunity* SDatagroup::getstCommunity(const QStringList& _sl)
{
    return m_SCommunity.get(m_SCommunity.getCommunityKey(_sl));
}

const QStringList* SDatagroup::getOrderedUrl()
{
    return &m_slOrderedBodyUrl;
}


SDatagroup::stBody* SDatagroup::SBody::get(const QString &_url)
{
    if (m_mapBody.contains(_url))
        return &(m_mapBody[_url]);
    else
        return 0;
}

const SDatagroup::stBody* SDatagroup::getstBody(const QString& _strUrl)
{
    return m_SBody.get(_strUrl);
}

const SDatagroup::stReply* SDatagroup::getstReply(const QString& _strUrl, const int &_order)
{
    return m_SReply.get(_strUrl, _order);
}

const QMap<int, SDatagroup::stReply>* SDatagroup::getstReply(const QString& _strUrl)
{
    return m_SReply.get(_strUrl);
}

int SDatagroup::getstBodyNum(const QString& _strUrl)
{
    return m_SBody.getBodyNum(_strUrl);
}
int SDatagroup::getstReplyNum(const QString& _strUrl, const int &_order)
{
    return m_SReply.getReplyNum(_strUrl, _order);
}

const QMap<QString, QStringList>* SDatagroup::getmapBody()
{
    return &m_mapBody;
}

const QMap<QString, QMap<int, QStringList> >* SDatagroup::getmapReply()
{
    return &m_mapReply;
}


bool SDatagroup::SBody::putCategory(const QString &_url, const int &_nCategory)
{
    if (m_mapBody.contains(_url.trimmed()))
    {
        m_mapBody[_url.trimmed()].realbycategorynum.append(_nCategory);
        return true;
    }
    else
        return false;
}

void SDatagroup::SBody::put(const QString &_url, const stBody &_stbody)
{
    m_mapBody.insert(_url.trimmed(), _stbody);
}

void SDatagroup::SBody::clear()
{
    m_mapBody.clear();
}

QString SDatagroup::SBody::vecToString(const QVector<int>& _realbycategorynum)
{
    QString str = "";
    QList<int> ilist = _realbycategorynum.toList();
    qSort(ilist.begin(), ilist.end(), qLess<int>());
    QList<int>::iterator it = std::unique(ilist.begin(), ilist.end());
    int totalsize = std::distance(ilist.begin(), it);

    for (int i = 0; i < totalsize; i++)
        str += ((QString::number(ilist.at(i))) + " ");

    return str.trimmed();
}
QString SDatagroup::SBody::vecToString(const QSet<int>& _realbycategorynum)
{
    QString str = "";
    QList<int> nlist = _realbycategorynum.toList();
    qSort(nlist.begin(), nlist.end(), qLess<int>());
    foreach (int real, nlist)
        str += (QString::number(real) + " ");
    return str.trimmed();
}

/*
bool SDatagroup::SBody::uploadData(QSqlDatabase &_db, const int& _nCompany, const QStringList &_order)
{
    QString strQuery = "truncate body_" + 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 = QString("insert into body_" + QString::number(_nCompany) +
                       "_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();
    query.prepare(strQuery);

    foreach (const QString& url, _order)
    {
        if (m_mapBody.contains(url.trimmed()))
        {
            stBody &body = m_mapBody[url.trimmed()];
            query.bindValue(":NUM", body.num);
            query.bindValue(":ID_NUM", body.id_num);
            query.bindValue(":NICKNAME_NUM", body.nickname_num);
            query.bindValue(":COMMUNITY_NUM", body.community_num);
            query.bindValue(":ID_ID", body.id_id);
            query.bindValue(":PLATFORMNAME_NUM", body.platformname_num);
            query.bindValue(":PLATFORMFORM_NUM", body.platformform_num);
            query.bindValue(":TITLE", sqlString(*(body.title)).replace(QChar(160),""));
            query.bindValue(":DATA", sqlString(*(body.data)).replace(QRegExp("\n{3,}"),"\n\n").replace(QChar(160),"").left(65535));
            query.bindValue(":URL", body.url);
            query.bindValue(":VIEW", body.view);
            query.bindValue(":DATE", body.date);
            query.bindValue(":REPLY_STARTNUM", body.reply_startnum);
            query.bindValue(":REPLY_ENDNUM", body.reply_endnum);
            query.bindValue(":REPLY_COUNT", body.reply_count);
            query.bindValue(":LASTUPDATE", body.lastupdate);
            query.bindValue(":REALBYCATEGORYNUM", vecToString(body.realbycategorynum));
            if (!query.exec())
            {
                m_pSInitializer->insertLog(query.lastQuery());
                m_pSInitializer->insertLog(query.lastError().text());

                return false;
            }
        }
    }
    return true;
}
*/

bool SDatagroup::SBody::uploadData(QSqlDatabase &_db, const int& _nCompany, const QStringList &_order)
{
    QString strQuery = "truncate body_" + 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_" + QString::number(_nCompany) +
            "_update(num,id_num,nickname_num,community_num,id_id,platformname_num,platformform_num, title, data, url, view, date, replystartnum, replyendnum, replycount, lastupdate, realbycategorynum,"
            "community_title, community_url, community_id, community_exid, platformname_name, platformform_form, nickname_nickname, nickname_exnickname) VALUES(";

    foreach (const QString& url, _order)
    {
        if (m_mapBody.contains(url.trimmed()))
        {
            stBody &body = m_mapBody[url.trimmed()];
            QString str = strQuery;
            str += QString::number(body.num) + ",";
            str += QString::number(body.id_num) + ",";
            str += QString::number(body.nickname_num) + ",";
            str += QString::number(body.community_num) + ",";
            str += "'" + sqlString(body.id_id) + "',";
            str += QString::number(body.platformname_num) + ",";
            str += QString::number(body.platformform_num) + ",";
            str += "'" + sqlString(*(body.title)).replace(QChar(160),"") + "',";
            str += "'" + sqlString((*(body.data)).replace(QRegExp("\n{3,}"),"\n\n").replace(QChar(160),"").left(60000)) + "',";
            str += "'" + body.url + "',";
            str += QString::number(body.view) + ",";
            str += "'" + body.date + "',";
            str += QString::number(body.replystartnum) + ",";
            str += QString::number(body.replyendnum) + ",";
            str += QString::number(body.replycount) + ",";
            str += "'" + body.lastupdate + "',";
            str += "'" + vecToString(body.realbycategorynum) + "',";


            str += "'" + sqlString(body.community_title) + "',";
            str += "'" + body.community_url + "',";
            str += "'" + body.community_id + "',";
            str += "'" + sqlString(body.community_exid) + "',";
            str += "'" + body.platformname_name + "',";
            str += "'" + body.platformform_form + "',";
            str += "'" + sqlString(body.nickname_nickname) + "',";
            str += "'" + sqlString(body.nickname_exnickname) + "')";

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


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

int SDatagroup::SBody::getBodyNum(const QString &_url)
{
    if (m_mapBody.contains(_url))
        return m_mapBody[_url].num;
    else
        return -1;
}


void SDatagroup::SReply::put(const QString &_url, const int &_order, const stReply &_streply)
{

    if (m_mapReply.contains(_url.trimmed()))
    {
        if (!m_mapReply[_url.trimmed()].contains(_order))
        {
            m_mapReply[_url.trimmed()].insert(_order, _streply);
        }
    }
    else
    {
        QMap<int, stReply> streply;
        streply.insert(_order, _streply);
        m_mapReply.insert(_url.trimmed(), streply);
    }
}

bool SDatagroup::SReply::putCategory(const QString &_url, const int &_order, int &_nCategory)
{
    if (m_mapReply.contains(_url.trimmed()) && m_mapReply[_url.trimmed()].contains(_order))
    {
        m_mapReply[_url.trimmed()][_order].realbycategorynum.push_back(_nCategory);
        return true;
    }
    else
    {
        return false;
    }
}

QMap<int, SDatagroup::stReply>* SDatagroup::SReply::get(const QString& _url)
{
    if (m_mapReply.contains(_url.trimmed()))
        return &(m_mapReply[_url.trimmed()]);
    else
        return 0;
}

void SDatagroup::SReply::clear()
{
    m_mapReply.clear();
}

int SDatagroup::SReply::getReplyNum(const QString &_url, const int &_order)
{
    if (m_mapReply.contains(_url.trimmed()) && m_mapReply[_url.trimmed()].contains(_order))
        return m_mapReply[_url.trimmed()][_order].num;
    else
        return -1;
}

SDatagroup::stReply* SDatagroup::SReply::get(const QString &_url, const int &_order)
{
    if (m_mapReply.contains(_url.trimmed()) && m_mapReply[_url.trimmed()].contains(_order))
        return &(m_mapReply[_url.trimmed()][_order]);
    else
        return 0;
}

/*
bool SDatagroup::SReply::uploadData(QSqlDatabase &_db, const int &_nCompany, QStringList &_order)
{
    QString strQuery = "truncate reply_" + 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 = QString("insert into reply_" + QString::number(_nCompany) + "_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();
    query.prepare(strQuery);

    foreach (const QString& url, _order)
    {
        if (m_mapReply.contains(url))
        {
            foreach (const stReply& reply, m_mapReply[url].values())
            {
                query.bindValue(":NUM", reply.num);
                query.bindValue(":ID_NUM", reply.id_num);
                query.bindValue(":NICKNAME_NUM", reply.nickname_num);
                query.bindValue(":COMMUNITY_NUM", reply.community_num);
                query.bindValue(":ID_ID", reply.id_id);
                query.bindValue(":PARENT", reply.parent);
                query.bindValue(":DATA", sqlString(*(reply.data)).replace(QRegExp("\n{3,}"),"\n\n").replace(QChar(160),""));
                query.bindValue(":DATE", reply.date);
                query.bindValue(":BODY_NUM", reply.body_num);
                query.bindValue(":REALBYCATEGORYNUM", vecToString(reply.realbycategorynum));
                if (!query.exec())
                {
                    m_pSInitializer->insertLog(query.lastQuery());
                    m_pSInitializer->insertLog(query.lastError().text());

                    return false;
                }
            }
        }
    }
    return true;
}
*/

bool SDatagroup::SReply::uploadData(QSqlDatabase &_db, const int &_nCompany, QStringList &_order)
{
    QString strQuery = "truncate reply_" + 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_" + QString::number(_nCompany) +
            "_update (num,id_num,nickname_num,community_num,id_id,parent,data,date,body_num,realbycategorynum,"
            "nickname_nickname,nickname_exnickname,platformname_num,platformname_name,platformform_num,platformform_form,body_date) VALUES (";

    foreach (const QString& url, _order)
    {
        if (m_mapReply.contains(url))
        {
            foreach (const stReply& reply, m_mapReply[url].values())
            {
                QString str = strQuery;
                str += QString::number(reply.num) + ",";
                str += QString::number(reply.id_num) + ",";
                str += QString::number(reply.nickname_num) + ",";
                str += QString::number(reply.community_num) + ",";
                str += "'" + sqlString(reply.id_id) + "',";
                str += "'" + sqlString(reply.parent) + "',";
                str += "'" + sqlString((*(reply.data)).replace(QRegExp("\n{3,}"),"\n\n").replace(QChar(160),"").left(60000)) + "',";
                str += "'" + reply.date + "',";
                str += QString::number(reply.body_num) + ",";
                str += "'" + vecToString(reply.realbycategorynum) + "',";
                str += "'" + sqlString(reply.nickname_nickname) + "',";
                str += "'" + sqlString(reply.nickname_exnickname) + "',";
                str += QString::number(reply.platformname_num) + ",";
                str += "'" + reply.platformname_name + "',";
                str += QString::number(reply.platformform_num) + ",";
                str += "'" + reply.platformform_form + "',";
                str += "'" + reply.body_date + "')";

                if (!query.exec(str.toUtf8()))
                {
                    m_pSInitializer->insertLog(query.lastQuery());
                    m_pSInitializer->insertLog(query.lastError().text());

                    return false;
                }
            }
        }
    }
    return true;
}


QString SDatagroup::SReply::vecToString(const QVector<int>& _realbycategorynum)
{
    /*
    QString str = "";
    foreach (int real, _realbycategorynum)
        str += (QString::number(real) + " ");
    return str.trimmed();
    */
    QString str = "";
    QList<int> ilist = _realbycategorynum.toList();
    qSort(ilist.begin(), ilist.end(), qLess<int>());
    QList<int>::iterator it = std::unique(ilist.begin(), ilist.end());
    int totalsize = std::distance(ilist.begin(), it);

    for (int i = 0; i < totalsize; i++)
        str += ((QString::number(ilist.at(i))) + " ");

    return str.trimmed();
}


bool SDatagroup::SReply::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "reply_" + QString::number(_nCompany) + " TO " + "reply_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "reply_" + QString::number(_nCompany) + "_update" + " TO " + "reply_" + QString::number(_nCompany) + ",";
    strQuery += "reply_" + QString::number(_nCompany) + "_temp" + " TO " + "reply_" + 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 SDatagroup::SId::contains(const QString &_strKey)
{
    return m_mapId.contains(_strKey.trimmed());
}

void SDatagroup::SId::put(const QString &_strKey, const stId &_stid)
{
    m_mapId.insert(_strKey.trimmed(), _stid);
}

void SDatagroup::SId::put(const stId &_stid)
{
    m_mapId.insert(_stid.id, _stid);
}

QString SDatagroup::SId::getIdKey(const QStringList &_strlist)
{
    return _strlist[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_article_id]].trimmed();
}

SDatagroup::stId* SDatagroup::SId::get(const QString &_strKey)
{
    if (m_mapId.contains(_strKey.trimmed()))
        return &(m_mapId[_strKey.trimmed()]);
    else
        return 0;
}

bool SDatagroup::SId::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "id_" + QString::number(_nCompany) + " TO " + "id_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "id_" + QString::number(_nCompany) + "_update" + " TO " + "id_" + QString::number(_nCompany) + ",";
    strQuery += "id_" + QString::number(_nCompany) + "_temp" + " TO " + "id_" + 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 SDatagroup::SId::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;

    strQuery = "delete from id_" + 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 = QString("insert into id_" + QString::number(_nCompany) + "_update (num,id,platformname_num,community_num) values (:NUM,:ID,:PLATFORMNAME_NUM,:COMMUNITY_NUM)").toUtf8();
    query.prepare(strQuery);

    foreach (const stId& _ids, m_mapId.values())
    {
        query.bindValue(":NUM", _ids.num);
        query.bindValue(":ID", _ids.id);
        query.bindValue(":PLATFORMNAME_NUM", _ids.platformname_num);
        query.bindValue(":COMMUNITY_NUM", _ids.platformname_num);
        if (!query.exec())
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }

    return true;
}
*/


bool SDatagroup::SId::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;

    strQuery = "delete from id_" + 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 = QString("insert into id_" + QString::number(_nCompany) + "_update (num,id,platformname_num,community_num) values (");

    foreach (const stId& _ids, m_mapId.values())
    {
        QString str = strQuery;
        str += QString::number(_ids.num) + ",";
        str += "'" + _ids.id + "',";
        str += QString::number(_ids.platformname_num) + ",";
        str += QString::number(_ids.community_num) + ")";

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

    return true;
}



void SDatagroup::SId::clear()
{
    m_mapId.clear();
}

SDatagroup::stNickname* SDatagroup::SNickname::get(const QString &_strKey)
{
    if (m_mapNickname.contains(_strKey.trimmed()))
        return &(m_mapNickname[_strKey.trimmed()]);
    else
        return 0;
}


void SDatagroup::SNickname::put(const QString &_strKey, const stNickname &_stnickname)
{
    m_mapNickname.insert(_strKey.trimmed(), _stnickname);
}

void SDatagroup::SNickname::put(const stNickname &_nickname)
{
    m_mapNickname.insert(_nickname.nickname, _nickname);
}

bool SDatagroup::SNickname::contains(const QString &_strKey)
{
    return m_mapNickname.contains(_strKey.trimmed());
}

bool SDatagroup::SNickname::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "nickname_" + QString::number(_nCompany) + " TO " + "nickname_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "nickname_" + QString::number(_nCompany) + "_update" + " TO " + "nickname_" + QString::number(_nCompany) + ",";
    strQuery += "nickname_" + QString::number(_nCompany) + "_temp" + " TO " + "nickname_" + 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 SDatagroup::SNickname::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    //_db.transaction();
    QString strQuery;
    strQuery = "truncate nickname_" + 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 = QString("insert into nickname_" + QString::number(_nCompany) + "_update (num,nickname,id_num,exnickname) values (:NUM,:NICKNAME,:ID_NUM,:EXNICKNAME)").toUtf8();
    query.prepare(strQuery);

    foreach (const stNickname& stnickname, m_mapNickname.values())
    {
        query.bindValue(":NUM", stnickname.num);
        query.bindValue(":NICKNAME", sqlString(stnickname.nickname).replace(QChar(160),""));
        query.bindValue(":ID_NUM", stnickname.id_num);
        query.bindValue(":EXNICKNAME", UniCodeToEucKr(stnickname.nickname));
        if (!query.exec())
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    return true;
}
*/


bool SDatagroup::SNickname::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    strQuery = "truncate nickname_" + 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 = QString("insert into nickname_" + QString::number(_nCompany) + "_update (num,nickname,id_num,exnickname) values (");

    foreach (const stNickname& stnickname, m_mapNickname.values())
    {
        QString str = strQuery;
        str += QString::number(stnickname.num) + ",";
        str += "'" + sqlString(stnickname.nickname).replace(QChar(160),"") + "',";
        str += QString::number(stnickname.id_num) + ",";
        str += "'" + sqlString(UniCodeToEucKr(stnickname.nickname)) + "')";
        if (!query.exec(str.toUtf8()))
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    return true;
}


QString UniCodeToEucKr(const QString& _strData)
{
    {
        QString strOut;
        const 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;
    }
}

QString SDatagroup::SCommunity::getCommunityKey(const QStringList &_strList)
{
    QString str;
    if (_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_name]].trimmed() == "naver" || _strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_name]].trimmed() == "daum")
    {
        str.append(_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_form]].trimmed());
        str.append(".");
        str.append(_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_name]].trimmed());
        str.append(".com/");
        str.append(_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_id]].trimmed());
    }
    else if (_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_name]].trimmed() == "instagram")
    {
        str.append("instagram.com/");
        str.append(_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_id]].trimmed());
    }
    else if (_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_name]].trimmed() == "kakaostory")
    {
        str.append("story.kakao.com/");
        str.append(_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_id]].trimmed());
    }
    else if (_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_name]].trimmed() == "facebook")
    {
        str.append("facebook.com/");
        str.append(_strList[m_pSInitializer->getColumnIntArray()[SInitializer::E_DATA_platform_id]].trimmed());
    }
    return str.trimmed();
}

SDatagroup::stCommunity* SDatagroup::SCommunity::get(const QString &_strKey)
{
    if (m_mapCommunity.contains(_strKey.trimmed()))
        return &(m_mapCommunity[_strKey.trimmed()]);
    else
        return 0;
}

bool SDatagroup::SCommunity::changeTable(QSqlDatabase &_db, const int &_nCompany)
{
    QString strQuery;
    QSqlQuery query(_db);
    strQuery = "RENAME TABLE ";
    strQuery += "community_" + QString::number(_nCompany) + " TO " + "community_" + QString::number(_nCompany) + "_temp" + ",";
    strQuery += "community_" + QString::number(_nCompany) + "_update" + " TO " + "community_" + QString::number(_nCompany) + ",";
    strQuery += "community_" + QString::number(_nCompany) + "_temp" + " TO " + "community_" + 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 SDatagroup::SCommunity::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    //_db.transaction();
    QString strQuery;
    strQuery = "delete from community_" + 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 = QString("insert into community_" + QString::number(_nCompany) +
                       "_update (num,id,title,url,exid,platformname_num,platformform_num) values (:NUM,:ID,:TITLE,:URL,:EXID,:PLATFORMNAME_NUM,:PLATFORMFORM_NUM)");
    query.prepare(strQuery);

    foreach (const stCommunity& stcommunity, m_mapCommunity.values())
    {
        query.bindValue(":NUM", stcommunity.num);
        query.bindValue(":ID", stcommunity.id);
        query.bindValue(":TITLE", sqlString(stcommunity.title).replace(QChar(160),""));
        query.bindValue(":URL", stcommunity.url);
        query.bindValue(":EXID", stcommunity.exid);
        query.bindValue(":PLATFORMNAME_NUM", stcommunity.platformname_num);
        query.bindValue(":PLATFORMFORM_NUM", stcommunity.platformform_num);

        if (!query.exec())
        {
            m_pSInitializer->insertLog(query.lastQuery());
            m_pSInitializer->insertLog(query.lastError().text());
            return false;
        }
    }
    return true;
}
*/


bool SDatagroup::SCommunity::uploadData(QSqlDatabase &_db, const int &_nCompany)
{
    //_db.transaction();
    QString strQuery;
    strQuery = "delete from community_" + 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 = QString("insert into community_" + QString::number(_nCompany) +
                       "_update (num,id,title,url,exid,platformname_num,platformform_num) values (");

    foreach (const stCommunity& stcommunity, m_mapCommunity.values())
    {
        QString str = strQuery;
        str += QString::number(stcommunity.num) + ",";
        str += "'" + sqlString(stcommunity.id) + "',";
        str += "'" + sqlString(stcommunity.title).replace(QChar(160),"") + "',";
        str += "'" + stcommunity.url + "',";
        str += "'" + stcommunity.exid + "',";
        str += QString::number(stcommunity.platformname_num) + ",";
        str += QString::number(stcommunity.platformform_num) + ")";

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


void SDatagroup::SCommunity::put(const QString &_strKey, const stCommunity &_stcommunity)
{
    m_mapCommunity.insert(_strKey.trimmed(), _stcommunity);
}

bool SDatagroup::SCommunity::contains(const QString &_strKey)
{
    return m_mapCommunity.contains(_strKey.trimmed());
}

void SDatagroup::SCommunity::clear()
{
    m_mapCommunity.clear();
}


