2023-04-09 23:59:17 +04:00
|
|
|
|
using AutomobilePlantContracts.BindingModels;
|
|
|
|
|
using AutomobilePlantContracts.BusinessLogicContracts;
|
|
|
|
|
using AutomobilePlantContracts.SearchModels;
|
|
|
|
|
using AutomobilePlantContracts.ViewModels;
|
|
|
|
|
using AutomobilePlantDataModels.Enums;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AutomobilePlantBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class WorkModeling : IWorkProcess
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly Random _rnd;
|
|
|
|
|
|
|
|
|
|
private IOrderLogic? _orderLogic;
|
|
|
|
|
|
|
|
|
|
public WorkModeling(ILogger<WorkModeling> logger)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_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;
|
|
|
|
|
}
|
2023-04-20 21:39:36 +04:00
|
|
|
|
var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят });
|
2023-04-24 01:40:32 +04:00
|
|
|
|
if (orders != null)
|
|
|
|
|
{
|
|
|
|
|
orders.AddRange(_orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Выполняется }) ?? new());
|
|
|
|
|
orders.AddRange(_orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Ожидание }) ?? new());
|
|
|
|
|
}
|
2023-04-20 21:39:36 +04:00
|
|
|
|
if (orders == null || orders.Count == 0)
|
2023-04-09 23:59:17 +04:00
|
|
|
|
{
|
|
|
|
|
_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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task WorkerWorkAsync(ImplementerViewModel implementer, List<OrderViewModel> orders)
|
|
|
|
|
{
|
|
|
|
|
if (_orderLogic == null || implementer == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-24 01:40:32 +04:00
|
|
|
|
await RunWaitingOrder(implementer);
|
|
|
|
|
|
|
|
|
|
await RunOrderInWork(implementer);
|
2023-04-09 23:59:17 +04:00
|
|
|
|
|
2023-04-20 21:39:36 +04:00
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
foreach (var order in orders)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id);
|
|
|
|
|
// пытаемся назначить заказ на исполнителя
|
|
|
|
|
if (_orderLogic.TakeOrderInWork(new OrderBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = order.Id,
|
|
|
|
|
ImplementerId = implementer.Id
|
|
|
|
|
}))
|
|
|
|
|
{
|
2023-04-10 10:11:26 +04:00
|
|
|
|
// делаем работу
|
2023-04-20 21:39:36 +04:00
|
|
|
|
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count);
|
2023-04-10 10:11:26 +04:00
|
|
|
|
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id);
|
|
|
|
|
_orderLogic.DeliveryOrder(new OrderBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = order.Id,
|
|
|
|
|
ImplementerId = implementer.Id
|
|
|
|
|
});
|
|
|
|
|
// отдыхаем
|
2023-04-20 21:39:36 +04:00
|
|
|
|
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
|
2023-04-10 10:11:26 +04:00
|
|
|
|
}
|
2023-04-20 21:39:36 +04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// кто-то мог уже перехватить заказ, игнорируем ошибку
|
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning(ex, "Error try get work");
|
|
|
|
|
}
|
|
|
|
|
// заканчиваем выполнение имитации в случае иной ошибки
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error while do work");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-09 23:59:17 +04:00
|
|
|
|
|
2023-04-23 16:03:47 +04:00
|
|
|
|
private async Task RunOrderInWork(ImplementerViewModel implementer)
|
2023-04-09 23:59:17 +04:00
|
|
|
|
{
|
2023-04-23 16:03:47 +04:00
|
|
|
|
if (_orderLogic == null || implementer == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-24 01:40:32 +04:00
|
|
|
|
// Выбираем из всех заказов тот, который выполняется данным исполнителем
|
2023-04-23 16:03:47 +04:00
|
|
|
|
var runOrder = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel
|
|
|
|
|
{
|
|
|
|
|
ImplementerId = implementer.Id,
|
|
|
|
|
Status = OrderStatus.Выполняется
|
|
|
|
|
}));
|
|
|
|
|
if (runOrder == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogDebug("DoWork. Worker {Id} back to order {Order}", implementer.Id, runOrder.Id);
|
2023-04-10 10:11:26 +04:00
|
|
|
|
// доделываем работу
|
2023-04-23 16:03:47 +04:00
|
|
|
|
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * runOrder.Count);
|
|
|
|
|
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id);
|
|
|
|
|
_orderLogic.DeliveryOrder(new OrderBindingModel
|
|
|
|
|
{
|
2023-04-24 01:40:32 +04:00
|
|
|
|
Id = runOrder.Id,
|
|
|
|
|
ImplementerId = implementer.Id
|
2023-04-23 16:03:47 +04:00
|
|
|
|
});
|
2023-04-10 10:11:26 +04:00
|
|
|
|
// отдыхаем
|
2023-04-23 16:03:47 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-24 01:40:32 +04:00
|
|
|
|
|
|
|
|
|
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} finish order {Order}", implementer.Id, order.Id);
|
|
|
|
|
_orderLogic.DeliveryOrder(new OrderBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = order.Id,
|
|
|
|
|
ImplementerId = implementer.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 23:59:17 +04:00
|
|
|
|
}
|