2024-02-09 16:47:02 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using PrecastConcretePlantContracts.BindingModels;
|
|
|
|
|
using PrecastConcretePlantContracts.BusinessLogicsContracts;
|
|
|
|
|
using PrecastConcretePlantContracts.SearchModels;
|
|
|
|
|
using PrecastConcretePlantContracts.StoragesContracts;
|
|
|
|
|
using PrecastConcretePlantContracts.ViewModels;
|
|
|
|
|
using PrecastConcretePlantDataModels.Enums;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace PrecastConcretePlantBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
|
|
|
|
}
|
2024-05-01 19:44:12 +04:00
|
|
|
|
public OrderViewModel? ReadElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. DateFrom:{DateFrom}. DateTo:{DateTo}. Id:{Id}", model.DateFrom, model.DateTo, 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-02-09 16:47:02 +04:00
|
|
|
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Id: {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 (model.Status != OrderStatus.Неизвестен)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
|
|
|
|
{
|
2024-05-01 19:44:12 +04:00
|
|
|
|
model.Status = OrderStatus.Неизвестен;
|
2024-02-09 16:47:02 +04:00
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-05-01 19:44:12 +04:00
|
|
|
|
private bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
|
2024-02-09 16:47:02 +04:00
|
|
|
|
{
|
2024-05-01 19:44:12 +04:00
|
|
|
|
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
if (viewModel == null)
|
2024-02-09 16:47:02 +04:00
|
|
|
|
{
|
2024-05-01 19:44:12 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
2024-02-09 16:47:02 +04:00
|
|
|
|
}
|
2024-05-01 19:44:12 +04:00
|
|
|
|
if (viewModel.Status + 1 != newStatus)
|
2024-02-09 16:47:02 +04:00
|
|
|
|
{
|
2024-05-01 19:44:12 +04:00
|
|
|
|
_logger.LogWarning("Change status operation failed");
|
|
|
|
|
throw new InvalidOperationException();
|
2024-02-09 16:47:02 +04:00
|
|
|
|
}
|
2024-05-01 19:44:12 +04:00
|
|
|
|
model.Status = newStatus;
|
|
|
|
|
if (model.Status == OrderStatus.Готов)
|
2024-02-09 16:47:02 +04:00
|
|
|
|
{
|
2024-05-01 19:44:12 +04:00
|
|
|
|
model.DateImplement = DateTime.Now;
|
2024-02-09 16:47:02 +04:00
|
|
|
|
}
|
2024-05-01 19:44:12 +04:00
|
|
|
|
else
|
2024-02-09 16:47:02 +04:00
|
|
|
|
{
|
2024-05-01 19:44:12 +04:00
|
|
|
|
model.DateImplement = viewModel.DateImplement;
|
2024-02-09 16:47:02 +04:00
|
|
|
|
}
|
2024-05-01 19:44:12 +04:00
|
|
|
|
if (viewModel.ImplementerId.HasValue)
|
2024-02-09 16:47:02 +04:00
|
|
|
|
{
|
2024-05-01 19:44:12 +04:00
|
|
|
|
model.ImplementerId = viewModel.ImplementerId.Value;
|
2024-02-09 16:47:02 +04:00
|
|
|
|
}
|
2024-05-01 19:44:12 +04:00
|
|
|
|
CheckModel(model, false);
|
2024-02-09 16:47:02 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
|
|
|
|
{
|
2024-05-01 19:44:12 +04:00
|
|
|
|
_logger.LogWarning("Change status operation failed");
|
|
|
|
|
return false;
|
2024-02-09 16:47:02 +04:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-05-01 19:44:12 +04:00
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Готов);
|
|
|
|
|
}
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выдан);
|
|
|
|
|
}
|
2024-02-09 16:47:02 +04:00
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Стоимость должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Order. Id: {Id}. Sum: {Sum}. ReinforcedId: {ReinforcedId}", model.Id, model.Sum, model.ReinforcedId);
|
|
|
|
|
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
|
|
|
if (element != null && element.Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Заказ с таким номером уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|