2021-04-03 10:02:32 +04:00
|
|
|
|
using DatabaseCore;
|
|
|
|
|
using DatabaseCore.Models.Department;
|
|
|
|
|
using DepartmentBusinessLogic.BindingModels;
|
|
|
|
|
using DepartmentBusinessLogic.Interfaces;
|
|
|
|
|
using DepartmentBusinessLogic.ViewModels;
|
|
|
|
|
using ModuleTools.BusinessLogics;
|
|
|
|
|
using ModuleTools.Enums;
|
|
|
|
|
using ModuleTools.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace DepartmentDatabaseImplementation.Implementations
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация IEmployeePostService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class EmployeePostService : IEmployeePostService
|
|
|
|
|
{
|
|
|
|
|
public OperationResultModel Create(EmployeePostSetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var exsistEntity = context.EmployeePosts.FirstOrDefault(x => x.EmployeePostName == model.EmployeePostName);
|
|
|
|
|
if (exsistEntity == null)
|
|
|
|
|
{
|
|
|
|
|
var entity = Mapper.MapToClass<EmployeePostSetBindingModel, EmployeePost>(model, true);
|
|
|
|
|
context.EmployeePosts.Add(entity);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<EmployeePost, EmployeePostViewModel>(entity, true));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (exsistEntity.IsDeleted)
|
|
|
|
|
{
|
2021-04-03 11:41:02 +04:00
|
|
|
|
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
|
2021-04-03 10:02:32 +04:00
|
|
|
|
exsistEntity.IsDeleted = false;
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<EmployeePost, EmployeePostViewModel>(exsistEntity, true));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Delete(EmployeePostGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var entity = context.EmployeePosts.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.IsDeleted = true;
|
|
|
|
|
entity.DateDelete = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
var links = context.EmployeeEmployeePosts.Where(x => x.EmployeePostId == 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(EmployeePostGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
int countPages = 0;
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
// для одной записи
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var entity = context.EmployeePosts.FirstOrDefault(x => x.Id == model.Id.Value);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<EmployeePost, EmployeePostViewModel>(entity, model.HaveRight));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = context.EmployeePosts.Where(x => !x.IsDeleted).AsQueryable();
|
|
|
|
|
|
|
|
|
|
query = query.OrderBy(x => x.EmployeePostName);
|
|
|
|
|
|
|
|
|
|
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 EmployeePostListViewModel
|
|
|
|
|
{
|
|
|
|
|
MaxCount = countPages,
|
|
|
|
|
List = query.Select(x => Mapper.MapToClass<EmployeePost, EmployeePostViewModel>(x, model.HaveRight)).ToList()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Update(EmployeePostSetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var entity = context.EmployeePosts.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<EmployeePostSetBindingModel, EmployeePost>(model, entity, true);
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<EmployeePost, EmployeePostViewModel>(entity, true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|