diff --git a/TypographyBusinessLogic/OrderLogic.cs b/TypographyBusinessLogic/OrderLogic.cs index bcf03a7..54c1497 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,39 +51,28 @@ 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 ChangeStatus(OrderBindingModel model, OrderStatus status) + private void CheckModel(OrderBindingModel model) { - CheckModel(model, false); - var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); - if (element == null) + if (model == null) { - _logger.LogWarning("Read operation failed"); - return false; + throw new ArgumentNullException(nameof(model)); } - if (element.Status != status - 1) + if (model.Count <= 0) { - _logger.LogWarning("Status change operation failed"); - throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); + throw new ArgumentNullException("Количество заказов должно быть больше нуля"); } - OrderStatus oldStatus = model.Status; - model.Status = status; - if (model.Status == OrderStatus.Выдан) - model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) + if (model.Sum <= 0) { - model.Status = oldStatus; - _logger.LogWarning("Update operation failed"); - return false; + throw new ArgumentNullException("Цена заказа должна быть больше нуля"); } - return true; + _logger.LogInformation("Order. OrderId:{Id}. Sum:{Sum}", model.Id, model.Sum); } - public bool TakeOrderInWork(OrderBindingModel model) { return ChangeStatus(model, OrderStatus.Выполняется); @@ -92,33 +88,97 @@ namespace TypographyBusinessLogic.BusinessLogics return ChangeStatus(model, OrderStatus.Выдан); } - private void CheckModel(OrderBindingModel model, bool withParams = true) + public bool CheckSupply(IPrintedModel printed, int count) { - if (model == null) + if (count <= 0) { - throw new ArgumentNullException(nameof(model)); + _logger.LogWarning("Check supply operation error. Printed count < 0"); + return false; } - if (!withParams) + int Capacity = _shopStorage.GetFullList().Select(x => x.MaxCountPrinteds).Sum(); + int currentCount = _shopStorage.GetFullList().Select(x => x.ShopPrinteds.Select(y => y.Value.Item2).Sum()).Sum(); + int freeSpace = Capacity - currentCount; + + if (freeSpace < count) { - return; + _logger.LogWarning("Check supply error. No place for new Printeds"); + return false; } - if (model.PrintedId < 0) + foreach (var shop in _shopStorage.GetFullList()) { - throw new ArgumentNullException("Некорректный идентификатор закусок", nameof(model.PrintedId)); + freeSpace = shop.MaxCountPrinteds; + foreach (var doc in shop.ShopPrinteds) + { + freeSpace -= doc.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace >= count) + { + if (_shopLogic.MakeShipment(new() + { + Id = shop.Id + }, printed, count)) + { + count = 0; + } + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + else + { + if (_shopLogic.MakeShipment(new() { Id = shop.Id }, printed, freeSpace)) + count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } } - if (model.Count <= 0) + return false; + } + + private bool ChangeStatus(OrderBindingModel model, OrderStatus status) + { + var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (element == null) { - throw new ArgumentNullException("Количество закусок в заказе должно быть больше 0", nameof(model.Count)); + _logger.LogWarning("Find order failed"); + return false; } - if (model.Sum <= 0) + if (element.Status != status - 1) { - throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); + _logger.LogWarning("Status change failed"); + throw new InvalidOperationException("Невозможно перевести состояние заказа"); } - if (model.Count <= 0) + if (status == OrderStatus.Готов) { - throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count)); + var printed = _printedStorage.GetElement(new PrintedSearchModel() { Id = model.PrintedId }); + if (printed == null) + { + _logger.LogWarning("Status change error. Printed not found"); + return false; + } + if (!CheckSupply(printed, model.Count)) + { + _logger.LogWarning("Status change error. Shop doesnt have printedes"); + return false; + } } - _logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id); + model.Status = status; + if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; + _orderStorage.Update(model); + return true; } } } diff --git a/TypographyBusinessLogic/ShopLogic.cs b/TypographyBusinessLogic/ShopLogic.cs index a9eea1d..1ea282c 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 new file mode 100644 index 0000000..81e711f --- /dev/null +++ b/TypographyFileImplement/DataFileSingleton.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using TypographyFileImplement.Models; + + +namespace TypographyFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + + private readonly string ComponentFileName = "Component.xml"; + + 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() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + + 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) + { + 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); + } + } + } +} diff --git a/TypographyFileImplement/Implements/ComponentStorage.cs b/TypographyFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..50705ab --- /dev/null +++ b/TypographyFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,73 @@ +using TypographyContracts.BindingModels; +using TypographyContracts.SearchModels; +using TypographyContracts.StoragesContracts; +using TypographyContracts.ViewModels; +using TypographyFileImplement.Models; + + +namespace TypographyFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/TypographyFileImplement/Implements/OrderStorage.cs b/TypographyFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..2a79e63 --- /dev/null +++ b/TypographyFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,83 @@ +using TypographyContracts.StoragesContracts; +using TypographyContracts.BindingModels; +using TypographyContracts.SearchModels; +using TypographyContracts.ViewModels; +using TypographyFileImplement.Models; + +namespace TypographyFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return GetViewModel(source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return GetViewModel(newOrder); + } + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return GetViewModel(order); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return GetViewModel(element); + } + return null; + } + + public OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var printed = source.Printeds.FirstOrDefault(x => x.Id == order.PrintedId); + if (printed != null) + { + viewModel.PrintedName = printed.PrintedName; + } + return viewModel; + } + } +} diff --git a/TypographyFileImplement/Implements/PrintedStorage.cs b/TypographyFileImplement/Implements/PrintedStorage.cs new file mode 100644 index 0000000..5b842bc --- /dev/null +++ b/TypographyFileImplement/Implements/PrintedStorage.cs @@ -0,0 +1,82 @@ +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 TypographyFileImplement.Models; + + +namespace TypographyFileImplement.Implements +{ + public class PrintedStorage : IPrintedStorage + { + private readonly DataFileSingleton source; + public PrintedStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Printeds.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(PrintedSearchModel model) + { + if (string.IsNullOrEmpty(model.PrintedName)) + { + return new(); + } + return source.Printeds.Where(x => x.PrintedName.Contains(model.PrintedName)).Select(x => x.GetViewModel).ToList(); + } + + public PrintedViewModel? GetElement(PrintedSearchModel model) + { + if (string.IsNullOrEmpty(model.PrintedName) && !model.Id.HasValue) + { + return null; + } + return source.Printeds.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PrintedName) && + x.PrintedName == model.PrintedName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public PrintedViewModel? Insert(PrintedBindingModel model) + { + model.Id = source.Printeds.Count > 0 ? source.Printeds.Max(x => x.Id) + 1 : 1; + var newPrinted = Printed.Create(model); + if (newPrinted == null) + { + return null; + } + source.Printeds.Add(newPrinted); + source.SavePrinteds(); + return newPrinted.GetViewModel; + } + public PrintedViewModel? Update(PrintedBindingModel model) + { + var printed = source.Printeds.FirstOrDefault(x => x.Id == model.Id); + if (printed == null) + { + return null; + } + printed.Update(model); + source.SavePrinteds(); + return printed.GetViewModel; + } + public PrintedViewModel? Delete(PrintedBindingModel model) + { + var element = source.Printeds.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Printeds.Remove(element); + source.SavePrinteds(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/TypographyFileImplement/Implements/ShopStorage.cs b/TypographyFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..5b8688b --- /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 = source.Shops + .SelectMany(x => x.ShopPrinteds) + .Where(y => y.Key == model.Id) + .Sum(y => y.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/Component.cs b/TypographyFileImplement/Models/Component.cs new file mode 100644 index 0000000..bfc5292 --- /dev/null +++ b/TypographyFileImplement/Models/Component.cs @@ -0,0 +1,64 @@ +using TypographyContracts.BindingModels; +using TypographyContracts.ViewModels; +using TypographyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace TypographyFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); + } +} diff --git a/TypographyFileImplement/Models/Order.cs b/TypographyFileImplement/Models/Order.cs new file mode 100644 index 0000000..437beda --- /dev/null +++ b/TypographyFileImplement/Models/Order.cs @@ -0,0 +1,88 @@ +using TypographyContracts.BindingModels; +using TypographyContracts.ViewModels; +using TypographyDataModels.Enums; +using TypographyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace TypographyFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int PrintedId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PrintedId = model.PrintedId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PrintedId = Convert.ToInt32(element.Element("PrintedId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : + Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + PrintedId = PrintedId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + public XElement GetXElement => + new("Order", + new XAttribute("Id", Id), + new XElement("PrintedId", PrintedId.ToString()), + new XElement("Count", PrintedId.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString()) + ); + } +} diff --git a/TypographyFileImplement/Models/Printed.cs b/TypographyFileImplement/Models/Printed.cs new file mode 100644 index 0000000..7ee8404 --- /dev/null +++ b/TypographyFileImplement/Models/Printed.cs @@ -0,0 +1,81 @@ +using TypographyDataModels.Models; +using TypographyContracts.BindingModels; +using TypographyContracts.ViewModels; +using System.Xml.Linq; + +namespace TypographyFileImplement.Models +{ + public class Printed : IPrintedModel + { + public int Id { get; private set; } + public string PrintedName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _srintedComponents = null; + public Dictionary PrintedComponents + { + get + { + if (_srintedComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _srintedComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _srintedComponents; + } + } + public static Printed? Create(PrintedBindingModel model) + { + if (model == null) + { + return null; + } + return new Printed() + { + Id = model.Id, + PrintedName = model.PrintedName, + Price = model.Price, + Components = model.PrintedComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Printed? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Printed() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PrintedName = element.Element("PrintedName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("PrintedComponents")!.Elements("PrintedComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(PrintedBindingModel model) + { + if (model == null) + { + return; + } + PrintedName = model.PrintedName; + Price = model.Price; + Components = model.PrintedComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _srintedComponents = null; + } + public PrintedViewModel GetViewModel => new() + { + Id = Id, + PrintedName = PrintedName, + Price = Price, + PrintedComponents = PrintedComponents + }; + public XElement GetXElement => new("Printed", + new XAttribute("Id", Id), + new XElement("PrintedName", PrintedName), + new XElement("Price", Price.ToString()), + new XElement("PrintedComponents", Components.Select(x => new XElement("PrintedComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} 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/TypographyFileImplement/TypographyFileImplement.csproj b/TypographyFileImplement/TypographyFileImplement.csproj new file mode 100644 index 0000000..95efe1f --- /dev/null +++ b/TypographyFileImplement/TypographyFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/TypographyListImplement/Implements/ShopStorage.cs b/TypographyListImplement/Implements/ShopStorage.cs index 4890152..8306c51 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 printed, 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..f075aa6 100644 --- a/TypographyView/FormShop.cs +++ b/TypographyView/FormShop.cs @@ -32,7 +32,7 @@ namespace TypographyView { if (_id.HasValue) { - _logger.LogInformation("Shop loading"); + _logger.LogInformation("Загрузка магазина"); try { var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value }); @@ -41,13 +41,14 @@ namespace TypographyView textBoxName.Text = view.ShopName; textBoxAddress.Text = view.Address; dateTimePicker.Value = view.DateOpening; + textBoxMaxCount.Text= view.MaxCountPrinteds.ToString(); _shopPrinteds = view.ShopPrinteds ?? new Dictionary(); LoadData(); } } catch (Exception ex) { - _logger.LogError(ex, "Shop loading error"); + _logger.LogError(ex, "Ошибка загрузки магазина"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -55,7 +56,7 @@ namespace TypographyView private void LoadData() { - _logger.LogInformation("Shop printeds loading"); + _logger.LogInformation("Загрузка магазина"); try { if (_shopPrinteds != null) @@ -86,12 +87,17 @@ namespace TypographyView MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (string.IsNullOrEmpty(textBoxMaxCount.Text)) + { + MessageBox.Show("Заполните макс. количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } if (string.IsNullOrEmpty(dateTimePicker.Text)) { MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _logger.LogInformation("Shop saving"); + _logger.LogInformation("Сохранение магазина"); try { var model = new ShopBindingModel @@ -99,6 +105,7 @@ namespace TypographyView Id = _id ?? 0, ShopName = textBoxName.Text, Address = textBoxAddress.Text, + MaxCountPrinteds = Convert.ToInt32(textBoxMaxCount.Text), DateOpening = dateTimePicker.Value, ShopPrinteds = _shopPrinteds }; @@ -123,5 +130,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..44dc7ae --- /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(71, 20); + labelPrinted.TabIndex = 0; + labelPrinted.Text = "Изделие:"; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(14, 51); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(93, 20); + labelCount.TabIndex = 1; + labelCount.Text = "Количество:"; + // + // 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 diff --git a/TypographyView/Program.cs b/TypographyView/Program.cs index 3bcde64..5e5658b 100644 --- a/TypographyView/Program.cs +++ b/TypographyView/Program.cs @@ -1,11 +1,12 @@ using TypographyBusinessLogic.BusinessLogics; using TypographyContracts.BusinessLogicsContracts; using TypographyContracts.StoragesContracts; -using TypographyListImplement.Implements; +using TypographyFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using TypographyBusinessLogic; +using AutomobilePlantView; namespace TypographyView { @@ -54,7 +55,7 @@ namespace TypographyView services.AddTransient(); services.AddTransient(); services.AddTransient(); - + services.AddTransient(); } } } \ No newline at end of file diff --git a/TypographyView/TypographyView.csproj b/TypographyView/TypographyView.csproj index 443c7f0..a892ebf 100644 --- a/TypographyView/TypographyView.csproj +++ b/TypographyView/TypographyView.csproj @@ -16,6 +16,7 @@ + diff --git a/TypographyView/TypographyView.sln b/TypographyView/TypographyView.sln index 71b5d33..246d0b9 100644 --- a/TypographyView/TypographyView.sln +++ b/TypographyView/TypographyView.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypographyContracts", "..\T EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypographyBusinessLogic", "..\TypographyBusinessLogic\TypographyBusinessLogic.csproj", "{1057A33D-538D-4E7F-862B-1FF8E9E021A0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypographyFileImplement", "..\TypographyFileImplement\TypographyFileImplement.csproj", "{DCBDF361-C514-4319-A542-5629650E7E8A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {1057A33D-538D-4E7F-862B-1FF8E9E021A0}.Debug|Any CPU.Build.0 = Debug|Any CPU {1057A33D-538D-4E7F-862B-1FF8E9E021A0}.Release|Any CPU.ActiveCfg = Release|Any CPU {1057A33D-538D-4E7F-862B-1FF8E9E021A0}.Release|Any CPU.Build.0 = Release|Any CPU + {DCBDF361-C514-4319-A542-5629650E7E8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DCBDF361-C514-4319-A542-5629650E7E8A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DCBDF361-C514-4319-A542-5629650E7E8A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DCBDF361-C514-4319-A542-5629650E7E8A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE