ПИбд-21 Кувшинов Тимур сложная лаба 6 #23

Closed
TImourka wants to merge 4 commits from laba6complicated into laba5complicated
4 changed files with 89 additions and 19 deletions
Showing only changes of commit 46199a56e7 - Show all commits

View File

@ -148,12 +148,19 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics
_logger.LogWarning("Read operation failed");
return false;
}
if (element.Status != status - 1)
if (element.Status != status - 1 && element.Status != OrderStatus.Ожидание)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
}
if (element.ImplementerId.HasValue)
model.ImplementerId = element.ImplementerId;
if (model.CarId == 0)
model.CarId = element.CarId;
if (model.Count == 0)
model.Count = element.Count;
if (status == OrderStatus.Выдан)
{
var car = _carStorage.GetElement(new CarSearchModel() { Id = model.CarId });
@ -166,19 +173,19 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics
if (!CheckThenSupplyMany(car, model.Count))
{
_logger.LogWarning("Status change operation failed. Shop supply error.");
model.Status = OrderStatus.Ожидание;
if (_orderStorage.Update(model) == null)
_logger.LogWarning("Update operation failed");
return false;
}
}
if (element.ImplementerId.HasValue)
model.ImplementerId = element.ImplementerId;
OrderStatus oldStatus = model.Status;
model.Status = status;
if (model.Status == OrderStatus.Выдан)
model.DateImplement = DateTime.Now;
if (_orderStorage.Update(model) == null)
{
model.Status = oldStatus;
_logger.LogWarning("Update operation failed");
return false;
}

View File

@ -30,13 +30,23 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics
_logger.LogWarning("DoWork. Implementers is null");
return;
}
var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят });
if (orders == null || orders.Count == 0)
if (orders == null)
{
_logger.LogWarning("DoWork. Orders is null or empty");
_logger.LogWarning("DoWork. Orders is null");
return;
}
orders.AddRange(_orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Выполняется }) ?? new());
orders.AddRange(_orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Ожидание }) ?? new());
if (orders.Count == 0)
{
_logger.LogWarning("DoWork. Orders is empty");
return;
}
_logger.LogDebug("DoWork for {Count} orders", orders.Count);
foreach (var implementer in implementers)
{
Task.Run(() => WorkerWorkAsync(implementer, orders));
@ -54,6 +64,9 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics
{
return;
}
await RunWaitingOrder(implementer);
await RunOrderInWork(implementer);
await Task.Run(() =>
@ -76,6 +89,12 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics
{
Id = order.Id
});
// доставляем
_logger.LogDebug("DoWork. Worker {Id} delivery order {Order}", implementer.Id, order.Id);
_orderLogic.DeliveryOrder(new OrderBindingModel
{
Id = order.Id
});
}
// кто-то мог уже перехватить заказ, игнорируем ошибку
catch (InvalidOperationException ex)
@ -125,6 +144,59 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics
{
Id = runOrder.Id
});
// доставляем
_logger.LogDebug("DoWork. Worker {Id} delivery order {Order}", implementer.Id, runOrder.Id);
_orderLogic.DeliveryOrder(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;
}
}
/// <summary>
/// Ищем заказ, которые ждут выдачи (вдруг место освободилось)
/// </summary>
/// <param name="implementer"></param>
/// <returns></returns>
private async Task RunWaitingOrder(ImplementerViewModel implementer)
{
if (_orderLogic == null || implementer == null)
{
return;
}
try
{
var order = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel
{
ImplementerId = implementer.Id,
Status = OrderStatus.Ожидание
}));
if (order == null)
{
return;
}
// доставляем
_logger.LogDebug("DoWork. Worker {Id} delivery order {Order}", implementer.Id, order.Id);
_orderLogic.DeliveryOrder(new OrderBindingModel
{
Id = order.Id
});
// отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
}

View File

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomobilePlantDataModels.Enums
namespace AutomobilePlantDataModels.Enums
{
public enum OrderStatus
{
@ -12,6 +6,7 @@ namespace AutomobilePlantDataModels.Enums
Принят = 0,
Выполняется = 1,
Готов = 2,
Выдан = 3
Выдан = 3,
Ожидание = 4
}
}

View File

@ -16,10 +16,6 @@ namespace AutomobilePlantDatabaseImplement.Implements
{
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new AutomobilePlantDatabase();
return context.Orders
.Include(x => x.Car)