2023-04-23 19:48:34 +04:00

173 lines
5.7 KiB
C#

using DocumentFormat.OpenXml.EMMA;
using Microsoft.Extensions.Logging;
using SushiBarBusinessLogic.MailWorker;
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Enums;
namespace SushiBarBusinessLogic.BusinessLogics
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
private readonly AbstractMailWorker _mailWorker;
private readonly IClientLogic _clientLogic;
public OrderLogic(ILogger<OrderLogic> logger,
IOrderStorage orderStorage,
AbstractMailWorker mailWorker,
IClientLogic clientLogic)
{
_logger = logger;
_orderStorage = orderStorage;
_mailWorker = mailWorker;
_clientLogic = clientLogic;
}
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", 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;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Unknown)
{
_logger.LogWarning("Insert operation failed. Order status incorrect.");
return false;
}
model.Status = OrderStatus.Accepted;
if (_orderStorage.Insert(model) != null) return true;
_logger.LogWarning("Insert operation failed");
return false;
}
public bool DeliveryOrder(OrderBindingModel model)
{
return UpdateStatus(model, OrderStatus.Ready);
}
public bool FinishOrder(OrderBindingModel model)
{
return UpdateStatus(model, OrderStatus.Issued);
}
public List<OrderViewModel>? 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 TakeOrderInWork(OrderBindingModel model)
{
return UpdateStatus(model, OrderStatus.Performed);
}
private bool UpdateStatus(OrderBindingModel model, OrderStatus status)
{
var order = _orderStorage.GetElement(new OrderSearchModel() { Id = model.Id });
if (order == null)
{
throw new ArgumentNullException(nameof(order));
}
if (order.Status + 1 != status)
{
_logger.LogWarning("Status update operation failed");
return false;
}
model.Status = status;
model.DateCreate = order.DateCreate;
model.DateImplement ??= order.DateImplement;
if (order.ImplementerId.HasValue)
model.ImplementerId = order.ImplementerId;
if (model.Status == OrderStatus.Issued)
{
model.DateImplement = DateTime.Now;
}
model.ClientId = order.ClientId;
model.SushiId = order.SushiId;
model.Sum = order.Sum;
model.Count = order.Count;
var result = _orderStorage.Update(model);
if (result == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
SendOrderMessage(result.ClientId,
$"Sushi Bar, Order №{result.Id}",
$"Order №{model.Id} changed status on {result.Status}");
return true;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
_logger.LogInformation("Order. OrderId:{Id} .Count:{Count} .Sum:{Sum} .Status{Status} .DateCreate{DateCreate}", model.Id, model.Count, model.Sum, model.Status.ToString(), model.DateCreate.ToString());
var element = _orderStorage.GetElement(new OrderSearchModel
{
Id = model.Id
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("This name is already exists");
}
}
private bool SendOrderMessage(int clientId, string subject, string text)
{
var client = _clientLogic.ReadElement(new ClientSearchModel { Id = clientId });
if (client == null)
{
return false;
}
_mailWorker.MailSendAsync(new MailSendInfoBindingModel
{
MailAddress = client.Email,
Subject = subject,
Text = text
});
return true;
}
}
}