#include "RunMaterialManualReplaceRefreshListModel.h"

#include "InteractiveData.h"

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

}


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


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

    QReadLocker locker(&pSingletonData->m_dataCacheLock);

    QStringList listParent;
    for (int i = 0; i < pSingletonData->m_dataCacheMaterialTypeId.size(); ++i)
    {
        QString typeId = pSingletonData->m_dataCacheMaterialTypeId.at(i);
        if (pSingletonData->m_dataCacheMaterialTypeAncestors[typeId].size() > 1)
        {
            if (!listParent.contains(pSingletonData->m_dataCacheMaterialTypeAncestors[typeId].last()))
            {
                listParent.append(pSingletonData->m_dataCacheMaterialTypeAncestors[typeId].last());
            }
        }
    }

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

        QVariantMap type;

        type["display"] = pSingletonData->m_dataCacheMaterialTypeName[typeId];
        type["materialsTypeId"] = typeId;
        type["materialsTypeName"] =  pSingletonData->m_dataCacheMaterialTypeName[typeId];
        type["materialsTypePicture"] = pSingletonData->m_dataCacheMaterialTypePicture[typeId];

        QStringList propertyIds = pSingletonData->m_dataCacheMaterialTypePropertyIds[typeId];

        QVariantList props;
        for (int j = 0; j < propertyIds.size(); ++j)
        {
            QString propertyId = propertyIds.at(j);
            for (int k = 0; k < pSingletonData->m_dataCacheMaterialPropId.size(); ++k)
            {
                QString propId = pSingletonData->m_dataCacheMaterialPropId.at(k);
                if (propertyId ==  pSingletonData->m_dataCacheMaterialPropPropertyId[propId])
                {
                    QVariantMap prop;

                    prop["display"] = pSingletonData->m_dataCacheMaterialPropName[propId];
                    prop["propertiesValue"] = pSingletonData->m_dataCacheMaterialPropName[propId];
                    prop["propertiesValueId"] = propId;

                    prop["propertiesProperty"] = pSingletonData->m_dataCacheMaterialPropPropertyName[propId];
                    prop["propertiesPropertyId"] = propertyId;

                    props.append(prop);
                }
            }
        }
        type["props"] = props;

        if (!listParent.contains(typeId))
        {
            types.append(type);
        }
    }


    for (int i = 0; i < pSingletonData->m_dataCacheExMaterialInfoId.size(); ++i)
    {
        QString infoId = pSingletonData->m_dataCacheExMaterialInfoId.at(i);
        if(pSingletonData->m_dataCacheExMaterialInfoStat[infoId] != "0")
        {
            QVariantMap info;

            QString typeId = pSingletonData->m_dataCacheExMaterialInfoTypeId[infoId];
            QString propId = pSingletonData->m_dataCacheExMaterialInfoPropId[infoId];

            info["materialsId"] = infoId;
            info["oldMaterialsId"] = infoId;

            info["materialsName"] = pSingletonData->m_dataCacheExMaterialInfoName[infoId];
            info["materialsRfid"] = pSingletonData->m_dataCacheExMaterialInfoRfid[infoId];
            info["expirationDate"] = pSingletonData->m_dataCacheExMaterialInfoDate[infoId];
            info["propertiesValue"] = pSingletonData->m_dataCacheMaterialPropName[propId];
            info["materialsTypeName"] = pSingletonData->m_dataCacheMaterialTypeName[typeId];
            info["materialsTypePicture"] = pSingletonData->m_dataCacheMaterialTypePicture[typeId];

            info["oldMaterialsName"] = pSingletonData->m_dataCacheExMaterialInfoName[infoId];
            info["oldMaterialsRfid"] = pSingletonData->m_dataCacheExMaterialInfoRfid[infoId];
            info["oldExpirationDate"] = pSingletonData->m_dataCacheExMaterialInfoDate[infoId];
            info["oldPropertiesValue"] = pSingletonData->m_dataCacheMaterialPropName[propId];
            info["oldMaterialsTypeName"] = pSingletonData->m_dataCacheMaterialTypeName[typeId];
            info["oldMaterialsTypePicture"] = pSingletonData->m_dataCacheMaterialTypePicture[typeId];

            info["status"] = pSingletonData->m_dataCacheExMaterialInfoStat[infoId];
            info["click"] = 0;      // 0.未点击    1.维修    2.更换
            info["types"] = types;

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

}
