Добавлены методы в бизнес-логике для проверки и пополнения магазинов изделиями при переводе заказа в статус Готов

This commit is contained in:
Данияр Аглиуллов 2023-02-18 18:21:15 +04:00
parent 977d357ea1
commit 64b59948b7
4 changed files with 52 additions and 2 deletions

View File

@ -12,10 +12,14 @@ namespace ConfectioneryBusinessLogic.BusinessLogics
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
private readonly IPastryStorage _pastryStorage;
private readonly IShopLogic _shopLogic;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IPastryStorage pastryStorage, IShopLogic shopLogic)
{
_logger = logger;
_shopLogic = shopLogic;
_pastryStorage = pastryStorage;
_orderStorage = orderStorage;
}
@ -96,6 +100,15 @@ namespace ConfectioneryBusinessLogic.BusinessLogics
$"Доступный статус: {(OrderStatus)((int)vmodel.Status + 1)}",
nameof(vmodel));
}
if (orderStatus == OrderStatus.Готов)
{
var vpastry = _pastryStorage.GetElement(new() { Id = vmodel.PastryId });
if (vpastry == null || !_shopLogic.AddPastriesInShops(vpastry, vmodel.Count))
{
throw new Exception($"Не удалось заполнить магазины изделием '{vpastry?.PastryName ?? string.Empty}' из заказа {vmodel.Id}");
}
}
model.Status = orderStatus;
model.DateCreate = vmodel.DateCreate;
if (model.DateImplement == null)

View File

@ -168,5 +168,41 @@ namespace ConfectioneryBusinessLogic
});
return true;
}
public int GetFreePlacesWithPastriesInShops(int countPastries)
{
// Сумма разностей между максимальный кол-вом изделий и суммой всех изделий в магазине
return _shopStorage.GetFullList()
.Select(x => x.MaxCountPastries - x.Pastries
.Select(p => p.Value.Item2).Sum())
.Sum() - countPastries;
}
public bool AddPastriesInShops(IPastryModel pastry, int count)
{
var freePlaces = GetFreePlacesWithPastriesInShops(count);
if (freePlaces < 0)
{
_logger.LogInformation("AddPastriesInShops. Не удалось добавить изделия в магазины, поскольку они переполнены." +
$"Освободите магазины на {-freePlaces} изделий");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
var cnt = Math.Min(count, shop.MaxCountPastries - shop.Pastries.Select(x => x.Value.Item2).Sum());
if (!AddPastry(new() { Id = shop.Id }, pastry, cnt))
{
_logger.LogWarning("При добавления изделий во все магазины произошла ошибка");
return false;
}
count -= cnt;
if (count == 0)
{
return true;
}
}
return true;
}
}
}

View File

@ -81,7 +81,6 @@ namespace ConfectioneryFileImplement
Name = model.Name;
Address = model.Address;
DateOpening = model.DateOpening;
MaxCountPastries = model.MaxCountPastries;
CountPastries = model.Pastries.ToDictionary(x => x.Key, x => x.Value.Item2);
_cachedPastries = null;
}

View File

@ -13,5 +13,7 @@ namespace ConfectioneryContracts.BusinessLogicsContracts
bool Update(ShopBindingModel model);
bool Delete(ShopBindingModel model);
bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count);
int GetFreePlacesWithPastriesInShops(int countPastries);
bool AddPastriesInShops(IPastryModel pastry, int count);
}
}