116 lines
3.9 KiB
C#
116 lines
3.9 KiB
C#
using LawFimDataModels.Enums;
|
||
using LawFimDataModels.Models;
|
||
using LawFirmContracts.BindingModels;
|
||
using LawFirmContracts.ViewModels;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
using System.ComponentModel.DataAnnotations;
|
||
|
||
namespace LawFirmDatabaseImplement.Models
|
||
{
|
||
public class Case : ICaseModel
|
||
{
|
||
public int Id { get; set; }
|
||
[Required]
|
||
public string Name { get; set; } = String.Empty;
|
||
[Required]
|
||
public string CaseType { get; set; } = String.Empty;
|
||
[Required]
|
||
public DateTime DateCreate { get; set; }
|
||
public DateTime? DateImplement { get; set; }
|
||
[Required]
|
||
public CaseStatus Status { get; set; }
|
||
private Dictionary<int, IClientModel>? _caseClients = null;
|
||
public int ExecutorId { get; set; }
|
||
|
||
[NotMapped]
|
||
public Dictionary<int, IClientModel> CaseClients
|
||
{
|
||
get
|
||
{
|
||
if (_caseClients == null)
|
||
{
|
||
using var context = new LawFirmDatabase();
|
||
_caseClients = Clients
|
||
.ToDictionary(x => x.ClientId, x => (context.Clients.FirstOrDefault(y => y.Id == x.ClientId) as IClientModel));
|
||
}
|
||
|
||
return _caseClients;
|
||
}
|
||
}
|
||
[ForeignKey("CaseId")]
|
||
public virtual List<CaseClient> Clients { get; set; } = new();
|
||
public static Case Create(LawFirmDatabase context, CaseBindingModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return null;
|
||
}
|
||
return new Case()
|
||
{
|
||
Id = model.Id,
|
||
Name = model.Name,
|
||
DateCreate = model.DateCreate,
|
||
DateImplement = model.DateImplement,
|
||
Status = model.Status,
|
||
ExecutorId = model.ExecutorId,
|
||
Clients = model.CaseClients.Select(x => new CaseClient
|
||
{
|
||
Client = context.Clients.First(y => y.Id == x.Key)
|
||
}).ToList()
|
||
};
|
||
}
|
||
public void Update(CaseBindingModel? model)
|
||
{
|
||
using var context = new LawFirmDatabase();
|
||
if (model == null)
|
||
{
|
||
return;
|
||
}
|
||
if (model.Status == CaseStatus.ЗакрытиеДела) DateImplement = DateTime.Now;
|
||
Name = model.Name;
|
||
Status = model.Status;
|
||
|
||
|
||
if (model.DateImplement.HasValue) DateImplement = model.DateImplement;
|
||
}
|
||
public CaseViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
Name = Name,
|
||
CaseType = CaseType,
|
||
DateCreate = DateCreate,
|
||
DateImplement = DateImplement,
|
||
ExecutorId = ExecutorId,
|
||
Status = Status,
|
||
CaseClients = CaseClients,
|
||
};
|
||
|
||
|
||
public void UpdateClients(LawFirmDatabase context, CaseBindingModel model)
|
||
{
|
||
var caseClients = context.CaseClients.Where(rec =>
|
||
rec.CaseId == model.Id).ToList();
|
||
if (caseClients != null && caseClients.Count > 0)
|
||
{ // удалили те, которых нет в модели
|
||
context.CaseClients.RemoveRange(caseClients.Where(rec
|
||
=> !model.CaseClients.ContainsKey(rec.ClientId)));
|
||
context.SaveChanges();
|
||
}
|
||
var _case = context.Cases.First(x => x.Id == Id);
|
||
foreach (var pc in model.CaseClients)
|
||
{
|
||
if (!CaseClients.ContainsKey(pc.Key))
|
||
{
|
||
context.CaseClients.Add(new CaseClient
|
||
{
|
||
Case = _case,
|
||
Client = context.Clients.First(x => x.Id == pc.Key),
|
||
});
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
_caseClients = null;
|
||
}
|
||
}
|
||
}
|