2023-02-20 08:36:19 +04:00
using Microsoft.Extensions.Logging ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
2023-06-03 00:30:53 +04:00
using TravelCompanyBusinessLogic.MailWorker ;
2023-02-20 08:36:19 +04:00
using TravelCompanyContracts.BindingModels ;
using TravelCompanyContracts.BusinessLogicsContracts ;
using TravelCompanyContracts.SearchModels ;
using TravelCompanyContracts.StoragesContracts ;
using TravelCompanyContracts.ViewModels ;
using TravelCompanyDataModels.Enums ;
namespace TravelCompanyBusinessLogic.BusinessLogic
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger ;
private readonly IOrderStorage _orderStorage ;
2023-06-03 00:30:53 +04:00
private readonly AbstractMailWorker _mailWorker ;
2023-05-15 02:51:05 +04:00
static readonly object _lock = new object ( ) ;
2023-06-03 00:30:53 +04:00
public OrderLogic ( ILogger < OrderLogic > logger , IOrderStorage orderStorage , AbstractMailWorker mailWorker )
2023-02-20 08:36:19 +04:00
{
_logger = logger ;
_orderStorage = orderStorage ;
2023-06-03 00:30:53 +04:00
_mailWorker = mailWorker ;
2023-02-20 08:36:19 +04:00
}
public bool CreateOrder ( OrderBindingModel model )
{
CheckModel ( model ) ;
if ( model . Status ! = OrderStatus . Н е и з в е с т е н ) return false ;
model . Status = OrderStatus . П р и н я т ;
2023-06-03 00:30:53 +04:00
var order = _orderStorage . Insert ( model ) ;
if ( order = = null )
2023-02-20 08:36:19 +04:00
{
_logger . LogWarning ( "Insert operation failed" ) ;
return false ;
}
2023-06-03 00:30:53 +04:00
_mailWorker . MailSendAsync ( new MailSendInfoBindingModel ( )
{
MailAddress = order . ClientEmail ,
Subject = "Создан заказ №" + order . Id ,
Text = $"Создан заказ №{order.Id} от {order.DateCreate} на путешествие {order.TravelName} в количестве {order.Count} шт. Сумма заказа: {order.Sum}."
} ) ;
2023-02-20 08:36:19 +04:00
return true ;
}
public bool DeliveryOrder ( OrderBindingModel model )
{
CheckModel ( model , false ) ;
var element = _orderStorage . GetElement ( new OrderSearchModel
{
Id = model . Id
} ) ;
if ( element = = null )
{
_logger . LogWarning ( "Read operation failed" ) ;
return false ;
}
if ( element . Status ! = OrderStatus . Г о т о в )
{
_logger . LogWarning ( "Status change operation failed" ) ;
throw new InvalidOperationException ( "Нельзя выдать заказ, если е г о статус не 'Готов'!" ) ;
}
model . Status = OrderStatus . В ы д а н ;
model . DateImplement = DateTime . Now ;
2023-06-03 00:30:53 +04:00
var order = _orderStorage . Update ( model ) ;
_mailWorker . MailSendAsync ( new MailSendInfoBindingModel ( )
{
MailAddress = order . ClientEmail ,
Subject = $"Заказ №{order.Id}. Статус изменен на Выдан" ,
Text = $"Выдан заказ №{order.Id} от {order.DateCreate} на путешествие {order.TravelName} в количестве {order.Count} шт. Сумма заказа: {order.Sum}."
} ) ;
2023-02-20 08:36:19 +04:00
return true ;
}
public bool FinishOrder ( OrderBindingModel model )
{
CheckModel ( model , false ) ;
var element = _orderStorage . GetElement ( new OrderSearchModel
{
Id = model . Id
} ) ;
if ( element = = null )
{
_logger . LogWarning ( "Read operation failed" ) ;
return false ;
}
if ( element . Status ! = OrderStatus . В ы п о л н я е т с я )
{
_logger . LogWarning ( "Status change operation failed" ) ;
throw new InvalidOperationException ( "Статус заказа 'Готов' может быть присвоен только заказам с о статусом 'Выполняется'!" ) ;
}
model . Status = OrderStatus . Г о т о в ;
2023-05-15 02:51:05 +04:00
model . DateImplement = DateTime . Now ;
2023-06-03 00:30:53 +04:00
var order = _orderStorage . Update ( model ) ;
_mailWorker . MailSendAsync ( new MailSendInfoBindingModel ( )
{
MailAddress = order . ClientEmail ,
Subject = $"Заказ №{order.Id}. Статус изменен на Готов" ,
Text = $"Готов заказ №{order.Id} от {order.DateCreate} на путешествие {order.TravelName} в количестве {order.Count} шт. Сумма заказа: {order.Sum}."
} ) ;
2023-02-20 08:36:19 +04:00
return true ;
}
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-05-15 02:51:05 +04:00
public OrderViewModel ? ReadElement ( OrderSearchModel ? model )
2023-02-20 08:36:19 +04:00
{
2023-05-15 02:51:05 +04:00
if ( model = = null )
2023-02-20 08:36:19 +04:00
{
2023-05-15 02:51:05 +04:00
throw new ArgumentNullException ( nameof ( model ) ) ;
}
_logger . LogInformation ( "ReadElement. ImplementerId:{ImplementerId}. OrderStatus:{OrderStatus}. Id:{Id}" , model . ImplementerId , model . OrderStatus , model . Id ) ;
var element = _orderStorage . GetElement ( model ) ;
2023-02-20 08:36:19 +04:00
if ( element = = null )
{
2023-05-15 02:51:05 +04:00
_logger . LogWarning ( "ReadElement element not found" ) ;
return null ;
2023-02-20 08:36:19 +04:00
}
2023-05-15 02:51:05 +04:00
_logger . LogInformation ( "ReadElement find. Id:{Id}" , element . Id ) ;
return element ;
}
public bool TakeOrderInWork ( OrderBindingModel model )
{
lock ( _lock )
2023-02-20 08:36:19 +04:00
{
2023-05-15 02:51:05 +04:00
CheckModel ( model , false ) ;
var element = _orderStorage . GetElement ( new OrderSearchModel
{
Id = model . Id
} ) ;
if ( element = = null )
{
_logger . LogWarning ( "Read operation failed" ) ;
return false ;
}
if ( element . Status ! = OrderStatus . П р и н я т )
{
_logger . LogWarning ( "Status change operation failed" ) ;
2023-05-27 12:20:39 +04:00
throw new InvalidOperationException ( "Заказ должен быть переведен " +
"в статус принятого перед е г о выполнением!" ) ;
2023-05-15 02:51:05 +04:00
}
model . Status = OrderStatus . В ы п о л н я е т с я ;
2023-06-03 00:30:53 +04:00
var order = _orderStorage . Update ( model ) ;
_mailWorker . MailSendAsync ( new MailSendInfoBindingModel ( )
{
MailAddress = order . ClientEmail ,
Subject = $"Заказ №{order.Id}. Статус изменен на Выполняется" ,
Text = $"Выполняется заказ №{order.Id} от {order.DateCreate} на путешествие {order.TravelName} в количестве {order.Count} шт. Сумма заказа: {order.Sum}."
} ) ;
2023-05-15 02:51:05 +04:00
return true ;
2023-02-20 08:36:19 +04:00
}
}
private void CheckModel ( OrderBindingModel model , bool withParams = true )
{
if ( model = = null )
{
throw new ArgumentNullException ( nameof ( model ) ) ;
}
if ( ! withParams )
{
return ;
}
if ( model . Sum < = 0 )
{
throw new ArgumentNullException ( "Сумма заказа должна быть больше 0!" , nameof ( model . Sum ) ) ;
}
if ( model . Count < = 0 )
{
throw new ArgumentNullException ( "Количество изделий должно быть более 0!" , nameof ( model . Count ) ) ;
}
_logger . LogInformation ( "Order. Sum:{Sum}. Id:{Id}" , model . Sum , model . Id ) ;
var element = _orderStorage . GetElement ( new OrderSearchModel
{
Id = model . Id
} ) ;
if ( element ! = null & & element . Id ! = model . Id )
{
throw new InvalidOperationException ( "Заказ с таким ID уже существует" ) ;
}
}
}
}