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

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

}

void RunHttpGet_getCabinetHomePage::run()
{

    QByteArray outData = InteractiveHttp::http_get_getCabinetHomePage();

    InteractiveData *pSingletonData = SINGLETON_CREATE(InteractiveData)();

    // 进行数据写操作
    QWriteLocker locker(&pSingletonData->m_dataCacheLock);
    // 如果数据发生改变，缓存信息
    if (!outData.isEmpty())
    {
        pconfig->changeNumber = 0;
        pconfig->checkNumber = 0;
        pconfig->rarNumber = 0;

        pconfig->bottomTypeName.clear();
        pconfig->bottomTypeIcon.clear();
        pconfig->bottomTypeAll.clear();
        pconfig->bottomTypeBorrowable.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"))
                {
                    if (jsonObjRoot.value("code").toInt() == 200)
                    {
                        QJsonObject jsonObjData = jsonObjRoot.value("data").toObject();

                        QString changeNumber = QString::number(jsonObjData.value("changeNumber").toInt());
                        QString checkNumber = QString::number(jsonObjData.value("checkNumber").toInt());
                        QString rarNumber = QString::number(jsonObjData.value("rarNumber").toInt());

                        pconfig->changeNumber = changeNumber;
                        pconfig->checkNumber = checkNumber;
                        pconfig->rarNumber = rarNumber;


                        QJsonArray jsonArrList = jsonObjData.value("list").toArray();
                        for (int i = 0; i < jsonArrList.size(); ++i)
                        {
                            QJsonObject jsonObjList = jsonArrList.at(i).toObject();

                            QString materialsTypeId = jsonObjList.value("materialsTypeId").toString();
                            QString materialsTypeName = jsonObjList.value("materialsTypeName").toString();
                            QString materialsTypeIcon = jsonObjList.value("materialsTypeIcon").toString();
                            QString allNumber = QString::number(jsonObjList.value("allNumber").toInt());
                            QString borrowableNumber = QString::number(jsonObjList.value("borrowableNumber").toInt());

                            pconfig->bottomTypeName.insert(materialsTypeId, materialsTypeName);
                            pconfig->bottomTypeIcon.insert(materialsTypeId, materialsTypeIcon);
                            pconfig->bottomTypeAll.insert(materialsTypeId, allNumber);
                            pconfig->bottomTypeBorrowable.insert(materialsTypeId, borrowableNumber);
                        }

                    }
                }
            }
        }

    }
}
