185 lines
6.9 KiB
C#
Raw Normal View History

2023-05-02 11:03:29 +04:00
using Microsoft.Extensions.Logging;
2023-05-01 18:29:41 +04:00
using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.BusinessLogicsContracts;
using SecuritySystemContracts.SearchModels;
2023-05-02 11:03:29 +04:00
using SecuritySystemContracts.StoragesContracts;
2023-05-01 18:29:41 +04:00
using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Enums;
2023-05-02 11:03:29 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SecuritySystemBusinessLogic.MailWorker;
2023-03-06 17:15:03 +04:00
using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.BusinessLogicsContracts;
using SecuritySystemContracts.SearchModels;
2023-05-02 11:03:29 +04:00
using SecuritySystemContracts.StorageContracts;
2023-03-06 17:15:03 +04:00
using SecuritySystemContracts.ViewModels;
2023-05-02 11:03:29 +04:00
using SecuritySystemDataModels;
2023-03-06 17:15:03 +04:00
using SecuritySystemDataModels.Enums;
2023-04-18 15:02:12 +04:00
namespace SecuritySystemBusinessLogic.BusinessLogics
2023-03-06 17:15:03 +04:00
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
2023-05-02 11:03:29 +04:00
private readonly AbstractMailWorker _abstractMailWorker;
private readonly IClientStorage _clientStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage
orderStorage, AbstractMailWorker abstractMailWorker, IClientStorage clientStorage)
2023-03-06 17:15:03 +04:00
{
_logger = logger;
_orderStorage = orderStorage;
2023-05-02 11:03:29 +04:00
_abstractMailWorker = abstractMailWorker;
_clientStorage = clientStorage;
2023-03-06 17:15:03 +04:00
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
2023-05-02 11:03:29 +04:00
_logger.LogInformation("ReadList. Order.Id:{ Id}", model?.Id);
2023-03-06 17:15:03 +04:00
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-05-02 11:03:29 +04:00
2023-05-01 18:29:41 +04:00
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2023-05-02 11:03:29 +04:00
_logger.LogInformation("ReadElement.Id:{ Id}", model.Id);
2023-05-01 18:29:41 +04:00
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;
}
2023-03-06 17:15:03 +04:00
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
2023-05-01 18:29:41 +04:00
if (model.Status != OrderStatus.Неизвестен)
{
2023-05-02 11:03:29 +04:00
_logger.LogWarning("Insert operation failed");
2023-05-01 18:29:41 +04:00
return false;
}
model.Status = OrderStatus.Принят;
2023-05-02 11:03:29 +04:00
var order = _orderStorage.Insert(model);
if (order == null)
2023-03-06 17:15:03 +04:00
{
_logger.LogWarning("Insert operation failed");
return false;
}
2023-05-02 11:03:29 +04:00
var client = _clientStorage.GetElement(new() { Id = order.ClientId });
if (client == null)
{
_logger.LogWarning("Client not found");
return false;
}
SendMail(client.Email, $"Новый заказ создан. Номер заказа - {order.Id}", $"Заказ №{order.Id} от {order.DateCreate} на сумму {order.Sum:C2} принят.");
2023-03-06 17:15:03 +04:00
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
2023-05-02 11:03:29 +04:00
return ChangeStatus(model, OrderStatus.Выполняется);
2023-03-06 17:15:03 +04:00
}
public bool DeliveryOrder(OrderBindingModel model)
{
2023-05-02 11:03:29 +04:00
return ChangeStatus(model, OrderStatus.Выдан);
2023-03-06 17:15:03 +04:00
}
2023-05-02 11:03:29 +04:00
2023-03-06 17:15:03 +04:00
public bool FinishOrder(OrderBindingModel model)
{
2023-05-02 11:03:29 +04:00
return ChangeStatus(model, OrderStatus.Готов);
2023-03-06 17:15:03 +04:00
}
2023-05-02 11:03:29 +04:00
public bool ChangeStatus(OrderBindingModel model, OrderStatus newStatus)
2023-03-06 17:15:03 +04:00
{
2023-05-02 11:03:29 +04:00
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (viewModel == null)
2023-03-06 17:15:03 +04:00
{
throw new ArgumentNullException(nameof(model));
}
2023-05-02 11:03:29 +04:00
if (viewModel.Status + 1 != newStatus)
2023-03-06 17:15:03 +04:00
{
2023-05-02 11:03:29 +04:00
_logger.LogWarning("Change status operation failed");
return false;
2023-03-06 17:15:03 +04:00
}
2023-05-02 11:03:29 +04:00
if (viewModel.ImplementerId.HasValue)
2023-03-06 17:15:03 +04:00
{
2023-05-02 11:03:29 +04:00
model.ImplementerId = viewModel.ImplementerId;
2023-03-06 17:15:03 +04:00
}
2023-05-02 11:03:29 +04:00
model.Status = newStatus;
model.SecureId = viewModel.SecureId;
model.Count = viewModel.Count;
model.Sum = viewModel.Sum;
model.DateCreate = viewModel.DateCreate;
if (model.Status == OrderStatus.Готов)
2023-03-06 17:15:03 +04:00
{
2023-05-02 11:03:29 +04:00
model.DateImplement = DateTime.Now;
2023-03-06 17:15:03 +04:00
}
2023-05-02 11:03:29 +04:00
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model);
var order = _orderStorage.Update(model);
if (order == null)
{
_logger.LogWarning("Change status operation failed");
return false;
}
var client = _clientStorage.GetElement(new() { Id = order.ClientId });
if (client == null)
2023-03-06 17:15:03 +04:00
{
2023-05-02 11:03:29 +04:00
_logger.LogWarning("Client not found");
return false;
2023-03-06 17:15:03 +04:00
}
2023-05-02 11:03:29 +04:00
SendMail(client.Email, $"Заказ №{order.Id}", $"Заказ №{order.Id} изменил статус на {order.Status}.");
return true;
2023-03-06 17:15:03 +04:00
}
2023-05-02 11:03:29 +04:00
private void CheckModel(OrderBindingModel model, bool withParams =
true)
2023-03-06 17:15:03 +04:00
{
2023-05-02 11:03:29 +04:00
if (model == null)
2023-03-06 17:15:03 +04:00
{
2023-05-01 18:29:41 +04:00
throw new ArgumentNullException(nameof(model));
}
2023-05-02 11:03:29 +04:00
if (!withParams)
2023-05-01 18:29:41 +04:00
{
2023-05-02 11:03:29 +04:00
return;
2023-03-06 17:15:03 +04:00
}
2023-05-02 11:03:29 +04:00
if (model.SecureId < 0)
2023-05-01 18:29:41 +04:00
{
2023-05-02 11:03:29 +04:00
throw new ArgumentNullException("Нет изделия c таким id", nameof(model));
2023-05-01 18:29:41 +04:00
}
2023-05-02 11:03:29 +04:00
if (model.Count <= 0)
2023-05-01 18:29:41 +04:00
{
2023-05-02 11:03:29 +04:00
throw new ArgumentNullException("Количество изделий заказа должна быть больше 0", nameof(model.Count));
2023-05-01 18:29:41 +04:00
}
2023-05-02 11:03:29 +04:00
if (model.Sum <= 0)
2023-05-01 18:29:41 +04:00
{
2023-05-02 11:03:29 +04:00
throw new ArgumentException("Сумма заказа должна быть больше 0", nameof(model.Sum));
2023-05-01 18:29:41 +04:00
}
2023-05-02 11:03:29 +04:00
_logger.LogInformation("Order. OrderId: { Id}. SecureId: {SecureId}. Count: {Count}. Sum: {Sum}", model.Id, model.SecureId, model.Count, model.Sum);
}
public void SendMail(string email, string title, string body)
{
_abstractMailWorker.MailSendAsync(new()
2023-03-06 17:15:03 +04:00
{
2023-05-02 11:03:29 +04:00
MailAddress = email,
Subject = title,
Text = body
});
2023-03-06 17:15:03 +04:00
}
}
}