This commit is contained in:
Калышев Ян 2023-04-15 19:12:38 +04:00
parent 2da63ac071
commit b606e6e7f0
2 changed files with 106 additions and 78 deletions

View File

@ -18,11 +18,15 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
private readonly IShopLogic _shopLogic;
private readonly IManufactureStorage _manufactureStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IManufactureStorage manufactureStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
_shopLogic = shopLogic;
_manufactureStorage = manufactureStorage;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. OrderId:{Id}", model?.Id);
@ -37,33 +41,33 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (!CheckStatus(model, OrderStatus.Принят, false)) return false;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
CheckModel(model);
if (model.Status != OrderStatus.Неизвестен)
{
_logger.LogWarning("Insert operation failed. Order status is incorrect.");
return false;
}
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
CheckModel(model);
if (!CheckStatus(model, OrderStatus.Выполняется)) return false;
return true;
}
return StatusUpdate(model, OrderStatus.Выполняется);
}
public bool DeliveryOrder(OrderBindingModel model)
{
CheckModel(model);
if (!CheckStatus(model, OrderStatus.Выдан)) return false;
return true;
}
return StatusUpdate(model, OrderStatus.Выдан);
}
public bool FinishOrder(OrderBindingModel model)
{
CheckModel(model);
if (!CheckStatus(model, OrderStatus.Готов)) return false;
return true;
}
return StatusUpdate(model, OrderStatus.Готов);
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
@ -89,22 +93,43 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
_logger.LogInformation("Order. OrderId: {Id}.Sum: {Sum}. ManufactureId: {ManufactureId}", model.Id, model.Sum, model.ManufactureId);
}
private bool CheckStatus(OrderBindingModel model, OrderStatus newstatus, bool update = true)
private bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
{
if (model.Status != newstatus - 1)
{
_logger.LogWarning("Failed to change status");
return false;
}
model.Status = newstatus;
if (!update) return true;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
return true;
}
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != newStatus)
{
_logger.LogWarning("Change status operation failed");
return false;
}
model.Status = newStatus;
if (model.Status == OrderStatus.Готов)
{
model.DateImplement = DateTime.Now;
var manufacture = _manufactureStorage.GetElement(new() { Id = viewModel.ManufactureId });
if (manufacture == null)
{
throw new ArgumentNullException(nameof(manufacture));
}
if (!_shopLogic.AddManufactures(manufacture, viewModel.Count))
{
throw new Exception($"AddManufactures operation failed");
}
}
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model, false);
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Change status operation failed");
return false;
}
return true;
}
}
}

View File

@ -107,45 +107,48 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
}
public bool AddManufactureInShop(ShopSearchModel model, IManufactureModel manufacture, int count)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (count <= 0)
{
throw new ArgumentException("Количество изделий должно быть больше 0", nameof(count));
}
_logger.LogInformation("AddManufactureInShop. ShopName:{ShopName}. Id:{Id}", model.ShopName, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("AddManufactureInShop element not found");
return false;
}
_logger.LogInformation("AddManufactureInShop find. Id:{Id}", element.Id);
if (element.ListManufacture.TryGetValue(manufacture.Id, out var pair))
{
element.ListManufacture[manufacture.Id] = (manufacture, count + pair.Item2);
_logger.LogInformation("AddManufactureInShop. Added {count} {manufacture} to '{ShopName}' shop",
count, manufacture.ManufactureName, element.ShopName);
}
else
{
element.ListManufacture[manufacture.Id] = (manufacture, count);
_logger.LogInformation("AddManufactureInShop. Added {count} new manufacture {manufacture} to '{ShopName}' shop",
count, manufacture.ManufactureName, element.ShopName);
}
_shopStorage.Update(new()
{
Id = element.Id,
Address = element.Address,
ShopName = element.ShopName,
DateOpening = element.DateOpening,
ListManufacture = element.ListManufacture
});
return true;
}
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (count <= 0)
{
throw new ArgumentException("Количество поездок должно быть больше 0", nameof(count));
}
_logger.LogInformation("AddManufactureInShop. ShopName:{ShopName}. Id:{Id}", model.ShopName, model.Id);
var shop = _shopStorage.GetElement(model);
if (shop == null)
{
_logger.LogWarning("AddManufactureInShop element not found");
return false;
}
if (shop.Capacity - shop.ListManufacture.Select(x => x.Value.Item2).Sum() < count)
{
throw new ArgumentNullException("В магазине не хватает места", nameof(count));
}
if (shop.ListManufacture.ContainsKey(manufacture.Id))
{
shop.ListManufacture[manufacture.Id] = (manufacture, count + shop.ListManufacture[manufacture.Id].Item2);
_logger.LogInformation("AddManufactureInShop. Added {count} {manufacture} to '{ShopName}' shop",
count, manufacture.ManufactureName, shop.ShopName);
}
else
{
shop.ListManufacture[manufacture.Id] = (manufacture, count);
_logger.LogInformation("AddManufactureInShop. Added {count} new manufacture {manufacture} to '{ShopName}' shop",
count, manufacture.ManufactureName, shop.ShopName);
}
_shopStorage.Update(new()
{
Id = shop.Id,
Address = shop.Address,
ShopName = shop.ShopName,
DateOpening = shop.DateOpening,
Capacity = shop.Capacity,
ListManufacture = shop.ListManufacture
});
return true;
}
public bool AddManufactures(IManufactureModel model, int count)
{
if (model == null)