#include "RunHttpGet_severity_level.h"

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

}



void RunHttpGet_severity_level::run()
{
    InteractiveData *pSingletonData = SINGLETON_CREATE(InteractiveData)();
    QByteArray outData = InteractiveHttp::http_get_severity_level();

    // 进行数据写操作
    QWriteLocker locker(&pSingletonData->m_dataCacheLock);
    // 如果数据发生改变，缓存信息
    if (pSingletonData->m_jsonCacheSeverityLevelDict != outData)
    {
        // 缓存json信息
        pSingletonData->m_jsonCacheSeverityLevelDict = outData;


        // 异常类型字典清理数据缓存
        pSingletonData->m_dataCacheSeverityLevelDictCode.clear();
        pSingletonData->m_dataCacheSeverityLevelDictLabel.clear();
        pSingletonData->m_dataCacheSeverityLevelDictValue.clear();


        QJsonParseError error;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(outData, &error);
        if (error.error == QJsonParseError::NoError)
        {
            if(!(jsonDoc.isNull() || jsonDoc.isEmpty()))
            {
                QJsonObject jsonObjRoot = jsonDoc.object();
                if(jsonObjRoot.contains("code") && jsonObjRoot.contains("data"))
                {
                    if (jsonObjRoot.value("code").toInt() == 200 && jsonObjRoot.value("data").isArray())
                    {
                        QJsonArray jsonArrData = jsonObjRoot.value("data").toArray();

                        for (int i = 0; i < jsonArrData.size(); ++i)
                        {
                            if (!jsonArrData.at(i).isObject())
                            {
                                continue;
                            }
                            QJsonObject jsonObjData = jsonArrData.at(i).toObject();

                            QString dictCode = jsonObjData.value("dictCode").toString();
                            QString dictLabel = jsonObjData.value("dictLabel").toString();
                            QString dictValue = jsonObjData.value("dictValue").toString();

                            if (pSingletonData->m_dataCacheSeverityLevelDictCode.contains(dictCode))
                            {
                                continue;
                            }

                            pSingletonData->m_dataCacheSeverityLevelDictCode.append(dictCode);
                            pSingletonData->m_dataCacheSeverityLevelDictLabel.insert(dictCode, dictLabel);
                            pSingletonData->m_dataCacheSeverityLevelDictValue.insert(dictCode, dictValue);
                        }
                    }
                }
            }
        }
    }
}
