DepartmentProject/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/EmployeeService.cs

74 lines
3.0 KiB
C#
Raw Normal View History

using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
using ToolsModule.Enums;
using ToolsModule.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация IEmployeeService
/// </summary>
public class EmployeeService :
AbstractGenerticEntityService<EmployeeGetBindingModel, EmployeeSetBindingModel, Employee, EmployeeListViewModel, EmployeeViewModel>,
IEmployeeService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, EmployeeSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, Employee entity, EmployeeGetBindingModel model)
{
if (entity.User == null)
{
return OperationResultModel.Error("Error:", "Не найден пользователь от сотрудника", ResultServiceStatusCode.NotFound);
}
if (context.Set<Classroom>().Any(x => x.EmployeeId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Есть аудитории, у которых этот сотрудник указан ответственным", ResultServiceStatusCode.ExsistItem);
}
return OperationResultModel.Success(null);
}
protected override IQueryable<Employee> AdditionalCheckingWhenReadingList(IQueryable<Employee> query, EmployeeGetBindingModel model)
{
if (model.UserId.HasValue)
{
query = query.Where(x => x.UserId == model.UserId);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, EmployeeSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, Employee entity, EmployeeGetBindingModel model)
{
entity.User.IsDeleted = true;
entity.User.DateDelete = DateTime.Now;
context.SaveChanges();
var links = context.Set<EmployeePost>().Where(x => x.EmployeeId == model.Id);
foreach (var link in links)
{
link.IsDeleted = true;
link.DateDelete = DateTime.Now;
}
context.SaveChanges();
}
protected override Employee GetUniqueEntity(EmployeeSetBindingModel model, DbContext context) => context.Set<Employee>().FirstOrDefault(x => x.LastName == model.LastName && x.FirstName == model.FirstName &&
x.Patronymic == model.Patronymic && x.Id != model.Id);
protected override IQueryable<Employee> IncludingWhenReading(IQueryable<Employee> query) => query
.Include(x => x.EmployeePosts).Include("EmployeePosts.Post");
protected override IQueryable<Employee> OrderingWhenReading(IQueryable<Employee> query) => query
.OrderByDescending(x => x.EmployeePosts.Max(y => y.Post.Order))
.ThenBy(x => x.LastName)
.ThenBy(x => x.FirstName);
}
}