using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
///
/// Реализация ILecturerService
///
public class LecturerService : ILecturerService
{
public OperationResultModel Create(LecturerSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var exsistEntity = context.Lecturers.FirstOrDefault(x => x.LastName == model.LastName && x.FirstName == model.FirstName &&
x.Patronymic == model.Patronymic);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass(model, true);
context.Lecturers.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass(exsistEntity, true));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(LecturerGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
using var transaction = context.Database.BeginTransaction();
try
{
var entity = context.Lecturers.Include(x => x.User).FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
else if (entity.User == null)
{
return OperationResultModel.Error("Error:", "Не найден пользователь от сотрудника", ResultServiceStatusCode.NotFound);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
entity.User.IsDeleted = true;
entity.User.DateDelete = DateTime.Now;
context.SaveChanges();
var links = context.LecturerEmployeePosts.Where(x => x.LecturerId == model.Id);
foreach (var link in links)
{
link.IsDeleted = true;
link.DateDelete = DateTime.Now;
}
context.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
return OperationResultModel.Success(true);
}
public OperationResultModel Read(LecturerGetBindingModel model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
{
var entity = context.Lecturers.FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
return OperationResultModel.Success(Mapper.MapToClass(entity, model.HaveRight));
}
var query = context.Lecturers.Where(x => !x.IsDeleted).AsQueryable();
if(model.UserId.HasValue)
{
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);
}
if (model.LecturerPostId.HasValue)
{
query = query.Where(x => x.LecturerPostId == model.LecturerPostId);
}
query = query
.OrderByDescending(x => x.LecturerEmployeePosts.Max(y => y.EmployeePost.Order))
.ThenBy(x => x.LecturerAcademicRank.Order)
.ThenBy(x => x.LecturerAcademicDegree.Order)
.ThenBy(x => x.LecturerPost.Order)
.ThenBy(x => x.LastName);
if (model.PageNumber.HasValue && model.PageSize.HasValue)
{
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
query = query
.Skip(model.PageSize.Value * model.PageNumber.Value)
.Take(model.PageSize.Value);
}
query = query
.Include(x => x.LecturerPost)
.Include(x => x.LecturerAcademicRank)
.Include(x => x.LecturerAcademicDegree)
.Include(x => x.LecturerEmployeePosts)
.Include(x => x.LecturerEmployeePosts).Include("LecturerEmployeePosts.EmployeePost");
var result = new LecturerListViewModel
{
MaxCount = countPages,
List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(LecturerSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.Lecturers.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass(entity, true));
}
}
}