2024-02-12 20:09:21 +04:00
|
|
|
|
using CarRepairShopContracts.BindingModels;
|
|
|
|
|
using CarRepairShopContracts.BusinessLogicsContracts;
|
|
|
|
|
using CarRepairShopContracts.SearchModels;
|
|
|
|
|
using CarRepairShopContracts.StoragesContracts;
|
|
|
|
|
using CarRepairShopContracts.ViewModels;
|
|
|
|
|
using CarRepairShopDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CarRepairShopBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
2024-04-21 00:32:56 +04:00
|
|
|
|
static readonly object locker = new object();
|
2024-02-12 20:09:21 +04:00
|
|
|
|
|
|
|
|
|
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.GetFilteredList(model);
|
|
|
|
|
if(list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 00:32:56 +04:00
|
|
|
|
public OrderViewModel? ReadElement(OrderSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
if(model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
var element = _orderStorage.GetElement(model);
|
|
|
|
|
if(element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("ReadElement. Элемент не найден");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. Id:{id}", element.Id);
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-12 20:09:21 +04:00
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
model.Status = OrderStatus.Принят;
|
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Create operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выдан);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Готов);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
|
|
|
|
{
|
2024-04-21 00:32:56 +04:00
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
|
|
|
|
}
|
2024-02-12 20:09:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
|
|
|
|
|
{
|
2024-04-21 00:32:56 +04:00
|
|
|
|
var element = _orderStorage.GetElement(new OrderSearchModel
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id
|
|
|
|
|
});
|
|
|
|
|
if (element == null) return false;
|
|
|
|
|
if (element.ImplementerId.HasValue) model.ImplementerId = element.ImplementerId;
|
|
|
|
|
if (element.Status + 1 != newStatus)
|
2024-02-12 20:09:21 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Status update operation failed. Incorrect order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
model.Status = newStatus;
|
2024-04-21 00:32:56 +04:00
|
|
|
|
if (model.Status == OrderStatus.Выдан)
|
2024-02-12 20:09:21 +04:00
|
|
|
|
{
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
}
|
2024-04-21 00:32:56 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
2024-02-12 20:09:21 +04:00
|
|
|
|
{
|
|
|
|
|
model.Status--;
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if(model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if(model.Id < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Некорректный номер заказа", nameof(model.Id));
|
|
|
|
|
}
|
|
|
|
|
if (model.Count < 1)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count));
|
|
|
|
|
}
|
|
|
|
|
if(model.Sum < 1)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Order. Id:{Id}. Sum:{Sum}. Count:{Count}", model.Id, model.Sum, model.Count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|