From 76255378d116dc388fea283acc387f1f861ecdff Mon Sep 17 00:00:00 2001 From: the Date: Sat, 22 Apr 2023 19:35:36 +0400 Subject: [PATCH] Lab2hard --- .../Models/IShopModel.cs | 1 + .../BusinessLogics/OrderLogic.cs | 85 +++++++++- .../BusinessLogics/ShopLogic.cs | 53 +++++-- .../BindingModels/ShopBindingModel.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 1 + .../StoragesContracts/IShopStorage.cs | 2 + .../ViewModels/ShopViewModel.cs | 2 + .../DataFileSingleton.cs | 4 + .../Implements/ShopStorage.cs | 148 ++++++++++++++++++ ComputerShopFileImplement/Models/Shop.cs | 122 +++++++++++++++ .../Implements/ShopStorage.cs | 6 + ComputerShopListImplement/Models/Shop.cs | 8 +- ComputersShop/FormMain.Designer.cs | 14 ++ ComputersShop/FormMain.cs | 9 ++ ComputersShop/FormShop.Designer.cs | 31 +++- ComputersShop/FormShop.cs | 9 +- ComputersShop/FormShop.resx | 9 -- ComputersShop/FormShopSell.Designer.cs | 124 +++++++++++++++ ComputersShop/FormShopSell.cs | 96 ++++++++++++ ComputersShop/FormShopSell.resx | 60 +++++++ ComputersShop/Program.cs | 1 + 21 files changed, 754 insertions(+), 32 deletions(-) create mode 100644 ComputerShopFileImplement/Implements/ShopStorage.cs create mode 100644 ComputerShopFileImplement/Models/Shop.cs create mode 100644 ComputersShop/FormShopSell.Designer.cs create mode 100644 ComputersShop/FormShopSell.cs create mode 100644 ComputersShop/FormShopSell.resx diff --git a/AbstractComputerDataModel/Models/IShopModel.cs b/AbstractComputerDataModel/Models/IShopModel.cs index 144ffbc..053bd21 100644 --- a/AbstractComputerDataModel/Models/IShopModel.cs +++ b/AbstractComputerDataModel/Models/IShopModel.cs @@ -12,5 +12,6 @@ namespace ComputerShopDataModels.Models String Adress { get; } DateTime OpeningDate { get; } Dictionary ShopComputers { get; } + int MaxCountComputers { get; } } } diff --git a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs index 28770f5..581247f 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ComputerShopDataModels.Models; namespace ComputerShopBusinessLogic.BusinessLogics { @@ -17,11 +18,17 @@ namespace ComputerShopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; + private readonly IComputerStorage _computerStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IComputerStorage computerStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; + _computerStorage = computerStorage; + _shopStorage = shopStorage; } public bool CreateOrder(OrderBindingModel model) @@ -50,6 +57,21 @@ namespace ComputerShopBusinessLogic.BusinessLogics _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } + if (newStatus == OrderStatus.Выдан) + { + var computer = _computerStorage.GetElement(new ComputerSearchModel() { Id = model.ComputerId }); + if (computer == null) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Computer not found."); + return false; + } + if (CheckThenSupplyMany(computer, model.Count) == false) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); + return false; + } + } + model.Status = newStatus; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; if (_orderStorage.Update(model) == null) @@ -61,6 +83,67 @@ namespace ComputerShopBusinessLogic.BusinessLogics return true; } + public bool CheckThenSupplyMany(IComputerModel computer, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. Computer count < 0."); + return false; + } + + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.MaxCountComputers; + foreach (var doc in shop.ShopComputers) + { + freeSpace -= doc.Value.Item2; + } + } + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new docs in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCountComputers; + foreach (var doc in shop.ShopComputers) + { + freeSpace -= doc.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (_shopLogic.SupplyComputers(new() { Id = shop.Id }, computer, count)) count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (_shopLogic.SupplyComputers(new() { Id = shop.Id }, computer, freeSpace)) count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } + public bool TakeOrderInWork(OrderBindingModel model) { return StatusUpdate(model, OrderStatus.Выполняется); diff --git a/ComputerShopBusinessLogic/BusinessLogics/ShopLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/ShopLogic.cs index 3b418ab..b1f7f48 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; @@ -81,29 +82,47 @@ namespace ComputerShopBusinessLogic.BusinessLogics } _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name); - if (shopElement.ShopComputers.TryGetValue(computer.Id, out var sameComputer)) + var countDocs = 0; + foreach (var doc in shopElement.ShopComputers) { - shopElement.ShopComputers[computer.Id] = (computer, sameComputer.Item2 + count); - _logger.LogInformation("Same computer found by supply. Added {0} of {1} in {2} shop", count, computer.ComputerName, shopElement.Name); + countDocs += doc.Value.Item2; + } + if (shopElement.MaxCountComputers - countDocs >= count) + { + if (shopElement.ShopComputers.TryGetValue(computer.Id, out var sameComputer)) + { + shopElement.ShopComputers[computer.Id] = (computer, sameComputer.Item2 + count); + _logger.LogInformation("Same computer found by supply. Added {0} of {1} in {2} shop", count, computer.ComputerName, shopElement.Name); + } + else + { + shopElement.ShopComputers[computer.Id] = (computer, count); + _logger.LogInformation("New computer added by supply. Added {0} of {1} in {2} shop", count, computer.ComputerName, shopElement.Name); + } + _shopStorage.Update(new() + { + Id = shopElement.Id, + Name = shopElement.Name, + Adress = shopElement.Adress, + OpeningDate = shopElement.OpeningDate, + ShopComputers = shopElement.ShopComputers, + MaxCountComputers = shopElement.MaxCountComputers + }); } else { - shopElement.ShopComputers[computer.Id] = (computer, count); - _logger.LogInformation("New computer added by supply. Added {0} of {1} in {2} shop", count, computer.ComputerName, shopElement.Name); + _logger.LogWarning("Required shop is overflowed"); + return false; } - - _shopStorage.Update(new() - { - Id = shopElement.Id, - Name = shopElement.Name, - Adress = shopElement.Adress, - OpeningDate = shopElement.OpeningDate, - ShopComputers = shopElement.ShopComputers - }); - return true; } + public bool SellComputer(IComputerModel computer, int count) + { + ; + return _shopStorage.SellComputer(computer, count); + } + public bool Create(ShopBindingModel model) { CheckModel(model); @@ -153,6 +172,10 @@ namespace ComputerShopBusinessLogic.BusinessLogics { throw new ArgumentNullException("Нет названия магазина!", nameof(model.Name)); } + if (model.MaxCountComputers < 0) + { + throw new InvalidOperationException("Магазин с отрицательным количеством максимального количества компьютеров!"); + } _logger.LogInformation("Shop. Name: {0}, Adress: {1}, ID: {2}", model.Name, model.Adress, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel { diff --git a/ComputerShopContracts/BindingModels/ShopBindingModel.cs b/ComputerShopContracts/BindingModels/ShopBindingModel.cs index 4a45d68..4f2cae2 100644 --- a/ComputerShopContracts/BindingModels/ShopBindingModel.cs +++ b/ComputerShopContracts/BindingModels/ShopBindingModel.cs @@ -18,5 +18,6 @@ namespace ComputerShopContracts.BindingModels public Dictionary ShopComputers { get; set; } = new(); public int Id { get; set; } + public int MaxCountComputers { get; set; } } } diff --git a/ComputerShopContracts/BusinessLogicsContracts/IShopLogic.cs b/ComputerShopContracts/BusinessLogicsContracts/IShopLogic.cs index 3fa9ce9..1cb4e31 100644 --- a/ComputerShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ComputerShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,5 +18,6 @@ namespace ComputerShopContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool SupplyComputers(ShopSearchModel model, IComputerModel computer, int count); + bool SellComputer(IComputerModel computer, int count); } } diff --git a/ComputerShopContracts/StoragesContracts/IShopStorage.cs b/ComputerShopContracts/StoragesContracts/IShopStorage.cs index 3b784dd..8c91530 100644 --- a/ComputerShopContracts/StoragesContracts/IShopStorage.cs +++ b/ComputerShopContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using ComputerShopContracts.BindingModels; using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -17,5 +18,6 @@ namespace ComputerShopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool SellComputer(IComputerModel model, int count); } } diff --git a/ComputerShopContracts/ViewModels/ShopViewModel.cs b/ComputerShopContracts/ViewModels/ShopViewModel.cs index 16d53b4..c58fc1e 100644 --- a/ComputerShopContracts/ViewModels/ShopViewModel.cs +++ b/ComputerShopContracts/ViewModels/ShopViewModel.cs @@ -20,5 +20,7 @@ namespace ComputerShopContracts.ViewModels public Dictionary ShopComputers { get; set; } = new(); public int Id { get; set; } + [DisplayName("Вместимость")] + public int MaxCountComputers { get; set; } } } diff --git a/ComputerShopFileImplement/DataFileSingleton.cs b/ComputerShopFileImplement/DataFileSingleton.cs index aafad08..adf055f 100644 --- a/ComputerShopFileImplement/DataFileSingleton.cs +++ b/ComputerShopFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace ComputerShopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string ComputerFileName = "Computer.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Computers { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -30,11 +32,13 @@ namespace ComputerShopFileImplement public void SaveComputers() => SaveData(Computers, ComputerFileName, "Computers", 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)!)!; Computers = LoadData(ComputerFileName, "Computer", x => Computer.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/ComputerShopFileImplement/Implements/ShopStorage.cs b/ComputerShopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..dbf71b7 --- /dev/null +++ b/ComputerShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,148 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StoragesContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using ComputerShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + return source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + return source.Shops.Select(shop => shop.GetViewModel).ToList(); + } + + 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 SellComputer(IComputerModel model, int count) + { + var computer = source.Computers.FirstOrDefault(x => x.Id == model.Id); + + if (computer == null) + { + return false; + } + + var countStore = count; + + var shopComputers = source.Shops.SelectMany(shop => shop.ShopComputers.Where(doc => doc.Value.Item1.Id == computer.Id)); + + foreach (var doc in shopComputers) + { + count -= doc.Value.Item2; + + if (count <= 0) + { + break; + } + } + + if (count > 0) + { + return false; + } + + count = countStore; + + foreach (var shop in source.Shops) + { + var computers = shop.ShopComputers; + + foreach (var doc in computers.Where(x => x.Value.Item1.Id == computer.Id)) + { + var min = Math.Min(doc.Value.Item2, count); + computers[doc.Value.Item1.Id] = (doc.Value.Item1, doc.Value.Item2 - min); + count -= min; + + if (count <= 0) + { + break; + } + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + MaxCountComputers = shop.MaxCountComputers, + ShopComputers = computers + }); + + source.SaveShops(); + + if (count <= 0) break; + } + + return count <= 0; + } + } +} diff --git a/ComputerShopFileImplement/Models/Shop.cs b/ComputerShopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..e788f8f --- /dev/null +++ b/ComputerShopFileImplement/Models/Shop.cs @@ -0,0 +1,122 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputerShopFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string Name { get; private set; } = string.Empty; + + public string Adress { get; private set; } = string.Empty; + + public int MaxCountComputers { get; private set; } + + public DateTime OpeningDate { get; private set; } + + public Dictionary Computers { get; private set; } = new(); + + private Dictionary? _shopComputers = null; + + public Dictionary ShopComputers + { + get + { + if (_shopComputers == null) + { + var source = DataFileSingleton.GetInstance(); + _shopComputers = Computers.ToDictionary( + x => x.Key, + y => ((source.Computers.FirstOrDefault(z => z.Id == y.Key) as IComputerModel)!, y.Value) + ); + } + return _shopComputers; + } + } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Adress = model.Adress, + MaxCountComputers = model.MaxCountComputers, + OpeningDate = model.OpeningDate, + Computers = model.ShopComputers.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Shop() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("Name")!.Value, + Adress = element.Element("Address")!.Value, + MaxCountComputers = Convert.ToInt32(element.Element("MaxCountComputers")!.Value), + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + Computers = element.Element("ShopComputers")!.Elements("ShopComputer").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ) + }; + } + + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + MaxCountComputers = model.MaxCountComputers; + if (model.ShopComputers.Count > 0) + { + Computers = model.ShopComputers.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopComputers = null; + } + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Adress = Adress, + OpeningDate = OpeningDate, + ShopComputers = ShopComputers, + MaxCountComputers = MaxCountComputers + }; + + public XElement GetXElement => new( + "Shop", + new XAttribute("Id", Id), + new XElement("Name", Name), + new XElement("Address", Adress), + new XElement("MaxCountComputers", MaxCountComputers), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopComputers", Computers.Select(x => + new XElement("ShopComputer", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} diff --git a/ComputerShopListImplement/Implements/ShopStorage.cs b/ComputerShopListImplement/Implements/ShopStorage.cs index 3094d52..a913633 100644 --- a/ComputerShopListImplement/Implements/ShopStorage.cs +++ b/ComputerShopListImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using ComputerShopContracts.SearchModels; using ComputerShopContracts.StoragesContracts; using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; using ComputerShopListImplement.Models; using System; using System.Collections.Generic; @@ -114,5 +115,10 @@ namespace ComputerShopListImplement.Implements } return null; } + + public bool SellComputer(IComputerModel model, int count) + { + throw new NotImplementedException(); + } } } diff --git a/ComputerShopListImplement/Models/Shop.cs b/ComputerShopListImplement/Models/Shop.cs index 0e1459c..f5e2ae7 100644 --- a/ComputerShopListImplement/Models/Shop.cs +++ b/ComputerShopListImplement/Models/Shop.cs @@ -20,6 +20,7 @@ namespace ComputerShopListImplement.Models public DateTime OpeningDate { get; private set; } public Dictionary ShopComputers { get; private set; } = new(); + public int MaxCountComputers { get; private set; } public static Shop? Create(ShopBindingModel model) { @@ -33,7 +34,8 @@ namespace ComputerShopListImplement.Models Name = model.Name, Adress = model.Adress, OpeningDate = model.OpeningDate, - ShopComputers = new() + ShopComputers = new(), + MaxCountComputers = model.MaxCountComputers }; } @@ -47,6 +49,7 @@ namespace ComputerShopListImplement.Models Adress = model.Adress; OpeningDate = model.OpeningDate; ShopComputers = model.ShopComputers; + MaxCountComputers = model.MaxCountComputers; } public ShopViewModel GetViewModel => new() @@ -55,7 +58,8 @@ namespace ComputerShopListImplement.Models Name = Name, Adress = Adress, OpeningDate = OpeningDate, - ShopComputers = ShopComputers + ShopComputers = ShopComputers, + MaxCountComputers = MaxCountComputers }; } } diff --git a/ComputersShop/FormMain.Designer.cs b/ComputersShop/FormMain.Designer.cs index 906a154..ab18b19 100644 --- a/ComputersShop/FormMain.Designer.cs +++ b/ComputersShop/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.ButtonIssuedOrder = new System.Windows.Forms.Button(); this.ButtonRef = new System.Windows.Forms.Button(); this.buttonSupplyShop = new System.Windows.Forms.Button(); + this.buttonSellComputers = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -155,11 +156,23 @@ this.buttonSupplyShop.UseVisualStyleBackColor = true; this.buttonSupplyShop.Click += new System.EventHandler(this.buttonSupplyShop_Click); // + // buttonSellComputers + // + this.buttonSellComputers.Location = new System.Drawing.Point(786, 181); + this.buttonSellComputers.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSellComputers.Name = "buttonSellComputers"; + this.buttonSellComputers.Size = new System.Drawing.Size(192, 22); + this.buttonSellComputers.TabIndex = 9; + this.buttonSellComputers.Text = "Продажа компьютеров"; + this.buttonSellComputers.UseVisualStyleBackColor = true; + this.buttonSellComputers.Click += new System.EventHandler(this.buttonSellComputers_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1006, 323); + this.Controls.Add(this.buttonSellComputers); this.Controls.Add(this.buttonSupplyShop); this.Controls.Add(this.ButtonRef); this.Controls.Add(this.ButtonIssuedOrder); @@ -194,5 +207,6 @@ private Button ButtonRef; private ToolStripMenuItem магазиныToolStripMenuItem; private Button buttonSupplyShop; + private Button buttonSellComputers; } } \ No newline at end of file diff --git a/ComputersShop/FormMain.cs b/ComputersShop/FormMain.cs index 544442a..e3b0472 100644 --- a/ComputersShop/FormMain.cs +++ b/ComputersShop/FormMain.cs @@ -193,5 +193,14 @@ namespace ComputersShop form.ShowDialog(); } } + + private void buttonSellComputers_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShopSell)); + if (service is FormShopSell form) + { + form.ShowDialog(); + } + } } } diff --git a/ComputersShop/FormShop.Designer.cs b/ComputersShop/FormShop.Designer.cs index d0cbdf4..35c6bb6 100644 --- a/ComputersShop/FormShop.Designer.cs +++ b/ComputersShop/FormShop.Designer.cs @@ -40,12 +40,14 @@ this.textBoxName = new System.Windows.Forms.TextBox(); this.labelAddress = new System.Windows.Forms.Label(); this.labelName = new System.Windows.Forms.Label(); + this.textBoxCapacity = new System.Windows.Forms.TextBox(); + this.labelCapacity = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); // // buttonCancel // - this.buttonCancel.Location = new System.Drawing.Point(200, 339); + this.buttonCancel.Location = new System.Drawing.Point(200, 375); this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(136, 22); @@ -56,7 +58,7 @@ // // buttonSave // - this.buttonSave.Location = new System.Drawing.Point(42, 339); + this.buttonSave.Location = new System.Drawing.Point(42, 375); this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonSave.Name = "buttonSave"; this.buttonSave.Size = new System.Drawing.Size(136, 22); @@ -72,7 +74,7 @@ this.ColumnId, this.ColumnComputerName, this.ColumnCount}); - this.dataGridView.Location = new System.Drawing.Point(12, 107); + this.dataGridView.Location = new System.Drawing.Point(12, 143); this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowHeadersWidth = 51; @@ -153,11 +155,30 @@ this.labelName.TabIndex = 13; this.labelName.Text = "Название:"; // + // textBoxCapacity + // + this.textBoxCapacity.Location = new System.Drawing.Point(117, 91); + this.textBoxCapacity.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxCapacity.Name = "textBoxCapacity"; + this.textBoxCapacity.Size = new System.Drawing.Size(246, 23); + this.textBoxCapacity.TabIndex = 23; + // + // labelCapacity + // + this.labelCapacity.AutoSize = true; + this.labelCapacity.Location = new System.Drawing.Point(11, 93); + this.labelCapacity.Name = "labelCapacity"; + this.labelCapacity.Size = new System.Drawing.Size(83, 15); + this.labelCapacity.TabIndex = 22; + this.labelCapacity.Text = "Вместимость:"; + // // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(383, 368); + this.ClientSize = new System.Drawing.Size(383, 399); + this.Controls.Add(this.textBoxCapacity); + this.Controls.Add(this.labelCapacity); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonSave); this.Controls.Add(this.dataGridView); @@ -190,5 +211,7 @@ private DataGridViewTextBoxColumn ColumnId; private DataGridViewTextBoxColumn ColumnComputerName; private DataGridViewTextBoxColumn ColumnCount; + private TextBox textBoxCapacity; + private Label labelCapacity; } } \ No newline at end of file diff --git a/ComputersShop/FormShop.cs b/ComputersShop/FormShop.cs index 2833572..d5529a9 100644 --- a/ComputersShop/FormShop.cs +++ b/ComputersShop/FormShop.cs @@ -48,6 +48,7 @@ namespace ComputersShop textBoxName.Text = view.Name; textBoxAddress.Text = view.Adress; _shopComputers = view.ShopComputers ?? new Dictionary(); + textBoxCapacity.Text = view.MaxCountComputers.ToString(); LoadData(); } } @@ -100,6 +101,11 @@ namespace ComputersShop MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (string.IsNullOrEmpty(textBoxCapacity.Text)) + { + MessageBox.Show("Заполните макс. количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } _logger.LogInformation("Сохранение магазина"); try { @@ -109,7 +115,8 @@ namespace ComputersShop Name = textBoxName.Text, Adress = textBoxAddress.Text, OpeningDate = dateTimePicker.Value.Date, - ShopComputers = _shopComputers + ShopComputers = _shopComputers, + MaxCountComputers = Convert.ToInt32(textBoxCapacity.Text) }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) diff --git a/ComputersShop/FormShop.resx b/ComputersShop/FormShop.resx index 32b6866..797c173 100644 --- a/ComputersShop/FormShop.resx +++ b/ComputersShop/FormShop.resx @@ -66,13 +66,4 @@ True - - True - - - True - - - True - \ No newline at end of file diff --git a/ComputersShop/FormShopSell.Designer.cs b/ComputersShop/FormShopSell.Designer.cs new file mode 100644 index 0000000..a20c85d --- /dev/null +++ b/ComputersShop/FormShopSell.Designer.cs @@ -0,0 +1,124 @@ +namespace ComputersShop +{ + partial class FormShopSell + { + /// + /// 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.buttonCancel = new System.Windows.Forms.Button(); + this.buttonSell = new System.Windows.Forms.Button(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.comboBoxComputer = new System.Windows.Forms.ComboBox(); + this.labelComputer = new System.Windows.Forms.Label(); + this.labelCount = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(263, 99); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(82, 22); + this.buttonCancel.TabIndex = 13; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(157, 99); + this.buttonSell.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(82, 22); + this.buttonSell.TabIndex = 12; + this.buttonSell.Text = "Продажа"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.buttonSell_Click); + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(101, 48); + this.textBoxCount.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(139, 23); + this.textBoxCount.TabIndex = 11; + // + // comboBoxComputer + // + this.comboBoxComputer.FormattingEnabled = true; + this.comboBoxComputer.Location = new System.Drawing.Point(101, 11); + this.comboBoxComputer.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.comboBoxComputer.Name = "comboBoxComputer"; + this.comboBoxComputer.Size = new System.Drawing.Size(244, 23); + this.comboBoxComputer.TabIndex = 10; + // + // labelComputer + // + this.labelComputer.AutoSize = true; + this.labelComputer.Location = new System.Drawing.Point(13, 11); + this.labelComputer.Name = "labelComputer"; + this.labelComputer.Size = new System.Drawing.Size(74, 15); + this.labelComputer.TabIndex = 9; + this.labelComputer.Text = "Компьютер:"; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(13, 50); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(75, 15); + this.labelCount.TabIndex = 8; + this.labelCount.Text = "Количество:"; + // + // FormShopSell + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(369, 136); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.comboBoxComputer); + this.Controls.Add(this.labelComputer); + this.Controls.Add(this.labelCount); + this.Name = "FormShopSell"; + this.Text = "FormShopSell"; + this.Load += new System.EventHandler(this.FormShopSell_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button buttonCancel; + private Button buttonSell; + private TextBox textBoxCount; + private ComboBox comboBoxComputer; + private Label labelComputer; + private Label labelCount; + } +} \ No newline at end of file diff --git a/ComputersShop/FormShopSell.cs b/ComputersShop/FormShopSell.cs new file mode 100644 index 0000000..3e0477f --- /dev/null +++ b/ComputersShop/FormShopSell.cs @@ -0,0 +1,96 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicsContracts; +using ComputerShopContracts.SearchModels; +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 ComputersShop +{ + public partial class FormShopSell : Form + { + private readonly ILogger _logger; + private readonly IComputerLogic _logicC; + private readonly IShopLogic _logicS; + + public FormShopSell(ILogger logger, IComputerLogic logicC, IShopLogic logicS) + { + InitializeComponent(); + _logger = logger; + _logicC = logicC; + _logicS = logicS; + } + + private void FormShopSell_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка компьютеров для продажи"); + try + { + var list = _logicC.ReadList(null); + if (list != null) + { + comboBoxComputer.DisplayMember = "ComputerName"; + comboBoxComputer.ValueMember = "Id"; + comboBoxComputer.DataSource = list; + comboBoxComputer.SelectedItem = null; + } + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка компьютеров"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonSell_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxComputer.SelectedValue == null) + { + MessageBox.Show("Выберите компьютер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание продажи"); + try + { + var operationResult = _logicS.SellComputer( + new ComputerBindingModel + { + Id = Convert.ToInt32(comboBoxComputer.SelectedValue) + }, + Convert.ToInt32(textBoxCount.Text) + ); + if (!operationResult) + { + throw new Exception("Ошибка при создании продажи. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + 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/ComputersShop/FormShopSell.resx b/ComputersShop/FormShopSell.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/FormShopSell.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/ComputersShop/Program.cs b/ComputersShop/Program.cs index 4de5487..48c64cc 100644 --- a/ComputersShop/Program.cs +++ b/ComputersShop/Program.cs @@ -51,6 +51,7 @@ namespace ComputersShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file