2024-02-10 02:12:37 +04:00
|
|
|
|
using ComputersShopContracts.BindingModels;
|
|
|
|
|
using ComputersShopContracts.BuisnessLogicsContracts;
|
|
|
|
|
using ComputersShopContracts.SearchModels;
|
|
|
|
|
using ComputersShopContracts.StoragesContracts;
|
|
|
|
|
using ComputersShopContracts.ViewModels;
|
|
|
|
|
using ComputersShopDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ComputersShopBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
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. Id:{Id}", model?.Id);
|
|
|
|
|
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFiltredList(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 (model.Status != OrderStatus.Неизвестен)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Wrong order status. Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-02-19 22:16:00 +04:00
|
|
|
|
public bool ChangeStatus(OrderBindingModel model, OrderStatus newStatus)
|
2024-02-10 02:12:37 +04:00
|
|
|
|
{
|
2024-02-19 22:16:00 +04:00
|
|
|
|
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
if (viewModel == null)
|
2024-02-10 02:12:37 +04:00
|
|
|
|
{
|
2024-02-19 22:16:00 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(viewModel));
|
2024-02-10 02:12:37 +04:00
|
|
|
|
}
|
2024-02-19 22:16:00 +04:00
|
|
|
|
if (viewModel.Status + 1 != newStatus)
|
2024-02-10 02:12:37 +04:00
|
|
|
|
{
|
2024-02-19 22:16:00 +04:00
|
|
|
|
_logger.LogWarning("Status change operation failed");
|
2024-02-10 02:12:37 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-02-19 22:16:00 +04:00
|
|
|
|
|
|
|
|
|
model.Status = newStatus;
|
|
|
|
|
model.ComputerId = viewModel.ComputerId;
|
|
|
|
|
model.Count = viewModel.Count;
|
|
|
|
|
model.Sum = viewModel.Sum;
|
|
|
|
|
model.DateCreate = viewModel.DateCreate;
|
|
|
|
|
if (model.Status == OrderStatus.Готов)
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-02-10 02:12:37 +04:00
|
|
|
|
{
|
2024-02-19 22:16:00 +04:00
|
|
|
|
model.DateImplement = viewModel.DateImplement;
|
2024-02-10 02:12:37 +04:00
|
|
|
|
}
|
2024-02-19 22:16:00 +04:00
|
|
|
|
CheckModel(model, false);
|
2024-02-10 02:12:37 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
|
|
|
|
{
|
2024-02-19 22:16:00 +04:00
|
|
|
|
_logger.LogWarning("Status change operation failed");
|
2024-02-10 02:12:37 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-02-19 22:16:00 +04:00
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return ChangeStatus(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return ChangeStatus(model, OrderStatus.Готов);
|
|
|
|
|
}
|
2024-02-10 02:12:37 +04:00
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2024-02-19 22:16:00 +04:00
|
|
|
|
return ChangeStatus(model, OrderStatus.Выдан);
|
2024-02-10 02:12:37 +04:00
|
|
|
|
}
|
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-02-10 02:14:22 +04:00
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Цена должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
if (model.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count));
|
|
|
|
|
}
|
2024-02-19 22:16:00 +04:00
|
|
|
|
if (model.DateImplement != null && model.DateImplement < model.DateCreate)
|
2024-02-10 02:14:22 +04:00
|
|
|
|
{
|
2024-02-19 22:16:00 +04:00
|
|
|
|
throw new ArithmeticException("Дата завершения не может быть раньше даты начала");
|
2024-02-10 02:14:22 +04:00
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Order. Id:{Id}. Sum:{Sum}. ComputerId:{ComputerId}", model.Id, model.Sum, model.ComputerId);
|
2024-02-10 02:12:37 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|