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 { /// /// Сущность "Магазин" /// public class Shop : IShopModel { /// /// Идентификатор /// public int Id { get; private set; } /// /// Название магазина /// public string ShopName { get; private set; } = string.Empty; /// /// Адрес магазина /// public string Address { get; private set; } = string.Empty; /// /// Дата открытия магазина /// public DateTime DateOpening { get; private set; } /// /// Коллекция изделий магазина в виде /// «идентификатор изделия – количество изделий» /// public Dictionary Planes { get; private set; } = new(); /// /// Коллекция изделий в магазине /// private Dictionary? _shopPlanes = null; public Dictionary ShopPlanes { get { if (_shopPlanes == null) { var source = DataFileSingleton.GetInstance(); _shopPlanes = Planes.ToDictionary(x => x.Key, y => ((source.Planes.FirstOrDefault(z => z.Id == y.Key) as IPlaneModel)!, y.Value)); } return _shopPlanes; } } /// /// Максимальное количество изделий /// public int MaxPlanes { get; private set; } /// /// Создание модели магазина из данных файла /// /// /// public static Shop? Create(XElement element) { if (element == null) { return null; } return new() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), ShopName = element.Element("ShopName")!.Value, Address = element.Element("Address")!.Value, DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), Planes = element.Element("ShopPlanes")!.Elements("ShopPlanes")! .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)), MaxPlanes = Convert.ToInt32(element.Element("MaxPlanes")!.Value) }; } /// /// Создание модели магазина /// /// /// public static Shop? Create(ShopBindingModel? model) { if (model == null) { return null; } return new Shop() { Id = model.Id, ShopName = model.ShopName, Address = model.Address, DateOpening = model.DateOpening, Planes = model.ShopPlanes.ToDictionary(x => x.Key, x => x.Value.Item2), MaxPlanes = model.MaxPlanes }; } /// /// Изменение модели магазина /// /// public void Update(ShopBindingModel? model) { if (model == null) { return; } ShopName = model.ShopName; Address = model.Address; DateOpening = model.DateOpening; Planes = model.ShopPlanes.ToDictionary(x => x.Key, x => x.Value.Item2); MaxPlanes = model.MaxPlanes; _shopPlanes = null; } /// /// Получение модели магазина /// public ShopViewModel GetViewModel => new() { Id = Id, ShopName = ShopName, Address = Address, DateOpening = DateOpening, ShopPlanes = ShopPlanes, MaxPlanes = MaxPlanes }; /// /// Запись данных о модели магазина в файл /// public XElement GetXElement => new("Shop", new XAttribute("Id", Id), new XElement("ShopName", ShopName), new XElement("Address", Address), new XElement("DateOpening", DateOpening.ToString()), new XElement("ShopPlanes", Planes.Select(x => new XElement("ShopPlanes", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()), new XElement("MaxPlanes", MaxPlanes.ToString())); } }