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.BussinessLogic { public class AdLogic : IAdLogic { private readonly IAdStorage _orderStorage; private readonly ILogger _logger; public AdLogic(ILogger logger, IAdStorage orderStorage) { _logger = logger; _orderStorage = orderStorage; } public List? ReadList(AdSearchModel? 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(AdBindingModel model) { CheckModel(model); if(model.Status != AdStatus.Неизвестен) { _logger.LogWarning("Insert operation failed, incorrect order status"); return false; } model.Status = AdStatus.Принят; if(_orderStorage.Insert(model) == null) { model.Status = AdStatus.Неизвестен; _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool TakeOrderInWork(AdBindingModel model) { return StatusUpdate(model, AdStatus.Выполняется); } public bool FinishOrder(AdBindingModel model) { return StatusUpdate(model, AdStatus.Готов); } public bool DeliveryOrder(AdBindingModel model) { return StatusUpdate(model, AdStatus.Выдан); } private void CheckModel(AdBindingModel 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)); } // Проверка корректности id у изделий 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(AdBindingModel model, AdStatus newOrderStatus) { var viewModel = _orderStorage.GetElement(new AdSearchModel { Id = model.Id }); if(viewModel == null) { throw new ArgumentNullException(nameof(model)); } if(viewModel.Status + 1 != newOrderStatus) { _logger.LogWarning("Status update operation failed. Status " + newOrderStatus.ToString() + "incorrect"); return false; } model.Status = newOrderStatus; if(model.Status == AdStatus.Выдан) { model.DateImplement = DateTime.Now; } else { model.DateImplement = viewModel.DateImplement; } CheckModel(model, false); if(_orderStorage.Update(model) == null) { model.Status--; _logger.LogWarning("Update operation failed"); return false; } return true; } } }