2024-06-21 16:28:36 +04:00
|
|
|
|
using FurnitureAssemblyBusinessLogic.MailWorker;
|
|
|
|
|
using FurnitureAssemblyContracts.BindingModels;
|
2024-02-28 01:13:41 +04:00
|
|
|
|
using FurnitureAssemblyContracts.BusinessLogicsContracts;
|
|
|
|
|
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.BussinessLogic
|
|
|
|
|
{
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// Класс, реализующий логику для заказов
|
2024-02-28 01:13:41 +04:00
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
2024-02-28 11:03:17 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2024-05-22 02:16:26 +04:00
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
|
|
|
|
|
2024-06-21 16:28:36 +04:00
|
|
|
|
private readonly AbstractMailWorker _mailWorker;
|
|
|
|
|
|
|
|
|
|
private readonly IClientLogic _clientLogic;
|
|
|
|
|
|
|
|
|
|
private readonly object locker = new object();
|
2024-06-21 14:50:51 +04:00
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// Конструктор
|
2024-06-21 16:28:36 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
2024-06-21 16:28:36 +04:00
|
|
|
|
_mailWorker = mailWorker;
|
|
|
|
|
_clientLogic = clientLogic;
|
|
|
|
|
|
2024-02-28 01:13:41 +04:00
|
|
|
|
}
|
2024-05-22 02:16:26 +04:00
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// Вывод отфильтрованного списка компонентов
|
2024-02-28 01:13:41 +04:00
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
|
2024-05-22 02:16:26 +04:00
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// list хранит весь список в случае, если model пришло со значением null на вход метода
|
2024-02-28 01:13:41 +04:00
|
|
|
|
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
|
|
|
|
|
2024-06-21 14:50:51 +04:00
|
|
|
|
if (list == null)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2024-05-22 02:16:26 +04:00
|
|
|
|
|
2024-06-21 14:50:51 +04:00
|
|
|
|
// Вывод конкретного чека
|
|
|
|
|
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)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
if(model.Status != OrderStatus.Неизвестен)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed, incorrect order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
|
|
|
|
|
2024-06-21 16:28:36 +04:00
|
|
|
|
var result = _orderStorage.Insert(model);
|
|
|
|
|
|
|
|
|
|
if (result == null)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
model.Status = OrderStatus.Неизвестен;
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 16:28:36 +04:00
|
|
|
|
SendOrderMessage(result.ClientId, $"Сборка мебели, Заказ №{result.Id}", $"Заказ №{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят");
|
|
|
|
|
|
2024-02-28 01:13:41 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
2024-06-21 14:50:51 +04:00
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
2024-02-28 01:13:41 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Готов);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выдан);
|
|
|
|
|
}
|
2024-05-22 02:16:26 +04:00
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// Проверка на пустоту входного параметра
|
2024-02-28 01:13:41 +04:00
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2024-05-22 02:16:26 +04:00
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// При удалении параметру withParams передаём false
|
2024-02-28 01:13:41 +04:00
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// Проверка на наличие товаров в заказе
|
2024-06-21 16:28:36 +04:00
|
|
|
|
if (model.Count <= 0)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("В заказе не может быть 0 изделий", nameof(model.Count));
|
|
|
|
|
}
|
2024-05-22 10:14:48 +04:00
|
|
|
|
|
|
|
|
|
// Проверка на наличие нормальной суммарной стоимости чека
|
2024-06-21 16:28:36 +04:00
|
|
|
|
if (model.Sum <= 0)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Суммарная стоимость заказа должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
2024-05-22 10:14:48 +04:00
|
|
|
|
|
|
|
|
|
// Проверка корректности id у изделий
|
2024-06-21 16:28:36 +04:00
|
|
|
|
if (model.FurnitureId < 0)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный id у изделия", nameof(model.FurnitureId));
|
|
|
|
|
}
|
2024-05-22 10:14:48 +04:00
|
|
|
|
|
|
|
|
|
// Проверка корректности дат
|
2024-06-21 16:28:36 +04:00
|
|
|
|
if (model.DateCreate > model.DateImplement)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения");
|
|
|
|
|
}
|
2024-05-22 10:14:48 +04:00
|
|
|
|
|
|
|
|
|
// Проверка на клиента
|
2024-05-22 02:16:26 +04:00
|
|
|
|
if (model.ClientId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientId));
|
|
|
|
|
}
|
2024-02-28 01:13:41 +04:00
|
|
|
|
|
2024-05-22 02:16:26 +04:00
|
|
|
|
_logger.LogInformation("Order. OrderId:{Id}. Sum:{Sum}. ClientId:{ClientId}. FurnitureId:{Id}", model.Id, model.Sum, model.ClientId, model.FurnitureId);
|
2024-02-28 01:13:41 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// Обновление статуса заказа
|
2024-02-28 01:13:41 +04:00
|
|
|
|
public bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus)
|
|
|
|
|
{
|
|
|
|
|
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// Если не смогли найти указанный заказ по его Id
|
|
|
|
|
if(viewModel == null)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// Проверка на возможность обновления статуса на следующий
|
|
|
|
|
if(viewModel.Status + 1 != newOrderStatus)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
2024-05-22 02:16:26 +04:00
|
|
|
|
_logger.LogWarning("Status update operation failed. New status " + newOrderStatus.ToString() + "incorrect");
|
2024-02-28 01:13:41 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = newOrderStatus;
|
|
|
|
|
|
2024-06-21 14:50:51 +04:00
|
|
|
|
// Помещаем id работника, не забываем про него...
|
|
|
|
|
if (viewModel.ImplementerId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
model.ImplementerId = viewModel.ImplementerId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Проверка на выдачу
|
|
|
|
|
if (model.Status == OrderStatus.Выдан)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = viewModel.DateImplement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
2024-05-22 10:14:48 +04:00
|
|
|
|
// Финальная проверка на возможность обновления
|
2024-06-21 16:28:36 +04:00
|
|
|
|
var result = _orderStorage.Update(model);
|
|
|
|
|
|
|
|
|
|
if(result == null)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
model.Status--;
|
|
|
|
|
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 16:28:36 +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
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-28 01:13:41 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|