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

141 lines
4.9 KiB
C#
Raw 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;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage
orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
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
{
CheckModel(model);
2024-02-27 22:51:12 +04:00
if (model.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;
if (model.Status == OrderStatus.Выдан)
2024-03-26 20:40:27 +04:00
model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
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
}
}
}