diff --git a/TypographyBusinessLogic/OrderLogic.cs b/TypographyBusinessLogic/OrderLogic.cs index bcf03a7..417d132 100644 --- a/TypographyBusinessLogic/OrderLogic.cs +++ b/TypographyBusinessLogic/OrderLogic.cs @@ -10,6 +10,7 @@ using TypographyContracts.StoragesContracts; using TypographyContracts.ViewModels; using TypographyDataModels.Enums; using Microsoft.Extensions.Logging; +using TypographyDataModels.Models; namespace TypographyBusinessLogic.BusinessLogics { @@ -17,11 +18,17 @@ namespace TypographyBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; + private readonly IPrintedStorage _printedStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage, IShopLogic shopLogic, IPrintedStorage printedStorage) { _logger = logger; _orderStorage = orderStorage; + _shopStorage = shopStorage; + _shopLogic = shopLogic; + _printedStorage = printedStorage; } public List? ReadList(OrderSearchModel? model) @@ -44,11 +51,74 @@ namespace TypographyBusinessLogic.BusinessLogics model.Status = OrderStatus.Принят; if (_orderStorage.Insert(model) == null) { + model.Status = OrderStatus.Неизвестен; _logger.LogWarning("Insert operation failed"); return false; } return true; } + public bool CheckThenSupplyMany(IPrintedModel car, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. Printed count < 0."); + return false; + } + + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.MaxCountPrinteds; + foreach (var c in shop.ShopPrinteds) + { + freeSpace -= c.Value.Item2; + } + } + + if (freeSpace < count) + { + _logger.LogWarning("Check then supply operation error. There's no place for new cars in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCountPrinteds; + + foreach (var c in shop.ShopPrinteds) + freeSpace -= c.Value.Item2; + + if (freeSpace <= 0) + continue; + + if (freeSpace >= count) + { + if (_shopLogic.MakeShipment(new ShopSearchModel() { Id = shop.Id }, car, count)) + count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace < count) + { + if (_shopLogic.MakeShipment(new ShopSearchModel() { Id = shop.Id }, car, freeSpace)) + count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } + public bool ChangeStatus(OrderBindingModel model, OrderStatus status) { @@ -104,11 +174,11 @@ namespace TypographyBusinessLogic.BusinessLogics } if (model.PrintedId < 0) { - throw new ArgumentNullException("Некорректный идентификатор закусок", nameof(model.PrintedId)); + throw new ArgumentNullException("Некорректный идентификатор изделий", nameof(model.PrintedId)); } if (model.Count <= 0) { - throw new ArgumentNullException("Количество закусок в заказе должно быть больше 0", nameof(model.Count)); + throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count)); } if (model.Sum <= 0) { diff --git a/TypographyBusinessLogic/ShopLogic.cs b/TypographyBusinessLogic/ShopLogic.cs index a9eea1d..ebc57ea 100644 --- a/TypographyBusinessLogic/ShopLogic.cs +++ b/TypographyBusinessLogic/ShopLogic.cs @@ -16,7 +16,6 @@ namespace TypographyBusinessLogic public class ShopLogic : IShopLogic { private readonly ILogger _logger; - private readonly IShopStorage _shopStorage; public ShopLogic(ILogger logger, IShopStorage shopStorage) @@ -25,117 +24,142 @@ namespace TypographyBusinessLogic _shopStorage = shopStorage; } - public List? ReadList(ShopSearchModel? model) - { - _logger.LogInformation("ReadList. ShopName: {ShopName}. Id: {Id}", model?.ShopName, model?.Id); - var list = model == null ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model); - if (list == null) - { - _logger.LogWarning("ReadList return null list"); - return null; - } - _logger.LogInformation("ReadList. Count: {Count}", list.Count); - return list; - } - public ShopViewModel? ReadElement(ShopSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } - _logger.LogInformation("ReadElement. ShopName: {ShopName}. Id: {Id}", model.ShopName, model.Id); + + _logger.LogInformation("ReadElement. Shop Name:{0}, ID:{1}", model.ShopName, model.Id); + var element = _shopStorage.GetElement(model); if (element == null) { - _logger.LogWarning("ReadElement element not found"); + _logger.LogWarning("ReadElement. element not found"); return null; } - _logger.LogInformation("ReadElement find. Id: {Id}", element.Id); + + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; } + public List? ReadList(ShopSearchModel? model) + { + _logger.LogInformation("ReadList. Shop Name:{0}, ID:{1} ", model?.ShopName, model?.Id); + + var list = (model == null) ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public bool MakeShipment(ShopSearchModel model, IPrintedModel printed, int count) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (printed == null) + { + throw new ArgumentNullException(nameof(printed)); + } + + if (count <= 0) + { + throw new ArgumentNullException("Count of printeds in supply must be more than 0", nameof(count)); + } + + var shopElement = _shopStorage.GetElement(model); + if (shopElement == null) + { + _logger.LogWarning("Required shop element not found in storage"); + return false; + } + + _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.ShopName); + + int countPrinteds = 0; + foreach (var c in shopElement.ShopPrinteds) + countPrinteds += c.Value.Item2; + + if (count > shopElement.MaxCountPrinteds - countPrinteds) + { + _logger.LogWarning("Required shop will be overflowed"); + return false; + } + else + { + if (shopElement.ShopPrinteds.TryGetValue(printed.Id, out var samePrinted)) + { + shopElement.ShopPrinteds[printed.Id] = (printed, samePrinted.Item2 + count); + _logger.LogInformation("Same printed found by supply. Added {0} of {1} in {2} shop", count, printed.PrintedName, shopElement.ShopName); + } + else + { + shopElement.ShopPrinteds[printed.Id] = (printed, count); + _logger.LogInformation("New printed added by supply. Added {0} of {1} in {2} shop", count, printed.PrintedName, shopElement.ShopName); + } + + _shopStorage.Update(new() + { + Id = shopElement.Id, + ShopName = shopElement.ShopName, + Address = shopElement.Address, + MaxCountPrinteds = shopElement.MaxCountPrinteds, + DateOpening = shopElement.DateOpening, + ShopPrinteds = shopElement.ShopPrinteds + }); + } + + return true; + } + public bool Create(ShopBindingModel model) { CheckModel(model); + if (_shopStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } + return true; } public bool Update(ShopBindingModel model) { CheckModel(model); + if (_shopStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } + return true; } public bool Delete(ShopBindingModel model) { CheckModel(model, false); - _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_shopStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } - return true; - } - public bool MakeShipment(ShopSearchModel shopModel, IPrintedModel iceCream, int count) - { - if (shopModel == null) - { - throw new ArgumentNullException(nameof(shopModel)); - } - if (iceCream == null) - { - throw new ArgumentNullException(nameof(iceCream)); - } - if (count <= 0) - { - throw new ArgumentException("Количество товаров(мороженого) в магазине должно быть больше нуля", nameof(count)); - } - _logger.LogInformation("MakeShipment(GetElement). ShopName: {ShopName}. Id: {Id}", shopModel.ShopName, shopModel.Id); - var shop = _shopStorage.GetElement(shopModel); - if (shop == null) - { - _logger.LogWarning("MakeShipment(GetElement). Element not found"); - return false; - } - if (shop.ShopPrinteds.ContainsKey(iceCream.Id)) - { - var shopIC = shop.ShopPrinteds[iceCream.Id]; - shopIC.Item2 += count; - shop.ShopPrinteds[iceCream.Id] = shopIC; - _logger.LogInformation("MakeShipment. Added {count} '{iceCream}' to '{ShopName}' shop", count, iceCream.PrintedName, - shop.ShopName); - } - else - { - shop.ShopPrinteds.Add(iceCream.Id, (iceCream, count)); - _logger.LogInformation("MakeShipment. Added {count} new '{iceCream}' to '{ShopName}' shop", count, iceCream.PrintedName, - shop.ShopName); - } - if (_shopStorage.Update(new ShopBindingModel() - { - Id = shop.Id, - ShopName = shop.ShopName, - Address = shop.Address, - DateOpening = shop.DateOpening, - ShopPrinteds = shop.ShopPrinteds, - }) == null) - { - _logger.LogWarning("MakeShipment. Update operation failed"); - return false; - } return true; } @@ -145,27 +169,38 @@ namespace TypographyBusinessLogic { throw new ArgumentNullException(nameof(model)); } + if (!withParams) { return; } + if (string.IsNullOrEmpty(model.ShopName)) { - throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); + throw new ArgumentNullException("Нет названия магазина!", nameof(model.ShopName)); } - if (string.IsNullOrEmpty(model.Address)) + + if (model.MaxCountPrinteds < 0) { - throw new ArgumentNullException("Нет адреса магазина", nameof(model.Address)); + throw new InvalidOperationException("Отрицательное максимальное количество авто!"); } - _logger.LogInformation("Shop. ShopName: {ShopName}. Address: {Address}. Id: {Id}", model.ShopName, model.Address, model.Id); + + _logger.LogInformation("Shop. Name: {0}, Address: {1}, ID: {2}", model.ShopName, model.Address, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel { ShopName = model.ShopName }); + if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + + public bool SellPrinted(IPrintedModel printed, int count) + { + return _shopStorage.SellPrinted(printed, count); + } } } diff --git a/TypographyContracts/BindingModels/ShopBindingModel.cs b/TypographyContracts/BindingModels/ShopBindingModel.cs index 78ede24..dc46f47 100644 --- a/TypographyContracts/BindingModels/ShopBindingModel.cs +++ b/TypographyContracts/BindingModels/ShopBindingModel.cs @@ -17,6 +17,7 @@ namespace TypographyContracts.BindingModels public string Address { get; set; } = string.Empty; public DateTime DateOpening { get; set; } = DateTime.Now; + public int MaxCountPrinteds { get; set; } public Dictionary ShopPrinteds { get; set; } = new(); } diff --git a/TypographyContracts/BusinessLogicsContracts/IShopLogic.cs b/TypographyContracts/BusinessLogicsContracts/IShopLogic.cs index ee813e5..47c150a 100644 --- a/TypographyContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/TypographyContracts/BusinessLogicsContracts/IShopLogic.cs @@ -22,6 +22,7 @@ namespace TypographyContracts.BusinessLogicsContracts bool Delete(ShopBindingModel model); - bool MakeShipment(ShopSearchModel shopModel, IPrintedModel iceCream, int count); + bool MakeShipment(ShopSearchModel shopModel, IPrintedModel printed, int count); + bool SellPrinted(IPrintedModel printed, int count); } } diff --git a/TypographyContracts/StoragesContracts/IShopStorage.cs b/TypographyContracts/StoragesContracts/IShopStorage.cs index dd0970a..c7533f2 100644 --- a/TypographyContracts/StoragesContracts/IShopStorage.cs +++ b/TypographyContracts/StoragesContracts/IShopStorage.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using TypographyContracts.BindingModels; using TypographyContracts.SearchModels; using TypographyContracts.ViewModels; +using TypographyDataModels.Models; namespace TypographyContracts.StoragesContracts { @@ -22,5 +23,6 @@ namespace TypographyContracts.StoragesContracts ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool SellPrinted(IPrintedModel model, int count); } } diff --git a/TypographyContracts/ViewModels/ShopViewModel.cs b/TypographyContracts/ViewModels/ShopViewModel.cs index 819cb46..e1955de 100644 --- a/TypographyContracts/ViewModels/ShopViewModel.cs +++ b/TypographyContracts/ViewModels/ShopViewModel.cs @@ -17,6 +17,8 @@ namespace TypographyContracts.ViewModels [DisplayName("Адрес")] public string Address { get; set; } = string.Empty; + [DisplayName("Максимальное количество изделий в магазине")] + public int MaxCountPrinteds { get; set; } [DisplayName("Дата открытия")] public DateTime DateOpening { get; set; } = DateTime.Now; diff --git a/TypographyDataModels/OrderStatus.cs b/TypographyDataModels/Enums/OrderStatus.cs similarity index 100% rename from TypographyDataModels/OrderStatus.cs rename to TypographyDataModels/Enums/OrderStatus.cs diff --git a/TypographyDataModels/IComponentModel.cs b/TypographyDataModels/Models/IComponentModel.cs similarity index 100% rename from TypographyDataModels/IComponentModel.cs rename to TypographyDataModels/Models/IComponentModel.cs diff --git a/TypographyDataModels/IOrderModel.cs b/TypographyDataModels/Models/IOrderModel.cs similarity index 74% rename from TypographyDataModels/IOrderModel.cs rename to TypographyDataModels/Models/IOrderModel.cs index 3358942..70c46de 100644 --- a/TypographyDataModels/IOrderModel.cs +++ b/TypographyDataModels/Models/IOrderModel.cs @@ -3,8 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using TypographyDataModels.Enums; -namespace TypographyDataModels.Enums +namespace TypographyDataModels.Models { public interface IOrderModel : IId { @@ -13,6 +14,6 @@ namespace TypographyDataModels.Enums double Sum { get; } OrderStatus Status { get; } DateTime DateCreate { get; } - DateTime? DateImplement { get; } + DateTime? DateImplement { get; } } } diff --git a/TypographyDataModels/IPrintedModel.cs b/TypographyDataModels/Models/IPrintedModel.cs similarity index 100% rename from TypographyDataModels/IPrintedModel.cs rename to TypographyDataModels/Models/IPrintedModel.cs diff --git a/TypographyDataModels/IShopModel.cs b/TypographyDataModels/Models/IShopModel.cs similarity index 91% rename from TypographyDataModels/IShopModel.cs rename to TypographyDataModels/Models/IShopModel.cs index 2f84af5..917e05f 100644 --- a/TypographyDataModels/IShopModel.cs +++ b/TypographyDataModels/Models/IShopModel.cs @@ -13,6 +13,7 @@ namespace TypographyDataModels.Models string Address { get; } DateTime DateOpening { get; } + int MaxCountPrinteds { get; } Dictionary ShopPrinteds { get; } } diff --git a/TypographyFileImplement/DataFileSingleton.cs b/TypographyFileImplement/DataFileSingleton.cs index 53cb395..81e711f 100644 --- a/TypographyFileImplement/DataFileSingleton.cs +++ b/TypographyFileImplement/DataFileSingleton.cs @@ -18,10 +18,12 @@ namespace TypographyFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string PrintedFileName = "Printed.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Printeds { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { @@ -35,12 +37,14 @@ namespace TypographyFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePrinteds() => SaveData(Printeds, PrintedFileName, "Printeds", 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)!)!; Printeds = LoadData(PrintedFileName, "Printed", x => Printed.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/TypographyFileImplement/Implements/ShopStorage.cs b/TypographyFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..9d1a804 --- /dev/null +++ b/TypographyFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TypographyContracts.BindingModels; +using TypographyContracts.SearchModels; +using TypographyContracts.StoragesContracts; +using TypographyContracts.ViewModels; +using TypographyDataModels.Models; +using TypographyFileImplement.Models; + +namespace TypographyFileImplement.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.ShopName)) + { + return new(); + } + return source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.ShopName.Contains(model.ShopName ?? 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 SellPrinted(IPrintedModel model, int count) + { + var printed = source.Printeds.FirstOrDefault(x => x.Id == model.Id); + + if (printed == null) + { + return false; + } + + + var shopPrinteds = source.Shops.SelectMany(shop => shop.ShopPrinteds.Where(c => c.Value.Item1.Id == printed.Id)); + + int countStore = 0; + + foreach (var it in shopPrinteds) + countStore += it.Value.Item2; + + if (count > countStore) + return false; + + foreach (var shop in source.Shops) + { + var printeds = shop.ShopPrinteds; + + foreach (var c in printeds.Where(x => x.Value.Item1.Id == printed.Id)) + { + int min = Math.Min(c.Value.Item2, count); + printeds[c.Value.Item1.Id] = (c.Value.Item1, c.Value.Item2 - min); + count -= min; + + if (count <= 0) + break; + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + MaxCountPrinteds = shop.MaxCountPrinteds, + DateOpening = shop.DateOpening, + ShopPrinteds = printeds + }); + + source.SaveShops(); + + if (count <= 0) + return true; + } + + return true; + } + } +} diff --git a/TypographyFileImplement/Models/Shop.cs b/TypographyFileImplement/Models/Shop.cs new file mode 100644 index 0000000..00d49f0 --- /dev/null +++ b/TypographyFileImplement/Models/Shop.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using TypographyContracts.BindingModels; +using TypographyContracts.ViewModels; +using TypographyDataModels.Models; + +namespace TypographyFileImplement.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 int MaxCountPrinteds { get; private set; } + public DateTime DateOpening { get; private set; } + public Dictionary Printeds { get; private set; } = new(); + private Dictionary? _shopPrinteds = null; + + public Dictionary ShopPrinteds + { + get + { + if (_shopPrinteds == null) + { + var source = DataFileSingleton.GetInstance(); + _shopPrinteds = Printeds.ToDictionary( + x => x.Key, + y => ((source.Printeds.FirstOrDefault(z => z.Id == y.Key) as IPrintedModel)!, y.Value) + ); + } + return _shopPrinteds; + } + } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + MaxCountPrinteds = model.MaxCountPrinteds, + DateOpening = model.DateOpening, + Printeds = model.ShopPrinteds.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), + ShopName = element.Element("ShopName")!.Value, + Address = element.Element("Address")!.Value, + MaxCountPrinteds = Convert.ToInt32(element.Element("MaxCountPrinteds")!.Value), + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + Printeds = element.Element("ShopPrinteds")!.Elements("ShopPrinted").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; + MaxCountPrinteds = model.MaxCountPrinteds; + DateOpening = model.DateOpening; + if (model.ShopPrinteds.Count > 0) + { + Printeds = model.ShopPrinteds.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopPrinteds = null; + } + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + MaxCountPrinteds = MaxCountPrinteds, + DateOpening = DateOpening, + ShopPrinteds = ShopPrinteds, + }; + + public XElement GetXElement => new( + "Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("MaxCountPrinteds", MaxCountPrinteds), + new XElement("DateOpening", DateOpening.ToString()), + new XElement("ShopPrinteds", Printeds.Select(x => + new XElement("ShopPrinted", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} diff --git a/TypographyListImplement/Implements/ShopStorage.cs b/TypographyListImplement/Implements/ShopStorage.cs index 4890152..c66c4ee 100644 --- a/TypographyListImplement/Implements/ShopStorage.cs +++ b/TypographyListImplement/Implements/ShopStorage.cs @@ -7,6 +7,7 @@ using TypographyContracts.BindingModels; using TypographyContracts.SearchModels; using TypographyContracts.StoragesContracts; using TypographyContracts.ViewModels; +using TypographyDataModels.Models; using TypographyListImplement.Models; namespace TypographyListImplement.Implements @@ -110,5 +111,9 @@ namespace TypographyListImplement.Implements } return null; } + public bool SellPrinted(IPrintedModel car, int count) + { + throw new NotImplementedException(); + } } } diff --git a/TypographyListImplement/Models/Shop.cs b/TypographyListImplement/Models/Shop.cs index c815360..d891953 100644 --- a/TypographyListImplement/Models/Shop.cs +++ b/TypographyListImplement/Models/Shop.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using TypographyContracts.BindingModels; @@ -16,6 +17,7 @@ namespace TypographyListImplement.Models public string ShopName { get; private set; } = string.Empty; public string Address { get; private set; } = string.Empty; + public int MaxCountPrinteds { get; private set; } public DateTime DateOpening { get; private set; } @@ -37,6 +39,7 @@ namespace TypographyListImplement.Models ShopName = model.ShopName, Address = model.Address, DateOpening = model.DateOpening, + MaxCountPrinteds = model.MaxCountPrinteds, ShopPrinteds = model.ShopPrinteds }; } @@ -50,6 +53,7 @@ namespace TypographyListImplement.Models ShopName = model.ShopName; Address = model.Address; DateOpening = model.DateOpening; + MaxCountPrinteds = model.MaxCountPrinteds; ShopPrinteds = model.ShopPrinteds; } @@ -59,6 +63,7 @@ namespace TypographyListImplement.Models ShopName = ShopName, Address = Address, DateOpening = DateOpening, + MaxCountPrinteds = MaxCountPrinteds, ShopPrinteds = ShopPrinteds }; } diff --git a/TypographyView/FormMain.Designer.cs b/TypographyView/FormMain.Designer.cs index 065af82..d024e45 100644 --- a/TypographyView/FormMain.Designer.cs +++ b/TypographyView/FormMain.Designer.cs @@ -34,6 +34,7 @@ изделиеToolStripMenuItem = new ToolStripMenuItem(); магазиныToolStripMenuItem = new ToolStripMenuItem(); пополнениеМагазинаToolStripMenuItem = new ToolStripMenuItem(); + продажаИзделийToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); buttonCreateOrder = new Button(); buttonTakeOrderInWork = new Button(); @@ -47,7 +48,7 @@ // menuStrip // menuStrip.ImageScalingSize = new Size(20, 20); - menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, пополнениеМагазинаToolStripMenuItem }); + menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, пополнениеМагазинаToolStripMenuItem, продажаИзделийToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; menuStrip.Size = new Size(1146, 28); @@ -64,21 +65,21 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(224, 26); + компонентыToolStripMenuItem.Size = new Size(182, 26); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += КомпонентыToolStripMenuItem_Click; // // изделиеToolStripMenuItem // изделиеToolStripMenuItem.Name = "изделиеToolStripMenuItem"; - изделиеToolStripMenuItem.Size = new Size(224, 26); + изделиеToolStripMenuItem.Size = new Size(182, 26); изделиеToolStripMenuItem.Text = "Изделие"; изделиеToolStripMenuItem.Click += ЗакускаToolStripMenuItem_Click; // // магазиныToolStripMenuItem // магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - магазиныToolStripMenuItem.Size = new Size(224, 26); + магазиныToolStripMenuItem.Size = new Size(182, 26); магазиныToolStripMenuItem.Text = "Магазины"; магазиныToolStripMenuItem.Click += МагазиныToolStripMenuItem_Click; // @@ -89,6 +90,13 @@ пополнениеМагазинаToolStripMenuItem.Text = "Пополнение магазина"; пополнениеМагазинаToolStripMenuItem.Click += ПополнениеМагазиныToolStripMenuItem_Click; // + // продажаИзделийToolStripMenuItem + // + продажаИзделийToolStripMenuItem.Name = "продажаИзделийToolStripMenuItem"; + продажаИзделийToolStripMenuItem.Size = new Size(149, 24); + продажаИзделийToolStripMenuItem.Text = "Продажа изделий"; + продажаИзделийToolStripMenuItem.Click += sellPrintedsToolStripMenuItem_Click; + // // dataGridView // dataGridView.BackgroundColor = SystemColors.ControlLightLight; @@ -188,5 +196,6 @@ private ToolStripMenuItem изделиеToolStripMenuItem; private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripMenuItem пополнениеМагазинаToolStripMenuItem; + private ToolStripMenuItem продажаИзделийToolStripMenuItem; } } \ No newline at end of file diff --git a/TypographyView/FormMain.cs b/TypographyView/FormMain.cs index c9283d4..96625d6 100644 --- a/TypographyView/FormMain.cs +++ b/TypographyView/FormMain.cs @@ -11,6 +11,7 @@ using TypographyContracts.BindingModels; using TypographyContracts.BusinessLogicsContracts; using Microsoft.Extensions.Logging; using TypographyDataModels.Enums; +using AutomobilePlantView; namespace TypographyView { @@ -51,6 +52,14 @@ namespace TypographyView MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + private void sellPrintedsToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShopSell)); + if (service is FormShopSell form) + { + form.ShowDialog(); + } + } private void КомпонентыToolStripMenuItem_Click(object sender, EventArgs e) { diff --git a/TypographyView/FormShop.Designer.cs b/TypographyView/FormShop.Designer.cs index 3331292..3d1035c 100644 --- a/TypographyView/FormShop.Designer.cs +++ b/TypographyView/FormShop.Designer.cs @@ -36,11 +36,13 @@ dateTimePicker = new DateTimePicker(); groupBoxPrinted = new GroupBox(); dataGridView = new DataGridView(); - buttonSave = new Button(); - buttonCancel = new Button(); Columnid = new DataGridViewTextBoxColumn(); ColumnPrinted = new DataGridViewTextBoxColumn(); ColumnCount = new DataGridViewTextBoxColumn(); + buttonSave = new Button(); + buttonCancel = new Button(); + labelMaxCountPrinteds = new Label(); + textBoxMaxCount = new TextBox(); groupBoxPrinted.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -96,7 +98,7 @@ // groupBoxPrinted // groupBoxPrinted.Controls.Add(dataGridView); - groupBoxPrinted.Location = new Point(12, 129); + groupBoxPrinted.Location = new Point(8, 201); groupBoxPrinted.Name = "groupBoxPrinted"; groupBoxPrinted.Size = new Size(598, 292); groupBoxPrinted.TabIndex = 7; @@ -108,33 +110,13 @@ dataGridView.BackgroundColor = SystemColors.ControlLightLight; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.Columns.AddRange(new DataGridViewColumn[] { Columnid, ColumnPrinted, ColumnCount }); - dataGridView.Location = new Point(0, 26); + dataGridView.Location = new Point(6, 22); dataGridView.Name = "dataGridView"; dataGridView.RowHeadersWidth = 51; dataGridView.RowTemplate.Height = 29; dataGridView.Size = new Size(592, 260); dataGridView.TabIndex = 0; // - // buttonSave - // - buttonSave.Location = new Point(330, 431); - buttonSave.Name = "buttonSave"; - buttonSave.Size = new Size(95, 34); - buttonSave.TabIndex = 8; - buttonSave.Text = "Сохранить"; - buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += ButtonSave_Click; - // - // buttonCancel - // - buttonCancel.Location = new Point(449, 431); - buttonCancel.Name = "buttonCancel"; - buttonCancel.Size = new Size(95, 34); - buttonCancel.TabIndex = 9; - buttonCancel.Text = "Отмена"; - buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += ButtonCancel_Click; - // // Columnid // Columnid.HeaderText = "Id"; @@ -157,11 +139,48 @@ ColumnCount.Name = "ColumnCount"; ColumnCount.Width = 125; // + // buttonSave + // + buttonSave.Location = new Point(343, 489); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(95, 34); + buttonSave.TabIndex = 8; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(459, 489); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(95, 34); + buttonCancel.TabIndex = 9; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // labelMaxCountPrinteds + // + labelMaxCountPrinteds.Location = new Point(8, 136); + labelMaxCountPrinteds.Name = "labelMaxCountPrinteds"; + labelMaxCountPrinteds.Size = new Size(160, 40); + labelMaxCountPrinteds.TabIndex = 10; + labelMaxCountPrinteds.Text = "Максимальное количество изделий :"; + // + // textBoxMaxCount + // + textBoxMaxCount.Location = new Point(174, 149); + textBoxMaxCount.Name = "textBoxMaxCount"; + textBoxMaxCount.Size = new Size(250, 27); + textBoxMaxCount.TabIndex = 11; + // // FormShop // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(618, 477); + ClientSize = new Size(618, 535); + Controls.Add(textBoxMaxCount); + Controls.Add(labelMaxCountPrinteds); Controls.Add(buttonCancel); Controls.Add(buttonSave); Controls.Add(groupBoxPrinted); @@ -195,5 +214,7 @@ private DataGridViewTextBoxColumn Columnid; private DataGridViewTextBoxColumn ColumnPrinted; private DataGridViewTextBoxColumn ColumnCount; + private Label labelMaxCountPrinteds; + private TextBox textBoxMaxCount; } } \ No newline at end of file diff --git a/TypographyView/FormShop.cs b/TypographyView/FormShop.cs index 96d70de..41d9470 100644 --- a/TypographyView/FormShop.cs +++ b/TypographyView/FormShop.cs @@ -123,5 +123,7 @@ namespace TypographyView DialogResult = DialogResult.Cancel; Close(); } + + } } \ No newline at end of file diff --git a/TypographyView/FormShopSell.Designer.cs b/TypographyView/FormShopSell.Designer.cs new file mode 100644 index 0000000..8d180e8 --- /dev/null +++ b/TypographyView/FormShopSell.Designer.cs @@ -0,0 +1,124 @@ +namespace AutomobilePlantView +{ + 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() + { + labelPrinted = new Label(); + labelCount = new Label(); + comboBoxPrinted = new ComboBox(); + textBoxCount = new TextBox(); + buttonCancel = new Button(); + buttonSell = new Button(); + SuspendLayout(); + // + // labelPrinted + // + labelPrinted.AutoSize = true; + labelPrinted.Location = new Point(14, 12); + labelPrinted.Name = "labelPrinted"; + labelPrinted.Size = new Size(59, 20); + labelPrinted.TabIndex = 0; + labelPrinted.Text = "Printed:"; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(14, 51); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(51, 20); + labelCount.TabIndex = 1; + labelCount.Text = "Count:"; + // + // comboBoxPrinted + // + comboBoxPrinted.FormattingEnabled = true; + comboBoxPrinted.Location = new Point(70, 8); + comboBoxPrinted.Margin = new Padding(3, 4, 3, 4); + comboBoxPrinted.Name = "comboBoxPrinted"; + comboBoxPrinted.Size = new Size(370, 28); + comboBoxPrinted.TabIndex = 2; + // + // textBoxCount + // + textBoxCount.Location = new Point(70, 47); + textBoxCount.Margin = new Padding(3, 4, 3, 4); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(370, 27); + textBoxCount.TabIndex = 3; + // + // buttonCancel + // + buttonCancel.Location = new Point(262, 85); + buttonCancel.Margin = new Padding(3, 4, 3, 4); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(86, 31); + buttonCancel.TabIndex = 4; + buttonCancel.Text = "Cancel"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // buttonSell + // + buttonSell.Location = new Point(354, 85); + buttonSell.Margin = new Padding(3, 4, 3, 4); + buttonSell.Name = "buttonSell"; + buttonSell.Size = new Size(86, 31); + buttonSell.TabIndex = 5; + buttonSell.Text = "Sell"; + buttonSell.UseVisualStyleBackColor = true; + buttonSell.Click += buttonSell_Click; + // + // FormShopSell + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(454, 124); + Controls.Add(buttonSell); + Controls.Add(buttonCancel); + Controls.Add(textBoxCount); + Controls.Add(comboBoxPrinted); + Controls.Add(labelCount); + Controls.Add(labelPrinted); + Margin = new Padding(3, 4, 3, 4); + Name = "FormShopSell"; + Text = "Sell"; + Load += FormShopSell_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelPrinted; + private Label labelCount; + private ComboBox comboBoxPrinted; + private TextBox textBoxCount; + private Button buttonCancel; + private Button buttonSell; + } +} \ No newline at end of file diff --git a/TypographyView/FormShopSell.cs b/TypographyView/FormShopSell.cs new file mode 100644 index 0000000..0c73d6f --- /dev/null +++ b/TypographyView/FormShopSell.cs @@ -0,0 +1,95 @@ +using TypographyContracts.BindingModels; +using TypographyContracts.BusinessLogicsContracts; +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 AutomobilePlantView +{ + public partial class FormShopSell : Form + { + private readonly ILogger _logger; + private readonly IPrintedLogic _logicPrinted; + private readonly IShopLogic _logicShop; + + public FormShopSell(ILogger logger, IPrintedLogic logicPrinted, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicPrinted = logicPrinted; + _logicShop = logicShop; + } + + private void FormShopSell_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка авто для продажи"); + try + { + var list = _logicPrinted.ReadList(null); + if (list != null) + { + comboBoxPrinted.DisplayMember = "PrintedName"; + comboBoxPrinted.ValueMember = "Id"; + comboBoxPrinted.DataSource = list; + comboBoxPrinted.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 (comboBoxPrinted.SelectedValue == null) + { + MessageBox.Show("Выберите авто", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание продажи"); + try + { + var operationResult = _logicShop.SellPrinted( + new PrintedBindingModel + { + Id = Convert.ToInt32(comboBoxPrinted.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/TypographyView/FormShopSell.resx b/TypographyView/FormShopSell.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/TypographyView/FormShopSell.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