From 5a735624947b0fa4dba5623327e2c89e2afecff7 Mon Sep 17 00:00:00 2001 From: Dasha Date: Thu, 20 Jun 2024 01:11:30 +0400 Subject: [PATCH] done hard --- .../BusinessLogics/OrderLogic.cs | 41 ++++-- .../BusinessLogics/ShopLogic.cs | 76 ++++++++++- .../BindingModels/ShopBindingModel.cs | 2 + .../BusinessLogicsContracts/IShopLogic.cs | 2 + .../StoragesContracts/IShopStorage.cs | 2 + .../ViewModels/ShopViewModel.cs | 3 + .../Models/IShopModel.cs | 1 + .../DataFileSingleton.cs | 100 +++++++------- .../Implements/ShopStorage.cs | 102 ++++++++++++++ .../FishFactoryFileImplement/Models/Shop.cs | 105 +++++++++++++++ .../Implements/ShopStorage.cs | 6 + .../FishFactoryListImplement/Models/Shop.cs | 2 + .../FishFactoryView/FormMain.Designer.cs | 50 ++++--- FishFactory/FishFactoryView/FormMain.cs | 39 +++--- .../FormSellCanned.Designer.cs | 126 ++++++++++++++++++ FishFactory/FishFactoryView/FormSellCanned.cs | 81 +++++++++++ .../FishFactoryView/FormSellCanned.resx | 120 +++++++++++++++++ .../FishFactoryView/FormShop.Designer.cs | 32 ++++- FishFactory/FishFactoryView/FormShop.cs | 2 + FishFactory/FishFactoryView/Program.cs | 1 + 20 files changed, 785 insertions(+), 108 deletions(-) create mode 100644 FishFactory/FishFactoryFileImplement/Implements/ShopStorage.cs create mode 100644 FishFactory/FishFactoryFileImplement/Models/Shop.cs create mode 100644 FishFactory/FishFactoryView/FormSellCanned.Designer.cs create mode 100644 FishFactory/FishFactoryView/FormSellCanned.cs create mode 100644 FishFactory/FishFactoryView/FormSellCanned.resx diff --git a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs index 211f4db..e271c7e 100644 --- a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs @@ -12,12 +12,16 @@ namespace FishFactoryBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopLogic _shopLogic; + private readonly ICannedStorage _cannedStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) - { + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, ICannedStorage cannedStorage) + { _logger = logger; _orderStorage = orderStorage; - } + _shopLogic = shopLogic; + _cannedStorage = cannedStorage; + } public bool CreateOrder(OrderBindingModel model) { @@ -39,15 +43,36 @@ namespace FishFactoryBusinessLogic.BusinessLogics public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { - CheckModel(model, false); - if (model.Status + 1 != newStatus) - { + var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (viewModel == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (viewModel.Status + 1 != newStatus) + { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } model.Status = newStatus; - if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) + if (model.Status == OrderStatus.Выдан) + { + model.DateImplement = DateTime.Now; + var canned = _cannedStorage.GetElement(new() { Id = viewModel.CannedId }); + if (canned == null) + { + throw new ArgumentNullException(nameof(canned)); + } + if (!_shopLogic.AddCanned(canned, viewModel.Count)) + { + throw new Exception($"AddCanned operation failed. Shop is full."); + } + } + else + { + model.DateImplement = viewModel.DateImplement; + } + CheckModel(model, false); + if (_orderStorage.Update(model) == null) { model.Status--; _logger.LogWarning("Update operation failed"); diff --git a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/ShopLogic.cs b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/ShopLogic.cs index 35a0675..3e22595 100644 --- a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/ShopLogic.cs @@ -93,6 +93,12 @@ namespace CannedBusinessLogic { throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); } + if (model.MaxCountCanned < 0) + { + throw new ArgumentException( + "Максимальное количество консервы в магазине не может быть меньше нуля", + nameof(model.MaxCountCanned)); + } _logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", model.ShopName, model.Address, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel @@ -105,7 +111,7 @@ namespace CannedBusinessLogic } } - public bool AddCannedInShop(ShopSearchModel model, ICannedModel Canned, int count) + public bool AddCannedInShop(ShopSearchModel model, ICannedModel canned, int count) { if (model == null) { @@ -123,21 +129,26 @@ namespace CannedBusinessLogic _logger.LogWarning("AddCannedInShop element not found"); return false; } + if (element.MaxCountCanned - + element.ListCanned.Select(x => x.Value.Item2).Sum() < count) + { + throw new ArgumentNullException("Магазин переполнен", nameof(count)); + } _logger.LogInformation("AddCannedInShop find. Id:{Id}", element.Id); - if (element.ListCanned.TryGetValue(Canned.Id, out var pair)) + if (element.ListCanned.TryGetValue(canned.Id, out var pair)) { - element.ListCanned[Canned.Id] = (Canned, count + pair.Item2); + element.ListCanned[canned.Id] = (canned, count + pair.Item2); _logger.LogInformation( "AddCannedInShop. Added {count} {Canned} to '{ShopName}' shop", - count, Canned.CannedName, element.ShopName); + count, canned.CannedName, element.ShopName); } else { - element.ListCanned[Canned.Id] = (Canned, count); + element.ListCanned[canned.Id] = (canned, count); _logger.LogInformation( "AddCannedInShop. Added {count} new Canned {Canned} to '{ShopName}' shop", - count, Canned.CannedName, element.ShopName); + count, canned.CannedName, element.ShopName); } _shopStorage.Update(new() { @@ -145,9 +156,62 @@ namespace CannedBusinessLogic Address = element.Address, ShopName = element.ShopName, DateOpening = element.DateOpening, + MaxCountCanned = element.MaxCountCanned, ListCanned = element.ListCanned }); return true; } + + public bool AddCanned(ICannedModel model, int count) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (count <= 0) + { + throw new ArgumentException("Количество консерв должно быть больше 0", nameof(count)); + } + var freeCount = _shopStorage.GetFullList() + .Select(x => x.MaxCountCanned - x.ListCanned + .Select(p => p.Value.Item2).Sum()).Sum() - count; + if (freeCount < 0) + { + _logger.LogInformation("AddCanned. Не удалось добавить изделия в магазины, они переполнены."); + return false; + } + foreach (var shop in _shopStorage.GetFullList()) + { + int countFree = shop.MaxCountCanned - shop.ListCanned.Select(x => x.Value.Item2).Sum(); + if (countFree < count) + { + if (!AddCannedInShop(new() { Id = shop.Id }, model, countFree)) + { + _logger.LogWarning("AddCannedInShop operation failed."); + return false; + } + count -= countFree; + } + else + { + if (!AddCannedInShop(new() { Id = shop.Id }, model, count)) + { + _logger.LogWarning("AddCannedInShop operation failed."); + return false; + } + count = 0; + } + if (count == 0) + { + return true; + } + } + return false; + } + + public bool SellCanned(ICannedModel model, int count) + { + return _shopStorage.SellCanned(model, count); + } } } \ No newline at end of file diff --git a/FishFactory/FishFactoryContracts/BindingModels/ShopBindingModel.cs b/FishFactory/FishFactoryContracts/BindingModels/ShopBindingModel.cs index d5bddef..79360dd 100644 --- a/FishFactory/FishFactoryContracts/BindingModels/ShopBindingModel.cs +++ b/FishFactory/FishFactoryContracts/BindingModels/ShopBindingModel.cs @@ -16,5 +16,7 @@ namespace FishFactoryContracts.BindingModels get; set; } = new(); + + public int MaxCountCanned { get; set; } } } \ No newline at end of file diff --git a/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IShopLogic.cs b/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IShopLogic.cs index c00089c..5d56dd3 100644 --- a/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -13,5 +13,7 @@ namespace FishFactoryContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddCannedInShop(ShopSearchModel model, ICannedModel canned, int count); + bool AddCanned(ICannedModel canned, int count); + bool SellCanned(ICannedModel canned, int count); } } \ No newline at end of file diff --git a/FishFactory/FishFactoryContracts/StoragesContracts/IShopStorage.cs b/FishFactory/FishFactoryContracts/StoragesContracts/IShopStorage.cs index e874627..b23b307 100644 --- a/FishFactory/FishFactoryContracts/StoragesContracts/IShopStorage.cs +++ b/FishFactory/FishFactoryContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using FishFactoryContracts.BindingModels; using FishFactoryContracts.SearchModels; using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; namespace FishFactoryContracts.StoragesContracts { @@ -12,5 +13,6 @@ namespace FishFactoryContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool SellCanned(ICannedModel model, int count); } } \ No newline at end of file diff --git a/FishFactory/FishFactoryContracts/ViewModels/ShopViewModel.cs b/FishFactory/FishFactoryContracts/ViewModels/ShopViewModel.cs index ed566ee..81503bc 100644 --- a/FishFactory/FishFactoryContracts/ViewModels/ShopViewModel.cs +++ b/FishFactory/FishFactoryContracts/ViewModels/ShopViewModel.cs @@ -15,6 +15,9 @@ namespace FishFactoryContracts.ViewModels [DisplayName("Дата открытия")] public DateTime DateOpening { get; set; } = DateTime.Now; + [DisplayName("Максимальное количество консерв")] + public int MaxCountCanned { get; set; } + public Dictionary ListCanned { get; diff --git a/FishFactory/FishFactoryDataModels/Models/IShopModel.cs b/FishFactory/FishFactoryDataModels/Models/IShopModel.cs index 75d8449..6c1f924 100644 --- a/FishFactory/FishFactoryDataModels/Models/IShopModel.cs +++ b/FishFactory/FishFactoryDataModels/Models/IShopModel.cs @@ -6,5 +6,6 @@ string Address { get; } DateTime DateOpening { get; } Dictionary ListCanned { get; } + int MaxCountCanned { get; } } } \ No newline at end of file diff --git a/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs b/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs index 53207ab..30e53ac 100644 --- a/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs +++ b/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs @@ -1,59 +1,63 @@ using FishFactoryFileImplement.Models; -using System.Threading.Channels; using System.Xml.Linq; namespace FishFactoryFileImplement { - public class DataFileSingleton - { - private static DataFileSingleton? instance; - private readonly string ComponentFileName = "Component.xml"; - private readonly string OrderFileName = "Order.xml"; - private readonly string CannedFileName = "Canned.xml"; - public List Components { get; private set; } - public List Orders { get; private set; } - public List ListCanned { get; private set; } - public static DataFileSingleton GetInstance() - { - if (instance == null) - { - instance = new DataFileSingleton(); - } - return instance; - } - public void SaveComponents() => SaveData(Components, ComponentFileName, - "Components", x => x.GetXElement); + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string CannedFileName = "Canned.xml"; + private readonly string ShopFileName = "Shop.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List ListCanned { get; private set; } + public List Shops { get; private set; } + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveComponents() => SaveData(Components, ComponentFileName, + "Components", x => x.GetXElement); - public void SaveListCanned() => SaveData(ListCanned, CannedFileName, - "ListCanned", x => x.GetXElement); + public void SaveListCanned() => SaveData(ListCanned, CannedFileName, + "ListCanned", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - private DataFileSingleton() - { - Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; - ListCanned = LoadData(CannedFileName, "Canned", x => Canned.Create(x)!)!; - Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; - } + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); - 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(); - } + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + ListCanned = LoadData(CannedFileName, "Canned", x => Canned.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; + } - 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(); + } + + 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); + } + } + } } \ No newline at end of file diff --git a/FishFactory/FishFactoryFileImplement/Implements/ShopStorage.cs b/FishFactory/FishFactoryFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..a6bda3b --- /dev/null +++ b/FishFactory/FishFactoryFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,102 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using FishFactoryFileImplement.Models; + +namespace FishFactoryFileImplement.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 element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; + } + return null; + } + + public bool SellCanned(ICannedModel model, int count) + { + if (source.Shops.Select(x => x.ListCanned.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < count) + { + return false; + } + foreach (var shop in source.Shops.Where(x => x.ListCanned.ContainsKey(model.Id))) + { + int countInCurrentShop = shop.ListCanned[model.Id].Item2; + if (countInCurrentShop <= count) + { + shop.ListCanned.Remove(model.Id); + count -= countInCurrentShop; + } + else + { + shop.ListCanned[model.Id] = (shop.ListCanned[model.Id].Item1, countInCurrentShop - count); + count = 0; + } + if (count == 0) + { + return true; + } + } + return false; + } + } +} diff --git a/FishFactory/FishFactoryFileImplement/Models/Shop.cs b/FishFactory/FishFactoryFileImplement/Models/Shop.cs new file mode 100644 index 0000000..5bd236b --- /dev/null +++ b/FishFactory/FishFactoryFileImplement/Models/Shop.cs @@ -0,0 +1,105 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using System.Xml.Linq; + +namespace FishFactoryFileImplement.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 int MaxCountCanned { get; private set; } + public Dictionary CountCanned + { + get; + private set; + } = new(); + + private Dictionary? _shopCanned = null; + public Dictionary ListCanned + { + get + { + if (_shopCanned == null) + { + var source = DataFileSingleton.GetInstance(); + _shopCanned = + CountCanned.ToDictionary(x => x.Key, + y => ((source.ListCanned.FirstOrDefault(z => z.Id == y.Key) as ICannedModel)!, y.Value)); + } + return _shopCanned; + } + } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + MaxCountCanned = model.MaxCountCanned, + DateOpening = model.DateOpening, + CountCanned = model.ListCanned.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + MaxCountCanned = Convert.ToInt32(element.Element("MaxCountCanned")!.Value), + CountCanned = element.Element("ListCanned")!.Elements("Canned").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; + MaxCountCanned = model.MaxCountCanned; + CountCanned = model.ListCanned.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopCanned = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + ListCanned = ListCanned, + DateOpening = DateOpening, + MaxCountCanned = MaxCountCanned, + }; + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening), + new XElement("MaxCountCanned", MaxCountCanned), + new XElement("ListCanned", CountCanned + .Select(x => new XElement("Canned", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + ).ToArray())); + } +} diff --git a/FishFactory/FishFactoryListImplement/Implements/ShopStorage.cs b/FishFactory/FishFactoryListImplement/Implements/ShopStorage.cs index 21507c5..28f99ca 100644 --- a/FishFactory/FishFactoryListImplement/Implements/ShopStorage.cs +++ b/FishFactory/FishFactoryListImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using FishFactoryContracts.SearchModels; using FishFactoryContracts.StoragesContracts; using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; using FishFactoryListImplement.Models; namespace FishFactoryListImplement.Implements @@ -98,5 +99,10 @@ namespace FishFactoryListImplement.Implements } return null; } + + public bool SellCanned(ICannedModel model, int count) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/FishFactory/FishFactoryListImplement/Models/Shop.cs b/FishFactory/FishFactoryListImplement/Models/Shop.cs index 593c43f..550d854 100644 --- a/FishFactory/FishFactoryListImplement/Models/Shop.cs +++ b/FishFactory/FishFactoryListImplement/Models/Shop.cs @@ -49,5 +49,7 @@ namespace FishFactoryListImplement.Models ListCanned = ListCanned, DateOpening = DateOpening, }; + + public int MaxCountCanned => throw new NotImplementedException(); } } \ No newline at end of file diff --git a/FishFactory/FishFactoryView/FormMain.Designer.cs b/FishFactory/FishFactoryView/FormMain.Designer.cs index 58faf46..b2c8e39 100644 --- a/FishFactory/FishFactoryView/FormMain.Designer.cs +++ b/FishFactory/FishFactoryView/FormMain.Designer.cs @@ -39,6 +39,7 @@ this.buttonSetToWork = new System.Windows.Forms.Button(); this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonAddCannedInShop = new System.Windows.Forms.Button(); + this.buttonSellCanned = new System.Windows.Forms.Button(); this.dataGridView = new System.Windows.Forms.DataGridView(); this.menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); @@ -87,55 +88,55 @@ // // buttonUpdate // - this.buttonUpdate.Location = new System.Drawing.Point(781, 299); + this.buttonUpdate.Location = new System.Drawing.Point(781, 212); this.buttonUpdate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonUpdate.Name = "buttonUpdate"; - this.buttonUpdate.Size = new System.Drawing.Size(170, 58); - this.buttonUpdate.TabIndex = 12; + this.buttonUpdate.Size = new System.Drawing.Size(170, 32); + this.buttonUpdate.TabIndex = 12; this.buttonUpdate.Text = "Обновить"; this.buttonUpdate.UseVisualStyleBackColor = true; this.buttonUpdate.Click += new System.EventHandler(this.ButtonRef_Click); // // buttonSetToFinish // - this.buttonSetToFinish.Location = new System.Drawing.Point(781, 237); + this.buttonSetToFinish.Location = new System.Drawing.Point(781, 176); this.buttonSetToFinish.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonSetToFinish.Name = "buttonSetToFinish"; - this.buttonSetToFinish.Size = new System.Drawing.Size(170, 58); - this.buttonSetToFinish.TabIndex = 11; + this.buttonSetToFinish.Size = new System.Drawing.Size(170, 32); + this.buttonSetToFinish.TabIndex = 11; this.buttonSetToFinish.Text = "Заказ выдан"; this.buttonSetToFinish.UseVisualStyleBackColor = true; this.buttonSetToFinish.Click += new System.EventHandler(this.ButtonIssuedOrder_Click); // // buttonSetToDone // - this.buttonSetToDone.Location = new System.Drawing.Point(781, 175); + this.buttonSetToDone.Location = new System.Drawing.Point(781, 140); this.buttonSetToDone.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonSetToDone.Name = "buttonSetToDone"; - this.buttonSetToDone.Size = new System.Drawing.Size(170, 58); - this.buttonSetToDone.TabIndex = 10; + this.buttonSetToDone.Size = new System.Drawing.Size(170, 32); + this.buttonSetToDone.TabIndex = 10; this.buttonSetToDone.Text = "Заказ готов"; this.buttonSetToDone.UseVisualStyleBackColor = true; this.buttonSetToDone.Click += new System.EventHandler(this.ButtonOrderReady_Click); // // buttonSetToWork // - this.buttonSetToWork.Location = new System.Drawing.Point(781, 113); + this.buttonSetToWork.Location = new System.Drawing.Point(781, 104); this.buttonSetToWork.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonSetToWork.Name = "buttonSetToWork"; - this.buttonSetToWork.Size = new System.Drawing.Size(170, 58); - this.buttonSetToWork.TabIndex = 9; + this.buttonSetToWork.Size = new System.Drawing.Size(170, 32); + this.buttonSetToWork.TabIndex = 9; this.buttonSetToWork.Text = "Отдать на выполнение"; this.buttonSetToWork.UseVisualStyleBackColor = true; this.buttonSetToWork.Click += new System.EventHandler(this.ButtonTakeOrderInWork_Click); // // buttonCreateOrder // - this.buttonCreateOrder.Location = new System.Drawing.Point(781, 51); + this.buttonCreateOrder.Location = new System.Drawing.Point(781, 68); this.buttonCreateOrder.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonCreateOrder.Name = "buttonCreateOrder"; - this.buttonCreateOrder.Size = new System.Drawing.Size(170, 58); - this.buttonCreateOrder.TabIndex = 8; + this.buttonCreateOrder.Size = new System.Drawing.Size(170, 32); + this.buttonCreateOrder.TabIndex = 8; this.buttonCreateOrder.Text = "Создать заказ"; this.buttonCreateOrder.UseVisualStyleBackColor = true; this.buttonCreateOrder.Click += new System.EventHandler(this.ButtonCreateOrder_Click); @@ -154,15 +155,26 @@ // // buttonAddCannedInShop // - this.buttonAddCannedInShop.Location = new System.Drawing.Point(781, 361); + this.buttonAddCannedInShop.Location = new System.Drawing.Point(781, 248); this.buttonAddCannedInShop.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonAddCannedInShop.Name = "buttonAddCannedInShop"; - this.buttonAddCannedInShop.Size = new System.Drawing.Size(170, 58); + this.buttonAddCannedInShop.Size = new System.Drawing.Size(170, 43); this.buttonAddCannedInShop.TabIndex = 13; this.buttonAddCannedInShop.Text = "Добавить консерву в магазин"; this.buttonAddCannedInShop.UseVisualStyleBackColor = true; this.buttonAddCannedInShop.Click += new System.EventHandler(this.ButtonAddCannedInShop_Click); - // + // + // buttonSellCanned + // + this.buttonSellCanned.Location = new System.Drawing.Point(781, 295); + this.buttonSellCanned.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSellCanned.Name = "buttonSellCanned"; + this.buttonSellCanned.Size = new System.Drawing.Size(170, 32); + this.buttonSellCanned.TabIndex = 14; + this.buttonSellCanned.Text = "Продать консерву"; + this.buttonSellCanned.UseVisualStyleBackColor = true; + this.buttonSellCanned.Click += new System.EventHandler(this.ButtonSellCanned_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -174,6 +186,7 @@ this.Controls.Add(this.buttonSetToWork); this.Controls.Add(this.buttonCreateOrder); this.Controls.Add(this.buttonAddCannedInShop); + this.Controls.Add(this.buttonSellCanned); this.Controls.Add(this.dataGridView); this.Controls.Add(this.menuStrip); this.MainMenuStrip = this.menuStrip; @@ -201,6 +214,7 @@ private Button buttonSetToWork; private Button buttonCreateOrder; private Button buttonAddCannedInShop; + private Button buttonSellCanned; private DataGridView dataGridView; } } \ No newline at end of file diff --git a/FishFactory/FishFactoryView/FormMain.cs b/FishFactory/FishFactoryView/FormMain.cs index 9f93631..8090c4c 100644 --- a/FishFactory/FishFactoryView/FormMain.cs +++ b/FishFactory/FishFactoryView/FormMain.cs @@ -71,6 +71,15 @@ namespace FishFactoryView form.ShowDialog(); } } + private void ButtonSellCanned_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellCanned)); + if (service is FormSellCanned form) + { + form.ShowDialog(); + LoadData(); + } + } private void ButtonCreateOrder_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); @@ -90,14 +99,8 @@ namespace FishFactoryView { var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { - Id = id, - CannedId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["CannedId"].Value), - CannedName = dataGridView.SelectedRows[0].Cells["CannedName"].Value.ToString(), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), - }); + Id = id + }); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); @@ -121,14 +124,8 @@ namespace FishFactoryView { var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { - Id = id, - CannedId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["CannedId"].Value), - CannedName = dataGridView.SelectedRows[0].Cells["CannedName"].Value.ToString(), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), - }); + Id = id + }); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); @@ -152,14 +149,8 @@ namespace FishFactoryView { var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { - Id = id, - CannedId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["CannedId"].Value), - CannedName = dataGridView.SelectedRows[0].Cells["CannedName"].Value.ToString(), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), - }); + Id = id + }); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); diff --git a/FishFactory/FishFactoryView/FormSellCanned.Designer.cs b/FishFactory/FishFactoryView/FormSellCanned.Designer.cs new file mode 100644 index 0000000..14a430d --- /dev/null +++ b/FishFactory/FishFactoryView/FormSellCanned.Designer.cs @@ -0,0 +1,126 @@ +namespace FishFactoryView +{ + partial class FormSellCanned + { + /// + /// 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.labelCanned = new System.Windows.Forms.Label(); + this.labelCount = new System.Windows.Forms.Label(); + this.comboBoxCanned = new System.Windows.Forms.ComboBox(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // labelCanned + // + this.labelCanned.AutoSize = true; + this.labelCanned.Location = new System.Drawing.Point(9, 11); + this.labelCanned.Name = "labelCanned"; + this.labelCanned.Size = new System.Drawing.Size(39, 15); + this.labelCanned.TabIndex = 0; + this.labelCanned.Text = "Консерва"; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(11, 51); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(72, 15); + this.labelCount.TabIndex = 1; + this.labelCount.Text = "Количество"; + // + // comboBoxCanned + // + this.comboBoxCanned.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxCanned.FormattingEnabled = true; + this.comboBoxCanned.Location = new System.Drawing.Point(100, 11); + this.comboBoxCanned.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.comboBoxCanned.Name = "comboBoxCanned"; + this.comboBoxCanned.Size = new System.Drawing.Size(230, 23); + this.comboBoxCanned.TabIndex = 3; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(100, 51); + this.textBoxCount.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(230, 23); + this.textBoxCount.TabIndex = 4; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(160, 78); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(82, 22); + this.buttonSave.TabIndex = 6; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(248, 78); + 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 = 7; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormSellCanned + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(349, 119); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.comboBoxCanned); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.labelCanned); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormSellCanned"; + this.Text = "Продажа консерв"; + this.Load += new System.EventHandler(this.FormSellCanned_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelCanned; + private Label labelCount; + private ComboBox comboBoxCanned; + private TextBox textBoxCount; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/FishFactory/FishFactoryView/FormSellCanned.cs b/FishFactory/FishFactoryView/FormSellCanned.cs new file mode 100644 index 0000000..3fa11c3 --- /dev/null +++ b/FishFactory/FishFactoryView/FormSellCanned.cs @@ -0,0 +1,81 @@ +using Microsoft.Extensions.Logging; +using FishFactoryContracts.BusinessLogicsContracts; +using FishFactoryContracts.SearchModels; + +namespace FishFactoryView +{ + public partial class FormSellCanned : Form + { + private readonly ILogger _logger; + private readonly ICannedLogic _logicCanned; + private readonly IShopLogic _logicShop; + + public FormSellCanned(ILogger logger, ICannedLogic logicCanned, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicCanned = logicCanned; + _logicShop = logicShop; + } + + private void FormSellCanned_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка списка консервы для продажи"); + try + { + var list = _logicCanned.ReadList(null); + if (list != null) + { + comboBoxCanned.DisplayMember = "CannedName"; + comboBoxCanned.ValueMember = "Id"; + comboBoxCanned.DataSource = list; + comboBoxCanned.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка консерв"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле 'Количество'", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxCanned.SelectedValue == null) + { + MessageBox.Show("Выберите консерву", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Продажа консерв"); + try + { + var operationResult = _logicShop.SellCanned(_logicCanned.ReadElement(new CannedSearchModel() + { + Id = Convert.ToInt32(comboBoxCanned.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(); + } + } +} \ No newline at end of file diff --git a/FishFactory/FishFactoryView/FormSellCanned.resx b/FishFactory/FishFactoryView/FormSellCanned.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/FishFactory/FishFactoryView/FormSellCanned.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/FishFactory/FishFactoryView/FormShop.Designer.cs b/FishFactory/FishFactoryView/FormShop.Designer.cs index 37ad5bb..985a4c1 100644 --- a/FishFactory/FishFactoryView/FormShop.Designer.cs +++ b/FishFactory/FishFactoryView/FormShop.Designer.cs @@ -40,7 +40,10 @@ this.ColumnID = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ColumnCannedName = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.labelCount = new System.Windows.Forms.Label(); + this.numericUpDownCount = new System.Windows.Forms.NumericUpDown(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).BeginInit(); this.SuspendLayout(); // // buttonSave @@ -69,7 +72,7 @@ // this.textBoxAddress.Location = new System.Drawing.Point(159, 27); this.textBoxAddress.Name = "textBoxAddress"; - this.textBoxAddress.Size = new System.Drawing.Size(221, 23); + this.textBoxAddress.Size = new System.Drawing.Size(141, 23); this.textBoxAddress.TabIndex = 14; // // labelTime @@ -126,9 +129,9 @@ // // dateTimePicker // - this.dateTimePicker.Location = new System.Drawing.Point(386, 27); + this.dateTimePicker.Location = new System.Drawing.Point(306, 27); this.dateTimePicker.Name = "dateTimePicker"; - this.dateTimePicker.Size = new System.Drawing.Size(207, 23); + this.dateTimePicker.Size = new System.Drawing.Size(141, 23); this.dateTimePicker.TabIndex = 19; // // ColumnID @@ -147,12 +150,30 @@ // this.ColumnCount.HeaderText = "Количество"; this.ColumnCount.Name = "ColumnCount"; - // + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(453, 9); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(134, 15); + this.labelCount.TabIndex = 20; + this.labelCount.Text = "Вместимость магазина"; + // + // numericUpDownCount + // + this.numericUpDownCount.Location = new System.Drawing.Point(453, 27); + this.numericUpDownCount.Name = "numericUpDownCount"; + this.numericUpDownCount.Size = new System.Drawing.Size(140, 23); + this.numericUpDownCount.TabIndex = 21; + // // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(605, 337); + this.Controls.Add(this.numericUpDownCount); + this.Controls.Add(this.labelCount); this.Controls.Add(this.dateTimePicker); this.Controls.Add(this.textBoxShop); this.Controls.Add(this.buttonSave); @@ -167,6 +188,7 @@ this.Load += new System.EventHandler(this.FormShop_Load); this.Click += new System.EventHandler(this.FormShop_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -186,5 +208,7 @@ private DataGridViewTextBoxColumn ColumnID; private DataGridViewTextBoxColumn ColumnCannedName; private DataGridViewTextBoxColumn ColumnCount; + private Label labelCount; + private NumericUpDown numericUpDownCount; } } \ No newline at end of file diff --git a/FishFactory/FishFactoryView/FormShop.cs b/FishFactory/FishFactoryView/FormShop.cs index 0097d07..57a20b5 100644 --- a/FishFactory/FishFactoryView/FormShop.cs +++ b/FishFactory/FishFactoryView/FormShop.cs @@ -39,6 +39,7 @@ namespace FishFactoryView textBoxShop.Text = view.ShopName; textBoxAddress.Text = view.Address; dateTimePicker.Text = view.DateOpening.ToString(); + numericUpDownCount.Value = view.MaxCountCanned; _shopListCanned = view.ListCanned ?? new Dictionary(); LoadData(); } @@ -94,6 +95,7 @@ namespace FishFactoryView ShopName = textBoxShop.Text, Address = textBoxAddress.Text, DateOpening = dateTimePicker.Value.Date, + MaxCountCanned = (int)numericUpDownCount.Value, ListCanned = _shopListCanned }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); diff --git a/FishFactory/FishFactoryView/Program.cs b/FishFactory/FishFactoryView/Program.cs index 36969ce..6fa19f5 100644 --- a/FishFactory/FishFactoryView/Program.cs +++ b/FishFactory/FishFactoryView/Program.cs @@ -56,6 +56,7 @@ namespace FishFactoryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } }