2024-05-03 23:54:49 +04:00
|
|
|
|
using ConfectioneryContracts.BindingModels;
|
|
|
|
|
using ConfectioneryContracts.BusinessLogicsContracts;
|
|
|
|
|
using ConfectioneryContracts.SearchModels;
|
|
|
|
|
using ConfectioneryContracts.ViewModels;
|
|
|
|
|
using ConfectioneryDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ConfectioneryBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class WorkModeling : IWorkProcess
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly Random _rnd;
|
|
|
|
|
|
|
|
|
|
private IOrderLogic? _orderLogic;
|
|
|
|
|
|
2024-05-23 15:09:31 +04:00
|
|
|
|
public WorkModeling(ILogger<WorkModeling> Logger)
|
2024-05-03 23:54:49 +04:00
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
_logger = Logger;
|
2024-05-03 23:54:49 +04:00
|
|
|
|
_rnd = new Random(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic)
|
|
|
|
|
{
|
|
|
|
|
_orderLogic = orderLogic;
|
|
|
|
|
var implementers = implementerLogic.ReadList(null);
|
|
|
|
|
if (implementers == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("DoWork. Implementers is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
foreach (var implementer in implementers)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(() => WorkerWorkAsync(implementer, orders));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 15:09:31 +04:00
|
|
|
|
private async Task WorkerWorkAsync(ImplementerViewModel Implementer, List<OrderViewModel> Orders)
|
2024-05-03 23:54:49 +04:00
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
if (_orderLogic == null || Implementer == null)
|
2024-05-03 23:54:49 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-23 15:09:31 +04:00
|
|
|
|
|
|
|
|
|
await RunOrderInWork(Implementer);
|
2024-05-03 23:54:49 +04:00
|
|
|
|
|
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
foreach (var Order in Orders)
|
2024-05-03 23:54:49 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
_logger.LogDebug("WorkerWorkAsync. Worker {Id} try get order {Order}", Implementer.Id, Order.Id);
|
|
|
|
|
bool AcquireResult = _orderLogic.TakeOrderInWork(new OrderBindingModel
|
2024-05-03 23:54:49 +04:00
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
Id = Order.Id,
|
|
|
|
|
ImplementerId = Implementer.Id
|
2024-05-03 23:54:49 +04:00
|
|
|
|
});
|
2024-05-23 15:09:31 +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-03 23:54:49 +04:00
|
|
|
|
_orderLogic.FinishOrder(new OrderBindingModel
|
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
Id = Order.Id
|
2024-05-03 23:54:49 +04:00
|
|
|
|
});
|
2024-05-23 15:09:31 +04:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(Implementer.Qualification * _rnd.Next(10, 100));
|
2024-05-03 23:54:49 +04:00
|
|
|
|
}
|
2024-05-23 15:09:31 +04:00
|
|
|
|
|
2024-05-03 23:54:49 +04:00
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning(ex, "Error try get work");
|
|
|
|
|
}
|
2024-05-23 15:09:31 +04:00
|
|
|
|
|
2024-05-03 23:54:49 +04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error while do work");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 15:09:31 +04:00
|
|
|
|
private async Task RunOrderInWork(ImplementerViewModel Implementer)
|
2024-05-03 23:54:49 +04:00
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
if (_orderLogic == null || Implementer == null)
|
2024-05-03 23:54:49 +04:00
|
|
|
|
return;
|
2024-05-23 15:09:31 +04:00
|
|
|
|
|
2024-05-03 23:54:49 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
var RunOrder = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel
|
2024-05-03 23:54:49 +04:00
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
ImplementerId = Implementer.Id,
|
2024-05-03 23:54:49 +04:00
|
|
|
|
Status = OrderStatus.Выполняется
|
|
|
|
|
}));
|
2024-05-23 15:09:31 +04:00
|
|
|
|
|
|
|
|
|
if (RunOrder == null)
|
2024-05-03 23:54:49 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 15:09:31 +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-03 23:54:49 +04:00
|
|
|
|
_orderLogic.FinishOrder(new OrderBindingModel
|
|
|
|
|
{
|
2024-05-23 15:09:31 +04:00
|
|
|
|
Id = RunOrder.Id
|
2024-05-03 23:54:49 +04:00
|
|
|
|
});
|
2024-05-23 15:09:31 +04:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(Implementer.Qualification * _rnd.Next(10, 100));
|
2024-05-03 23:54:49 +04:00
|
|
|
|
}
|
2024-05-23 15:09:31 +04:00
|
|
|
|
|
2024-05-03 23:54:49 +04:00
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning(ex, "Error try get work");
|
|
|
|
|
}
|
2024-05-23 15:09:31 +04:00
|
|
|
|
|
2024-05-03 23:54:49 +04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error while do work");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|