2023-01-31 13:05:58 +04:00
|
|
|
|
using SofrwareInstallationContracts.BindingModels;
|
2023-01-30 14:48:39 +04:00
|
|
|
|
using SofrwareInstallationContracts.BusinessLogicsContracts;
|
|
|
|
|
using SofrwareInstallationContracts.SearchModels;
|
|
|
|
|
using SofrwareInstallationContracts.StoragesContracts;
|
|
|
|
|
using SofrwareInstallationContracts.ViewModels;
|
2023-01-29 22:45:01 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-01-30 19:31:32 +04:00
|
|
|
|
using SoftwareInstallationDataModels.Enums;
|
2023-04-21 23:21:49 +04:00
|
|
|
|
using SoftwareInstallationBusinessLogic.MailWorker;
|
2023-01-29 22:45:01 +04:00
|
|
|
|
|
2023-01-30 14:48:39 +04:00
|
|
|
|
namespace SoftwareInstallationBusinessLogic.BusinessLogic
|
2023-01-29 22:45:01 +04:00
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
2023-01-30 19:31:32 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
2023-04-21 23:21:49 +04:00
|
|
|
|
private readonly AbstractMailWorker _mailWorker;
|
|
|
|
|
private readonly IClientLogic _clientLogic;
|
2023-01-30 19:31:32 +04:00
|
|
|
|
|
2023-04-21 23:21:49 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic)
|
2023-01-30 19:31:32 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
2023-04-21 23:21:49 +04:00
|
|
|
|
_mailWorker = mailWorker;
|
|
|
|
|
_clientLogic = clientLogic;
|
2023-01-30 19:31:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-29 22:45:01 +04:00
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-01-30 19:31:32 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (model.Status != OrderStatus.Неизвестен)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed. Order status incorrect.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
2023-04-21 23:21:49 +04:00
|
|
|
|
var result = _orderStorage.Insert(model);
|
2023-01-30 19:31:32 +04:00
|
|
|
|
|
2023-04-21 23:21:49 +04:00
|
|
|
|
if (result == null)
|
2023-01-30 19:31:32 +04:00
|
|
|
|
{
|
|
|
|
|
model.Status = OrderStatus.Неизвестен;
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 23:21:49 +04:00
|
|
|
|
SendOrderMessage(result.ClientId, $"Установка ПО, Заказ №{result.Id}", $"Заказ №{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят");
|
|
|
|
|
|
2023-01-30 19:31:32 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
|
|
|
|
|
{
|
2023-04-10 23:07:27 +04:00
|
|
|
|
var vmodel = _orderStorage.GetElement(new() { Id = model.Id });
|
2023-01-30 19:31:32 +04:00
|
|
|
|
|
2023-04-10 23:07:27 +04:00
|
|
|
|
if (vmodel == null)
|
2023-01-30 19:31:32 +04:00
|
|
|
|
{
|
2023-04-10 23:07:27 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((int)vmodel.Status + 1 != (int)newStatus)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"Попытка перевести заказ не в следующий статус: " +
|
|
|
|
|
$"Текущий статус: {vmodel.Status} \n" +
|
|
|
|
|
$"Планируемый статус: {newStatus} \n" +
|
|
|
|
|
$"Доступный статус: {(OrderStatus)((int)vmodel.Status + 1)}");
|
2023-01-30 19:31:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = newStatus;
|
2023-04-10 23:07:27 +04:00
|
|
|
|
model.DateCreate = vmodel.DateCreate;
|
|
|
|
|
|
|
|
|
|
if (model.DateImplement == null)
|
|
|
|
|
model.DateImplement = vmodel.DateImplement;
|
2023-01-30 19:31:32 +04:00
|
|
|
|
|
2023-04-10 23:07:27 +04:00
|
|
|
|
if (vmodel.ImplementerId.HasValue)
|
|
|
|
|
model.ImplementerId = vmodel.ImplementerId;
|
|
|
|
|
|
|
|
|
|
model.PackageId = vmodel.PackageId;
|
|
|
|
|
model.Sum = vmodel.Sum;
|
|
|
|
|
model.Count = vmodel.Count;
|
2023-01-30 19:31:32 +04:00
|
|
|
|
|
2023-04-21 23:21:49 +04:00
|
|
|
|
var result = _orderStorage.Update(model);
|
|
|
|
|
|
|
|
|
|
if (result == null)
|
2023-01-30 19:31:32 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-04-10 23:07:27 +04:00
|
|
|
|
|
2023-01-30 19:31:32 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 23:21:49 +04:00
|
|
|
|
|
|
|
|
|
SendOrderMessage(result.ClientId, $"Установка ПО, Заказ №{result.Id}", $"Заказ №{model.Id} изменен статус на {result.Status}");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool SendOrderMessage(int clientId, string subject, string text)
|
|
|
|
|
{
|
|
|
|
|
var client = _clientLogic.ReadElement(new() { Id = clientId });
|
|
|
|
|
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mailWorker.MailSendAsync(new()
|
|
|
|
|
{
|
|
|
|
|
MailAddress = client.Email,
|
|
|
|
|
Subject = subject,
|
|
|
|
|
Text = text
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-30 19:31:32 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
2023-01-29 22:45:01 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-04-10 23:07:27 +04:00
|
|
|
|
model.DateImplement = DateTime.Now;
|
2023-01-30 19:31:32 +04:00
|
|
|
|
return StatusUpdate(model, OrderStatus.Готов);
|
2023-01-29 22:45:01 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-01-30 19:31:32 +04:00
|
|
|
|
return StatusUpdate(model, OrderStatus.Выдан);
|
2023-01-29 22:45:01 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
2023-01-30 19:31:32 +04:00
|
|
|
|
_logger.LogInformation("Order. OrderId:{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;
|
2023-01-29 22:45:01 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 23:21:49 +04:00
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
2023-01-29 22:45:01 +04:00
|
|
|
|
{
|
2023-01-30 19:31:32 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2023-04-21 23:21:49 +04:00
|
|
|
|
if (!withParams)
|
2023-01-30 19:31:32 +04:00
|
|
|
|
{
|
2023-04-21 23:21:49 +04:00
|
|
|
|
return;
|
2023-01-30 19:31:32 +04:00
|
|
|
|
}
|
2023-04-21 23:21:49 +04:00
|
|
|
|
if (model.PackageId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный идентификатор у изделия", nameof(model.PackageId));
|
|
|
|
|
}
|
|
|
|
|
if (model.Count <= 0)
|
2023-04-10 23:07:27 +04:00
|
|
|
|
{
|
2023-04-21 23:21:49 +04:00
|
|
|
|
throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count));
|
2023-04-10 23:07:27 +04:00
|
|
|
|
}
|
2023-04-21 23:21:49 +04:00
|
|
|
|
if (model.Sum <= 0)
|
2023-01-30 19:31:32 +04:00
|
|
|
|
{
|
2023-04-21 23:21:49 +04:00
|
|
|
|
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
|
2023-01-30 19:31:32 +04:00
|
|
|
|
}
|
2023-04-21 23:21:49 +04:00
|
|
|
|
_logger.LogInformation("Order. OrderID:{Id}. Sum:{ Sum}. PackageId: { PackageId}", model.Id, model.Sum, model.PackageId);
|
2023-04-10 23:07:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? ReadElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
2023-01-30 19:31:32 +04:00
|
|
|
|
{
|
2023-04-10 23:07:27 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
2023-01-30 19:31:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 23:07:27 +04:00
|
|
|
|
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _orderStorage.GetElement(model);
|
|
|
|
|
|
|
|
|
|
if (element == null)
|
2023-01-30 19:31:32 +04:00
|
|
|
|
{
|
2023-04-10 23:07:27 +04:00
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
|
|
|
|
|
return null;
|
2023-01-30 19:31:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 23:07:27 +04:00
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
|
|
|
|
|
return element;
|
2023-01-29 22:45:01 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|