2023-01-29 18:09:59 +04:00
|
|
|
|
using FurnitureAssemblyContracts.BindingModels;
|
|
|
|
|
using FurnitureAssemblyContracts.BusinessLogicsContarcts;
|
|
|
|
|
using FurnitureAssemblyContracts.SearchModels;
|
|
|
|
|
using FurnitureAssemblyContracts.StoragesContracts;
|
|
|
|
|
using FurnitureAssemblyContracts.ViewModels;
|
|
|
|
|
using FurnitureAssemblyDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace FurnitureAssemblyBusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
2023-03-23 19:10:03 +04:00
|
|
|
|
private readonly IShopLogic _shopLogic;
|
2023-01-29 18:09:59 +04:00
|
|
|
|
|
2023-03-23 19:10:03 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic)
|
2023-01-29 18:09:59 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
2023-03-23 19:10:03 +04:00
|
|
|
|
_shopLogic = shopLogic;
|
2023-01-29 18:09:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 09:38:42 +04:00
|
|
|
|
private bool ChangeStatus(OrderBindingModel model, OrderStatus orderStatus)
|
|
|
|
|
{
|
2023-04-20 21:15:57 +04:00
|
|
|
|
if (model.Status + 1 != orderStatus && !(model.Status == OrderStatus.Ожидание && orderStatus == OrderStatus.Готов) && !(model.Status == OrderStatus.Выполняется && orderStatus == OrderStatus.Ожидание))
|
2023-02-11 09:38:42 +04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
model.Status = orderStatus;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-29 18:09:59 +04:00
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2023-02-13 09:02:44 +04:00
|
|
|
|
|
2023-02-11 09:38:42 +04:00
|
|
|
|
if (!ChangeStatus(model, OrderStatus.Принят))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Order's status is wrong");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-29 18:09:59 +04:00
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-02-11 09:38:42 +04:00
|
|
|
|
|
2023-01-29 18:09:59 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 09:38:42 +04:00
|
|
|
|
public bool TakeOrderInWork(OrderBindingModel model)
|
2023-01-29 18:09:59 +04:00
|
|
|
|
{
|
2023-04-09 18:55:27 +04:00
|
|
|
|
var modelNew = ReadElement(new OrderSearchModel { Id = model.Id }); // в связи с обновлением контракта, предыдущий метод не нужен
|
|
|
|
|
if (modelNew == null)
|
2023-02-13 09:02:44 +04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-20 21:15:57 +04:00
|
|
|
|
_logger.LogWarning($"");
|
|
|
|
|
if (modelNew.ImplementerId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Order {model.Id} is already in Work");
|
|
|
|
|
throw new InvalidOperationException($"Order {model.Id} is already in Work");
|
|
|
|
|
}
|
2023-04-09 18:55:27 +04:00
|
|
|
|
model.Status = modelNew.Status;
|
2023-02-11 09:38:42 +04:00
|
|
|
|
if (!ChangeStatus(model, OrderStatus.Выполняется))
|
2023-01-29 18:09:59 +04:00
|
|
|
|
{
|
2023-02-11 09:38:42 +04:00
|
|
|
|
_logger.LogWarning("Order's status is wrong");
|
|
|
|
|
return false;
|
2023-01-29 18:09:59 +04:00
|
|
|
|
}
|
2023-02-11 09:38:42 +04:00
|
|
|
|
_orderStorage.Update(model);
|
|
|
|
|
return true;
|
2023-01-29 18:09:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-04-09 18:55:27 +04:00
|
|
|
|
var modelNew = ReadElement(new OrderSearchModel { Id = model.Id}); // в связи с обновлением контракта, предыдущий метод не нужен
|
|
|
|
|
if (modelNew == null) {
|
2023-02-13 09:02:44 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-20 21:15:57 +04:00
|
|
|
|
model.ImplementerId = modelNew.ImplementerId;
|
|
|
|
|
model.Status = modelNew.Status;
|
|
|
|
|
if (!_shopLogic.AddFurnituresAtShops(new FurnitureBindingModel() { Id = modelNew.FurnitureId }, modelNew.Count))
|
2023-04-19 18:22:49 +04:00
|
|
|
|
{
|
2023-04-20 21:15:57 +04:00
|
|
|
|
if (!ChangeStatus(model, OrderStatus.Ожидание))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Order's status {model.Status} is wrong");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_orderStorage.Update(model);
|
2023-04-19 18:22:49 +04:00
|
|
|
|
_logger.LogWarning("There are not empty places at shops. Replenishment is impossible");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-20 21:15:57 +04:00
|
|
|
|
|
2023-02-11 09:38:42 +04:00
|
|
|
|
if (!ChangeStatus(model, OrderStatus.Готов))
|
2023-01-29 18:09:59 +04:00
|
|
|
|
{
|
2023-02-11 09:38:42 +04:00
|
|
|
|
_logger.LogWarning("Order's status is wrong");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-09 18:55:27 +04:00
|
|
|
|
model.DateImplement = DateTime.Now;
|
2023-02-11 09:38:42 +04:00
|
|
|
|
_orderStorage.Update(model);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-04-09 18:55:27 +04:00
|
|
|
|
var modelNew = ReadElement(new OrderSearchModel { Id = model.Id }); // в связи с обновлением контракта, предыдущий метод не нужен
|
|
|
|
|
if (modelNew == null)
|
2023-02-13 09:02:44 +04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-19 18:22:49 +04:00
|
|
|
|
|
2023-03-23 19:10:03 +04:00
|
|
|
|
|
2023-04-09 18:55:27 +04:00
|
|
|
|
model.Status = modelNew.Status;
|
|
|
|
|
model.ImplementerId = modelNew.ImplementerId;
|
|
|
|
|
model.DateImplement = modelNew.DateImplement;
|
2023-02-11 09:38:42 +04:00
|
|
|
|
if (!ChangeStatus(model, OrderStatus.Выдан))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Order's status is wrong");
|
|
|
|
|
return false;
|
2023-01-29 18:09:59 +04:00
|
|
|
|
}
|
2023-04-09 18:55:27 +04:00
|
|
|
|
|
2023-02-11 09:38:42 +04:00
|
|
|
|
_orderStorage.Update(model);
|
|
|
|
|
return true;
|
2023-01-29 18:09:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 19:10:03 +04:00
|
|
|
|
|
2023-01-29 18:09:59 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 09:38:42 +04:00
|
|
|
|
|
2023-01-29 18:09:59 +04:00
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (model.FurnitureId <= 0)
|
|
|
|
|
{
|
2023-02-11 09:38:42 +04:00
|
|
|
|
throw new ArgumentNullException("Неверный идентификатор изделия", nameof(model.FurnitureId));
|
2023-01-29 18:09:59 +04:00
|
|
|
|
}
|
|
|
|
|
if (model.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count));
|
|
|
|
|
}
|
|
|
|
|
if (model.Sum <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Стоимость заказа должна быть больше 0", nameof(model.Sum));
|
|
|
|
|
}
|
2023-02-11 09:38:42 +04:00
|
|
|
|
_logger.LogInformation("Order. OrderId: { Id}. OrderStatus: {OrderStatus} DateCreate: {DateCreate} " +
|
2023-03-09 21:16:13 +04:00
|
|
|
|
"FurnitureId:{FurnitureId}. Count:{ Count}. Sum:{ Sum}. ",
|
|
|
|
|
model.Id, model.Status, model.DateCreate, model.FurnitureId, model.Count, model.Sum);
|
2023-01-29 18:09:59 +04:00
|
|
|
|
}
|
2023-02-13 09:02:44 +04:00
|
|
|
|
|
2023-04-09 18:55:27 +04:00
|
|
|
|
public OrderViewModel? ReadElement(OrderSearchModel? model)
|
2023-02-13 09:02:44 +04:00
|
|
|
|
{
|
2023-04-09 18:55:27 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. Status: {Status}. ImplementerId: {ImplementerId} Id:{ Id}", model.Status, model.ImplementerId, model.Id);
|
|
|
|
|
var element = _orderStorage.GetElement(model);
|
|
|
|
|
if (element == null)
|
2023-02-13 09:02:44 +04:00
|
|
|
|
{
|
2023-04-09 18:55:27 +04:00
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
2023-02-13 09:02:44 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-09 18:55:27 +04:00
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
return element;
|
2023-02-13 09:02:44 +04:00
|
|
|
|
}
|
2023-01-29 18:09:59 +04:00
|
|
|
|
}
|
|
|
|
|
}
|