Написана какая-то логика продажи в магазине

This commit is contained in:
Данила Мочалов 2023-02-27 09:49:29 +04:00
parent 5b5b8cc6ac
commit 35ee01f552

View File

@ -11,27 +11,6 @@ using System.Threading.Tasks;
namespace LawFirmFileImplement.Implements namespace LawFirmFileImplement.Implements
{ {
// Нужно узнать, проверять ли на наличие всех товаров, или только
// наличие определенного товара
// То же самое с продажей
public class ShopStorage : IShopStorage public class ShopStorage : IShopStorage
{ {
private readonly DataFileSingleton source; private readonly DataFileSingleton source;
@ -101,5 +80,90 @@ namespace LawFirmFileImplement.Implements
return shop.GetViewModel; return shop.GetViewModel;
} }
public bool SellDocument(DocumentSearchModel model, int count)
{
var document = source.Documents.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id);
var countStore = count;
if (document == null)
{
return false;
}
foreach (var shop in source.Shops)
{
foreach (var doc in shop.ShopDocuments)
{
if (doc.Value.Item1.Id == document.Id)
{
if (count > 0)
{
count = count - doc.Value.Item2;
}
}
if (count < 1)
{
break;
}
}
}
if (count > 0)
{
return false;
}
count = countStore;
for (int i = 0; i < source.Shops.Count; i++)
{
var shop = source.Shops[i];
var documents = shop.ShopDocuments;
for (int j = 0; j < documents.Count; j++)
{
if (documents[j].Item1.Id == document.Id)
{
while (documents[j].Item2 > 0 || count > 0)
{
int tempItem2 = documents[j].Item2;
tempItem2--;
count--;
documents[j] = (documents[j].Item1, tempItem2);
}
}
if (count < 1)
{
shop.Update(new ShopBindingModel
{
Id = shop.Id,
Name = shop.Name,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
MaxCountDocuments = shop.MaxCountDocuments,
ShopDocuments= documents
});
break;
}
}
shop.Update(new ShopBindingModel
{
Id = shop.Id,
Name = shop.Name,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
MaxCountDocuments = shop.MaxCountDocuments,
ShopDocuments = documents
});
}
if (count > 0)
{
return false;
}
return true;
}
} }
} }