From b4c8722cd4e1fec2c0aa749aa2085dc5b4bc79c7 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Tue, 9 Apr 2024 23:44:27 +0400 Subject: [PATCH] Shop File implement --- .../DataFileSingleton.cs | 14 +- .../Implements/ShopStorage.cs | 155 ++++++++++++++++++ AutoWorkshopFileImplement/Models/Shop.cs | 110 +++++++++++++ .../Implements/ShopStorage.cs | 11 ++ AutoWorkshopImplement/Models/Shop.cs | 10 +- 5 files changed, 292 insertions(+), 8 deletions(-) create mode 100644 AutoWorkshopFileImplement/Implements/ShopStorage.cs create mode 100644 AutoWorkshopFileImplement/Models/Shop.cs diff --git a/AutoWorkshopFileImplement/DataFileSingleton.cs b/AutoWorkshopFileImplement/DataFileSingleton.cs index 266f506..18a6f92 100644 --- a/AutoWorkshopFileImplement/DataFileSingleton.cs +++ b/AutoWorkshopFileImplement/DataFileSingleton.cs @@ -10,18 +10,19 @@ namespace AutoWorkshopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string RepairFileName = "Repair.xml"; - - public List Components { get; private set; } - - public List Orders { get; private set; } - + private readonly string ShopFileName = "Shop.xml"; + + public List Components { get; private set; } + public List Orders { get; private set; } public List Repairs { get; private set; } - + public List Shops { get; private set; } + private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Repairs = LoadData(RepairFileName, "Repair", x => Repair.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; } public static DataFileSingleton GetInstance() @@ -37,6 +38,7 @@ namespace AutoWorkshopFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveRepairs() => SaveData(Repairs, RepairFileName, "Repairs", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); private static List? LoadData(string FileName, string XmlNodeName, Func SelectFunction) { diff --git a/AutoWorkshopFileImplement/Implements/ShopStorage.cs b/AutoWorkshopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..25576c9 --- /dev/null +++ b/AutoWorkshopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,155 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopFileImplement.Models; +using PizzeriaContracts.SearchModels; + +namespace AutoWorkshopFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton _source; + + public ShopStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return _source.Shops.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ShopSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ShopName)) + return new(); + + return _source.Shops.Where(x => x.ShopName.Contains(Model.ShopName)).Select(x => x.GetViewModel).ToList(); + } + + public ShopViewModel? GetElement(ShopSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ShopName) && !Model.Id.HasValue) + return null; + + return _source.Shops.FirstOrDefault(x => + (!string.IsNullOrEmpty(Model.ShopName) && x.ShopName == Model.ShopName) || + (Model.Id.HasValue && x.Id == Model.Id))?.GetViewModel; + } + + public ShopViewModel? Insert(ShopBindingModel Model) + { + Model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x => x.Id) + 1 : 1; + + var NewShop = Shop.Create(Model); + if (NewShop == null) + return null; + + _source.Shops.Add(NewShop); + _source.SaveShops(); + + return NewShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel Model) + { + var Shop = _source.Shops.FirstOrDefault(x => x.Id == Model.Id); + if (Shop == null) + return null; + + Shop.Update(Model); + _source.SaveShops(); + + return Shop.GetViewModel; + } + + public ShopViewModel? Delete(ShopBindingModel Model) + { + var Shop = _source.Shops.FirstOrDefault(x => x.Id == Model.Id); + + if (Shop == null) + return null; + + _source.Shops.Remove(Shop); + _source.SaveShops(); + + return Shop.GetViewModel; + } + + public bool Sale(SupplySearchModel Model) + { + if (Model == null || !Model.RepairId.HasValue || !Model.Count.HasValue) + return false; + + int RemainingSpace = _source.Shops.Select(x => x.Repairs.ContainsKey(Model.RepairId.Value) ? x.Repairs[Model.RepairId.Value] : 0).Sum(); + if (RemainingSpace < Model.Count) + return false; + + var Shops = _source.Shops + .Where(x => x.Repairs.ContainsKey(Model.RepairId.Value)) + .OrderByDescending(x => x.Repairs[Model.RepairId.Value]).ToList(); + + foreach (var Shop in Shops) + { + int Residue = Model.Count.Value - Shop.Repairs[Model.RepairId.Value]; + + if (Residue > 0) + { + Shop.Repairs.Remove(Model.RepairId.Value); + Shop.RepairsUpdate(); + + Model.Count = Residue; + } + else + { + if (Residue == 0) + Shop.Repairs.Remove(Model.RepairId.Value); + else + Shop.Repairs[Model.RepairId.Value] = -Residue; + + Shop.RepairsUpdate(); + _source.SaveShops(); + + return true; + } + } + + _source.SaveShops(); + return false; + } + + public bool RestockingShops(SupplyBindingModel Model) + { + if (Model == null || _source.Shops.Select(x => x.RepairsMaxCount - x.ShopRepairs.Select(y => y.Value.Item2).Sum()).Sum() < Model.Count) + return false; + + foreach (Shop shop in _source.Shops) + { + int FreeSpaceNum = shop.RepairsMaxCount - shop.ShopRepairs.Select(x => x.Value.Item2).Sum(); + + if (FreeSpaceNum <= 0) + continue; + + FreeSpaceNum = Math.Min(FreeSpaceNum, Model.Count); + Model.Count -= FreeSpaceNum; + + if (shop.Repairs.ContainsKey(Model.RepairId)) + shop.Repairs[Model.RepairId] += FreeSpaceNum; + else + shop.Repairs.Add(Model.RepairId, FreeSpaceNum); + + shop.RepairsUpdate(); + + if (Model.Count == 0) + { + _source.SaveShops(); + return true; + } + } + + return false; + } + } +} diff --git a/AutoWorkshopFileImplement/Models/Shop.cs b/AutoWorkshopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..ac2e8e0 --- /dev/null +++ b/AutoWorkshopFileImplement/Models/Shop.cs @@ -0,0 +1,110 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Models; +using System.Xml.Linq; + +namespace AutoWorkshopFileImplement.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 OpeningDate { get; private set; } + + public Dictionary Repairs { get; private set; } = new(); + + private Dictionary? _shopRepairs = null; + + public Dictionary ShopRepairs + { + get + { + if (_shopRepairs == null) + { + var Source = DataFileSingleton.GetInstance(); + _shopRepairs = Repairs.ToDictionary(x => x.Key, y => ((Source.Repairs.FirstOrDefault(z => z.Id == y.Key) as IRepairModel)!, y.Value)); + } + + return _shopRepairs; + } + } + + public int RepairsMaxCount { 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, + OpeningDate = Model.OpeningDate, + Repairs = Model.ShopRepairs.ToDictionary(x => x.Key, x => x.Value.Item2), + RepairsMaxCount = Model.RepairsMaxCount + }; + } + + 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, + OpeningDate = Convert.ToDateTime(Element.Element("OpeningDate")!.Value), + Repairs = Element.Element("ShopRepairs")!.Elements("ShopRepair")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)), + RepairsMaxCount = Convert.ToInt32(Element.Element("RepairsMaxCount")!.Value) + }; + } + + public void Update(ShopBindingModel? Model) + { + if (Model == null) + return; + + ShopName = Model.ShopName; + Address = Model.Address; + OpeningDate = Model.OpeningDate; + RepairsMaxCount = Model.RepairsMaxCount; + Repairs = Model.ShopRepairs.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopRepairs = null; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + OpeningDate = OpeningDate, + ShopRepairs = ShopRepairs, + RepairsMaxCount = RepairsMaxCount + }; + + public XElement GetXElement => new( + "Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopRepairs", Repairs.Select( + x => new XElement("ShopRepair", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()), + new XElement("RepairsMaxCount", RepairsMaxCount.ToString()) + ); + + public void RepairsUpdate() + { + _shopRepairs = null; + } + } +} diff --git a/AutoWorkshopImplement/Implements/ShopStorage.cs b/AutoWorkshopImplement/Implements/ShopStorage.cs index 8e6d8e7..75612c2 100644 --- a/AutoWorkshopImplement/Implements/ShopStorage.cs +++ b/AutoWorkshopImplement/Implements/ShopStorage.cs @@ -3,6 +3,7 @@ using AutoWorkshopContracts.SearchModels; using AutoWorkshopContracts.StoragesContracts; using AutoWorkshopContracts.ViewModels; using AutoWorkshopListImplement.Models; +using PizzeriaContracts.SearchModels; namespace AutoWorkshopListImplement.Implements { @@ -108,5 +109,15 @@ namespace AutoWorkshopListImplement.Implements return null; } + + public bool Sale(SupplySearchModel Model) + { + throw new NotImplementedException(); + } + + public bool RestockingShops(SupplyBindingModel Model) + { + throw new NotImplementedException(); + } } } diff --git a/AutoWorkshopImplement/Models/Shop.cs b/AutoWorkshopImplement/Models/Shop.cs index bf6eec9..a01d9b1 100644 --- a/AutoWorkshopImplement/Models/Shop.cs +++ b/AutoWorkshopImplement/Models/Shop.cs @@ -1,6 +1,7 @@ using AutoWorkshopContracts.BindingModels; using AutoWorkshopContracts.ViewModels; using AutoWorkshopDataModels.Models; +using System.Reflection; namespace AutoWorkshopListImplement.Models { @@ -16,6 +17,8 @@ namespace AutoWorkshopListImplement.Models public Dictionary ShopRepairs { get; private set; } = new(); + public int RepairsMaxCount { get; private set; } + public static Shop? Create(ShopBindingModel? Model) { if (Model is null) @@ -26,7 +29,8 @@ namespace AutoWorkshopListImplement.Models Id = Model.Id, ShopName = Model.ShopName, Address = Model.Address, - OpeningDate = Model.OpeningDate + OpeningDate = Model.OpeningDate, + RepairsMaxCount = Model.RepairsMaxCount, }; } @@ -38,6 +42,7 @@ namespace AutoWorkshopListImplement.Models ShopName = Model.ShopName; Address = Model.Address; OpeningDate = Model.OpeningDate; + RepairsMaxCount = Model.RepairsMaxCount; } public ShopViewModel GetViewModel => new() @@ -46,7 +51,8 @@ namespace AutoWorkshopListImplement.Models ShopName = ShopName, Address = Address, OpeningDate = OpeningDate, - ShopRepairs = ShopRepairs + ShopRepairs = ShopRepairs, + RepairsMaxCount = Model.RepairsMaxCount, }; } }