108 lines
3.5 KiB
C#
108 lines
3.5 KiB
C#
|
using CarCenterContracts.BindingModels;
|
|||
|
using CarCenterContracts.ViewModels;
|
|||
|
using CarCenterDataModels.Models;
|
|||
|
using System.ComponentModel.DataAnnotations;
|
|||
|
using System.ComponentModel.DataAnnotations.Schema;
|
|||
|
using System.Text.Json.Serialization;
|
|||
|
|
|||
|
namespace CarCenterDataBaseImplement.Models
|
|||
|
{
|
|||
|
public class Employee : IEmployeeModel
|
|||
|
{
|
|||
|
[Required]
|
|||
|
public string EmployeeFIO { get; set; } = string.Empty;
|
|||
|
[Required]
|
|||
|
public string Specialization { get; set; } = string.Empty;
|
|||
|
|
|||
|
public int ManagerId { get; private set; }
|
|||
|
|
|||
|
public int Id { get; private set; }
|
|||
|
|
|||
|
public virtual Manager Manager { get; set; }
|
|||
|
|
|||
|
private Dictionary<int, ISaleModel> _employeeSale = null;
|
|||
|
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, ISaleModel> EmployeeSale
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_employeeSale == null)
|
|||
|
{
|
|||
|
using var context = new CarCenterDataBase();
|
|||
|
_employeeSale = Sale
|
|||
|
.ToDictionary(x => x.SaleId, x => (context.Sale
|
|||
|
.FirstOrDefault(y => y.Id == x.SaleId)! as ISaleModel));
|
|||
|
}
|
|||
|
return _employeeSale;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[ForeignKey("EmployeeId")]
|
|||
|
public virtual List<Inspection> Inspections { get; set; } = new();
|
|||
|
|
|||
|
[ForeignKey("EmployeeId")]
|
|||
|
public virtual List<EmployeeSale> Sale { get; set; } = new();
|
|||
|
|
|||
|
public static Employee Create(CarCenterDataBase context, EmployeeBindingModel model)
|
|||
|
{
|
|||
|
return new Employee()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
EmployeeFIO = model.EmployeeFIO,
|
|||
|
Specialization = model.Specialization,
|
|||
|
ManagerId=model.ManagerId,
|
|||
|
Sale = model.EmployeeSale.Select(x => new EmployeeSale
|
|||
|
{
|
|||
|
Sale = context.Sale.First(y => y.Id == x.Key),
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
public void Update(EmployeeBindingModel model)
|
|||
|
{
|
|||
|
EmployeeFIO = model.EmployeeFIO;
|
|||
|
Specialization = model.Specialization;
|
|||
|
ManagerId = model.ManagerId;
|
|||
|
}
|
|||
|
|
|||
|
public EmployeeViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
EmployeeFIO = EmployeeFIO,
|
|||
|
Specialization = Specialization,
|
|||
|
ManagerId = ManagerId,
|
|||
|
EmployeeSale = EmployeeSale
|
|||
|
};
|
|||
|
|
|||
|
public void UpdateSale(CarCenterDataBase context, EmployeeBindingModel model)
|
|||
|
{
|
|||
|
var EmployeeSale = context.EmployeeSale.Where(rec => rec.EmployeeId == model.Id).ToList();
|
|||
|
|
|||
|
if (EmployeeSale != null && EmployeeSale.Any())
|
|||
|
{
|
|||
|
context.EmployeeSale.RemoveRange(EmployeeSale.Where(rec => !model.EmployeeSale.ContainsKey(rec.SaleId)));
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
foreach (var updateSale in EmployeeSale)
|
|||
|
{
|
|||
|
model.EmployeeSale.Remove(updateSale.SaleId);
|
|||
|
}
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
var Employee = context.Employees.First(x => x.Id == Id);
|
|||
|
foreach (var cm in model.EmployeeSale)
|
|||
|
{
|
|||
|
context.EmployeeSale.Add(new EmployeeSale
|
|||
|
{
|
|||
|
Employee = Employee,
|
|||
|
Sale = context.Sale.First(x => x.Id == cm.Key),
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_employeeSale = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|