PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantFileImplement/Models/Shop.cs

160 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 Shop : IShopModel
{
/// <summary>
/// Идентификатор
/// </summary>
public int Id { get; private set; }
/// <summary>
/// Название магазина
/// </summary>
public string ShopName { get; private set; } = string.Empty;
/// <summary>
/// Адрес магазина
/// </summary>
public string Address { get; private set; } = string.Empty;
/// <summary>
/// Дата открытия магазина
/// </summary>
public DateTime DateOpening { get; private set; }
/// <summary>
/// Коллекция изделий магазина в виде
/// «идентификатор изделия количество изделий»
/// </summary>
public Dictionary<int, int> Planes { get; private set; } = new();
/// <summary>
/// Коллекция изделий в магазине
/// </summary>
private Dictionary<int, (IPlaneModel, int)>? _shopPlanes = null;
public Dictionary<int, (IPlaneModel, int)> 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;
}
}
/// <summary>
/// Максимальное количество изделий
/// </summary>
public int MaxPlanes { get; private set; }
/// <summary>
/// Создание модели магазина из данных файла
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
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)
};
}
/// <summary>
/// Создание модели магазина
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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
};
}
/// <summary>
/// Изменение модели магазина
/// </summary>
/// <param name="model"></param>
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;
}
/// <summary>
/// Получение модели магазина
/// </summary>
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
ShopPlanes = ShopPlanes,
MaxPlanes = MaxPlanes
};
/// <summary>
/// Запись данных о модели магазина в файл
/// </summary>
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)))),
new XElement("MaxPlanes", MaxPlanes.ToString()
.ToArray()));
}
}