141 lines
4.6 KiB
C#
141 lines
4.6 KiB
C#
using AircraftPlantContracts.BindingModels;
|
||
using AircraftPlantContracts.ViewModels;
|
||
using AircraftPlantDataModels.Models;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Xml.Linq;
|
||
|
||
namespace AircraftPlantFileImplement.Models
|
||
{
|
||
/// <summary>
|
||
/// Сущность "Изделие"
|
||
/// </summary>
|
||
public class Plane : IPlaneModel
|
||
{
|
||
/// <summary>
|
||
/// Идентификатор
|
||
/// </summary>
|
||
public int Id { get; private set; }
|
||
|
||
/// <summary>
|
||
/// Название изделия
|
||
/// </summary>
|
||
public string PlaneName { get; private set; } = string.Empty;
|
||
|
||
/// <summary>
|
||
/// Стоимость изделия
|
||
/// </summary>
|
||
public double Price { get; private set; }
|
||
|
||
/// <summary>
|
||
/// Коллекция компонентов изделия в виде
|
||
/// «идентификатор компонента – количество компонентов»
|
||
/// </summary>
|
||
public Dictionary<int, int> Components { get; private set; } = new();
|
||
|
||
/// <summary>
|
||
/// Коллекция компонентов изделия
|
||
/// </summary>
|
||
private Dictionary<int, (IComponentModel, int)>? _planeComponents = null;
|
||
public Dictionary<int, (IComponentModel, int)> PlaneComponents
|
||
{
|
||
get
|
||
{
|
||
if (_planeComponents == null)
|
||
{
|
||
var source = DataFileSingleton.GetInstance();
|
||
_planeComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
|
||
}
|
||
return _planeComponents;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание модели изделия
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public static Plane? Create(PlaneBindingModel? model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
return new Plane()
|
||
{
|
||
Id = model.Id,
|
||
PlaneName = model.PlaneName,
|
||
Price = model.Price,
|
||
Components = model.PlaneComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание модели изделия из данных файла
|
||
/// </summary>
|
||
/// <param name="element"></param>
|
||
/// <returns></returns>
|
||
public static Plane? Create(XElement element)
|
||
{
|
||
if (element == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
return new Plane()
|
||
{
|
||
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
||
PlaneName = element.Element("PlaneName")!.Value,
|
||
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
||
Components = element.Element("PlaneComponents")!.Elements("PlaneComponent")
|
||
.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// Изменение модели изделия
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
public void Update(PlaneBindingModel? model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
PlaneName = model.PlaneName;
|
||
Price = model.Price;
|
||
Components = model.PlaneComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
|
||
_planeComponents = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение модели изделия
|
||
/// </summary>
|
||
public PlaneViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
PlaneName = PlaneName,
|
||
Price = Price,
|
||
PlaneComponents = PlaneComponents
|
||
};
|
||
|
||
/// <summary>
|
||
/// Запись данных о модели изделия в файл
|
||
/// </summary>
|
||
public XElement GetXElement => new("Plane",
|
||
new XAttribute("Id", Id),
|
||
new XElement("PlaneName", PlaneName),
|
||
new XElement("Price", Price.ToString()),
|
||
new XElement("PlaneComponents", Components.Select(x =>
|
||
new XElement("PlaneComponent",
|
||
new XElement("Key", x.Key),
|
||
new XElement("Value", x.Value)))
|
||
.ToArray()));
|
||
}
|
||
}
|