2024-11-12 22:10:44 +04:00
|
|
|
|
using EmployeeManagmentContracts.SearchModels;
|
|
|
|
|
using EmployeeManagmentContracts.StoragesContracts;
|
|
|
|
|
using EmployeeManagmentContracts.ViewModels;
|
|
|
|
|
using EmployeeManagmentDataBaseImplement.Models;
|
2024-11-12 21:25:53 +04:00
|
|
|
|
|
|
|
|
|
namespace EmployeeManagmentDataBaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class EmployeeStorage : IEmployeeStorage
|
|
|
|
|
{
|
2024-12-01 14:29:10 +04:00
|
|
|
|
// Получить полный список сотрудников
|
|
|
|
|
public List<EmployeeViewModel> GetFullList()
|
2024-11-12 21:25:53 +04:00
|
|
|
|
{
|
2024-12-01 14:29:10 +04:00
|
|
|
|
using var context = new EmployeeManagementDbContext();
|
2024-11-12 21:25:53 +04:00
|
|
|
|
|
2024-12-01 14:29:10 +04:00
|
|
|
|
return context.Employees
|
|
|
|
|
.Select(e => new EmployeeViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = e.Id,
|
|
|
|
|
NameJob = e.NameJob,
|
|
|
|
|
StartJob = e.StartJob,
|
|
|
|
|
EndJob = e.EndJob,
|
|
|
|
|
PartTimeJob = e.PartTimeJob,
|
|
|
|
|
Bid = e.Bid,
|
|
|
|
|
PhysicalPersonsId = e.PhisicalPersonsId,
|
|
|
|
|
PhysicalPersonName = $"{e.PhisicalPerson.Surname} {e.PhisicalPerson.Name}"
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
2024-11-12 21:25:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 14:29:10 +04:00
|
|
|
|
// Получить отфильтрованный список сотрудников
|
2024-11-12 22:10:44 +04:00
|
|
|
|
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
|
2024-11-12 21:25:53 +04:00
|
|
|
|
{
|
2024-12-01 14:29:10 +04:00
|
|
|
|
using var context = new EmployeeManagementDbContext();
|
|
|
|
|
|
|
|
|
|
if (model == null) return new List<EmployeeViewModel>();
|
|
|
|
|
|
|
|
|
|
return context.Employees
|
|
|
|
|
.Where(e =>
|
|
|
|
|
(string.IsNullOrEmpty(model.NameJob) || e.NameJob.Contains(model.NameJob)) &&
|
|
|
|
|
(!model.StartDateFrom.HasValue || e.StartJob >= model.StartDateFrom) &&
|
|
|
|
|
(!model.StartDateTo.HasValue || e.EndJob <= model.StartDateTo))
|
|
|
|
|
.Select(e => new EmployeeViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = e.Id,
|
|
|
|
|
NameJob = e.NameJob,
|
|
|
|
|
StartJob = e.StartJob,
|
|
|
|
|
EndJob = e.EndJob,
|
|
|
|
|
PartTimeJob = e.PartTimeJob,
|
|
|
|
|
Bid = e.Bid,
|
|
|
|
|
PhysicalPersonsId = e.PhisicalPersonsId,
|
|
|
|
|
PhysicalPersonName = $"{e.PhisicalPerson.Surname} {e.PhisicalPerson.Name}"
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
2024-11-12 21:25:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 14:29:10 +04:00
|
|
|
|
// Получить сотрудника по ID
|
|
|
|
|
public EmployeeViewModel? GetElement(int id)
|
2024-11-12 21:25:53 +04:00
|
|
|
|
{
|
2024-12-01 14:29:10 +04:00
|
|
|
|
using var context = new EmployeeManagementDbContext();
|
|
|
|
|
|
|
|
|
|
var entity = context.Employees
|
|
|
|
|
.FirstOrDefault(e => e.Id == id);
|
|
|
|
|
|
|
|
|
|
return entity == null ? null : new EmployeeViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = entity.Id,
|
|
|
|
|
NameJob = entity.NameJob,
|
|
|
|
|
StartJob = entity.StartJob,
|
|
|
|
|
EndJob = entity.EndJob,
|
|
|
|
|
PartTimeJob = entity.PartTimeJob,
|
|
|
|
|
Bid = entity.Bid,
|
|
|
|
|
PhysicalPersonsId = entity.PhisicalPersonsId,
|
2024-12-01 15:27:38 +04:00
|
|
|
|
PhysicalPersonName = entity.PhisicalPerson != null
|
|
|
|
|
? $"{entity.PhisicalPerson.Surname} {entity.PhisicalPerson.Name}"
|
|
|
|
|
: "Не указано" // Обработка отсутствующего физического лица
|
2024-12-01 14:29:10 +04:00
|
|
|
|
};
|
2024-11-12 21:25:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 15:27:38 +04:00
|
|
|
|
|
2024-12-01 14:29:10 +04:00
|
|
|
|
// Добавить нового сотрудника
|
2024-11-12 22:10:44 +04:00
|
|
|
|
public void Insert(EmployeeViewModel model)
|
2024-11-12 21:25:53 +04:00
|
|
|
|
{
|
2024-12-01 14:29:10 +04:00
|
|
|
|
using var context = new EmployeeManagementDbContext();
|
|
|
|
|
|
|
|
|
|
var newEmployee = new Employee
|
|
|
|
|
{
|
|
|
|
|
NameJob = model.NameJob,
|
|
|
|
|
StartJob = model.StartJob,
|
|
|
|
|
EndJob = model.EndJob,
|
|
|
|
|
PartTimeJob = model.PartTimeJob,
|
|
|
|
|
Bid = model.Bid,
|
|
|
|
|
PhisicalPersonsId = model.PhysicalPersonsId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.Employees.Add(newEmployee);
|
|
|
|
|
context.SaveChanges();
|
2024-11-12 21:25:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 14:29:10 +04:00
|
|
|
|
// Обновить сотрудника
|
2024-11-12 22:10:44 +04:00
|
|
|
|
public void Update(EmployeeViewModel model)
|
2024-11-12 21:25:53 +04:00
|
|
|
|
{
|
2024-12-01 14:29:10 +04:00
|
|
|
|
using var context = new EmployeeManagementDbContext();
|
|
|
|
|
|
|
|
|
|
var entity = context.Employees.FirstOrDefault(e => e.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (entity != null)
|
|
|
|
|
{
|
|
|
|
|
entity.NameJob = model.NameJob;
|
|
|
|
|
entity.StartJob = model.StartJob;
|
|
|
|
|
entity.EndJob = model.EndJob;
|
|
|
|
|
entity.PartTimeJob = model.PartTimeJob;
|
|
|
|
|
entity.Bid = model.Bid;
|
|
|
|
|
entity.PhisicalPersonsId = model.PhysicalPersonsId;
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Удалить сотрудника
|
|
|
|
|
public void Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
using var context = new EmployeeManagementDbContext();
|
|
|
|
|
|
|
|
|
|
var entity = context.Employees.FirstOrDefault(e => e.Id == id);
|
|
|
|
|
|
|
|
|
|
if (entity != null)
|
|
|
|
|
{
|
|
|
|
|
context.Employees.Remove(entity);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2024-11-12 21:25:53 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|