| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- #include "restsmodel.h"
- RestsModel::RestsModel(QObject *parent) : BaseModel { "rests", parent }
- {
- connect(this, &BaseModel::dataChanged, this, &RestsModel::clearCache);
- }
- float RestsModel::productRest(const QString &storeId, const QString &productId)
- {
- return findRest(storeId, productId).value("rest", 0.0).toFloat();
- }
- void RestsModel::submitRest(const QString &storeId, const QString &productId, float rest)
- {
- QVariantMap productRest = findRest(storeId, productId);
- productRest["rest"] = rest;
- submitItem(productRest);
- }
- QVariantMap RestsModel::findRest(const QString &storeId, const QString &productId)
- {
- Key restKey(storeId, productId);
- if (m_rests.isEmpty()) {
- loadCache();
- }
- return m_rests.value(restKey, { { "storeId", storeId}, { "productId", productId} });
- }
- void RestsModel::loadCache()
- {
- for (const QVariant &var : items()) {
- QVariantMap rest = var.toMap();
- m_rests[Key(rest.value("storeId").toString(), rest.value("productId").toString())] = rest;
- }
- }
- void RestsModel::clearCache()
- {
- m_rests.clear();
- }
|