#include "scrawlerdata.h"
#include <QSqlQuery>
#include <iostream>
#include <QVariant>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDateTime>
#include <QDebug>
using namespace std;
SCrawlerData::SCrawlerData()
{    
    m_strColumn[ARTICLE_DATA] = "article_data";
    m_strColumn[ARTICLE_DATE] = "article_date";
    m_strColumn[ARTICLE_FORM] = "article_form";
    m_strColumn[ARTICLE_HIT] = "article_hit";
    m_strColumn[ARTICLE_ID] = "article_id";
    m_strColumn[ARTICLE_NICKNAME] = "article_nickname";
    m_strColumn[ARTICLE_ORDER] = "article_order";
    m_strColumn[ARTICLE_PARENT] = "article_parent";
    m_strColumn[ARTICLE_PROFILE] = "article_profile";
    m_strColumn[ARTICLE_PROFILEURL] = "article_profileurl";
    m_strColumn[ARTICLE_TITLE] = "article_title";
    m_strColumn[ARTICLE_URL] = "article_url";
    m_strColumn[KEYWORD_ID] = "keyword_id";
    m_strColumn[PLATFORM_FORM] = "platform_form";
    m_strColumn[PLATFORM_ID] = "platform_id";
    m_strColumn[PLATFORM_NAME] = "platform_name";
    m_strColumn[PLATFORM_TITLE] = "platform_title";
    m_strColumn[REPLY_URL] = "reply_url";
    m_strColumn[ETC] = "etc";
}

SCrawlerData::~SCrawlerData()
{
    clear();
    for(int i = 0; i < TOTAL_COUNT; i++)
    {
        m_strColumn[i].clear();
    }
}

void SCrawlerData::clear()
{
    for(int i = 0; i < TOTAL_COUNT; i++)
    {
        m_strData[i].clear();
    }
}

void SCrawlerData::clear(int _num)
{
    m_strData[_num].clear();
}

QString SCrawlerData::getData(int _num)
{
    return m_strData[_num];
}

void SCrawlerData::setTable(QString _str)
{
    m_strTable = _str;
}

void SCrawlerData::setData(QString _str, int _num)
{
    m_strData[_num] = _str;
}

QString SCrawlerData::GetTable()
{
    return m_strTable;
}

bool SCrawlerData::sendDB()
{
    QSqlQuery query;

    QString strQuery;
    strQuery = "insert into " + m_strTable + "(";

    for(int i = 0; i < TOTAL_COUNT; i++)
    {
        strQuery += (m_strColumn[i] + ",");
    }

    strQuery = strQuery.left(strQuery.size() - 1);
    strQuery += ") VALUES (";

    for(int i = 0; i < TOTAL_COUNT; i++)
    {
        strQuery += (":" + m_strColumn[i] + ",");
    }

    strQuery = strQuery.left(strQuery.size() - 1);
    strQuery += ")";

    query.prepare(strQuery.toUtf8());

    for(int i = 0; i < TOTAL_COUNT; i++)
    {
        if(i == ARTICLE_ORDER)
            query.bindValue(QString(":" + m_strColumn[i]), m_strData[i].trimmed().toInt());
        else
            query.bindValue(QString(":" + m_strColumn[i]), m_strData[i].trimmed().toUtf8());
    }

    if (query.exec()==false)
    {
        cout << "error : " << query.lastError().text().toStdString();
        return false;
    }

    return true;
}

QString SCrawlerData::GetSafeUtf(QString _strData)
{
    QString str;
    QChar *pch = _strData.data();

    for (int i = 0; i < _strData.length(); i++)
    {
        /*
        if (pch[i].unicode() >= 12593 && pch[i].unicode() <= 12622)
            str += pch[i];
        if (pch[i].unicode() >= 44032 && pch[i].unicode() <= 55203)
            str += pch[i];
        //if (pch[i].isDigit() || pch[i].isNumber() || pch[i].isSpace() || pch[i].isLower() || pch[i].isUpper() || pch[i].isSymbol())
            str += pch[i];
        */
        if (pch[i].isPrint() || pch[i].isSpace())
            str += pch[i];
    }
    return str;
}

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

bool SCrawlerData::deleteDB(QString _str, int _num)
{
    QSqlQuery sql;

    QString strQuery = "delete from ";
    strQuery += m_strTable;
    strQuery += QString(" where ");
    strQuery += m_strColumn[_num];
    strQuery += QString(" = '");
    strQuery += _str + "'";

    if (sql.exec(strQuery.toUtf8()) == false)
    {
        cout << "error " << sql.lastError().text().toStdString();
        cout << strQuery.toStdString();
        return false;
    }
    return true;
}

QString SCrawlerData::GetDate(QString _strDate)
{
    QString strOut;
    if (_strDate.contains("오전"))
        strOut = _strDate.replace(" 오전","");
    if (_strDate.contains("오후"))
    {
        strOut = _strDate.replace(" 오후","");
        QDateTime dateTime = QDateTime::fromString(strOut,"yyyy.MM.dd h:mm");
        strOut = dateTime.addSecs(60*60*12).toString("yyyy.MM.dd HH:mm");
    }
    return strOut;
}


