#include "RunMaterialReplaceRefreshListModel.h"

#include "InteractiveData.h"

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

}

void RunMaterialReplaceRefreshListModel::run()
{
    InteractiveData *pSingletonData = SINGLETON_CREATE(InteractiveData)();

    qDebug() << "开始将缓存数据写入model(5)";

    // 清理缓存
    QMetaObject::invokeMethod(&pSingletonData->m_modelMaterialType, "clear", Qt::QueuedConnection);
    QMetaObject::invokeMethod(&pSingletonData->m_modelMaterialInfo, "clear", Qt::QueuedConnection);

    // 获取传入参数
    QString strRequestTypeId = getRequest().toString();

    QReadLocker locker(&pSingletonData->m_dataCacheLock);

    int numberAll = 0;

    for (int i = 0; i < pSingletonData->m_dataCacheExMaterialInfoId.size(); ++i)
    {
        QString infoId = pSingletonData->m_dataCacheExMaterialInfoId.at(i);
        QString typeId = pSingletonData->m_dataCacheExMaterialInfoTypeId[infoId];
        if (strRequestTypeId == MATERIALS_TYPE_ALL || strRequestTypeId == typeId)
        {
            QVariantMap info;
            info["materialsId"] = infoId;
            info["materialsName"] = pSingletonData->m_dataCacheExMaterialInfoName[infoId];
            info["materialsRfid"] = pSingletonData->m_dataCacheExMaterialInfoRfid[infoId];
            info["expirationDate"] = pSingletonData->m_dataCacheExMaterialInfoDate[infoId];
            info["status"] = pSingletonData->m_dataCacheExMaterialInfoStat[infoId];

            info["materialsTypePicture"] = pSingletonData->m_dataCacheMaterialTypePicture
                                               [pSingletonData->m_dataCacheExMaterialInfoTypeId[infoId]];

            QMetaObject::invokeMethod(&pSingletonData->m_modelMaterialInfo, "append", Qt::QueuedConnection,
                                      Q_ARG(QVariantMap, info));

        }
        numberAll++;
    }

    {
        QVariantMap type;
        type["materialsTypeId"] = 0;
        type["materialsTypeName"] = "全部";
        type["materialsTypeIcon"] = "qrc:/png/allsel.png";
        type["materialsTypeError"] = numberAll;
        type["materialsTypePicture"] = "qrc:/png/allsel.png";
        type["materialsTypeSelected"] = ("0" == strRequestTypeId);

        QMetaObject::invokeMethod(&pSingletonData->m_modelMaterialType, "append", Qt::QueuedConnection,
                                  Q_ARG(QVariantMap, type));
    }

    for (int i = 0; i < pSingletonData->m_dataCacheExMaterialTypeId.size(); ++i)
    {
        QString typeId = pSingletonData->m_dataCacheExMaterialTypeId.at(i);

        QVariantMap type;
        type["materialsTypeId"] = typeId;
        type["materialsTypeName"] = pSingletonData->m_dataCacheMaterialTypeName[typeId];
        type["materialsTypeIcon"] = pSingletonData->m_dataCacheMaterialTypeIcon[typeId];
        type["materialsTypeError"] = pSingletonData->m_dataCacheExMaterialTypeExNumber[typeId];
        type["materialsTypePicture"] = pSingletonData->m_dataCacheMaterialTypePicture[typeId];
        type["materialsTypeSelected"] = (typeId == strRequestTypeId);

        QMetaObject::invokeMethod(&pSingletonData->m_modelMaterialType, "append", Qt::QueuedConnection,
                                  Q_ARG(QVariantMap, type));
    }

}


