using FurnitureAssemblyContracts.BindingModels; using FurnitureAssemblyContracts.BusinessLogicsContracts; using FurnitureAssemblyContracts.SearchModels; using FurnitureAssemblyContracts.StoragesContracts; using FurnitureAssemblyContracts.ViewModels; using FurnitureAssemblyDataModels.Enums; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FurnitureAssemblyBusinessLogic { public class OrderLogic : IOrderLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; public OrderLogic(ILogger logger, IOrderStorage orderStorage) { _logger = logger; _orderStorage = orderStorage; } public List? ReadList(OrderSearchModel? model) { _logger.LogInformation("ReadList. Id:{Id}", model?.Id); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public bool CreateOrder(OrderBindingModel model) { CheckModel(model); if (model.Status != OrderStatus.Неизвестен) { _logger.LogWarning("Insert operation failed, incorrect order status"); 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) { return StatusUpdate(model, OrderStatus.Выполняется); } public bool FinishOrder(OrderBindingModel model) { return StatusUpdate(model, OrderStatus.Готов); } public bool DeliveryOrder(OrderBindingModel model) { return StatusUpdate(model, OrderStatus.Выдан); } private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (model.Count <= 0) { throw new ArgumentNullException("В заказе не может быть 0 заготовок", nameof(model.Count)); } if (model.Sum <= 0) { throw new ArgumentNullException("Суммарная стоимость заказа должна быть больше 0", nameof(model.Sum)); } if (model.FurnitureId < 0) { throw new ArgumentNullException("Некорректный id у заготовки", nameof(model.FurnitureId)); } if (model.DateCreate > model.DateImplement) { throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения"); } _logger.LogInformation("Order. OrderId:{Id}, Sum:{Sum}. FurnitureId:{Id}", model.Id, model.Sum, model.FurnitureId); } public bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus) { var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); if (viewModel == null) { throw new ArgumentNullException(nameof(model)); } if (viewModel.Status + 1 != newOrderStatus) { _logger.LogWarning("Status update operation failed. New status " + newOrderStatus.ToString() + "incorrect"); return false; } model.Status = newOrderStatus; if (model.Status == OrderStatus.Выдан) { model.DateImplement = DateTime.Now.ToUniversalTime(); } else { model.DateImplement = viewModel.DateImplement; } CheckModel(model, false); if (_orderStorage.Update(model) == null) { model.Status--; _logger.LogWarning("Update operation failed"); return false; } return true; } } }