2024-02-28 01:13:41 +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
|
|
|
|
|
{
|
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-05-22 10:14:48 +04:00
|
|
|
|
// Конструктор
|
2024-02-28 01:13:41 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
|
|
|
|
}
|
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-05-22 10:14:48 +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-05-22 10:14:48 +04:00
|
|
|
|
// Создание чека
|
2024-02-28 01:13:41 +04:00
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
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-05-22 10:14:48 +04:00
|
|
|
|
if(_orderStorage.Insert(model) == null)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
model.Status = OrderStatus.Неизвестен;
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
// Проверка на наличие товаров в заказе
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
// Проверка на наличие нормальной суммарной стоимости чека
|
|
|
|
|
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 у изделий
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
// Проверка корректности дат
|
|
|
|
|
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-05-22 10:14:48 +04:00
|
|
|
|
// Проверка на выдачу
|
|
|
|
|
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
|
|
|
|
// Финальная проверка на возможность обновления
|
|
|
|
|
if(_orderStorage.Update(model) == null)
|
2024-02-28 01:13:41 +04:00
|
|
|
|
{
|
|
|
|
|
model.Status--;
|
|
|
|
|
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|