2023-03-06 17:15:03 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SecuritySystemContracts.BindingModels;
|
|
|
|
|
using SecuritySystemContracts.BusinessLogicsContracts;
|
|
|
|
|
using SecuritySystemContracts.SearchModels;
|
|
|
|
|
using SecuritySystemContracts.StoragesContracts;
|
|
|
|
|
using SecuritySystemContracts.ViewModels;
|
|
|
|
|
using SecuritySystemDataModels.Enums;
|
|
|
|
|
|
|
|
|
|
namespace SecuritySystemBusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
|
|
|
|
|
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. OrderId:{Id}", model?.Id);
|
|
|
|
|
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (!CheckStatus(model, OrderStatus.Принят, false)) return false;
|
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (!CheckStatus(model, OrderStatus.Выполняется)) return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (!CheckStatus(model, OrderStatus.Выдан)) return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (!CheckStatus(model, OrderStatus.Готов)) return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-07 12:47:29 +04:00
|
|
|
|
if (model.SecureId < 0)
|
2023-03-06 17:15:03 +04:00
|
|
|
|
{
|
2023-03-07 12:47:29 +04:00
|
|
|
|
throw new ArgumentNullException("Некорректный идентификатор камеры", nameof(model.SecureId));
|
2023-03-06 17:15:03 +04:00
|
|
|
|
}
|
|
|
|
|
if (model.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Количество камер в заказе должно быть больше 0", nameof(model.Count));
|
|
|
|
|
}
|
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
2023-03-07 12:47:29 +04:00
|
|
|
|
_logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. SecureId: { SecureId}", model.Id, model.Sum, model.SecureId);
|
2023-03-06 17:15:03 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckStatus(OrderBindingModel model, OrderStatus newstatus, bool update = true)
|
|
|
|
|
{
|
|
|
|
|
if (model.Status != newstatus - 1)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Failed to change status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
model.Status = newstatus;
|
|
|
|
|
if (!update) return true;
|
|
|
|
|
if (_orderStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|