#include "RunHttpPost_insertIsException.h"

#include "../user/config.h"
extern config *pconfig;

RunHttpPost_insertIsException::RunHttpPost_insertIsException(InteractiveTask *task, QMutex *mutex)
    :RunAbstract(task, mutex)
{

}


void RunHttpPost_insertIsException::run()
{
    InteractiveData *pSingletonData = SINGLETON_CREATE(InteractiveData)();
    QReadLocker locker(&pSingletonData->m_dataCacheLock);

    QString inData = getRequest().toString();

    QJsonParseError error;
    QJsonDocument jsonDocRead = QJsonDocument::fromJson(inData.toUtf8());
    QJsonDocument jsonDocWrite;

    if (error.error == QJsonParseError::NoError)
    {
        QJsonObject jsonObjRoot = jsonDocRead.object();

        QString exceptionTypeName = jsonObjRoot.value("exceptionTypeName").toString();
        QString exceptionLevelName = jsonObjRoot.value("exceptionLevelName").toString();
        QString exceptionDescription = jsonObjRoot.value("exceptionDescription").toString();

        jsonObjRoot.insert("exceptionCategory", "2");
        jsonObjRoot.insert("raiser", pconfig->userId.toInt());

        for (auto it = pSingletonData->m_dataCacheExceptionTypeDictLabel.begin(); it != pSingletonData->m_dataCacheExceptionTypeDictLabel.end(); ++it)
        {
            if (it.value() == exceptionTypeName)
            {
                jsonObjRoot.insert("exceptionType", pSingletonData->m_dataCacheExceptionTypeDictValue[it.key()]);
            }
        }

        for (auto it = pSingletonData->m_dataCacheSeverityLevelDictLabel.begin(); it != pSingletonData->m_dataCacheSeverityLevelDictLabel.end(); ++it)
        {
            if (it.value() == exceptionLevelName)
            {
                jsonObjRoot.insert("exceptionLevel", pSingletonData->m_dataCacheSeverityLevelDictValue[it.key()]);
            }
        }

        if (!pconfig->devId.isEmpty())
        {
            jsonObjRoot.insert("sourceId", pconfig->devId);
        }

        jsonDocWrite.setObject(jsonObjRoot);
    }

    QByteArray json = jsonDocWrite.toJson(QJsonDocument::Compact);

    QByteArray outData = InteractiveHttp::http_post_insertIsException(json);

    setRespond(outData);
}
