2024-05-07 15:10:34 +04:00
|
|
|
|
using SecuritySystemContracts.BindingModels;
|
|
|
|
|
using SecuritySystemContracts.BusinessLogicsContracts;
|
|
|
|
|
using SecuritySystemContracts.SearchModels;
|
|
|
|
|
using SecuritySystemContracts.StoragesContracts;
|
|
|
|
|
using SecuritySystemContracts.ViewModels;
|
|
|
|
|
using SecuritySystemDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SecuritySystemBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
|
|
|
|
|
2024-05-08 08:03:51 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//вывод отфильтрованного списка компонентов
|
|
|
|
|
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-08 08:03:51 +04:00
|
|
|
|
if (list == null)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 08:37:10 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 15:10:34 +04:00
|
|
|
|
//создание чека
|
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
2024-05-08 08:03:51 +04:00
|
|
|
|
if (model.Status != OrderStatus.Неизвестен)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed, incorrect order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
|
|
|
|
|
2024-05-08 08:03:51 +04:00
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
model.Status = OrderStatus.Неизвестен;
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-05-08 08:03:51 +04:00
|
|
|
|
|
2024-05-07 15:10:34 +04:00
|
|
|
|
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.Выдан);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на пустоту входного параметра
|
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
2024-05-08 08:03:51 +04:00
|
|
|
|
if (model == null)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//так как при удалении параметром withParams передаём false
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие товаров в заказе
|
2024-05-08 08:03:51 +04:00
|
|
|
|
if (model.Count <= 0)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("В заказе не может быть 0 изделий", nameof(model.Count));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на наличие нормальной суммарной стоимости чека
|
2024-05-08 08:03:51 +04:00
|
|
|
|
if (model.Sum <= 0)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Суммарная стоимость заказа должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка корректности id у изделий
|
|
|
|
|
if (model.SecureId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный id у изделия", nameof(model.SecureId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка корректности дат
|
2024-05-08 08:03:51 +04:00
|
|
|
|
if (model.DateCreate > model.DateImplement)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 08:03:51 +04:00
|
|
|
|
if (model.ClientId < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Order. OrderId:{Id}. Sum:{Sum}. ClientId:{ClientId}. SecureId:{Id}", model.Id, model.Sum, model.ClientId, model.SecureId);
|
2024-05-07 15:10:34 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//обновление статуса заказа
|
|
|
|
|
public bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus)
|
|
|
|
|
{
|
|
|
|
|
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
|
|
|
|
|
//если не смогли найти указанный заказ по его Id
|
2024-05-08 08:03:51 +04:00
|
|
|
|
if (viewModel == null)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка на возможность обновления статуса на следующий
|
|
|
|
|
if (viewModel.Status + 1 != newOrderStatus)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Status update operation failed. New status " + newOrderStatus.ToString() + " incorrect");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = newOrderStatus;
|
|
|
|
|
|
2024-05-08 08:37:10 +04:00
|
|
|
|
//не забываем положить id работника
|
|
|
|
|
if (viewModel.ImplementerId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
model.ImplementerId = viewModel.ImplementerId;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 15:10:34 +04:00
|
|
|
|
//проверка на выдачу
|
2024-05-08 08:03:51 +04:00
|
|
|
|
if (model.Status == OrderStatus.Выдан)
|
2024-05-07 15:10:34 +04:00
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = viewModel.DateImplement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
//финальная проверка на возможность обновления
|
|
|
|
|
if (_orderStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
model.Status--;
|
|
|
|
|
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-08 08:03:51 +04:00
|
|
|
|
}
|