#include "RunHttpGet_getIsMaterialsPage.h"

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

}

void RunHttpGet_getIsMaterialsPage::run()
{
    QByteArray outData = InteractiveHttp::http_get_getIsMaterialsPage();

    InteractiveData *pSingletonData = SINGLETON_CREATE(InteractiveData)();

    // 进行数据写操作
    QWriteLocker locker(&pSingletonData->m_dataCacheLock);

    if (pSingletonData->m_jsonCacheMaterialInfo != outData)
    {
        // 清理缓存数据
        pSingletonData->m_dataCacheMaterialInfoId.clear();
        pSingletonData->m_dataCacheMaterialInfoName.clear();
        pSingletonData->m_dataCacheMaterialInfoRfid.clear();

        pSingletonData->m_dataCacheMaterialInfoLoan.clear();
        pSingletonData->m_dataCacheMaterialInfoStat.clear();
        pSingletonData->m_dataCacheMaterialInfoDate.clear();

        pSingletonData->m_dataCacheMaterialInfoTypeId.clear();
        pSingletonData->m_dataCacheMaterialInfoPropId.clear();
        pSingletonData->m_dataCacheMaterialInfoCabinetId.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"))
                {
                    QJsonObject jsonObjData = jsonObjRoot.value("data").toObject();
                    if(jsonObjRoot.value("code").toInt() == 200 && jsonObjData.contains("records"))
                    {
                        QJsonArray jsonArrRecords = jsonObjData.value("records").toArray();
                        for (int i = 0; i < jsonArrRecords.count(); ++i)
                        {
                            if (!jsonArrRecords.at(i).isObject())
                            {
                                continue;
                            }

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

                            QString materialsId = jsonObjRecord.value("materialsId").toString();
                            QString materialsName = jsonObjRecord.value("materialsName").toString();
                            QString materialsRfid = jsonObjRecord.value("materialsRfid").toString();

                            QString loanState = jsonObjRecord.value("loanState").toString();
                            QString status = jsonObjRecord.value("status").toString();
                            QString expirationDate = jsonObjRecord.value("expirationDate").toString();

                            QString materialsTypeId = jsonObjRecord.value("materialsTypeId").toString();
                            QString propertiesValueId = jsonObjRecord.value("propertiesValueId").toString();
                            QString materialsCabinetId = jsonObjRecord.value("materialsCabinetId").toString();

                            if (!pSingletonData->m_dataCacheMaterialInfoId.contains(materialsId))
                            {
                                pSingletonData->m_dataCacheMaterialInfoId.append(materialsId);
                                pSingletonData->m_dataCacheMaterialInfoName.insert(materialsId, materialsName);
                                pSingletonData->m_dataCacheMaterialInfoRfid.insert(materialsId, materialsRfid);

                                pSingletonData->m_dataCacheMaterialInfoLoan.insert(materialsId, loanState);
                                pSingletonData->m_dataCacheMaterialInfoStat.insert(materialsId, status);
                                pSingletonData->m_dataCacheMaterialInfoDate.insert(materialsId, expirationDate);

                                pSingletonData->m_dataCacheMaterialInfoTypeId.insert(materialsId, materialsTypeId);
                                pSingletonData->m_dataCacheMaterialInfoPropId.insert(materialsId, propertiesValueId);
                                pSingletonData->m_dataCacheMaterialInfoCabinetId.insert(materialsId, materialsCabinetId);
                            }
                        }
                    }
                }
            }
        }
    }
}
