201 lines
6.8 KiB
C#
201 lines
6.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using VetClinicBusinessLogic.BindingModels;
|
|
using VetClinicBusinessLogic.Interfaces;
|
|
using VetClinicBusinessLogic.ViewModels;
|
|
using VetClinicDatabaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
namespace VetClinicDatabaseImplement.Implement
|
|
{
|
|
public class VisitStorage : IVisitStorage
|
|
{
|
|
public List<VisitViewModel> GetFullList()
|
|
{
|
|
using var context = new VetClinicDatabase();
|
|
return context.Visits
|
|
.Include(rec => rec.VisitServices)
|
|
.ThenInclude(rec => rec.Service)
|
|
.Include(x => x.Client)
|
|
.Include(x => x.Employee)
|
|
.Select(CreateModel)
|
|
.ToList();
|
|
}
|
|
public List<VisitViewModel> GetFilteredList(VisitBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new VetClinicDatabase();
|
|
return context.Visits.Include(rec => rec.VisitServices)
|
|
.ThenInclude(rec => rec.Service)
|
|
.Include(x => x.Client)
|
|
.Include(x => x.Employee).Where(rec => rec.ClientId == model.ClientId)
|
|
.Select(CreateModel)
|
|
|
|
.ToList();
|
|
}
|
|
public List<VisitViewModel> GetFilteredListEmployee(VisitBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new VetClinicDatabase();
|
|
return context.Visits.Include(rec => rec.VisitServices)
|
|
.ThenInclude(rec => rec.Service)
|
|
.Include(x => x.Client)
|
|
.Include(x => x.Employee).Where(rec => rec.EmployeeId == model.EmployeeId)
|
|
.Select(CreateModel)
|
|
|
|
.ToList();
|
|
}
|
|
public List<VisitViewModel> GetFilteredListDate(VisitBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new VetClinicDatabase();
|
|
return context.Visits.Include(rec => rec.VisitServices)
|
|
.ThenInclude(rec => rec.Service)
|
|
.Include(x => x.Client)
|
|
.Include(x => x.Employee).Where(rec => rec.Id.Equals(model.Id) || rec.DateVisit >= model.DateFrom && rec.DateVisit <= model.DateTo)
|
|
.Select(CreateModel)
|
|
|
|
.ToList();
|
|
}
|
|
public VisitViewModel GetElement(VisitBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new VetClinicDatabase();
|
|
var visit = context.Visits
|
|
.Include(rec => rec.VisitServices)
|
|
.ThenInclude(rec => rec.Service)
|
|
.Include(x => x.Client)
|
|
.Include(x => x.Employee)
|
|
.FirstOrDefault(rec => rec.Id == model.Id)
|
|
;
|
|
if (visit == null)
|
|
{
|
|
return null;
|
|
}
|
|
return CreateModel(visit);
|
|
}
|
|
public void Insert(VisitBindingModel model)
|
|
{
|
|
using var context = new VetClinicDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
CreateModel(model, new Visit(), context);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
public void Update(VisitBindingModel model)
|
|
{
|
|
using var context = new VetClinicDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var element = context.Visits.FirstOrDefault(rec => rec.Id ==
|
|
model.Id);
|
|
if (element == null)
|
|
{
|
|
throw new Exception("Элемент не найден");
|
|
}
|
|
CreateModel(model, element, context);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
public void Delete(VisitBindingModel model)
|
|
{
|
|
using var context = new VetClinicDatabase();
|
|
Visit element = context.Visits.FirstOrDefault(rec => rec.Id ==
|
|
model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Visits.Remove(element);
|
|
context.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Элемент не найден");
|
|
}
|
|
}
|
|
private static Visit CreateModel(VisitBindingModel model, Visit visit,
|
|
VetClinicDatabase context)
|
|
{
|
|
visit.DateVisit = model.VisitsDate;
|
|
visit.ClientId = model.ClientId;
|
|
visit.EmployeeId = model.EmployeeId;
|
|
visit.Sum = model.Sum;
|
|
if (visit.Id == 0)
|
|
{
|
|
context.Visits.Add(visit);
|
|
context.SaveChanges();
|
|
}
|
|
if (model.Id != 0)
|
|
{
|
|
var visitServices = context.VisitServices.Where(rec =>
|
|
rec.VisitId == model.Id).ToList();
|
|
// удалили те, которых нет в модели
|
|
context.VisitServices.RemoveRange(visitServices.Where(rec =>
|
|
!model.VisitServices.ContainsKey(rec.VisitId)).ToList());
|
|
context.SaveChanges();
|
|
context.SaveChanges();
|
|
}
|
|
// добавили новые
|
|
foreach (var pc in model.VisitServices)
|
|
{
|
|
context.VisitServices.Add(new VisitService
|
|
{
|
|
VisitId = visit.Id,
|
|
ServiceId = pc.Key,
|
|
});
|
|
var temp = context.VisitServices;
|
|
context.SaveChanges();
|
|
}
|
|
return visit;
|
|
}
|
|
private static VisitViewModel CreateModel(Visit visit)
|
|
{
|
|
return new VisitViewModel
|
|
{
|
|
Id = visit.Id,
|
|
ClientName = visit.Client.Name,
|
|
EmployeeName = visit?.Employee?.Name,
|
|
ClientId = visit.ClientId,
|
|
EmployeeId = visit?.EmployeeId,
|
|
Sum = visit.Sum,
|
|
VisitsDate = visit.DateVisit,
|
|
VisitServices = visit.VisitServices
|
|
.ToDictionary(recPC => recPC.ServiceId,
|
|
recPC => recPC.Service?.Name)
|
|
};
|
|
}
|
|
}
|
|
}
|