183 lines
6.3 KiB
C#
183 lines
6.3 KiB
C#
using CarRepairShopBusinessLogic.MailWorker;
|
||
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;
|
||
using System.Xml.Linq;
|
||
|
||
namespace CarRepairShopBusinessLogic.BusinessLogics
|
||
{
|
||
public class OrderLogic : IOrderLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IOrderStorage _orderStorage;
|
||
private readonly IClientStorage _clientStorage;
|
||
private readonly AbstractMailWorker _mailLogic;
|
||
static readonly object locker = new object();
|
||
|
||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IClientStorage clientStorage, AbstractMailWorker worker)
|
||
{
|
||
_logger = logger;
|
||
_orderStorage = orderStorage;
|
||
_clientStorage = clientStorage;
|
||
_mailLogic = worker;
|
||
}
|
||
|
||
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 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;
|
||
}
|
||
|
||
public bool CreateOrder(OrderBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
model.Status = OrderStatus.Принят;
|
||
var order = _orderStorage.Insert(model);
|
||
if (order == null)
|
||
{
|
||
_logger.LogInformation("Create operation failed");
|
||
return false;
|
||
}
|
||
|
||
var clientView = _clientStorage.GetElement(new() { Id = order.ClientId });
|
||
SendMail(clientView, order);
|
||
|
||
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)
|
||
{
|
||
lock (locker)
|
||
{
|
||
return StatusUpdate(model, OrderStatus.Выполняется);
|
||
}
|
||
}
|
||
|
||
private bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
|
||
{
|
||
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)
|
||
{
|
||
_logger.LogInformation("Status update operation failed. Incorrect order status");
|
||
return false;
|
||
}
|
||
model.Status = newStatus;
|
||
if (model.Status == OrderStatus.Выдан)
|
||
{
|
||
model.DateImplement = DateTime.Now;
|
||
}
|
||
if (_orderStorage.Update(model) == null)
|
||
{
|
||
model.Status--;
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
|
||
element.Status = newStatus;
|
||
|
||
var clientView = _clientStorage.GetElement(new() { Id = element.ClientId });
|
||
SendMail(clientView, element);
|
||
|
||
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);
|
||
}
|
||
|
||
private void SendMail(ClientViewModel clientModel, OrderViewModel orderModel)
|
||
{
|
||
if(clientModel == null || orderModel == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
MailSendInfoBindingModel mailModel;
|
||
|
||
if(orderModel.Status == OrderStatus.Принят)
|
||
{
|
||
mailModel = new MailSendInfoBindingModel
|
||
{
|
||
MailAddress = clientModel.Email,
|
||
Subject = $"Заказ №{orderModel.Id}",
|
||
Text = $"Ваш заказ №{orderModel.Id} на сумму {orderModel.Sum} был принят"
|
||
};
|
||
}
|
||
else
|
||
{
|
||
mailModel = new MailSendInfoBindingModel
|
||
{
|
||
MailAddress = clientModel.Email,
|
||
Subject = $"Заказ №{orderModel.Id}",
|
||
Text = $"Статус заказа №{orderModel.Id} был изменен на {orderModel.Status}"
|
||
};
|
||
}
|
||
_mailLogic.MailSendAsync(mailModel);
|
||
}
|
||
}
|
||
}
|