From 4faa4cd16b809877f26a1cac60dfa44fa515b66f Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Fri, 21 Apr 2023 18:24:38 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/OrderLogic.cs | 20 +++++---- .../BusinessLogic/ShopLogic.cs | 1 + .../BusinessLogic/WorkModeling.cs | 42 ++++++++++++++++++- .../Implements/OrderStorage.cs | 6 +-- .../Models/Shop.cs | 15 ++++++- .../Implements/OrderStorage.cs | 26 ++++++++---- .../Implements/OrderStorage.cs | 37 +++++++++------- 7 files changed, 107 insertions(+), 40 deletions(-) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs index eee779b..e762df8 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -40,7 +40,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic //list хранит весь список в случае, если model пришло со значением null на вход метода var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); - if(list == null) + if (list == null) { _logger.LogWarning("ReadList return null list"); @@ -90,8 +90,8 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic if(_orderStorage.Insert(model) == null) { - model.Status = OrderStatus.Неизвестен; _logger.LogWarning("Insert operation failed"); + return false; } @@ -171,7 +171,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic } //проверка на возможность обновления статуса на следующий - if (viewModel.Status + 1 != newOrderStatus) + if (viewModel.Status + 1 != newOrderStatus && viewModel.Status != OrderStatus.Ожидание) { _logger.LogWarning("Status update operation failed. New status " + newOrderStatus.ToString() + " incorrect"); return false; @@ -186,10 +186,8 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic } //проверка на выдачу - if (model.Status == OrderStatus.Выдан) - { - model.DateImplement = DateTime.Now; - + if (model.Status == OrderStatus.Готов || viewModel.Status == OrderStatus.Ожидание) + { var manufacture = _manufactureStorage.GetElement(new() { Id = viewModel.ManufactureId }); if (manufacture == null) @@ -199,7 +197,13 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic if (!_shopLogic.AddManufactures(manufacture, viewModel.Count)) { + model.Status = OrderStatus.Ожидание; + throw new Exception($"AddManufactures operation failed. Shop is full."); + } + else + { + model.DateImplement = DateTime.Now; } } else @@ -212,8 +216,6 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic //финальная проверка на возможность обновления if (_orderStorage.Update(model) == null) { - model.Status--; - _logger.LogWarning("Update operation failed"); return false; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs index b86e500..aaf49ec 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs @@ -78,6 +78,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic } _logger.LogInformation("AddManufacture. ShopName:{ShopName}. Id: {Id}", model?.ShopName, model?.Id); + var shop = _shopStorage.GetElement(model); if (shop == null) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/WorkModeling.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/WorkModeling.cs index f0c013f..b798760 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/WorkModeling.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/WorkModeling.cs @@ -64,7 +64,45 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic return; } - await RunOrderInWork(implementer); + await Task.Run(() => + { + try + { + var runOrder = _orderLogic.ReadElement(new OrderSearchModel + { + ImplementerId = implementer.Id, + Status = OrderStatus.Ожидание + }); + + if (runOrder == null) + { + return; + } + + _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id); + + _orderLogic.FinishOrder(new OrderBindingModel + { + Id = runOrder.Id + }); + + // отдыхаем + Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); + } + // заказа может не быть, просто игнорируем ошибку + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Error try get work"); + } + // а может возникнуть иная ошибка, тогда просто заканчиваем выполнение имитации + catch (Exception ex) + { + _logger.LogError(ex, "Error while do work"); + throw; + } + }); + + await RunOrderInWork(implementer); await Task.Run(() => { @@ -157,5 +195,5 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic throw; } } - } + } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs index 10b750e..bc002f2 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -150,11 +150,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements { using var context = new BlacksmithWorkshopDatabase(); - var order = context.Orders - .Include(x => x.Manufacture) - .Include(x => x.Client) - .Include(x => x.Implementer) - .FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); if (order == null) { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs index d529c67..c74dac1 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs @@ -36,8 +36,21 @@ namespace BlacksmithWorkshopDatabaseImplement.Models { if (_shopManufactures == null) { - _shopManufactures = Manufactures.ToDictionary(recSI => recSI.ManufactureId, recSI => (recSI.Manufacture as IManufactureModel, recSI.Count)); + _shopManufactures = new(); + + Manufactures.ForEach(x => + { + if (_shopManufactures.ContainsKey(x.ManufactureId)) + { + _shopManufactures[x.ManufactureId] = (x.Manufacture as IManufactureModel, _shopManufactures[x.ManufactureId].Item2 + x.Count); + } + else + { + _shopManufactures[x.ManufactureId] = (x.Manufacture as IManufactureModel, x.Count); + } + }); } + return _shopManufactures; } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs index 7bdaac1..d62eeea 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs @@ -34,13 +34,6 @@ namespace BlacksmithWorkshopFileImplement.Implements .Select(x => GetViewModel(x)) .ToList(); } - else if (model.Id.HasValue) - { - return source.Orders - .Where(x => x.Id == model.Id) - .Select(x => GetViewModel(x)) - .ToList(); - } else if (model.ClientId.HasValue) { return source.Orders @@ -56,11 +49,28 @@ namespace BlacksmithWorkshopFileImplement.Implements .ToList(); } - return new(); + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => GetViewModel(x)) + .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) { + if (model.ImplementerId.HasValue && model.Status.HasValue) + { + return source.Orders + .FirstOrDefault(x => x.ImplementerId == model.ImplementerId && x.Status == model.Status) + ?.GetViewModel; + } + + if (model.ImplementerId.HasValue) + { + return source.Orders + .FirstOrDefault(x => x.ImplementerId == model.ImplementerId) + ?.GetViewModel; + } + if (!model.Id.HasValue) { return null; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs index 411cf4b..2bc0602 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs @@ -39,10 +39,10 @@ namespace BlacksmithWorkshopListImplement.Implements //получение отфильтрованного списка заказов public List GetFilteredList(OrderSearchModel model) { + var result = new List(); + if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) { - var result = new List(); - foreach (var order in _source.Orders) { if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) @@ -53,20 +53,8 @@ namespace BlacksmithWorkshopListImplement.Implements return result; } - else if (model.Id.HasValue) - { - foreach (var order in _source.Orders) - { - if (order.Id == model.Id) - { - return new() { GetViewModel(order) }; - } - } - } else if (model.ClientId.HasValue) { - var result = new List(); - foreach (var order in _source.Orders) { if (order.ClientId == model.ClientId) @@ -77,8 +65,27 @@ namespace BlacksmithWorkshopListImplement.Implements return result; } + else if (model.Status.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.Status == model.Status) + { + result.Add(GetViewModel(order)); + } + } + return result; + } - return new(); + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(GetViewModel(order)); + } + } + + return result; } //получение элемента из списка заказов