PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs
2024-03-10 01:33:27 +04:00

146 lines
5.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantDatabaseImplement.Models
{
/// <summary>
/// Сущность "Изделие"
/// </summary>
public class Plane : IPlaneModel
{
/// <summary>
/// Идентификатор
/// </summary>
public int Id { get; set; }
/// <summary>
/// Название изделия
/// </summary>
[Required]
public string PlaneName { get; set; } = string.Empty;
/// <summary>
/// Стоимость изделия
/// </summary>
[Required]
public double Price { get; set; }
/// <summary>
/// Коллекция компонентов изделия
/// </summary>
private Dictionary<int, (IComponentModel, int)>? _planeComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> PlaneComponents
{
get
{
if (_planeComponents == null)
{
_planeComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
}
return _planeComponents;
}
}
/// <summary>
/// Связь с классом связи изделия и компонента
/// </summary>
[ForeignKey("PlaneId")]
public virtual List<PlaneComponent> Components { get; set; } = new();
/// <summary>
/// Связь с заказами
/// </summary>
[ForeignKey("PlaneId")]
public virtual List<Order> Orders { get; set; } = new();
/// <summary>
/// Созданме модели изделия
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
/// <returns></returns>
public static Plane Create(AircraftPlantDatabase context, PlaneBindingModel model)
{
return new Plane()
{
Id = model.Id,
PlaneName = model.PlaneName,
Price = model.Price,
Components = model.PlaneComponents.Select(x => new PlaneComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
/// <summary>
/// Изменение модели изделия
/// </summary>
/// <param name="model"></param>
public void Update(PlaneBindingModel model)
{
PlaneName = model.PlaneName;
Price = model.Price;
}
/// <summary>
/// Получение модели изделия
/// </summary>
public PlaneViewModel GetViewModel => new()
{
Id = Id,
PlaneName = PlaneName,
Price = Price,
PlaneComponents = PlaneComponents
};
/// <summary>
/// Метод обновления списка связей
/// </summary>
/// <param name="context"></param>
/// <param name="model"></param>
public void UpdateComponents(AircraftPlantDatabase context, PlaneBindingModel model)
{
var planeComponents = context.PlaneComponents.Where(rec => rec.PlaneId == model.Id).ToList();
if (planeComponents != null && planeComponents.Count > 0)
{
// Удаление компонентов, которых нет в модели
context.PlaneComponents.RemoveRange(planeComponents.Where(rec => !model.PlaneComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// Обновление количества у существующих записей
foreach (var updateComponent in planeComponents)
{
updateComponent.Count = model.PlaneComponents[updateComponent.ComponentId].Item2;
model.PlaneComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var plane = context.Planes.First(x => x.Id == Id);
foreach (var pc in model.PlaneComponents)
{
context.PlaneComponents.Add(new PlaneComponent
{
Plane = plane,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_planeComponents = null;
}
}
}