2023-04-30 19:27:47 +04:00
|
|
|
|
using JewelryStoreBusinessLogic.MailWorker;
|
|
|
|
|
using JewelryStoreContracts.BindingModels;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
using JewelryStoreContracts.BusinessLogicsContracts;
|
|
|
|
|
using JewelryStoreContracts.SearchModels;
|
2023-02-06 15:42:50 +04:00
|
|
|
|
using JewelryStoreContracts.StoragesContracts;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
using JewelryStoreContracts.ViewModels;
|
2023-02-06 15:42:50 +04:00
|
|
|
|
using JewelryStoreDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace JewelryStoreBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
2023-02-05 21:54:36 +04:00
|
|
|
|
public class OrderLogic : IOrderLogic //TODO реализовать интерфейс
|
2023-02-06 15:42:50 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
2023-04-30 19:27:47 +04:00
|
|
|
|
private readonly AbstractMailWorker _mailWorker;
|
|
|
|
|
private readonly IClientLogic _clientLogic;
|
2023-04-17 23:38:44 +04:00
|
|
|
|
|
2023-04-30 19:27:47 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic)
|
2023-02-06 15:42:50 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
2023-04-30 19:27:47 +04:00
|
|
|
|
_mailWorker = mailWorker;
|
|
|
|
|
_clientLogic = clientLogic;
|
|
|
|
|
}
|
2023-02-06 15:42:50 +04:00
|
|
|
|
|
2023-02-05 19:24:53 +04:00
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-02-06 15:42:50 +04:00
|
|
|
|
CheckModel(model);
|
2023-04-17 23:38:44 +04:00
|
|
|
|
|
|
|
|
|
if (model.Status != OrderStatus.Неизвестен)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed. Order status incorrect.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 15:42:50 +04:00
|
|
|
|
model.Status = OrderStatus.Принят;
|
2023-04-30 19:27:47 +04:00
|
|
|
|
var result = _orderStorage.Update(model);
|
2023-04-17 23:38:44 +04:00
|
|
|
|
|
2023-04-30 19:27:47 +04:00
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
2023-04-17 23:38:44 +04:00
|
|
|
|
model.Status = OrderStatus.Неизвестен;
|
2023-02-06 15:42:50 +04:00
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-30 19:27:47 +04:00
|
|
|
|
SendOrderMessage(result.ClientId, $"Установка ПО, Заказ №{result.Id}", $"Заказ №{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят");
|
|
|
|
|
return true;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
|
2023-02-05 19:24:53 +04:00
|
|
|
|
{
|
2023-04-17 23:38:44 +04:00
|
|
|
|
var vmodel = _orderStorage.GetElement(new() { Id = model.Id });
|
2023-02-06 15:42:50 +04:00
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
if (vmodel == null)
|
2023-02-06 15:42:50 +04:00
|
|
|
|
{
|
2023-04-17 23:38:44 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
2023-02-06 15:42:50 +04:00
|
|
|
|
}
|
2023-02-05 19:24:53 +04:00
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
if ((int)vmodel.Status + 1 != (int)newStatus)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"Попытка перевести заказ не в следующий статус: " +
|
|
|
|
|
$"Текущий статус: {vmodel.Status} \n" +
|
|
|
|
|
$"Планируемый статус: {newStatus} \n" +
|
|
|
|
|
$"Доступный статус: {(OrderStatus)((int)vmodel.Status + 1)}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = newStatus;
|
|
|
|
|
model.DateCreate = vmodel.DateCreate;
|
|
|
|
|
|
|
|
|
|
if (model.DateImplement == null)
|
|
|
|
|
model.DateImplement = vmodel.DateImplement;
|
|
|
|
|
|
|
|
|
|
if (vmodel.ImplementerId.HasValue)
|
|
|
|
|
model.ImplementerId = vmodel.ImplementerId;
|
|
|
|
|
|
|
|
|
|
model.JewelId = vmodel.JewelId;
|
|
|
|
|
model.Sum = vmodel.Sum;
|
|
|
|
|
model.Count = vmodel.Count;
|
2023-04-30 19:27:47 +04:00
|
|
|
|
var result = _orderStorage.Update(model);
|
2023-02-06 15:42:50 +04:00
|
|
|
|
|
2023-04-30 19:27:47 +04:00
|
|
|
|
if (result == null)
|
2023-02-06 15:42:50 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-04-17 23:38:44 +04:00
|
|
|
|
|
2023-02-06 15:42:50 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-30 19:27:47 +04:00
|
|
|
|
SendOrderMessage(result.ClientId, $"Установка ПО, Заказ №{result.Id}", $"Заказ №{model.Id} изменен статус на {result.Status}");
|
|
|
|
|
return true;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 19:27:47 +04:00
|
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
2023-04-17 23:38:44 +04:00
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Готов);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выдан);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 19:24:53 +04:00
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
2023-04-17 23:38:44 +04:00
|
|
|
|
_logger.LogInformation("Order. OrderId:{Id}", model?.Id);
|
|
|
|
|
|
2023-02-06 15:42:50 +04:00
|
|
|
|
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
2023-04-17 23:38:44 +04:00
|
|
|
|
|
2023-02-06 15:42:50 +04:00
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-17 23:38:44 +04:00
|
|
|
|
|
2023-02-06 15:42:50 +04:00
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
return list;
|
2023-02-05 19:24:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
private bool CheckModel(OrderBindingModel model)
|
2023-02-05 19:24:53 +04:00
|
|
|
|
{
|
2023-04-17 23:38:44 +04:00
|
|
|
|
if (model == null)
|
2023-02-06 15:42:50 +04:00
|
|
|
|
{
|
2023-04-17 23:38:44 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (model.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Количество изделий в заказе должно быть больше 0", nameof(model.Count));
|
2023-02-06 15:42:50 +04:00
|
|
|
|
}
|
2023-04-17 23:38:44 +04:00
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Суммарная стоимость заказа должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
if (model.DateCreate > model.DateImplement)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Время создания заказа не может быть больше времени его выполнения", nameof(model.DateImplement));
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 15:42:50 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
public OrderViewModel? ReadElement(OrderSearchModel model)
|
2023-02-06 15:42:50 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
|
2023-02-06 15:42:50 +04:00
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
var element = _orderStorage.GetElement(model);
|
2023-02-06 15:42:50 +04:00
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
if (element == null)
|
2023-02-06 15:42:50 +04:00
|
|
|
|
{
|
2023-04-17 23:38:44 +04:00
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
2023-02-06 15:42:50 +04:00
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
return null;
|
2023-02-06 15:42:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
2023-02-06 15:42:50 +04:00
|
|
|
|
|
2023-04-17 23:38:44 +04:00
|
|
|
|
return element;
|
|
|
|
|
}
|
2023-02-05 19:24:53 +04:00
|
|
|
|
}
|
|
|
|
|
}
|