В интерфес логик добавлен необходимый функционал, нужно реализовать

This commit is contained in:
2024-04-08 12:36:34 +04:00
parent 122db5c9af
commit dcba13f0ed
5 changed files with 119 additions and 150 deletions

View File

@@ -75,100 +75,5 @@ namespace SecuritySystemFileImplement.Implements
source.SaveShops();
return shop.GetViewModel;
}
public bool SellSecure(ISecureModel model, int count)
{
var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id);
if (secure == null)
{
return false;
}
var shopSecures = source.Shops.SelectMany(shop => shop.ShopSecures.Where(c => c.Value.Item1.Id == secure.Id));
int countStore = 0;
foreach (var shopSec in shopSecures)
countStore += shopSec.Value.Item2;
if (count > countStore)
return false;
foreach (var shop in source.Shops)
{
var secures = shop.ShopSecures;
foreach (var sec in secures.Where(x => x.Value.Item1.Id == secure.Id))
{
int min = Math.Min(sec.Value.Item2, count);
secures[sec.Value.Item1.Id] = (sec.Value.Item1, sec.Value.Item2 - min);
count -= min;
if (count <= 0)
break;
}
shop.Update(new ShopBindingModel
{
Id = shop.Id,
Name = shop.Name,
Address = shop.Address,
MaxSecuresCount = shop.MaxSecuresCount,
OpeningDate = shop.OpeningDate,
ShopSecures = secures
});
source.SaveShops();
if (count <= 0)
return true;
}
return true;
}
public bool CheckCountSecure(ISecureModel model, int count)
{
int store = source.Shops.Select(x => x.ShopSecures.Select(y => (y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)).Sum()).Sum();
return store >= count;
}
public bool SellSecures(ISecureModel model, int count)
{
var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id);
if (secure == null || !CheckCountSecure(model, count))
{
throw new ArgumentNullException("В магазинах нет такого количества товара");
}
foreach (var shop in source.Shops)
{
var secures = shop.ShopSecures;
foreach (var elem in secures.Where(x => x.Value.Item1.Id == secure.Id))
{
var selling = Math.Min(elem.Value.Item2, count);
secures[elem.Value.Item1.Id] = (elem.Value.Item1, elem.Value.Item2 - selling);
count -= selling;
if (count <= 0)
{
break;
}
}
shop.Update(new ShopBindingModel
{
Id = model.Id,
Name = shop.Name,
Address = shop.Address,
OpeningDate = shop.OpeningDate,
ShopSecures = secures,
MaxSecuresCount = shop.MaxSecuresCount
});
}
source.SaveShops();
return true;
}
}
}