PIbd-21_Ihonkina_E.S._Preca.../PrecastConcreteFileImplement/Models/Reinforced.cs

97 lines
3.4 KiB
C#
Raw Normal View History

2023-03-18 17:55:02 +04:00
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System;
2023-03-18 17:47:29 +04:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-03-18 17:55:02 +04:00
using System.Xml.Linq;
2023-03-18 17:47:29 +04:00
namespace PrecastConcretePlantFileImplement.Models
{
2023-03-18 17:55:02 +04:00
public class Reinforced : IReinforcedModel
2023-03-18 17:47:29 +04:00
{
2023-03-18 17:55:02 +04:00
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()));
2023-03-18 17:47:29 +04:00
}
}