2024-05-04 17:55:29 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.BusinessLogicsContracts;
|
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDataModels.Enums;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarBusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class WorkModeling : IWorkProcess
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly Random _rnd;
|
|
|
|
|
|
|
|
|
|
private IOrderLogic? _orderLogic;
|
|
|
|
|
|
2024-05-04 18:40:09 +04:00
|
|
|
|
public WorkModeling(ILogger<WorkModeling> Logger)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
_logger = Logger;
|
2024-05-04 17:55:29 +04:00
|
|
|
|
_rnd = new Random(1000);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 21:31:27 +04:00
|
|
|
|
public void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
2024-05-22 21:31:27 +04:00
|
|
|
|
_orderLogic = orderLogic;
|
|
|
|
|
var implementers = implementerLogic.ReadList(null);
|
|
|
|
|
if (implementers == null)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("DoWork. Implementers is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-22 21:31:27 +04:00
|
|
|
|
var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят });
|
|
|
|
|
if (orders == null || orders.Count == 0)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("DoWork. Orders is null or empty");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-22 21:31:27 +04:00
|
|
|
|
_logger.LogDebug("DoWork for {Count} orders", orders.Count);
|
|
|
|
|
foreach (var implementer in implementers)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
2024-05-22 21:31:27 +04:00
|
|
|
|
Task.Run(() => WorkerWorkAsync(implementer, orders));
|
2024-05-04 17:55:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 18:40:09 +04:00
|
|
|
|
private async Task WorkerWorkAsync(ImplementerViewModel Implementer, List<OrderViewModel> Orders)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
if (_orderLogic == null || Implementer == null)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
|
|
|
|
await RunOrderInWork(Implementer);
|
2024-05-04 17:55:29 +04:00
|
|
|
|
|
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
foreach (var Order in Orders)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
_logger.LogDebug("WorkerWorkAsync. Worker {Id} try get order {Order}", Implementer.Id, Order.Id);
|
|
|
|
|
bool AcquireResult = _orderLogic.TakeOrderInWork(new OrderBindingModel
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
Id = Order.Id,
|
|
|
|
|
ImplementerId = Implementer.Id
|
2024-05-04 17:55:29 +04:00
|
|
|
|
});
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
|
|
|
|
if (!AcquireResult)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogDebug("WorkerWorkAsync. Worker {Id} tried to get order {Order} but it's already acquired by other worker", Implementer.Id, Order.Id);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(Implementer.WorkExperience * _rnd.Next(100, 1000) * Order.Count);
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("WorkerWorkAsync. Worker {Id} finish order {Order}", Implementer.Id, Order.Id);
|
2024-05-04 17:55:29 +04:00
|
|
|
|
_orderLogic.FinishOrder(new OrderBindingModel
|
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
Id = Order.Id
|
2024-05-04 17:55:29 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
2024-05-04 17:55:29 +04:00
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning(ex, "Error try get work");
|
|
|
|
|
}
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
2024-05-04 17:55:29 +04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error while do work");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(Implementer.Qualification * _rnd.Next(10, 100));
|
2024-05-04 17:55:29 +04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 18:40:09 +04:00
|
|
|
|
private async Task RunOrderInWork(ImplementerViewModel Implementer)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
if (_orderLogic == null || Implementer == null)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
return;
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
2024-05-04 17:55:29 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
var RunOrder = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
ImplementerId = Implementer.Id,
|
2024-05-04 17:55:29 +04:00
|
|
|
|
Status = OrderStatus.Выполняется
|
|
|
|
|
}));
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
|
|
|
|
if (RunOrder == null)
|
2024-05-04 17:55:29 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 18:40:09 +04:00
|
|
|
|
_logger.LogDebug("RunOrderInWork. Worker {Id} back to order {Order}", Implementer.Id, RunOrder.Id);
|
|
|
|
|
Thread.Sleep(Implementer.WorkExperience * _rnd.Next(100, 300) * RunOrder.Count);
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("RunOrderInWork. Worker {Id} finish order {Order}", Implementer.Id, RunOrder.Id);
|
2024-05-04 17:55:29 +04:00
|
|
|
|
_orderLogic.FinishOrder(new OrderBindingModel
|
|
|
|
|
{
|
2024-05-04 18:40:09 +04:00
|
|
|
|
Id = RunOrder.Id
|
2024-05-04 17:55:29 +04:00
|
|
|
|
});
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(Implementer.Qualification * _rnd.Next(10, 100));
|
2024-05-04 17:55:29 +04:00
|
|
|
|
}
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
2024-05-04 17:55:29 +04:00
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning(ex, "Error try get work");
|
|
|
|
|
}
|
2024-05-04 18:40:09 +04:00
|
|
|
|
|
2024-05-04 17:55:29 +04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error while do work");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|