2024-05-01 02:06:03 +04:00
|
|
|
|
using LawCompanyDataModels.Enums;
|
|
|
|
|
using LawCompanyDataModels.Models;
|
|
|
|
|
using LawCompanyContracts.BindingModels;
|
|
|
|
|
using LawCompanyContracts.ViewModels;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
|
|
|
|
|
namespace LawCompanyDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Case : ICaseModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public string Name { get; private set; } = String.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public DateTime DateCreate { get; private set; }
|
|
|
|
|
public DateTime? DateImplement { get; private set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public CaseStatus Status { get; private set; }
|
|
|
|
|
private Dictionary<int, IClientModel>? _caseClients = null;
|
2024-05-01 15:08:34 +04:00
|
|
|
|
public int ExecutorId { get; set; }
|
2024-05-01 02:06:03 +04:00
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IClientModel> CaseClients
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_caseClients == null)
|
|
|
|
|
{
|
2024-05-27 02:43:57 +04:00
|
|
|
|
_caseClients = Clients.ToDictionary(x => x.ClientId, x => (x.Client as IClientModel));
|
2024-05-01 02:06:03 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _caseClients;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("CaseId")]
|
|
|
|
|
public virtual List<CaseClient> Clients { get; set; } = new();
|
|
|
|
|
public static Case Create(LawCompanyDatabase context, CaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Case()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
DateCreate = model.DateCreate,
|
|
|
|
|
DateImplement = model.DateImplement,
|
|
|
|
|
Status = model.Status,
|
2024-05-01 15:08:34 +04:00
|
|
|
|
ExecutorId = model.ExecutorId,
|
2024-05-01 02:06:03 +04:00
|
|
|
|
Clients = model.CaseClients.Select(x => new CaseClient
|
|
|
|
|
{
|
|
|
|
|
Client = context.Clients.First(y => y.Id == x.Key)
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-05-27 02:43:57 +04:00
|
|
|
|
public void Update(CaseBindingModel model)
|
2024-05-01 02:06:03 +04:00
|
|
|
|
{
|
2024-05-27 02:43:57 +04:00
|
|
|
|
if (model.Status == CaseStatus.ЗакрытиеДела)
|
2024-05-01 02:06:03 +04:00
|
|
|
|
{
|
2024-05-27 02:43:57 +04:00
|
|
|
|
DateImplement = DateTime.Now;
|
2024-05-01 02:06:03 +04:00
|
|
|
|
}
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Status = model.Status;
|
|
|
|
|
|
|
|
|
|
|
2024-05-27 02:43:57 +04:00
|
|
|
|
if (model.DateImplement.HasValue)
|
|
|
|
|
{
|
|
|
|
|
DateImplement = model.DateImplement;
|
|
|
|
|
}
|
2024-05-01 02:06:03 +04:00
|
|
|
|
}
|
2024-05-27 02:43:57 +04:00
|
|
|
|
|
2024-05-01 02:06:03 +04:00
|
|
|
|
public CaseViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
DateCreate = DateCreate,
|
|
|
|
|
DateImplement = DateImplement,
|
2024-05-01 15:08:34 +04:00
|
|
|
|
ExecutorId = ExecutorId,
|
2024-05-01 02:06:03 +04:00
|
|
|
|
Status = Status,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void UpdateClients(LawCompanyDatabase context, CaseBindingModel model)
|
|
|
|
|
{
|
2024-05-27 02:43:57 +04:00
|
|
|
|
var caseClients = context.CaseClients.Where(rec => rec.CaseId == model.Id).ToList();
|
|
|
|
|
|
2024-05-01 02:06:03 +04:00
|
|
|
|
if (caseClients != null && caseClients.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
2024-05-27 02:43:57 +04:00
|
|
|
|
context.CaseClients.RemoveRange(caseClients.Where(rec => !model.CaseClients.ContainsKey(rec.ClientId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
foreach (var updateLunch in caseClients)
|
|
|
|
|
{
|
|
|
|
|
model.CaseClients.Remove(updateLunch.ClientId);
|
|
|
|
|
}
|
2024-05-01 02:06:03 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2024-05-27 02:43:57 +04:00
|
|
|
|
|
2024-05-01 02:06:03 +04:00
|
|
|
|
var _case = context.Cases.First(x => x.Id == Id);
|
2024-05-27 02:43:57 +04:00
|
|
|
|
|
2024-05-01 02:06:03 +04:00
|
|
|
|
foreach (var pc in model.CaseClients)
|
|
|
|
|
{
|
2024-05-27 02:43:57 +04:00
|
|
|
|
context.CaseClients.Add(new CaseClient
|
2024-05-01 02:06:03 +04:00
|
|
|
|
{
|
2024-05-27 02:43:57 +04:00
|
|
|
|
Case = _case,
|
|
|
|
|
Client = context.Clients.First(x => x.Id == pc.Key),
|
|
|
|
|
});
|
2024-05-01 02:06:03 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_caseClients = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|