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; } //получение элемента из списка заказов