2023-05-04 23:41:52 +04:00
using DocumentFormat.OpenXml.EMMA ;
2023-04-10 15:36:30 +04:00
using Microsoft.Extensions.Logging ;
2023-01-30 19:25:38 +04:00
using SushiBarContracts.BindingModels ;
using SushiBarContracts.BusinessLogicsContracts ;
using SushiBarContracts.SearchModels ;
using SushiBarContracts.StoragesContracts ;
using SushiBarContracts.ViewModels ;
2023-01-30 23:13:26 +04:00
using SushiBarDataModels.Enums ;
2023-01-30 19:25:38 +04:00
namespace SushiBarBusinessLogic.BusinessLogics
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger ;
private readonly IOrderStorage _orderStorage ;
2023-03-11 21:23:26 +04:00
private readonly IStoreLogic _storeLogic ;
private readonly ISushiStorage _sushiStorage ;
2023-04-23 19:30:07 +04:00
private readonly AbstractMailWorker _mailWorker ;
private readonly IClientLogic _clientLogic ;
2023-01-30 19:25:38 +04:00
2023-04-23 19:30:07 +04:00
public OrderLogic ( ILogger < OrderLogic > logger ,
2023-05-04 23:41:52 +04:00
IOrderStorage orderStorage ,
IStoreLogic storeLogic ,
ISushiStorage sushiStorage ,
2023-04-23 19:30:07 +04:00
IOrderStorage orderStorage ,
AbstractMailWorker mailWorker ,
IClientLogic clientLogic )
2023-01-30 19:25:38 +04:00
{
_logger = logger ;
_orderStorage = orderStorage ;
2023-03-11 21:23:26 +04:00
_storeLogic = storeLogic ;
_sushiStorage = sushiStorage ;
2023-04-23 19:30:07 +04:00
_mailWorker = mailWorker ;
_clientLogic = clientLogic ;
2023-01-30 19:25:38 +04:00
}
2023-04-10 13:35:57 +04:00
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 ;
}
2023-01-30 19:25:38 +04:00
public bool CreateOrder ( OrderBindingModel model )
{
CheckModel ( model ) ;
2023-01-31 20:14:33 +04:00
if ( model . Status ! = OrderStatus . Unknown )
{
_logger . LogWarning ( "Insert operation failed. Order status incorrect." ) ;
return false ;
}
2023-01-31 01:11:18 +04:00
model . Status = OrderStatus . Accepted ;
2023-01-31 20:14:33 +04:00
2023-04-24 00:10:19 +04:00
var result = _orderStorage . Insert ( model ) ;
if ( result = = null )
{
_logger . LogWarning ( "Insert operation failed" ) ;
return false ;
}
SendOrderMessage ( result . ClientId , $"Sushi Bar, Order №{result.Id}" , $"Order №{result.Id} date {result.DateCreate} with sum on {result.Sum:0.00} accepted" ) ;
return true ;
2023-01-30 19:25:38 +04:00
}
public bool DeliveryOrder ( OrderBindingModel model )
{
2023-05-04 23:28:39 +04:00
return UpdateStatus ( model , OrderStatus . Issued ) ;
2023-01-30 19:25:38 +04:00
}
public bool FinishOrder ( OrderBindingModel model )
{
2023-05-04 23:28:39 +04:00
model . DateImplement = DateTime . Now ;
return UpdateStatus ( model , OrderStatus . Ready ) ;
2023-01-30 19:25:38 +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 ;
}
public bool TakeOrderInWork ( OrderBindingModel model )
{
2023-01-30 23:13:26 +04:00
return UpdateStatus ( model , OrderStatus . Performed ) ;
}
private bool UpdateStatus ( OrderBindingModel model , OrderStatus status )
{
2023-05-04 17:21:47 +04:00
var order = _orderStorage . GetElement ( new OrderSearchModel { Id = model . Id } ) ;
if ( ( int ) order . Status + 1 ! = ( int ) status
& & ! ( order . Status = = OrderStatus . Waiting & & status = = OrderStatus . Ready ) )
2023-04-10 15:36:30 +04:00
{
2023-05-04 17:21:47 +04:00
throw new InvalidOperationException ( $"Error on change status" ) ;
2023-04-10 15:36:30 +04:00
}
2023-05-04 23:28:39 +04:00
if ( status is OrderStatus . Ready or OrderStatus . Waiting )
2023-03-11 21:23:26 +04:00
{
2023-05-04 23:28:39 +04:00
var sushi = _sushiStorage . GetElement ( new ( ) { Id = order . SushiId } ) ;
2023-05-04 17:21:47 +04:00
2023-05-04 23:28:39 +04:00
if ( sushi = = null | | ! _storeLogic . CheckToSupply ( sushi , order . Count ) )
2023-03-11 21:23:26 +04:00
{
2023-05-04 17:21:47 +04:00
_logger . LogWarning ( $"Failed to fill magazines with product " +
$"'{sushi?.SushiName ?? string.Empty}' " +
$"from order {model.Id}" ) ;
status = OrderStatus . Waiting ;
2023-03-11 21:23:26 +04:00
}
2023-05-04 17:21:47 +04:00
else
2023-03-11 21:23:26 +04:00
{
2023-05-04 17:21:47 +04:00
status = OrderStatus . Ready ;
2023-03-11 21:23:26 +04:00
}
}
2023-01-30 23:13:26 +04:00
model . Status = status ;
2023-05-04 23:28:39 +04:00
model . DateCreate = order . DateCreate ;
model . DateImplement ? ? = order . DateImplement ;
if ( order . ImplementerId . HasValue )
model . ImplementerId = order . ImplementerId ;
model . ClientId = order . ClientId ;
model . SushiId = order . SushiId ;
model . Sum = order . Sum ;
model . Count = order . Count ;
2023-04-23 19:48:34 +04:00
var result = _orderStorage . Update ( model ) ;
if ( result = = null )
2023-01-30 23:13:26 +04:00
{
_logger . LogWarning ( "Update operation failed" ) ;
return false ;
}
2023-04-23 19:48:34 +04:00
SendOrderMessage ( result . ClientId ,
$"Sushi Bar, Order №{result.Id}" ,
$"Order №{model.Id} changed status on {result.Status}" ) ;
2023-01-30 23:13:26 +04:00
return true ;
2023-01-30 19:25:38 +04:00
}
private void CheckModel ( OrderBindingModel model , bool withParams = true )
{
if ( model = = null )
{
throw new ArgumentNullException ( nameof ( model ) ) ;
}
if ( ! withParams )
{
return ;
}
_logger . LogInformation ( "Order. OrderId:{Id} .Count:{Count} .Sum:{Sum} .Status{Status} .DateCreate{DateCreate}" , model . Id , model . Count , model . Sum , model . Status . ToString ( ) , model . DateCreate . ToString ( ) ) ;
2023-02-14 22:45:26 +04:00
var element = _orderStorage . GetElement ( new OrderSearchModel
{
2023-01-30 19:25:38 +04:00
Id = model . Id
} ) ;
if ( element ! = null & & element . Id ! = model . Id )
{
throw new InvalidOperationException ( "This name is already exists" ) ;
}
}
2023-04-23 19:30:07 +04:00
private bool SendOrderMessage ( int clientId , string subject , string text )
{
var client = _clientLogic . ReadElement ( new ClientSearchModel { Id = clientId } ) ;
if ( client = = null )
{
return false ;
}
_mailWorker . MailSendAsync ( new MailSendInfoBindingModel
{
MailAddress = client . Email ,
Subject = subject ,
Text = text
} ) ;
return true ;
}
2023-01-30 19:25:38 +04:00
}
2023-02-14 22:45:26 +04:00
}