using FurnitureAssemblyBusinessLogic.MailWorker; using FurnitureAssemblyContracts.BindingModels; using FurnitureAssemblyContracts.BusinessLogicsContarcts; 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; private readonly AbstractMailWorker _mailWorker; private readonly IClientLogic _clientLogic; public OrderLogic(ILogger logger, IOrderStorage orderStorage, IClientLogic clientLogic, AbstractMailWorker abstractMailWorker) { _logger = logger; _orderStorage = orderStorage; _clientLogic = clientLogic; _mailWorker = abstractMailWorker; } private bool ChangeStatus(OrderBindingModel model, OrderStatus orderStatus) { if (model.Status + 1 != orderStatus) { return false; } model.Status = orderStatus; SendMail(model.ClientId, $"Статус заказа {model.Id} изменен", $"Заказ {model.Id} переведен в статус {model.Status}"); return true; } public bool CreateOrder(OrderBindingModel model) { CheckModel(model); if (!ChangeStatus(model, OrderStatus.Принят)) { _logger.LogWarning("Order's status is wrong"); throw new InvalidOperationException("Order's status is wrong"); } var order = _orderStorage.Insert(model); if (order == null) { _logger.LogWarning("Insert operation failed"); return false; } SendMail(model.ClientId, $"Создание заказа {order.Id}", $"Заказ под номером {order.Id} создан {model.DateCreate}"); return true; } public bool TakeOrderInWork(OrderBindingModel model) { var modelNew = ReadElement(new OrderSearchModel { Id = model.Id }); // в связи с обновлением контракта, предыдущий метод не нужен if (modelNew == null) { return false; } model.Status = modelNew.Status; model.ClientId = modelNew.ClientId; if (!ChangeStatus(model, OrderStatus.Выполняется)) { _logger.LogWarning("Order's status is wrong"); throw new InvalidOperationException("Order's status is wrong"); } _orderStorage.Update(model); return true; } public bool FinishOrder(OrderBindingModel model) { var modelNew = ReadElement(new OrderSearchModel { Id = model.Id}); // в связи с обновлением контракта, предыдущий метод не нужен if (modelNew == null) { return false; } model.Status = modelNew.Status; model.ClientId = modelNew.ClientId; model.ImplementerId = modelNew.ImplementerId; if (!ChangeStatus(model, OrderStatus.Готов)) { _logger.LogWarning("Order's status is wrong"); throw new InvalidOperationException("Order's status is wrong"); } model.DateImplement = DateTime.Now; _orderStorage.Update(model); return true; } public bool DeliveryOrder(OrderBindingModel model) { var modelNew = ReadElement(new OrderSearchModel { Id = model.Id }); // в связи с обновлением контракта, предыдущий метод не нужен if (modelNew == null) { return false; } model.Status = modelNew.Status; model.ClientId = modelNew.ClientId; model.ImplementerId = modelNew.ImplementerId; model.DateImplement = modelNew.DateImplement; if (!ChangeStatus(model, OrderStatus.Выдан)) { _logger.LogWarning("Order's status is wrong"); return false; } _orderStorage.Update(model); return true; } 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; } private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (model.FurnitureId <= 0) { throw new ArgumentNullException("Неверный идентификатор изделия", nameof(model.FurnitureId)); } if (model.Count <= 0) { throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count)); } if (model.Sum <= 0) { throw new ArgumentNullException("Стоимость заказа должна быть больше 0", nameof(model.Sum)); } _logger.LogInformation("Order. OrderId: { Id}. OrderStatus: {OrderStatus} DateCreate: {DateCreate} " + "FurnitureId:{FurnitureId}. Count:{ Count}. Sum:{ Sum}. ", model.Id, model.Status, model.DateCreate, model.FurnitureId, model.Count, model.Sum); } public OrderViewModel? ReadElement(OrderSearchModel? model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Status: {Status}. ImplementerId: {ImplementerId} Id:{ Id}", model.Status, model.ImplementerId, model.Id); var element = _orderStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } private bool SendMail(int clientId, string subject, string text) { var client = _clientLogic.ReadElement(new() { Id = clientId}); if (client == null) { _logger.LogWarning("Email sending failed. Client with id {Id} not found", clientId); return false; } _mailWorker.MailSendAsync(new MailSendInfoBindingModel() { Subject = subject, Text = text, MailAddress = client.Email }); return true; } } }