2024-04-23 21:50:08 +04:00
|
|
|
|
using LawFimDataModels.Models;
|
|
|
|
|
using LawFirmContracts.BindingModels;
|
|
|
|
|
using LawFirmContracts.ViewModels;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
|
|
|
|
|
namespace LawFirmDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Visit : IVisitModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public DateTime VisitDate { get; private set; }
|
|
|
|
|
|
|
|
|
|
//связь один-к-одному
|
|
|
|
|
public int HearingId { get; private set; }
|
|
|
|
|
public Hearing Hearing { get; set; } = null!;
|
|
|
|
|
//---
|
2024-04-27 23:00:03 +04:00
|
|
|
|
public int ExecutorId { get; set; }
|
2024-04-23 21:50:08 +04:00
|
|
|
|
|
|
|
|
|
private Dictionary<int, IClientModel>? _visitClients = null;
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IClientModel> VisitClients
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (_visitClients == null)
|
|
|
|
|
{
|
|
|
|
|
using var context = new LawFirmDatabase();
|
|
|
|
|
_visitClients = Clients
|
|
|
|
|
.ToDictionary(x => x.ClientId, x => (context.Clients
|
|
|
|
|
.FirstOrDefault(y => y.Id == x.ClientId) as IClientModel));
|
|
|
|
|
}
|
|
|
|
|
return _visitClients;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("VisitId")]
|
|
|
|
|
public virtual List<VisitClient> Clients { get; set; } = new();
|
2024-04-27 23:00:03 +04:00
|
|
|
|
public static Visit? Create(LawFirmDatabase context, VisitBindingModel? model)
|
2024-04-23 21:50:08 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var visits = context.Visits.Where(x => x.HearingId == model.HearingId).ToList();
|
|
|
|
|
if (visits.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Visit()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
VisitDate = model.VisitDate,
|
|
|
|
|
HearingId = model.HearingId,
|
2024-04-27 23:00:03 +04:00
|
|
|
|
ExecutorId = model.ExecutorId,
|
2024-04-23 21:50:08 +04:00
|
|
|
|
Hearing = context.Hearings.First(x => x.Id == model.HearingId),
|
|
|
|
|
Clients = model.VisitClients.Select(x => new VisitClient
|
|
|
|
|
{
|
|
|
|
|
Client = context.Clients.First(y => y.Id == x.Key)
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(VisitBindingModel? model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new LawFirmDatabase();
|
|
|
|
|
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VisitDate = model.VisitDate;
|
|
|
|
|
HearingId = model.HearingId;
|
|
|
|
|
}
|
|
|
|
|
public VisitViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
VisitDate = VisitDate,
|
|
|
|
|
HearingId = HearingId,
|
2024-04-27 23:00:03 +04:00
|
|
|
|
VisitClients = VisitClients,
|
|
|
|
|
ExecutorId = ExecutorId,
|
2024-04-23 21:50:08 +04:00
|
|
|
|
};
|
|
|
|
|
public void UpdateClients(LawFirmDatabase context,
|
|
|
|
|
VisitBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var visitClients = context.VisitClients.Where(rec =>
|
|
|
|
|
rec.VisitId == model.Id).ToList();
|
|
|
|
|
if (visitClients != null && visitClients.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
|
|
|
|
context.VisitClients.RemoveRange(visitClients.Where(rec
|
|
|
|
|
=> !model.VisitClients.ContainsKey(rec.ClientId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var _visit = context.Visits.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.VisitClients)
|
|
|
|
|
{
|
|
|
|
|
if (!VisitClients.ContainsKey(pc.Key))
|
|
|
|
|
{
|
|
|
|
|
context.VisitClients.Add(new VisitClient
|
|
|
|
|
{
|
|
|
|
|
Visit = _visit,
|
|
|
|
|
Client = context.Clients.First(x => x.Id == pc.Key),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_visitClients = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|