diff --git a/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/OrderLogic.cs b/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/OrderLogic.cs index 0d0dabb..88da590 100644 --- a/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/OrderLogic.cs @@ -211,7 +211,7 @@ namespace AircraftPlantBusinessLogic.BusinessLogics model.Status = newStatus; - if (newStatus == OrderStatus.Выдан) + if (model.Status == OrderStatus.Готов || element.Status == OrderStatus.Ожидание) { var plane = _planeStorage.GetElement(new PlaneSearchModel { Id = element.PlaneId }); if (plane == null) @@ -219,27 +219,22 @@ namespace AircraftPlantBusinessLogic.BusinessLogics _logger.LogWarning("Status change error. Plane not found"); return false; } + if (!CheckSupply(plane, element.Count)) { - _logger.LogWarning("Status change error. Shop is overflowed"); - model.Status = OrderStatus.Ожидание; - if (_orderStorage.Update(model) == null) - { - _logger.LogWarning("Change status operation failed"); - } - return false; + _logger.LogWarning("Status change error. Shop is overflowed"); + } + else + { + model.DateImplement = DateTime.Now; } - } - - if (model.Status == OrderStatus.Выдан) - { - model.DateImplement = DateTime.Now; } else { model.DateImplement = element.DateImplement; } + CheckModel(model, false); if (_orderStorage.Update(model) == null) { diff --git a/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/WorkModeling.cs b/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/WorkModeling.cs index 10f540a..7aba89f 100644 --- a/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/WorkModeling.cs +++ b/AircraftPlant/AircraftPlantBusinessLogic/BusinessLogics/WorkModeling.cs @@ -27,10 +27,13 @@ namespace AircraftPlantBusinessLogic.BusinessLogics /// private readonly Random _rnd; - /// - /// Бизнес-логика для заказов - /// - private IOrderLogic? _orderLogic; + private readonly object orderLock = new object(); + private readonly object waitingOrderLock = new object(); + + /// + /// Бизнес-логика для заказов + /// + private IOrderLogic? _orderLogic; /// /// Конструктор @@ -58,13 +61,8 @@ namespace AircraftPlantBusinessLogic.BusinessLogics } var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят }); - if (orders == null || orders.Count == 0) - { - _logger.LogWarning("DoWork. Orders is null or empty"); - return; - } - _logger.LogDebug("DoWork for {Count} orders", orders.Count); + _logger.LogDebug("DoWork for {Count} orders", orders?.Count); foreach (var implementer in implementers) { Task.Run(() => WorkerWorkAsync(implementer, orders)); @@ -95,17 +93,20 @@ namespace AircraftPlantBusinessLogic.BusinessLogics try { _logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id); - // пытаемся назначить заказ на исполнителя - _orderLogic.TakeOrderInWork(new OrderBindingModel - { - Id = order.Id, - ImplementerId = implementer.Id - }); + // пытаемся назначить заказ на исполнителя + lock (orderLock) + { + _orderLogic.TakeOrderInWork(new OrderBindingModel + { + Id = order.Id, + ImplementerId = implementer.Id + }); + } - // делаем работу - Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count); + // делаем работу + Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count); _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id); - _orderLogic.FinishOrder(new OrderBindingModel + _orderLogic.DeliveryOrder(new OrderBindingModel { Id = order.Id }); @@ -155,7 +156,7 @@ namespace AircraftPlantBusinessLogic.BusinessLogics // доделываем работу Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * runOrder.Count); _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id); - _orderLogic.FinishOrder(new OrderBindingModel + _orderLogic.DeliveryOrder(new OrderBindingModel { Id = runOrder.Id }); @@ -203,10 +204,13 @@ namespace AircraftPlantBusinessLogic.BusinessLogics foreach (var order in orders) { _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id); - _orderLogic.FinishOrder(new OrderBindingModel - { - Id = order.Id - }); + lock (waitingOrderLock) + { + _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = order.Id + }); + } // отдыхаем Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));