#include "RunHttpGet_getIsMaterialsLoanExceptionPage.h"
#include "InteractiveData.h"

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

}

void RunHttpGet_getIsMaterialsLoanExceptionPage::http_get_getIsMaterialsLoanExceptionPage()
{

    QByteArray outData = InteractiveHttp::http_get_getIsMaterialsLoanExceptionPage();

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

        // 物资信息清理数据缓存
        pSingletonData->m_dataCacheMaterialLoadRfid.clear();
        pSingletonData->m_dataCacheMaterialLoanName.clear();
        // pSingletonData->m_dataCacheMaterialLoanRfid.clear();
        pSingletonData->m_dataCacheMaterialLoanTypeIcon.clear();
        pSingletonData->m_dataCacheMaterialLoanTypePicture.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").isObject())
                    {
                        QJsonObject jsonObjData = jsonObjRoot.value("data").toObject();

                        if (jsonObjData.contains("records") && jsonObjData.value("records").isArray())
                        {
                            QJsonArray jsonArrRecords = jsonObjData.value("records").toArray();

                            for (int i = 0; i < jsonArrRecords.size(); ++i)
                            {
                                if (!jsonArrRecords.at(i).isObject())
                                {
                                    continue;
                                }

                                QJsonObject jsonObjRecords = jsonArrRecords.at(i).toObject();

                                // QString materialsId = jsonObjRecords.value("materialsId").toString();
                                QString materialsName = jsonObjRecords.value("materialsName").toString();
                                QString materialsRfid = jsonObjRecords.value("materialsRfid").toString();
                                QString materialsTypeIcon = jsonObjRecords.value("materialsTypeIcon").toString();
                                QString materialsTypePicture = jsonObjRecords.value("materialsTypePicture").toString();

                                if (!pSingletonData->m_dataCacheMaterialLoadRfid.contains(materialsRfid) && materialsRfid != "")
                                {
                                    pSingletonData->m_dataCacheMaterialLoadRfid.append(materialsRfid);
                                    pSingletonData->m_dataCacheMaterialLoanName.insert(materialsRfid, materialsName);
                                    // pSingletonData->m_dataCacheMaterialLoanRfid.insert(materialsId, materialsRfid);
                                    pSingletonData->m_dataCacheMaterialLoanTypeIcon.insert(materialsRfid, materialsTypeIcon);
                                    pSingletonData->m_dataCacheMaterialLoanTypePicture.insert(materialsRfid, materialsTypePicture);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

void RunHttpGet_getIsMaterialsLoanExceptionPage::run()
{
    QByteArray outData = InteractiveHttp::http_get_getIsMaterialsLoanExceptionPage();

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

        // 物资信息清理数据缓存
        pSingletonData->m_dataCacheMaterialLoadRfid.clear();
        pSingletonData->m_dataCacheMaterialLoanName.clear();
        // pSingletonData->m_dataCacheMaterialLoanRfid.clear();
        pSingletonData->m_dataCacheMaterialLoanTypeIcon.clear();
        pSingletonData->m_dataCacheMaterialLoanTypePicture.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").isObject())
                    {
                        QJsonObject jsonObjData = jsonObjRoot.value("data").toObject();

                        if (jsonObjData.contains("records") && jsonObjData.value("records").isArray())
                        {
                            QJsonArray jsonArrRecords = jsonObjData.value("records").toArray();

                            for (int i = 0; i < jsonArrRecords.size(); ++i)
                            {
                                if (!jsonArrRecords.at(i).isObject())
                                {
                                    continue;
                                }

                                QJsonObject jsonObjRecords = jsonArrRecords.at(i).toObject();

                                // QString materialsId = jsonObjRecords.value("materialsId").toString();
                                QString materialsName = jsonObjRecords.value("materialsName").toString();
                                QString materialsRfid = jsonObjRecords.value("materialsRfid").toString();
                                QString materialsTypeIcon = jsonObjRecords.value("materialsTypeIcon").toString();
                                QString materialsTypePicture = jsonObjRecords.value("materialsTypePicture").toString();

                                if (!pSingletonData->m_dataCacheMaterialLoadRfid.contains(materialsRfid) && materialsRfid != "")
                                {
                                    pSingletonData->m_dataCacheMaterialLoadRfid.append(materialsRfid);
                                    pSingletonData->m_dataCacheMaterialLoanName.insert(materialsRfid, materialsName);
                                    // pSingletonData->m_dataCacheMaterialLoanRfid.insert(materialsId, materialsRfid);
                                    pSingletonData->m_dataCacheMaterialLoanTypeIcon.insert(materialsRfid, materialsTypeIcon);
                                    pSingletonData->m_dataCacheMaterialLoanTypePicture.insert(materialsRfid, materialsTypePicture);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
