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