2024-02-26 10:12:21 +04:00
|
|
|
|
using FurnitureAssemblyContracts.BindingModels;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
// Класс, реализующий логику для заказов
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
private readonly IShopLogic _shopLogic;
|
|
|
|
|
|
|
|
|
|
private readonly IFurnitureStorage _furnitureStorage;
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-12 02:24:28 +04:00
|
|
|
|
static readonly object locker = new object();
|
|
|
|
|
|
2024-02-26 10:12:21 +04:00
|
|
|
|
// Конструктор
|
2024-05-11 23:46:17 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IFurnitureStorage furnitureStorage)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
2024-05-11 23:46:17 +04:00
|
|
|
|
_shopLogic = shopLogic;
|
|
|
|
|
_furnitureStorage = furnitureStorage;
|
2024-02-26 10:12:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Вывод отфильтрованного списка компонентов
|
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
|
|
|
|
|
|
|
|
|
|
// list хранит весь список в случае, если model пришло со значением null на вход метода
|
|
|
|
|
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
|
|
|
|
|
2024-05-11 02:30:02 +04:00
|
|
|
|
if (list == null)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
public OrderViewModel? ReadElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
_logger.LogInformation("ReadElement. Id:{Id}", model?.Id);
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
var element = _orderStorage.GetElement(model);
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
return element;
|
|
|
|
|
}
|
2024-05-11 02:30:02 +04:00
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
// Создание чека
|
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (model.Status != OrderStatus.Неизвестен)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed, incorrect order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
2024-05-11 23:46:17 +04:00
|
|
|
|
|
2024-02-26 10:12:21 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
2024-05-12 02:24:28 +04:00
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
2024-02-26 10:12:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Готов);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выдан);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Проверка на пустоту входного параметра
|
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// При удалении параметру withParams передаём false
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Проверка на наличие товаров в заказе
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (model.Count <= 0)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("В заказе не может быть 0 изделий", nameof(model.Count));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Проверка на наличие нормальной суммарной стоимости чека
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (model.Sum <= 0)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Суммарная стоимость заказа должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Проверка корректности id у изделий
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (model.FurnitureId < 0)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный id у изделия", nameof(model.FurnitureId));
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 22:00:13 +04:00
|
|
|
|
// Проверка на клиента
|
|
|
|
|
if (model.ClientId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientId));
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
// Проверка корректности дат
|
|
|
|
|
if (model.DateCreate > model.DateImplement)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-12 00:27:31 +04:00
|
|
|
|
_logger.LogInformation("Order. OrderId:{Id}, Sum:{Sum}. ClientId:{ClientId}. FurnitureId:{Id}", model.Id, model.Sum, model.ClientId, model.FurnitureId);
|
2024-02-26 10:12:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Обновление статуса заказа
|
|
|
|
|
public bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus)
|
|
|
|
|
{
|
|
|
|
|
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
|
|
|
|
|
// Если не смогли найти указанный заказ по его Id
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (viewModel == null)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Проверка на возможность обновления статуса на следующий
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (viewModel.Status + 1 != newOrderStatus && viewModel.Status != OrderStatus.Ожидание)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Status update operation failed. New status " + newOrderStatus.ToString() + "incorrect");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = newOrderStatus;
|
|
|
|
|
|
2024-05-12 02:24:28 +04:00
|
|
|
|
// Помещаем id работника, не забываем про него...
|
|
|
|
|
if (viewModel.ImplementerId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
model.ImplementerId = viewModel.ImplementerId;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
// Проверка на выдачу
|
|
|
|
|
if (model.Status == OrderStatus.Готов || viewModel.Status == OrderStatus.Ожидание)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
2024-05-11 23:46:17 +04:00
|
|
|
|
|
|
|
|
|
var furniture = _furnitureStorage.GetElement(new() { Id = viewModel.FurnitureId });
|
|
|
|
|
|
|
|
|
|
if (furniture == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(furniture));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_shopLogic.AddFurnitures(furniture, viewModel.Count))
|
|
|
|
|
{
|
|
|
|
|
model.Status = OrderStatus.Ожидание;
|
|
|
|
|
_logger.LogWarning($"Невозможно выдать изделия, магазины переполнены. AddFurnitures operation failed. Shop is full.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
}
|
2024-02-26 10:12:21 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = viewModel.DateImplement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
// Финальная проверка на возможность обновления
|
2024-05-11 23:46:17 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
2024-02-26 10:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|