From 30ac4f566a740fb1b622c097f2369d2f1f5f70f5 Mon Sep 17 00:00:00 2001 From: Katanaa Die Date: Wed, 19 Jun 2024 13:23:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bar/Bar.sln | 6 + .../BusinessLogic/OrderLogic.cs | 21 ++- .../BusinessLogic/ShopLogic.cs | 16 ++ .../BindingModels/ShopBindingModel.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 1 + .../SearchModels/SupplySearchModel.cs | 14 ++ .../StoragesContracts/IShopStorage.cs | 2 + Bar/BarContracts/ViewModels/ShopViewModel.cs | 2 + Bar/BarDataModels/Models/IShopModel.cs | 1 + Bar/BarFileImplement/BarFileImplement.csproj | 14 ++ Bar/BarFileImplement/DataFileSingleton.cs | 60 +++++++ .../Implements/CocktailStorage.cs | 75 +++++++++ .../Implements/ComponentStorage.cs | 79 +++++++++ .../Implements/OrderStorage.cs | 88 ++++++++++ .../Implements/ShopStorage.cs | 154 ++++++++++++++++++ Bar/BarFileImplement/Models/Cocktail.cs | 78 +++++++++ Bar/BarFileImplement/Models/Component.cs | 60 +++++++ Bar/BarFileImplement/Models/Order.cs | 88 ++++++++++ Bar/BarFileImplement/Models/Shop.cs | 111 +++++++++++++ .../Implements/ShopStorage.cs | 10 ++ Bar/BarListImplement/Models/Shop.cs | 10 +- Bar/BarView/BarView.csproj | 1 + Bar/BarView/FormSellCocktail.Designer.cs | 120 ++++++++++++++ Bar/BarView/FormSellCocktail.cs | 87 ++++++++++ Bar/BarView/FormSellCocktail.resx | 120 ++++++++++++++ Bar/BarView/Program.cs | 3 +- 26 files changed, 1217 insertions(+), 5 deletions(-) create mode 100644 Bar/BarContracts/SearchModels/SupplySearchModel.cs create mode 100644 Bar/BarFileImplement/BarFileImplement.csproj create mode 100644 Bar/BarFileImplement/DataFileSingleton.cs create mode 100644 Bar/BarFileImplement/Implements/CocktailStorage.cs create mode 100644 Bar/BarFileImplement/Implements/ComponentStorage.cs create mode 100644 Bar/BarFileImplement/Implements/OrderStorage.cs create mode 100644 Bar/BarFileImplement/Implements/ShopStorage.cs create mode 100644 Bar/BarFileImplement/Models/Cocktail.cs create mode 100644 Bar/BarFileImplement/Models/Component.cs create mode 100644 Bar/BarFileImplement/Models/Order.cs create mode 100644 Bar/BarFileImplement/Models/Shop.cs create mode 100644 Bar/BarView/FormSellCocktail.Designer.cs create mode 100644 Bar/BarView/FormSellCocktail.cs create mode 100644 Bar/BarView/FormSellCocktail.resx diff --git a/Bar/Bar.sln b/Bar/Bar.sln index f39173a..302835b 100644 --- a/Bar/Bar.sln +++ b/Bar/Bar.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarView", "BarView\BarView. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarDataModels", "BarDataModels\BarDataModels.csproj", "{DCE9F610-B4A7-4BF8-9A09-517847F0C00B}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarFileImplement", "BarFileImplement\BarFileImplement.csproj", "{DC0F5ACB-CED8-4916-A2F9-56F5DA10C810}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {DCE9F610-B4A7-4BF8-9A09-517847F0C00B}.Debug|Any CPU.Build.0 = Debug|Any CPU {DCE9F610-B4A7-4BF8-9A09-517847F0C00B}.Release|Any CPU.ActiveCfg = Release|Any CPU {DCE9F610-B4A7-4BF8-9A09-517847F0C00B}.Release|Any CPU.Build.0 = Release|Any CPU + {DC0F5ACB-CED8-4916-A2F9-56F5DA10C810}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC0F5ACB-CED8-4916-A2F9-56F5DA10C810}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC0F5ACB-CED8-4916-A2F9-56F5DA10C810}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC0F5ACB-CED8-4916-A2F9-56F5DA10C810}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Bar/BarBusinessLogic/BusinessLogic/OrderLogic.cs b/Bar/BarBusinessLogic/BusinessLogic/OrderLogic.cs index 841eb60..1ffb3b6 100644 --- a/Bar/BarBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/Bar/BarBusinessLogic/BusinessLogic/OrderLogic.cs @@ -17,11 +17,13 @@ namespace BarBusinessLogic.BusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; + _shopStorage = shopStorage; } public List? ReadList(OrderSearchModel? model) { @@ -80,6 +82,23 @@ namespace BarBusinessLogic.BusinessLogic public bool DeliveryOrder(OrderBindingModel model) { + var order = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id, + }); + if (order == null) + { + throw new ArgumentNullException(nameof(order)); + } + if (!_shopStorage.RestockingShops(new SupplyBindingModel + { + CocktailId = order.CocktailId, + Count = order.Count + })) + { + throw new ArgumentException("Недостаточно места"); + } + return ChangeStatus(model, OrderStatus.Выдан); } private void CheckModel(OrderBindingModel model, bool withParams = diff --git a/Bar/BarBusinessLogic/BusinessLogic/ShopLogic.cs b/Bar/BarBusinessLogic/BusinessLogic/ShopLogic.cs index fa9ca9d..6924e72 100644 --- a/Bar/BarBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/Bar/BarBusinessLogic/BusinessLogic/ShopLogic.cs @@ -156,5 +156,21 @@ namespace BarBusinessLogic.BusinessLogic throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + + public bool Sale(SupplySearchModel model) + { + if (!model.CocktailId.HasValue || !model.Count.HasValue) + { + return false; + } + _logger.LogInformation("Check cocktail count in all shops"); + if (_shopStorage.Sale(model)) + { + _logger.LogInformation("Selling sucsess"); + return true; + } + _logger.LogInformation("Selling failed"); + return false; + } } } diff --git a/Bar/BarContracts/BindingModels/ShopBindingModel.cs b/Bar/BarContracts/BindingModels/ShopBindingModel.cs index e57d274..d2a2bb9 100644 --- a/Bar/BarContracts/BindingModels/ShopBindingModel.cs +++ b/Bar/BarContracts/BindingModels/ShopBindingModel.cs @@ -14,5 +14,6 @@ namespace BarContracts.BindingModels public string Adress { get; set; } = string.Empty; public DateTime OpeningDate { get; set; } = DateTime.Now; public Dictionary ShopCocktails { get; set; } = new(); + public int CocktailMaxCount { get; set; } } } diff --git a/Bar/BarContracts/BusinessLogicsContracts/IShopLogic.cs b/Bar/BarContracts/BusinessLogicsContracts/IShopLogic.cs index 2aefec4..0f3b858 100644 --- a/Bar/BarContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/Bar/BarContracts/BusinessLogicsContracts/IShopLogic.cs @@ -17,5 +17,6 @@ namespace BarContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool MakeSupply(SupplyBindingModel model); + bool Sale(SupplySearchModel model); } } diff --git a/Bar/BarContracts/SearchModels/SupplySearchModel.cs b/Bar/BarContracts/SearchModels/SupplySearchModel.cs new file mode 100644 index 0000000..a4c5a5c --- /dev/null +++ b/Bar/BarContracts/SearchModels/SupplySearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BarContracts.SearchModels +{ + public class SupplySearchModel + { + public int? CocktailId { get; set; } + public int? Count { get; set; } + } +} diff --git a/Bar/BarContracts/StoragesContracts/IShopStorage.cs b/Bar/BarContracts/StoragesContracts/IShopStorage.cs index 46d0d58..80f0163 100644 --- a/Bar/BarContracts/StoragesContracts/IShopStorage.cs +++ b/Bar/BarContracts/StoragesContracts/IShopStorage.cs @@ -17,5 +17,7 @@ namespace BarContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool Sale(SupplySearchModel model); + bool RestockingShops(SupplyBindingModel model); } } diff --git a/Bar/BarContracts/ViewModels/ShopViewModel.cs b/Bar/BarContracts/ViewModels/ShopViewModel.cs index 04574c2..03c2bd9 100644 --- a/Bar/BarContracts/ViewModels/ShopViewModel.cs +++ b/Bar/BarContracts/ViewModels/ShopViewModel.cs @@ -18,5 +18,7 @@ namespace BarContracts.ViewModels [DisplayName("Дата открытия")] public DateTime OpeningDate { get; set; } public Dictionary ShopCocktails { get; set; } = new(); + [DisplayName("Вместимость")] + public int CocktailMaxCount { get; set; } } } diff --git a/Bar/BarDataModels/Models/IShopModel.cs b/Bar/BarDataModels/Models/IShopModel.cs index 52dad99..ed710fa 100644 --- a/Bar/BarDataModels/Models/IShopModel.cs +++ b/Bar/BarDataModels/Models/IShopModel.cs @@ -12,5 +12,6 @@ namespace BarDataModels.Models string Adress { get; } DateTime OpeningDate { get; } Dictionary ShopCocktails { get; } + public int CocktailMaxCount { get; } } } diff --git a/Bar/BarFileImplement/BarFileImplement.csproj b/Bar/BarFileImplement/BarFileImplement.csproj new file mode 100644 index 0000000..c55e9fc --- /dev/null +++ b/Bar/BarFileImplement/BarFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0-windows + enable + enable + + + + + + + + diff --git a/Bar/BarFileImplement/DataFileSingleton.cs b/Bar/BarFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..c1d0c69 --- /dev/null +++ b/Bar/BarFileImplement/DataFileSingleton.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Controls; +using BarFileImplement.Models; +using System.Xml.Linq; + + +namespace BarContracts.BindingModels +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string CocktailFileName = "Cocktail.xml"; + private readonly string ShopFileName = "Shop.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Cocktails { get; private set; } + public List Shops { get; private set; } + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + + } + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveCocktails() => SaveData(Cocktails, CocktailFileName, "Cocktails", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Cocktails = LoadData(CocktailFileName, "Cocktail", x => Cocktail.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; + } + } +} diff --git a/Bar/BarFileImplement/Implements/CocktailStorage.cs b/Bar/BarFileImplement/Implements/CocktailStorage.cs new file mode 100644 index 0000000..74cf5c0 --- /dev/null +++ b/Bar/BarFileImplement/Implements/CocktailStorage.cs @@ -0,0 +1,75 @@ +using BarContracts.BindingModels; +using BarContracts.SearchModels; +using BarContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BarContracts.StoragesContracts; +using BarFileImplement.Models; + + +namespace BarFileImplement.Implements +{ + public class CocktailStorage : ICocktailStorage + { + private readonly DataFileSingleton source; + public CocktailStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Cocktails.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(CocktailSearchModel model) + { + return source.Cocktails.Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public CocktailViewModel? GetElement(CocktailSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Cocktails.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CocktailName) && x.CocktailName == model.CocktailName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public CocktailViewModel? Insert(CocktailBindingModel model) + { + model.Id = source.Cocktails.Count > 0 ? source.Cocktails.Max(x => x.Id) + 1 : 1; + var newCocktail = Cocktail.Create(model); + if (newCocktail == null) + { + return null; + } + source.Cocktails.Add(newCocktail); + source.SaveCocktails(); + return newCocktail.GetViewModel; + } + public CocktailViewModel? Update(CocktailBindingModel model) + { + var component = source.Cocktails.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveCocktails(); + return component.GetViewModel; + } + public CocktailViewModel? Delete(CocktailBindingModel model) + { + var component = source.Cocktails.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + source.Cocktails.Remove(component); + source.SaveCocktails(); + return component.GetViewModel; + } + } +} diff --git a/Bar/BarFileImplement/Implements/ComponentStorage.cs b/Bar/BarFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..e2d0aff --- /dev/null +++ b/Bar/BarFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,79 @@ +using BarContracts.BindingModels; +using BarContracts.SearchModels; +using BarContracts.StoragesContracts; +using BarContracts.ViewModels; +using BarFileImplement.Models; +using System.Collections.Generic; +using System; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace BarFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + source.Components.Remove(component); + source.SaveComponents(); + return component.GetViewModel; + } + } +} \ No newline at end of file diff --git a/Bar/BarFileImplement/Implements/OrderStorage.cs b/Bar/BarFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..48c308d --- /dev/null +++ b/Bar/BarFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using BarContracts.BindingModels; +using BarContracts.SearchModels; +using BarContracts.ViewModels; +using BarContracts.StoragesContracts; +using BarFileImplement.Models; +using System.Text; +using System.Threading.Tasks; + + +namespace BarFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders.Select(x => AcessCocktailsStorage(x.GetViewModel)).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + return source.Orders.Where(x => x.Id == model.Id) + .Select(x => AcessCocktailsStorage(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + var component = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveOrders(); + return component.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var component = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + source.Orders.Remove(component); + source.SaveOrders(); + return component.GetViewModel; + } + public OrderViewModel AcessCocktailsStorage(OrderViewModel model) + { + if (model == null) + { + return null; + } + var component = source.Cocktails.FirstOrDefault(x => x.Id == model.CocktailId); + if (component != null) + { + model.CocktailName = component.CocktailName; + } + return model; + } + } +} diff --git a/Bar/BarFileImplement/Implements/ShopStorage.cs b/Bar/BarFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..6854bd2 --- /dev/null +++ b/Bar/BarFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,154 @@ +using BarContracts.BindingModels; +using BarContracts.SearchModels; +using BarContracts.StoragesContracts; +using BarContracts.ViewModels; +using BarFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BarFileImplement.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) + { + source.Shops.Remove(shop); + source.SaveShops(); + return shop.GetViewModel; + } + return null; + } + + public bool Sale(SupplySearchModel model) + { + if (model == null || !model.CocktailId.HasValue || !model.Count.HasValue) + return false; + int remainingSpace = source.Shops.Select(x => x.Cocktails.ContainsKey(model.CocktailId.Value) ? x.Cocktails[model.CocktailId.Value] : 0).Sum(); + if (remainingSpace < model.Count) + { + return false; + } + var shops = source.Shops.Where(x => x.Cocktails.ContainsKey(model.CocktailId.Value)).OrderByDescending(x => x.Cocktails[model.CocktailId.Value]).ToList(); + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.Cocktails[model.CocktailId.Value]; + if (residue > 0) + { + shop.Cocktails.Remove(model.CocktailId.Value); + shop.CocktailsUpdate(); + model.Count = residue; + } + else + { + if (residue == 0) + { + shop.Cocktails.Remove(model.CocktailId.Value); + } + else + { + shop.Cocktails[model.CocktailId.Value] = -residue; + } + shop.CocktailsUpdate(); + source.SaveShops(); + return true; + } + } + source.SaveShops(); + return false; + } + + public bool RestockingShops(SupplyBindingModel model) + { + if (model == null || source.Shops.Select(x => x.CocktailMaxCount - x.ShopCocktails.Select(y => y.Value.Item2).Sum()).Sum() < model.Count) + { + return false; + } + foreach (Shop shop in source.Shops) + { + int free_places = shop.CocktailMaxCount - shop.ShopCocktails.Select(x => x.Value.Item2).Sum(); + if (free_places <= 0) + continue; + free_places = Math.Min(free_places, model.Count); + model.Count -= free_places; + if (shop.Cocktails.ContainsKey(model.CocktailId)) + { + shop.Cocktails[model.CocktailId] += free_places; + } + else + { + shop.Cocktails.Add(model.CocktailId, free_places); + } + shop.CocktailsUpdate(); + if (model.Count == 0) + { + source.SaveShops(); + return true; + } + } + return false; + } + } +} diff --git a/Bar/BarFileImplement/Models/Cocktail.cs b/Bar/BarFileImplement/Models/Cocktail.cs new file mode 100644 index 0000000..6eb7318 --- /dev/null +++ b/Bar/BarFileImplement/Models/Cocktail.cs @@ -0,0 +1,78 @@ +using BarContracts.BindingModels; +using BarContracts.ViewModels; +using BarDataModels.Models; +using System.Collections.Generic; +using System.Xml.Linq; + +namespace BarFileImplement.Models +{ + public class Cocktail : ICocktailModel + { + public int Id { get; private set; } + public string CocktailName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _cocktailComponents = null; + public Dictionary CocktailComponents + { + get + { + if (_cocktailComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _cocktailComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _cocktailComponents; + } + } + public static Cocktail? Create(CocktailBindingModel model) + { + if (model == null) + { + return null; + } + return new Cocktail() + { + Id = model.Id, + CocktailName = model.CocktailName, + Price = model.Price, + Components = model.CocktailComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Cocktail? Create(XElement element) + { + if (element == null) + { return null; } + return new Cocktail() + { + Id = Convert.ToInt32(element.Attribute("Id")?.Value), + CocktailName = element.Element("CocktailName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value, new System.Globalization.CultureInfo("en-US")), + Components = element.Element("CocktailComponents")!.Elements("CocktailComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(CocktailBindingModel model) + { + if (model == null) + { + return; + } + CocktailName = model.CocktailName; + Price = model.Price; + Components = model.CocktailComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _cocktailComponents = null; + } + public CocktailViewModel GetViewModel => new() + { + Id = Id, + CocktailName = CocktailName, + Price = Price, + CocktailComponents = CocktailComponents + }; + public XElement GetXElement => new("Cocktail", + new XAttribute("Id", Id), + new XElement("CocktailName", CocktailName), + new XElement("Price", Price), + new XElement("CocktailComponents", Components.Select(x => new XElement("CocktailComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/Bar/BarFileImplement/Models/Component.cs b/Bar/BarFileImplement/Models/Component.cs new file mode 100644 index 0000000..d9563fd --- /dev/null +++ b/Bar/BarFileImplement/Models/Component.cs @@ -0,0 +1,60 @@ +using BarContracts.BindingModels; +using BarContracts.ViewModels; +using BarDataModels.Models; +using System; +using System.Collections.Generic; +using System.Xml.Linq; + +namespace BarFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; private set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value, new System.Globalization.CultureInfo("en-US")) + }; + } + + public void Update(ComponentBindingModel model) + { + if (model == null) + { return; } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); + } +} diff --git a/Bar/BarFileImplement/Models/Order.cs b/Bar/BarFileImplement/Models/Order.cs new file mode 100644 index 0000000..83f2df0 --- /dev/null +++ b/Bar/BarFileImplement/Models/Order.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BarContracts.BindingModels; +using BarContracts.ViewModels; +using BarDataModels.Models; +using BarDataModels.Enums; +using System.Xml.Linq; + + +namespace BarFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int CocktailId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + CocktailId = model.CocktailId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + var dateImplement = element.Element("DateImplement")?.Value; + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + CocktailId = Convert.ToInt32(element.Element("CocktailId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value, new System.Globalization.CultureInfo("en-US")), + Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")?.Value), + DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement) + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + CocktailId = CocktailId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("CocktailId", CocktailId), + new XElement("Count", Count), + new XElement("Sum", Sum), + new XElement("Status", Status - OrderStatus.Принят), + new XElement("DateCreate", DateCreate), + new XElement("DateImplement", DateImplement)); + } +} + diff --git a/Bar/BarFileImplement/Models/Shop.cs b/Bar/BarFileImplement/Models/Shop.cs new file mode 100644 index 0000000..3895e4f --- /dev/null +++ b/Bar/BarFileImplement/Models/Shop.cs @@ -0,0 +1,111 @@ +using BarContracts.BindingModels; +using BarContracts.ViewModels; +using BarDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace BarFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string Adress { get; private set; } = string.Empty; + public DateTime OpeningDate { get; private set; } + public Dictionary Cocktails { get; private set; } = new(); + private Dictionary? _shopCocktails = null; + + public Dictionary ShopCocktails + { + get + { + if (_shopCocktails == null) + { + var source = DataFileSingleton.GetInstance(); + _shopCocktails = Cocktails.ToDictionary(x => x.Key, y => ((source.Cocktails.FirstOrDefault(z => z.Id == y.Key) as ICocktailModel)!, y.Value)); + } + return _shopCocktails; + } + } + + public int CocktailMaxCount { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + Cocktails = model.ShopCocktails.ToDictionary(x => x.Key, x => x.Value.Item2), + CocktailMaxCount = model.CocktailMaxCount + }; + } + + 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, + Adress = element.Element("Adress")!.Value, + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + Cocktails = element.Element("ShopCocktails")!.Elements("ShopCocktail")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)), + CocktailMaxCount = Convert.ToInt32(element.Element("CocktailMaxCount")!.Value) + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + CocktailMaxCount = model.CocktailMaxCount; + Cocktails = model.ShopCocktails.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopCocktails = null; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopCocktails = ShopCocktails, + CocktailMaxCount = CocktailMaxCount + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Adress", Adress), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopCocktails", Cocktails.Select( + x => new XElement("ShopCocktail", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()), + new XElement("CocktailMaxCount", CocktailMaxCount.ToString()) + ); + + public void CocktailsUpdate() + { + _shopCocktails = null; + } + } +} diff --git a/Bar/BarListImplement/Implements/ShopStorage.cs b/Bar/BarListImplement/Implements/ShopStorage.cs index de54f32..85cd77b 100644 --- a/Bar/BarListImplement/Implements/ShopStorage.cs +++ b/Bar/BarListImplement/Implements/ShopStorage.cs @@ -109,5 +109,15 @@ namespace BarListImplement.Implements } return null; } + + public bool Sale(SupplySearchModel model) + { + throw new NotImplementedException(); + } + + public bool RestockingShops(SupplyBindingModel model) + { + throw new NotImplementedException(); + } } } diff --git a/Bar/BarListImplement/Models/Shop.cs b/Bar/BarListImplement/Models/Shop.cs index 951bb04..aedb209 100644 --- a/Bar/BarListImplement/Models/Shop.cs +++ b/Bar/BarListImplement/Models/Shop.cs @@ -16,6 +16,7 @@ namespace BarListImplement.Models public string Adress { get; private set; } = string.Empty; public DateTime OpeningDate { get; private set; } public Dictionary ShopCocktails { get; private set; } = new(); + public int CocktailMaxCount { get; private set; } public static Shop? Create(ShopBindingModel? model) { @@ -28,8 +29,9 @@ namespace BarListImplement.Models Id = model.Id, ShopName = model.ShopName, Adress = model.Adress, - OpeningDate = model.OpeningDate - }; + OpeningDate = model.OpeningDate, + CocktailMaxCount = model.CocktailMaxCount + }; } public void Update(ShopBindingModel? model) @@ -41,6 +43,7 @@ namespace BarListImplement.Models ShopName = model.ShopName; Adress = model.Adress; OpeningDate = model.OpeningDate; + CocktailMaxCount = model.CocktailMaxCount; } public ShopViewModel GetViewModel => new() @@ -49,7 +52,8 @@ namespace BarListImplement.Models ShopName = ShopName, Adress = Adress, OpeningDate = OpeningDate, - ShopCocktails = ShopCocktails + ShopCocktails = ShopCocktails, + CocktailMaxCount = CocktailMaxCount, }; } } diff --git a/Bar/BarView/BarView.csproj b/Bar/BarView/BarView.csproj index cc8f5b6..2831877 100644 --- a/Bar/BarView/BarView.csproj +++ b/Bar/BarView/BarView.csproj @@ -21,6 +21,7 @@ + diff --git a/Bar/BarView/FormSellCocktail.Designer.cs b/Bar/BarView/FormSellCocktail.Designer.cs new file mode 100644 index 0000000..50ccf3e --- /dev/null +++ b/Bar/BarView/FormSellCocktail.Designer.cs @@ -0,0 +1,120 @@ +namespace BarView +{ + partial class FormSellCocktail + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.labelCocktail = new System.Windows.Forms.Label(); + this.comboBoxCocktail = new System.Windows.Forms.ComboBox(); + this.labelCount = new System.Windows.Forms.Label(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // labelCocktail + // + this.labelCocktail.AutoSize = true; + this.labelCocktail.Location = new System.Drawing.Point(12, 14); + this.labelCocktail.Name = "labelCocktail"; + this.labelCocktail.Size = new System.Drawing.Size(75, 20); + this.labelCocktail.TabIndex = 0; + this.labelCocktail.Text = "Коктейль: "; + // + // comboBoxCocktail + // + this.comboBoxCocktail.FormattingEnabled = true; + this.comboBoxCocktail.Location = new System.Drawing.Point(115, 11); + this.comboBoxCocktail.Name = "comboBoxCocktail"; + this.comboBoxCocktail.Size = new System.Drawing.Size(239, 28); + this.comboBoxCocktail.TabIndex = 1; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(12, 55); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(97, 20); + this.labelCount.TabIndex = 2; + this.labelCount.Text = "Количество: "; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(115, 52); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(239, 27); + this.textBoxCount.TabIndex = 3; + // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(128, 99); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(94, 29); + this.buttonSell.TabIndex = 4; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(242, 99); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormSellCocktail + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(366, 140); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.comboBoxCocktail); + this.Controls.Add(this.labelCocktail); + this.Name = "FormSellCocktail"; + this.Text = "Продажа коктейлей"; + this.Load += new System.EventHandler(this.FormSellingCocktail_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelCocktail; + private ComboBox comboBoxCocktail; + private Label labelCount; + private TextBox textBoxCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Bar/BarView/FormSellCocktail.cs b/Bar/BarView/FormSellCocktail.cs new file mode 100644 index 0000000..c10f4ac --- /dev/null +++ b/Bar/BarView/FormSellCocktail.cs @@ -0,0 +1,87 @@ +using BarContracts.BusinessLogicsContracts; +using BarContracts.SearchModels; +using BarContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace BarView +{ + public partial class FormSellCocktail : Form + { + private readonly ILogger _logger; + private readonly ICocktailLogic _logicP; + private readonly IShopLogic _logicS; + private List _cocktailList = new List(); + + public FormSellCocktail(ILogger logger, ICocktailLogic logicP, IShopLogic logicS) + { + InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicS = logicS; + } + + private void FormSellingCocktail_Load(object sender, EventArgs e) + { + _cocktailList = _logicP.ReadList(null); + if (_cocktailList != null) + { + comboBoxCocktail.DisplayMember = "CocktailName"; + comboBoxCocktail.ValueMember = "Id"; + comboBoxCocktail.DataSource = _cocktailList; + comboBoxCocktail.SelectedItem = null; + _logger.LogInformation("Загрузка коктейлей для продажи"); + } + } + + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxCocktail.SelectedValue == null) + { + MessageBox.Show("Выберите коктейль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание покупки"); + try + { + bool resout = _logicS.Sale(new SupplySearchModel + { + CocktailId = Convert.ToInt32(comboBoxCocktail.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text) + }); + if (resout) + { + _logger.LogInformation("Проверка пройдена, продажа проведена"); + MessageBox.Show("Продажа проведена", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + else + { + _logger.LogInformation("Проверка не пройдена"); + MessageBox.Show("Продажа не может быть создана.", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания покупки"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/Bar/BarView/FormSellCocktail.resx b/Bar/BarView/FormSellCocktail.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Bar/BarView/FormSellCocktail.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Bar/BarView/Program.cs b/Bar/BarView/Program.cs index a7a50d4..1ddceda 100644 --- a/Bar/BarView/Program.cs +++ b/Bar/BarView/Program.cs @@ -1,7 +1,7 @@ using BarContracts.BusinessLogicsContracts; using BarContracts.StoragesContracts; using Microsoft.Extensions.DependencyInjection; -using BarListImplement.Implements; +using BarFileImplement.Implements; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using BarBusinessLogic.BusinessLogic; @@ -54,6 +54,7 @@ namespace BarView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file