using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.ViewModels; using PrecastConcretePlantDataModels; using PrecastConcretePlantDataModels.Models; using System.Xml.Linq; namespace PrecastConcretePlantFileImplement { public class Shop : IShopModel { public string Name { get; private set; } = string.Empty; public string Address { get; private set; } = string.Empty; public int MaxCountReinforceds { get; private set; } public DateTime DateOpening { get; private set; } public Dictionary CountReinforceds { get; private set; } = new(); private Dictionary? _cachedReinforceds = null; public Dictionary Reinforceds { get { if (_cachedReinforceds == null) { var source = DataFileSingleton.GetInstance(); _cachedReinforceds = CountReinforceds .ToDictionary(x => x.Key, x => (source.Reinforceds .FirstOrDefault(y => y.Id == x.Key)! as IReinforcedModel, x.Value)); } return _cachedReinforceds; } } public int Id { get; private set; } public static Shop? Create(ShopBindingModel? model) { if (model == null) { return null; } return new Shop() { Id = model.Id, Name = model.Name, Address = model.Address, DateOpening = model.DateOpening, MaxCountReinforceds = model.MaxCountReinforceds, CountReinforceds = new() }; } public static Shop? Create(XElement element) { if (element == null) { return null; } return new() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), Name = element.Element("Name")!.Value, Address = element.Element("Address")!.Value, DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), MaxCountReinforceds = Convert.ToInt32(element.Element("MaxCountReinforceds")!.Value), CountReinforceds = element.Element("CountReinforceds")!.Elements("CountReinforced") .ToDictionary( x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value) ) }; } public void Update(ShopBindingModel? model) { if (model == null) { return; } Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; CountReinforceds = model.Reinforceds.ToDictionary(x => x.Key, x => x.Value.Item2); _cachedReinforceds = null; } public ShopViewModel GetViewModel => new() { Id = Id, Name = Name, Address = Address, Reinforceds = Reinforceds, DateOpening = DateOpening, MaxCountReinforceds = MaxCountReinforceds, }; public XElement GetXElement => new("Shop", new XAttribute("Id", Id), new XElement("Name", Name), new XElement("Address", Address), new XElement("DateOpening", DateOpening), new XElement("MaxCountReinforceds", MaxCountReinforceds), new XElement("CountReinforceds", CountReinforceds .Select(x => new XElement("CountReinforced", new XElement("Key", x.Key), new XElement("Value", x.Value)) )) ); } }