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

160 lines
5.1 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 ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация IEmployeeService
/// </summary>
public class EmployeeService : IEmployeeService
{
public OperationResultModel Create(EmployeeSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var exsistEntity = context.Employees.FirstOrDefault(x => x.LastName == model.LastName && x.FirstName == model.FirstName &&
x.Patronymic == model.Patronymic);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<EmployeeSetBindingModel, Employee>(model, true);
context.Employees.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Employee, EmployeeViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
2021-04-03 11:41:02 +04:00
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Employee, EmployeeViewModel>(exsistEntity, true));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(EmployeeGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
using var transaction = context.Database.BeginTransaction();
try
{
var entity = context.Employees.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);
}
if (context.Classrooms.Any(x => x.EmployeeId == model.Id && !x.IsDeleted))
2021-04-03 13:41:19 +04:00
{
return OperationResultModel.Error("Error:", "Есть аудитории, у которых этот сотрудник указан ответственным", ResultServiceStatusCode.ExsistItem);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
entity.User.IsDeleted = true;
entity.User.DateDelete = DateTime.Now;
context.SaveChanges();
var links = context.EmployeeEmployeePosts.Where(x => x.EmployeeId == 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(EmployeeGetBindingModel model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
{
var entity = context.Employees.FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
return OperationResultModel.Success(Mapper.MapToClass<Employee, EmployeeViewModel>(entity, model.HaveRight));
}
var query = context.Employees.Where(x => !x.IsDeleted).AsQueryable();
if (model.UserId.HasValue)
{
query = query.Where(x => x.UserId == model.UserId);
}
query = query.OrderBy(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);
}
var result = new EmployeeListViewModel
{
MaxCount = countPages,
List = query.Select(x => Mapper.MapToClass<Employee, EmployeeViewModel>(x, model.HaveRight)).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(EmployeeSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.Employees.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<Employee, EmployeeViewModel>(entity, true));
}
}
}