Pibd-21_Ievlewa_MD._Precast.../PrecastConcretePlant/PrecastConcretePlantFileImplement/Models/Reinforced.cs

91 lines
3.4 KiB
C#
Raw Normal View History

2024-03-23 12:04:42 +04:00
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System.Xml.Linq;
namespace PrecastConcretePlantFileImplement.Models
{
public class Reinforced : IReinforcedModel
{
public int Id { get; private set; }
public string ReinforcedName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _reinforcedComponents = null;
public Dictionary<int, (IComponentModel, int)> ReinforcedComponents
{
get
{
if (_reinforcedComponents == null)
{
var source = DataFileSingleton.GetInstance();
_reinforcedComponents = Components.ToDictionary(x => x.Key, y =>
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
y.Value));
}
return _reinforcedComponents;
}
}
public static Reinforced? Create(ReinforcedBindingModel model)
{
if (model == null)
{
return null;
}
return new Reinforced()
{
Id = model.Id,
ReinforcedName = model.ReinforcedName,
Price = model.Price,
Components = model.ReinforcedComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Reinforced? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Reinforced()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ReinforcedName = element.Element("ReinforcedName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components =
element.Element("ReinforcedComponents")!.Elements("ReinforcedComponent")
.ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(ReinforcedBindingModel model)
{
if (model == null)
{
return;
}
ReinforcedName = model.ReinforcedName;
Price = model.Price;
Components = model.ReinforcedComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_reinforcedComponents = null;
}
public ReinforcedViewModel GetViewModel => new()
{
Id = Id,
ReinforcedName = ReinforcedName,
Price = Price,
ReinforcedComponents = ReinforcedComponents
};
public XElement GetXElement => new("Reinforced",
new XAttribute("Id", Id),
new XElement("ReinforcedName", ReinforcedName),
new XElement("Price", Price.ToString()),
new XElement("ReinforcedComponents", Components.Select(x =>
new XElement("ReinforcedComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value)))
.ToArray()));
}
}