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

114 lines
4.1 KiB
C#
Raw Normal View History

2024-05-16 21:48:52 +04:00
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace PrecastConcretePlantFileImplement.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<int, int> Reinforceds { get; private set; } = new();
private Dictionary<int, (IReinforcedModel, int)>? _shopReinforceds = null;
public Dictionary<int, (IReinforcedModel, int)> ShopReinforceds
{
get
{
if (_shopReinforceds == null)
{
var source = DataFileSingleton.GetInstance();
_shopReinforceds = Reinforceds.ToDictionary(x => x.Key,
y => ((source.Reinforceds.FirstOrDefault(z => z.Id == y.Key) as IReinforcedModel)!, y.Value));
}
return _shopReinforceds;
}
}
public int ReinforcedsMax { get; private set; }
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,
Reinforceds = model.ShopReinforceds.ToDictionary(x => x.Key, x => x.Value.Item2),
ReinforcedsMax = model.ReinforcedsMax
};
}
public static Shop? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Shop()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ShopName = element.Element("ShopName")!.Value,
Address = element.Element("Address")!.Value,
DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
ReinforcedsMax = Convert.ToInt32(element.Element("ReinforcedsMax")!.Value),
Reinforceds = element.Element("ShopReinforceds")!.Elements("ShopReinforced")
.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
ReinforcedsMax = model.ReinforcedsMax;
Reinforceds = model.ShopReinforceds.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopReinforceds = null;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
ShopReinforceds = ShopReinforceds,
ReinforcedsMax = ReinforcedsMax
};
public XElement GetXElement => new("Shop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("Address", Address),
new XElement("DateOpening", DateOpening.ToString()),
new XElement("ReinforcedsMax", ReinforcedsMax.ToString()),
new XElement("ShopReinforceds",
Reinforceds.Select(x => new XElement("ShopReinforced",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}