219 lines
6.5 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
2023-05-05 22:16:51 +04:00
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using PersonnelDepartmentContracts.BindingModels;
using PersonnelDepartmentContracts.SearchModels;
using PersonnelDepartmentContracts.StoragesContracts;
using PersonnelDepartmentContracts.ViewModels;
using PersonnelDepartmentDatabaseImplement.Models;
using System;
using System.Collections.Generic;
2023-05-05 22:16:51 +04:00
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PersonnelDepartmentDatabaseImplement.Implements
{
public class DealStorage : IDealStorage
{
2023-05-05 22:16:51 +04:00
public bool ClearList()
{
try
{
using var context = new PersonnelDepartmentDatabase();
/*var list = context.Deals.ToList();
context.Deals.RemoveRange(list);
context.SaveChanges();*/
var tableName = context.Model.FindEntityType(typeof(Deal)).GetTableName();
context.Database.ExecuteSqlRaw($"DELETE FROM \"{ tableName }\"");
}
catch (Exception)
{
return false;
}
return true;
}
public DealViewModel? Delete(DealBindingModel model)
{
using var context = new PersonnelDepartmentDatabase();
var element = context.Deals
2023-05-02 23:10:47 +04:00
.Include(x => x.Employee)
.Include(x => x.Department)
.Include(x => x.Type)
.Include(x => x.Position)
.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
context.Deals.Remove(element);
2023-05-02 23:10:47 +04:00
context.SaveChanges();
return element.GetViewModel;
}
2023-05-05 22:16:51 +04:00
public string DiffGetTest(int count)
{
using var context = new PersonnelDepartmentDatabase();
Stopwatch stopwatch = new();
stopwatch.Start();
var list = context.Deals
.Include(x => x.Department)
.Include(x => x.Employee)
.Include(x => x.Type)
.Include(x => x.Position)
.Join(context.Departments,
deal => deal.DepartmentId,
department => department.Id,
(deal, department) => new { Deal = deal, Department = department })
.Join(context.Employees,
deal => deal.Deal.EmployeeId,
employee => employee.Id,
(deal, employee) => new { deal.Deal, deal.Department, Employee = employee })
.Join(context.Positions,
deal => deal.Deal.PositionId,
position => position.Id,
(deal, position) => new { deal.Deal, deal.Department, deal.Employee, Position = position })
.Join(context.Types,
deal => deal.Deal.TypeId,
type => type.Id,
(deal, type) => new { deal.Deal, deal.Department, deal.Employee, deal.Position, Type = type })
.Select(deal => new
{
Deal = deal.Deal.GetViewModel,
Department = deal.Department.GetViewModel,
Type = deal.Type.GetViewModel,
Position = deal.Position.GetViewModel
})
.Take(count)
.ToList();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds.ToString();
}
public DealViewModel? GetElement(DealSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new PersonnelDepartmentDatabase();
return context.Deals
.Include(x => x.Employee)
.Include(x => x.Department)
.Include(x => x.Type)
2023-05-02 23:10:47 +04:00
.Include(x => x.Position)
.FirstOrDefault(x => (model.Id.HasValue && model.Id == x.Id) ||
(model.DateFrom != null && x.DateFrom == model.DateFrom) ||
(model.DateTo != null && x.DateTo == model.DateTo) ||
(model.EmployeeId.HasValue && x.EmployeeId == model.EmployeeId) ||
(model.DepartmentId.HasValue && x.DepartmentId == model.DepartmentId) ||
(model.PositionId.HasValue && x.PositionId == model.PositionId) ||
(model.TypeId.HasValue && x.TypeId == model.TypeId))?.GetViewModel;
}
public List<DealViewModel> GetFilteredList(DealSearchModel model)
{
using var context = new PersonnelDepartmentDatabase();
return context.Deals
.Include(x => x.Employee)
.Include(x => x.Department)
.Include(x => x.Type)
2023-05-02 23:10:47 +04:00
.Include(x => x.Position)
.Where(x => (model.Id.HasValue && model.Id == x.Id) ||
(model.DateFrom != null && x.DateFrom == model.DateFrom) ||
(model.DateTo != null && x.DateTo == model.DateTo) ||
(model.EmployeeId.HasValue && x.EmployeeId == model.EmployeeId) ||
(model.DepartmentId.HasValue && x.DepartmentId == model.DepartmentId) ||
(model.PositionId.HasValue && x.PositionId == model.PositionId) ||
(model.TypeId.HasValue && x.TypeId == model.TypeId))
.Select(x => x.GetViewModel) .ToList();
}
public List<DealViewModel> GetFullList()
{
using var context = new PersonnelDepartmentDatabase();
return context.Deals
.Include(x => x.Department)
.Include(x => x.Position)
.Include(x => x.Employee)
.Include(x => x.Type)
.Select(x => x.GetViewModel)
.ToList();
}
2023-05-05 22:16:51 +04:00
public string GetTest(int count)
{
using var context = new PersonnelDepartmentDatabase();
Stopwatch stopwatch = new();
stopwatch.Start();
var list = context.Deals
.Include(x => x.Department)
.Include(x => x.Employee)
.Include(x => x.Type)
.Include(x => x.Position)
.Take(count)
.Select(x => x.GetViewModel)
.ToList();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds.ToString();
}
public DealViewModel? Insert(DealBindingModel model)
{
var newElement = Deal.Create(model);
if (newElement == null)
{
return null;
}
using var context = new PersonnelDepartmentDatabase();
context.Deals.Add(newElement);
context.SaveChanges();
return GetElement(new DealSearchModel { Id = newElement.Id } );
}
2023-05-05 22:16:51 +04:00
public string SetTest(int count)
{
Random rnd = new Random();
using var context = new PersonnelDepartmentDatabase();
for (int i = 0; i < count; ++i)
{
context.Deals.Add(Deal.Create(new DealBindingModel
{
Id = 0,
DateFrom = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
DateTo = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
DepartmentId = 1,
EmployeeId = 1,
TypeId = 1,
PositionId = 1
}));
}
Stopwatch stopwatch = new();
stopwatch.Start();
context.SaveChanges();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds.ToString();
}
public DealViewModel? Update(DealBindingModel model)
{
using var context = new PersonnelDepartmentDatabase();
var element = context.Deals
.Include(x => x.Department)
.Include(x => x.Employee)
.Include(x => x.Type)
.Include (x => x.Position)
.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return GetElement(new DealSearchModel { Id = element.Id });
}
}
}