Emelyanov A.S. LabWork_2_Hard #11
@ -17,10 +17,14 @@ namespace FishFactoryBusinessLogic.BusinessLogics
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
||||
private readonly IShopLogic _shopLogic;
|
||||
private readonly ICannedStorage _cannedStorage;
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, ICannedStorage cannedStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_orderStorage = orderStorage;
|
||||
_shopLogic = shopLogic;
|
||||
_cannedStorage = cannedStorage;
|
||||
}
|
||||
public bool CreateOrder(OrderBindingModel model)
|
||||
{
|
||||
@ -41,31 +45,42 @@ namespace FishFactoryBusinessLogic.BusinessLogics
|
||||
}
|
||||
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
var element = _orderStorage.GetElement(new OrderSearchModel()
|
||||
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
||||
if (viewModel == null)
|
||||
{
|
||||
Id = model.Id
|
||||
});
|
||||
if (element == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(element));
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
model.DateImplement = element.DateImplement;
|
||||
model.Status = element.Status;
|
||||
if (newStatus - model.Status == 1)
|
||||
if (viewModel.Status + 1 != newStatus)
|
||||
{
|
||||
model.Status = newStatus;
|
||||
if (model.Status == OrderStatus.Выдан)
|
||||
model.DateImplement = DateTime.Now;
|
||||
if (_orderStorage.Update(model) == null)
|
||||
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect.");
|
||||
return false;
|
||||
}
|
||||
model.Status = newStatus;
|
||||
if (model.Status == OrderStatus.Готов)
|
||||
{
|
||||
model.DateImplement = DateTime.Now;
|
||||
var canned = _cannedStorage.GetElement(new() { Id = viewModel.CannedId });
|
||||
if (canned == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
throw new ArgumentNullException(nameof(canned));
|
||||
}
|
||||
if (!_shopLogic.AddCanneds(canned, viewModel.Count))
|
||||
{
|
||||
throw new Exception($"AddCanneds operation failed. Shop is full.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, newStatus);
|
||||
throw new ArgumentException($"Невозможно приствоить статус {newStatus} заказу с текущим статусом {model.Status}");
|
||||
else
|
||||
{
|
||||
model.DateImplement = viewModel.DateImplement;
|
||||
}
|
||||
CheckModel(model, false);
|
||||
if (_orderStorage.Update(model) == null)
|
||||
{
|
||||
model.Status--;
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool DeliveryOrder(OrderBindingModel model)
|
||||
{
|
||||
@ -83,8 +98,7 @@ namespace FishFactoryBusinessLogic.BusinessLogics
|
||||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. OrderId:{Id}", model?.Id);
|
||||
var list = model == null ? _orderStorage.GetFullList() :
|
||||
_orderStorage.GetFilteredList(model);
|
||||
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
|
@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -39,6 +40,10 @@ namespace FishFactoryBusinessLogic
|
||||
_logger.LogWarning("AddCannedInShop element not found");
|
||||
return false;
|
||||
}
|
||||
if (element.MaxCountCanneds - element.ListCanneds.Select(x => x.Value.Item2).Sum() < count)
|
||||
{
|
||||
throw new ArgumentNullException("Магазин переполнен", nameof(count));
|
||||
}
|
||||
_logger.LogInformation("AddCannedInShop find. Id:{Id}", element.Id);
|
||||
|
||||
if (element.ListCanneds.TryGetValue(canned.Id, out var pair))
|
||||
@ -57,7 +62,8 @@ namespace FishFactoryBusinessLogic
|
||||
Address = element.Address,
|
||||
ShopName = element.ShopName,
|
||||
DateOpening = element.DateOpening,
|
||||
ListCanneds = element.ListCanneds
|
||||
ListCanneds = element.ListCanneds,
|
||||
MaxCountCanneds = element.MaxCountCanneds,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
@ -140,6 +146,10 @@ namespace FishFactoryBusinessLogic
|
||||
{
|
||||
throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName));
|
||||
}
|
||||
if (model.MaxCountCanneds < 0)
|
||||
{
|
||||
throw new ArgumentException("Максимальное количество консерв в магазине не может быть меньше нуля", nameof(model.MaxCountCanneds));
|
||||
}
|
||||
_logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", model.ShopName, model.Address, model.Id);
|
||||
var element = _shopStorage.GetElement(new ShopSearchModel
|
||||
{
|
||||
@ -150,5 +160,57 @@ namespace FishFactoryBusinessLogic
|
||||
throw new InvalidOperationException("Магазин с таким названием уже есть");
|
||||
}
|
||||
}
|
||||
|
||||
public bool SellCanneds(ICannedModel model, int count)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (count <= 0)
|
||||
{
|
||||
throw new ArgumentException("Количество консерв должно быть больше 0", nameof(count));
|
||||
}
|
||||
var freeCount = _shopStorage.GetFullList()
|
||||
.Select(x => x.MaxCountCanneds - x.ListCanneds
|
||||
.Select(p => p.Value.Item2).Sum()).Sum() - count;
|
||||
if (freeCount < 0)
|
||||
{
|
||||
_logger.LogInformation("AddCanned. Не удалось добавить изделия в магазины, они переполнены.");
|
||||
return false;
|
||||
}
|
||||
foreach (var shop in _shopStorage.GetFullList())
|
||||
{
|
||||
int countFree = shop.MaxCountCanneds - shop.ListCanneds.Select(x => x.Value.Item2).Sum();
|
||||
if (countFree < count)
|
||||
{
|
||||
if (!AddCannedInShop(new() { Id = shop.Id }, model, countFree))
|
||||
{
|
||||
_logger.LogWarning("AddCannedInShop operation failed.");
|
||||
return false;
|
||||
}
|
||||
count -= countFree;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!AddCannedInShop(new() { Id = shop.Id }, model, count))
|
||||
{
|
||||
_logger.LogWarning("AddCannedInShop operation failed.");
|
||||
return false;
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
if (count == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AddCanneds(ICannedModel model, int count)
|
||||
{
|
||||
return _shopStorage.SellCanneds(model, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user