224 lines
7.0 KiB
C#
224 lines
7.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
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;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace PersonnelDepartmentDatabaseImplement.Implements
|
|
{
|
|
public class DealStorage : IDealStorage
|
|
{
|
|
public bool ClearList()
|
|
{
|
|
try
|
|
{
|
|
using var context = new PersonnelDepartmentDatabase();
|
|
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
|
|
.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);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
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)
|
|
.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)
|
|
.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();
|
|
}
|
|
|
|
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 } );
|
|
}
|
|
|
|
public string SetTest(int count)
|
|
{
|
|
Random rnd = new Random();
|
|
using var context = new PersonnelDepartmentDatabase();
|
|
var listDepartments = context.Departments.Select(x => x.GetViewModel).ToList();
|
|
var listEmployees = context.Employees.Select(x => x.GetViewModel).ToList();
|
|
var listPositions = context.Positions.Select(x => x.GetViewModel).ToList();
|
|
var listTypes = context.Types.Select(x => x.GetViewModel).ToList();
|
|
if (listDepartments.Count < 1 || listEmployees.Count < 1 || listPositions.Count < 1 || listTypes.Count < 1)
|
|
{
|
|
throw new Exception("Недостаточно для генерации!");
|
|
}
|
|
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 = listDepartments[rnd.Next(listDepartments.Count)].Id,
|
|
EmployeeId = listEmployees[rnd.Next(listEmployees.Count)].Id,
|
|
TypeId = listTypes[rnd.Next(listTypes.Count)].Id,
|
|
PositionId = listPositions[rnd.Next(listPositions.Count)].Id
|
|
}));
|
|
}
|
|
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 });
|
|
}
|
|
}
|
|
}
|