2023-06-21 23:38:26 +04:00
using HospitalBusinessLogic.MailWorker ;
using HospitalContracts.BindingModels ;
2023-04-05 00:33:37 +04:00
using HospitalContracts.BusinessLogicsContracts ;
using HospitalContracts.SearchModels ;
using HospitalContracts.StoragesContracts ;
using HospitalContracts.ViewModels ;
2023-05-20 06:49:33 +04:00
using HospitalDataModels.Models ;
2023-04-05 00:33:37 +04:00
using Microsoft.Extensions.Logging ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
namespace HospitalBusinessLogic.BusinessLogics
{
public class RecipesLogic : IRecipesLogic
{
private readonly ILogger _logger ;
private readonly IRecipesStorage _recipesStorage ;
2023-06-21 23:38:26 +04:00
private readonly AbstractMailWorker _mailWorker ;
private readonly IClientLogic _clientLogic ;
public RecipesLogic ( ILogger < RecipesLogic > logger , IRecipesStorage recipesStorage , AbstractMailWorker mailWorker , IClientLogic clientLogic )
2023-04-05 00:33:37 +04:00
{
_logger = logger ;
_recipesStorage = recipesStorage ;
2023-06-21 23:38:26 +04:00
_mailWorker = mailWorker ;
_clientLogic = clientLogic ;
2023-04-05 00:33:37 +04:00
}
2023-05-20 06:49:33 +04:00
public bool AddProcedures ( RecipesSearchModel model , IProceduresModel procedure )
{
if ( model = = null )
{
throw new ArgumentNullException ( nameof ( model ) ) ;
}
_logger . LogInformation ( "AddProcedure.Id:{ Id}" , model . Id ) ;
var element = _recipesStorage . GetElement ( model ) ;
if ( element = = null )
{
_logger . LogWarning ( "AddProcedure element not found" ) ;
return false ;
}
_logger . LogInformation ( "AddProcedure find. Id:{Id}" , element . Id ) ;
element . RecipeProcedures [ procedure . Id ] = procedure ;
_recipesStorage . Update ( new ( )
{
Id = element . Id ,
Dose = element . Dose ,
ModeOfApplication = element . ModeOfApplication ,
Date = element . Date ,
ClientId = element . ClientId ,
2023-06-19 13:29:55 +04:00
SymptomsId = element . SymptomsId ,
2023-05-20 06:49:33 +04:00
RecipeProcedures = element . RecipeProcedures
} ) ;
return true ;
}
2023-04-05 00:33:37 +04:00
public bool Create ( RecipesBindingModel model )
{
CheckModel ( model ) ;
2023-06-21 23:38:26 +04:00
var result = _recipesStorage . Insert ( model ) ;
if ( result = = null ) {
2023-04-05 00:33:37 +04:00
_logger . LogWarning ( "Insert operation failed" ) ;
return false ;
}
2023-06-21 23:38:26 +04:00
SendRecipeMessage ( result . ClientId , $"Поликлиника \" В ы б о л ь н ы \ ", Рецепт №{result.Id}" , $"Рецепт №{result.Id} созданный {result.Date} с дозировкой {result.Dose} и способом применения {result.ModeOfApplication} добавлен" ) ;
2023-04-05 00:33:37 +04:00
return true ;
}
public bool Delete ( RecipesBindingModel model )
{
CheckModel ( model , false ) ;
_logger . LogInformation ( "Delete. Id:{Id}" , model . Id ) ;
if ( _recipesStorage . Delete ( model ) = = null )
{
_logger . LogWarning ( "Delete operation failed" ) ;
return false ;
}
return true ;
}
public RecipesViewModel ? ReadElement ( RecipesSearchModel model )
{
if ( model = = null )
{
throw new ArgumentNullException ( nameof ( model ) ) ;
}
_logger . LogInformation ( "ReadElement. Id:{ Id}" , model . Id ) ;
var element = _recipesStorage . GetElement ( model ) ;
if ( element = = null )
{
_logger . LogWarning ( "ReadElement element not found" ) ;
return null ;
}
_logger . LogInformation ( "ReadElement find. Id:{Id}" , element . Id ) ;
return element ;
}
public List < RecipesViewModel > ? ReadList ( RecipesSearchModel ? model )
{
_logger . LogInformation ( "ReadElement. Id:{ Id}" , model ? . Id ) ;
var list = model = = null ? _recipesStorage . GetFullList ( ) : _recipesStorage . GetFilteredList ( model ) ;
if ( list = = null )
{
_logger . LogWarning ( "ReadList return null list" ) ;
return null ;
}
_logger . LogInformation ( "ReadList. Count:{Count}" , list . Count ) ;
return list ;
}
public bool Update ( RecipesBindingModel model )
{
CheckModel ( model ) ;
if ( _recipesStorage . Update ( model ) = = null )
{
_logger . LogWarning ( "Update operation failed" ) ;
return false ;
}
return true ;
}
private void CheckModel ( RecipesBindingModel model , bool withParams = true )
{
if ( model = = null )
{
throw new ArgumentNullException ( nameof ( model ) ) ;
}
if ( ! withParams )
{
return ;
}
2023-06-03 15:50:25 +04:00
if ( model . SymptomsId < 0 )
{
throw new ArgumentNullException ( "Некорректный идентификатор симптома" , nameof ( model . SymptomsId ) ) ;
}
2023-04-05 00:33:37 +04:00
if ( string . IsNullOrEmpty ( model . Dose ) )
{
throw new ArgumentNullException ( "В рецепте нет дозировки" , nameof ( model . Dose ) ) ;
}
if ( string . IsNullOrEmpty ( model . ModeOfApplication ) )
{
throw new ArgumentNullException ( "Нет способа приема" , nameof ( model . ModeOfApplication ) ) ;
}
2023-06-03 15:50:25 +04:00
_logger . LogInformation ( "Recipes.RecipesId:{Id}.Dose:{ Dose}.ModeOfApplication:{ ModeOfApplication}. MedicineId: { MedicineId}" , model . Id , model . Dose , model . ModeOfApplication , model . SymptomsId ) ;
2023-04-05 00:33:37 +04:00
}
2023-06-21 23:38:26 +04:00
private bool SendRecipeMessage ( int clientId , string subject , string text )
{
var client = _clientLogic . ReadElement ( new ( ) { Id = clientId } ) ;
if ( client = = null )
{
return false ;
}
_mailWorker . MailSendAsync ( new ( )
{
MailAddress = client . Email ,
Subject = subject ,
Text = text
} ) ;
return true ;
}
2023-04-05 00:33:37 +04:00
}
}