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;
|
2021-04-03 10:02:32 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2022-03-19 22:48:13 +04:00
|
|
|
|
using ToolsModule.Enums;
|
|
|
|
|
using ToolsModule.Models;
|
2021-04-03 10:02:32 +04:00
|
|
|
|
|
2022-03-19 22:48:13 +04:00
|
|
|
|
namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService
|
2021-04-03 10:02:32 +04:00
|
|
|
|
{
|
2022-03-19 22:48:13 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация IEmployeeService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class EmployeeService :
|
2021-04-05 12:25:10 +04:00
|
|
|
|
AbstractGenerticEntityService<EmployeeGetBindingModel, EmployeeSetBindingModel, Employee, EmployeeListViewModel, EmployeeViewModel>,
|
|
|
|
|
IEmployeeService
|
2021-04-03 10:02:32 +04:00
|
|
|
|
{
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, EmployeeSetBindingModel model) => OperationResultModel.Success(null);
|
2021-04-03 10:02:32 +04:00
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, Employee entity, EmployeeGetBindingModel model)
|
2021-04-03 10:02:32 +04:00
|
|
|
|
{
|
2021-04-05 12:25:10 +04:00
|
|
|
|
if (entity.User == null)
|
2021-04-03 10:02:32 +04:00
|
|
|
|
{
|
2021-04-05 12:25:10 +04:00
|
|
|
|
return OperationResultModel.Error("Error:", "Не найден пользователь от сотрудника", ResultServiceStatusCode.NotFound);
|
2021-04-03 10:02:32 +04:00
|
|
|
|
}
|
2021-04-05 12:25:10 +04:00
|
|
|
|
if (context.Set<Classroom>().Any(x => x.EmployeeId == model.Id && !x.IsDeleted))
|
2021-04-03 10:02:32 +04:00
|
|
|
|
{
|
2021-04-05 12:25:10 +04:00
|
|
|
|
return OperationResultModel.Error("Error:", "Есть аудитории, у которых этот сотрудник указан ответственным", ResultServiceStatusCode.ExsistItem);
|
2021-04-03 10:02:32 +04:00
|
|
|
|
}
|
2021-04-05 12:25:10 +04:00
|
|
|
|
return OperationResultModel.Success(null);
|
2021-04-03 10:02:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected override IQueryable<Employee> AdditionalCheckingWhenReadingList(IQueryable<Employee> query, EmployeeGetBindingModel model)
|
2021-04-03 10:02:32 +04:00
|
|
|
|
{
|
2021-04-03 23:46:48 +04:00
|
|
|
|
if (model.UserId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(x => x.UserId == model.UserId);
|
|
|
|
|
}
|
2021-04-05 12:25:10 +04:00
|
|
|
|
return query;
|
2021-04-03 10:02:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, EmployeeSetBindingModel model) => OperationResultModel.Success(null);
|
|
|
|
|
|
|
|
|
|
protected override void AdditionalDeleting(DbContext context, Employee entity, EmployeeGetBindingModel model)
|
2021-04-03 10:02:32 +04:00
|
|
|
|
{
|
2021-04-05 12:25:10 +04:00
|
|
|
|
entity.User.IsDeleted = true;
|
|
|
|
|
entity.User.DateDelete = DateTime.Now;
|
2021-04-03 10:02:32 +04:00
|
|
|
|
|
2021-04-05 12:25:10 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
var links = context.Set<EmployeePost>().Where(x => x.EmployeeId == model.Id);
|
|
|
|
|
foreach (var link in links)
|
2021-04-03 10:02:32 +04:00
|
|
|
|
{
|
2021-04-05 12:25:10 +04:00
|
|
|
|
link.IsDeleted = true;
|
|
|
|
|
link.DateDelete = DateTime.Now;
|
2021-04-03 10:02:32 +04:00
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2021-04-05 12:25:10 +04:00
|
|
|
|
|
|
|
|
|
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);
|
2021-04-03 10:02:32 +04:00
|
|
|
|
}
|
|
|
|
|
}
|