2022-03-18 22:55:48 +04:00
using CoreDatabase ;
using CoreDatabase.Models.Department ;
2022-03-19 22:48:13 +04:00
using DepartmentContract.BindingModels ;
using DepartmentContract.Services.IGenericEntityService ;
using DepartmentContract.ViewModels ;
2021-04-03 23:46:48 +04:00
using Microsoft.EntityFrameworkCore ;
using System ;
using System.Linq ;
2022-03-20 10:10:44 +04:00
using ToolsModule.ManagmentEntity ;
2022-12-24 09:10:07 +04:00
using ToolsModule.ManagmentExtension ;
2021-04-03 23:46:48 +04:00
2022-03-19 22:48:13 +04:00
namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService
2021-04-03 23:46:48 +04:00
{
2022-03-19 22:48:13 +04:00
/// <summary>
/// Реализация ILecturerService
/// </summary>
public class LecturerService :
2021-04-05 12:25:10 +04:00
AbstractGenerticEntityService < LecturerGetBindingModel , LecturerSetBindingModel , Lecturer , LecturerListViewModel , LecturerViewModel > ,
ILecturerService
2021-04-03 23:46:48 +04:00
{
2021-04-05 12:25:10 +04:00
protected override OperationResultModel AdditionalCheckingWhenAdding ( DbContext context , LecturerSetBindingModel model ) = > OperationResultModel . Success ( null ) ;
2021-04-03 23:46:48 +04:00
2021-04-05 12:25:10 +04:00
protected override OperationResultModel AdditionalCheckingWhenDeleting ( DbContext context , Lecturer entity , LecturerGetBindingModel model )
2021-04-03 23:46:48 +04:00
{
2021-04-05 12:25:10 +04:00
if ( entity . User = = null )
2021-04-03 23:46:48 +04:00
{
2021-04-05 12:25:10 +04:00
return OperationResultModel . Error ( "Error:" , "Н е найден пользователь от сотрудника" , ResultServiceStatusCode . NotFound ) ;
2021-04-03 23:46:48 +04:00
}
2021-04-05 14:20:07 +04:00
if ( context . Set < EducationDirection > ( ) . Any ( x = > x . LecturerId = = model . Id & & ! x . IsDeleted ) )
{
return OperationResultModel . Error ( "Error:" , "Есть направления обучения, у которых этот преподаватель указан руководителем" , ResultServiceStatusCode . ExsistItem ) ;
}
2021-04-12 10:01:42 +04:00
if ( context . Set < StudentGroup > ( ) . Any ( x = > x . LecturerId = = model . Id & & ! x . IsDeleted ) )
{
return OperationResultModel . Error ( "Error:" , "Есть учебные группы, у которых этот преподаватель числится как куратор" , ResultServiceStatusCode . ExsistItem ) ;
}
2021-04-05 12:25:10 +04:00
return OperationResultModel . Success ( null ) ; ;
2021-04-03 23:46:48 +04:00
}
2021-04-05 12:25:10 +04:00
protected override IQueryable < Lecturer > AdditionalCheckingWhenReadingList ( IQueryable < Lecturer > query , LecturerGetBindingModel model )
2021-04-03 23:46:48 +04:00
{
2021-04-05 12:25:10 +04:00
if ( model . UserId . HasValue )
2021-04-03 23:46:48 +04:00
{
query = query . Where ( x = > x . UserId = = model . UserId ) ;
}
if ( model . LecturerAcademicDegreeId . HasValue )
{
query = query . Where ( x = > x . LecturerAcademicDegreeId = = model . LecturerAcademicDegreeId ) ;
}
if ( model . LecturerAcademicRankId . HasValue )
{
query = query . Where ( x = > x . LecturerAcademicRankId = = model . LecturerAcademicRankId ) ;
}
2022-12-24 09:10:07 +04:00
if ( model . FIO . IsNotEmpty ( ) )
{
var fio = model . FIO . Split ( ' ' ) ;
if ( fio . Length > 1 )
{
query = query . Where ( x = > x . LastName = = fio [ 0 ] ) ;
}
if ( fio . Length > 1 )
{
query = query . Where ( x = > x . FirstName = = fio [ 1 ] ) ;
}
if ( fio . Length > 2 )
{
query = query . Where ( x = > x . Patronymic = = fio [ 2 ] ) ;
}
}
2021-04-05 12:25:10 +04:00
return query ;
2021-04-03 23:46:48 +04:00
}
2021-04-05 12:25:10 +04:00
protected override OperationResultModel AdditionalCheckingWhenUpdateing ( DbContext context , LecturerSetBindingModel model ) = > OperationResultModel . Success ( null ) ;
protected override void AdditionalDeleting ( DbContext context , Lecturer entity , LecturerGetBindingModel model )
2021-04-03 23:46:48 +04:00
{
2021-04-05 12:25:10 +04:00
entity . User . IsDeleted = true ;
entity . User . DateDelete = DateTime . Now ;
2021-04-03 23:46:48 +04:00
2021-04-05 12:25:10 +04:00
context . SaveChanges ( ) ;
var links = context . Set < LecturerPost > ( ) . Where ( x = > x . LecturerId = = model . Id ) ;
foreach ( var link in links )
2021-04-03 23:46:48 +04:00
{
2021-04-05 12:25:10 +04:00
link . IsDeleted = true ;
link . DateDelete = DateTime . Now ;
2021-04-03 23:46:48 +04:00
}
context . SaveChanges ( ) ;
2021-04-05 12:25:10 +04:00
}
2021-04-03 23:46:48 +04:00
2022-03-29 13:33:32 +04:00
protected override Lecturer GetUniqueEntity ( LecturerSetBindingModel model , IQueryable < Lecturer > query ) = > query . FirstOrDefault ( x = > x . LastName = = model . LastName & & x . FirstName = = model . FirstName & &
2021-04-05 12:25:10 +04:00
x . Patronymic = = model . Patronymic & & x . Id ! = model . Id ) ;
2022-12-21 09:32:44 +04:00
protected override IQueryable < Lecturer > IncludingWhenReading ( IQueryable < Lecturer > query ) = > query
. Include ( x = > x . LecturerAcademicRank )
. Include ( x = > x . LecturerAcademicDegree )
. Include ( x = > x . LecturerPosts ) . Include ( "LecturerPosts.Post" ) ;
2021-04-05 12:25:10 +04:00
2022-12-21 09:32:44 +04:00
protected override IQueryable < Lecturer > OrderingWhenReading ( IQueryable < Lecturer > query ) = > query
2021-04-05 12:25:10 +04:00
. OrderByDescending ( x = > x . LecturerPosts . Max ( y = > y . Post . Order ) )
. ThenBy ( x = > x . LecturerAcademicRank . Order )
. ThenBy ( x = > x . LecturerAcademicDegree . Order )
. ThenBy ( x = > x . LastName )
. ThenBy ( x = > x . FirstName ) ;
2021-04-03 23:46:48 +04:00
}
}