PIbd-22-Stroev-V.M.-Plumbin.../PlumbingRepair/PlumbingRepairBusinessLogic/BusinessLogics/OrderLogic.cs

168 lines
5.9 KiB
C#
Raw Permalink Normal View History

2024-02-14 11:14:37 +04:00
using Microsoft.Extensions.Logging;
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.BusinessLogicsContracts;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.StoragesContracts;
using PlumbingRepairContracts.ViewModels;
2024-02-27 17:37:08 +04:00
using PlumbingRepairDataModels.Enums;
2024-02-14 11:14:37 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlumbingRepairBusinessLogic.BusinessLogics
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
2024-05-05 21:39:31 +04:00
private readonly IStoreLogic _storeLogic;
private readonly IWorkStorage _workStorage;
2024-02-14 11:14:37 +04:00
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage
2024-05-05 21:39:31 +04:00
orderStorage, IStoreLogic storeLogic, IWorkStorage workStorage)
2024-02-14 11:14:37 +04:00
{
_logger = logger;
_orderStorage = orderStorage;
2024-05-05 21:39:31 +04:00
_storeLogic = storeLogic;
_workStorage = workStorage;
2024-02-14 11:14:37 +04:00
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
2024-02-27 17:37:08 +04:00
_logger.LogInformation("ReadList.OrderId:{Id} ", model?.Id);
2024-02-14 11:14:37 +04:00
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 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-02-27 16:18:37 +04:00
public bool CreateOrder(OrderBindingModel model)
2024-02-14 11:14:37 +04:00
{
CheckModel(model);
2024-02-27 17:37:08 +04:00
if (model.Status != OrderStatus.Неизвестен)
{
_logger.LogWarning("Insert operation failed. Order Status incorrect.");
return false;
}
model.Status = OrderStatus.Принят;
2024-02-14 11:14:37 +04:00
if (_orderStorage.Insert(model) == null)
{
2024-02-27 17:37:08 +04:00
model.Status = OrderStatus.Неизвестен;
2024-02-14 11:14:37 +04:00
_logger.LogWarning("Insert operation failed");
return false;
}
2024-02-27 17:37:08 +04:00
2024-02-14 11:14:37 +04:00
return true;
}
2024-02-27 22:51:12 +04:00
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
2024-02-14 11:14:37 +04:00
{
2024-05-05 21:39:31 +04:00
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != newStatus)
2024-02-27 17:37:08 +04:00
{
2024-02-27 22:51:12 +04:00
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect.");
2024-02-27 17:37:08 +04:00
return false;
}
2024-02-27 22:51:12 +04:00
model.Status = newStatus;
2024-05-05 21:39:31 +04:00
if (model.Status == OrderStatus.Готов)
{
2024-05-06 11:24:06 +04:00
model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
2024-05-05 21:39:31 +04:00
var work = _workStorage.GetElement(new() { Id = viewModel.WorkId });
if (work == null)
{
throw new ArgumentNullException(nameof(work));
}
if (!_storeLogic.AddWork(work, viewModel.Count))
{
throw new Exception($"AddWork operation failed. Store is full.");
}
}
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model, false);
2024-02-27 22:51:12 +04:00
2024-02-14 11:14:37 +04:00
if (_orderStorage.Update(model) == null)
{
2024-02-27 17:37:08 +04:00
model.Status--;
2024-02-14 11:14:37 +04:00
_logger.LogWarning("Update operation failed");
return false;
}
2024-02-27 22:51:12 +04:00
2024-02-14 11:14:37 +04:00
return true;
}
2024-02-27 17:37:08 +04:00
public bool TakeOrderInWork(OrderBindingModel model)
2024-02-14 11:14:37 +04:00
{
2024-02-27 22:51:12 +04:00
return StatusUpdate(model, OrderStatus.Выполняется);
2024-02-27 17:37:08 +04:00
}
public bool DeliveryOrder(OrderBindingModel model)
{
2024-02-27 22:51:12 +04:00
return StatusUpdate(model, OrderStatus.Готов);
2024-02-27 17:37:08 +04:00
}
public bool FinishOrder(OrderBindingModel model)
{
2024-02-27 22:51:12 +04:00
return StatusUpdate(model, OrderStatus.Выдан);
2024-02-14 11:14:37 +04:00
}
2024-02-27 17:37:08 +04:00
2024-02-14 11:14:37 +04:00
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
2024-02-27 17:37:08 +04:00
if (model.WorkId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.WorkId));
}
if (model.Count <= 0)
2024-02-14 11:14:37 +04:00
{
2024-02-27 17:37:08 +04:00
throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count));
}
if (model.Sum <= 0)
2024-02-14 11:14:37 +04:00
{
2024-02-27 17:37:08 +04:00
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
2024-02-14 11:14:37 +04:00
}
2024-02-27 17:37:08 +04:00
_logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. WorkId: { WorkId}", model.Id, model.Sum, model.WorkId);
2024-02-14 11:14:37 +04:00
}
}
}