Переделан под LINQ метод SellDocument

This commit is contained in:
Данила Мочалов 2023-04-07 16:04:49 +04:00
parent 793c2eae4e
commit 0b3b9b391a

View File

@ -81,7 +81,7 @@ namespace LawFirmFileImplement.Implements
return shop.GetViewModel;
}
public bool SellDocument(IDocumentModel model, int count)
/* public bool SellDocument(IDocumentModel model, int count)
{
// переделать под linq
@ -151,5 +151,69 @@ namespace LawFirmFileImplement.Implements
}
return true;
}
*/
public bool SellDocument(IDocumentModel model, int count)
{
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return false;
}
var countStore = count;
var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(doc => doc.Value.Item1.Id == document.Id));
foreach (var doc in shopDocuments)
{
count -= doc.Value.Item2;
if (count <= 0)
{
break;
}
}
if (count > 0)
{
return false;
}
count = countStore;
foreach (var shop in source.Shops)
{
var documents = shop.ShopDocuments;
foreach (var doc in documents.Where(x => x.Value.Item1.Id == document.Id))
{
var min = Math.Min(doc.Value.Item2, count);
documents[doc.Value.Item1.Id] = (doc.Value.Item1, doc.Value.Item2 - min);
count -= min;
if (count <= 0)
{
break;
}
}
shop.Update(new ShopBindingModel
{
Id = shop.Id,
Name = shop.Name,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
MaxCountDocuments = shop.MaxCountDocuments,
ShopDocuments = documents
});
source.SaveShops();
if (count <= 0) break;
}
return count <= 0;
}
}
}