рабочая

This commit is contained in:
goldfest 2024-05-03 12:04:00 +04:00
parent b65ba122c0
commit 8029254363
5 changed files with 283 additions and 190 deletions

View File

@ -101,7 +101,8 @@ namespace TravelCompany.Forms
ShopName = textBoxName.Text, ShopName = textBoxName.Text,
Adress = textBoxAdress.Text, Adress = textBoxAdress.Text,
OpeningDate = dateTimeOpen.Value, OpeningDate = dateTimeOpen.Value,
TravelMaxCount = (int)numericUpTravelMaxCount.Value TravelMaxCount = (int)numericUpTravelMaxCount.Value,
ShopTravels = null
}; };
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
if (!operationResult) if (!operationResult)

View File

@ -1,15 +1,10 @@
using TravelCompanyContracts.BindingModels; using TravelCompanyDataModels.Enums;
using Microsoft.Extensions.Logging;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyContracts.BusinessLogicsContracts;
using TravelCompanyContracts.SearchModels; using TravelCompanyContracts.SearchModels;
using TravelCompanyContracts.StoragesContracts; using TravelCompanyContracts.StoragesContracts;
using TravelCompanyContracts.ViewModels; using TravelCompanyContracts.ViewModels;
using TravelCompanyDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TravelCompanyBusinessLogic.BusinessLogic namespace TravelCompanyBusinessLogic.BusinessLogic
{ {
@ -17,10 +12,9 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
static readonly object _locker = new object();
private readonly IShopStorage _shopStorage; private readonly IShopStorage _shopStorage;
static readonly object locker = new object();
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage) public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage)
{ {
_logger = logger; _logger = logger;
@ -34,7 +28,8 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
{ {
throw new ArgumentNullException(nameof(model)); throw new ArgumentNullException(nameof(model));
} }
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id); _logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model.ClientId, model.Status, model.ImplementerId, model.DateFrom, model.DateTo, model.Id);
var element = _orderStorage.GetElement(model); var element = _orderStorage.GetElement(model);
if (element == null) if (element == null)
{ {
@ -47,7 +42,8 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
public List<OrderViewModel>? ReadList(OrderSearchModel? model) public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{ {
_logger.LogInformation("ReadList. OrderId:{Id}", model?.Id); _logger.LogInformation("ReadList. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null) if (list == null)
{ {
@ -61,11 +57,9 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
public bool CreateOrder(OrderBindingModel model) public bool CreateOrder(OrderBindingModel model)
{ {
CheckModel(model); CheckModel(model);
if (model.Status != OrderStatus.Неизвестен) if (model.Status != OrderStatus.Неизвестен)
return false; return false;
model.Status = OrderStatus.Принят; model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null) if (_orderStorage.Insert(model) == null)
{ {
_logger.LogWarning("Insert operation failed"); _logger.LogWarning("Insert operation failed");
@ -76,42 +70,42 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
public bool TakeOrderInWork(OrderBindingModel model) public bool TakeOrderInWork(OrderBindingModel model)
{ {
lock (locker) lock (_locker)
{ {
return ToNextStatus(model, OrderStatus.Выполняется); return ChangeStatus(model, OrderStatus.Выполняется);
} }
} }
public bool FinishOrder(OrderBindingModel model) public bool FinishOrder(OrderBindingModel model)
{ {
return ToNextStatus(model, OrderStatus.Готов); return ChangeStatus(model, OrderStatus.Готов);
} }
public bool DeliveryOrder(OrderBindingModel model) public bool DeliveryOrder(OrderBindingModel model)
{ {
return ToNextStatus(model, OrderStatus.Выдан); lock (_locker)
}
public bool ToNextStatus(OrderBindingModel model, OrderStatus orderStatus)
{ {
CheckModel(model, false); model = FillOrderBindingModel(model);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); if (model.Status != OrderStatus.Готов && model.Status != OrderStatus.Ожидает)
if (element == null)
{ {
_logger.LogWarning("Read operation failed"); _logger.LogWarning("Changing status operation faled: Current-{Status}:required-Выдан.", model.Status);
return false; throw new InvalidOperationException($"Невозможно приствоить статус выдан заказу с текущим статусом {model.Status}");
} }
if (element.Status != orderStatus - 1) if (!_shopStorage.RestockingShops(new SupplyBindingModel
{ {
_logger.LogWarning("Status change operation failed"); TravelId = model.TravelId,
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); Count = model.Count
}))
{
if (model.Status == OrderStatus.Готов || model.Status == OrderStatus.Ожидает)
{
model.Status = OrderStatus.Ожидает;
return UpdateOrder(model);
}
}
model.Status = OrderStatus.Выдан;
return UpdateOrder(model);
} }
model.Status = orderStatus;
if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now;
if (element.ImplementerId.HasValue)
model.ImplementerId = element.ImplementerId;
_orderStorage.Update(model);
return true;
} }
private void CheckModel(OrderBindingModel model, bool withParams = true) private void CheckModel(OrderBindingModel model, bool withParams = true)
@ -126,13 +120,69 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
} }
if (model.Count <= 0) if (model.Count <= 0)
{ {
throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count)); throw new ArgumentException("Колличество пиццы в заказе не может быть меньше 1", nameof(model.Count));
} }
if (model.Sum <= 0) if (model.Sum <= 0)
{ {
throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); throw new ArgumentException("Стоимость заказа на может быть меньше 1", nameof(model.Sum));
} }
_logger.LogInformation("Travel. TravelId:{TravelId}.Count:{Count}.Sum:{Sum}Id:{Id}", model.TravelId, model.Count, model.Sum, model.Id); if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate)
{
throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}");
}
_logger.LogInformation("Travel. TravelId:{TravelId}.Count:{Count}.Sum:{Sum}Id:{Id}",
model.TravelId, model.Count, model.Sum, model.Id);
}
private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus)
{
model = FillOrderBindingModel(model);
if (requiredStatus - model.Status == 1)
{
model.Status = requiredStatus;
if (model.Status == OrderStatus.Готов)
model.DateImplement = DateTime.Now;
return UpdateOrder(model);
}
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus);
throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}");
}
private OrderBindingModel FillOrderBindingModel(OrderBindingModel model)
{
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel()
{
Id = model.Id
});
if (element == null)
{
throw new InvalidOperationException(nameof(element));
}
model.Id = element.Id;
model.DateCreate = element.DateCreate;
model.TravelId = element.TravelId;
model.DateImplement = element.DateImplement;
model.ClientId = element.ClientId;
model.Status = element.Status;
model.Count = element.Count;
model.Sum = element.Sum;
if (!model.ImplementerId.HasValue)
{
model.ImplementerId = element.ImplementerId;
}
return model;
}
private bool UpdateOrder(OrderBindingModel model)
{
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
_logger.LogWarning("Update operation sucsess");
return true;
} }
} }
} }

View File

@ -1,29 +1,27 @@
using TravelCompanyContracts.BindingModels; using Microsoft.Extensions.Logging;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyContracts.BusinessLogicsContracts;
using TravelCompanyContracts.SearchModels; using TravelCompanyContracts.SearchModels;
using TravelCompanyContracts.ViewModels; using TravelCompanyContracts.ViewModels;
using TravelCompanyDataModels.Enums; using TravelCompanyDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TravelCompanyBusinessLogic.BusinessLogic namespace TravelCompanyBusinessLogic.BusinessLogics
{ {
public class WorkModeling : IWorkProcess public class WorkModeling : IWorkProcess
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly Random _rnd; private readonly Random _rnd;
private IOrderLogic? _orderLogic; private IOrderLogic? _orderLogic;
public WorkModeling(ILogger<WorkModeling> logger) public WorkModeling(ILogger<WorkModeling> logger)
{ {
_logger = logger; _logger = logger;
_rnd = new Random(1000); _rnd = new Random(1000);
} }
public void DoWork(IImplementerLogic implementerLogic, IOrderLogic
orderLogic) public void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic)
{ {
_orderLogic = orderLogic; _orderLogic = orderLogic;
var implementers = implementerLogic.ReadList(null); var implementers = implementerLogic.ReadList(null);
@ -32,38 +30,37 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
_logger.LogWarning("DoWork. Implementers is null"); _logger.LogWarning("DoWork. Implementers is null");
return; return;
} }
var orders = _orderLogic.ReadList(new OrderSearchModel var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Выдан });
{ int count = _orderLogic.ReadList(null).Count;
Status = if (orders == null || count == orders.Count)
OrderStatus.Принят
});
if (orders == null || orders.Count == 0)
{ {
_logger.LogWarning("DoWork. Orders is null or empty"); _logger.LogWarning("DoWork. Orders is null or empty");
return; return;
} }
orders = _orderLogic.ReadList(null);
_logger.LogDebug("DoWork for {Count} orders", orders.Count); _logger.LogDebug("DoWork for {Count} orders", orders.Count);
foreach (var implementer in implementers) foreach (var implementer in implementers)
{ {
Task.Run(() => WorkerWorkAsync(implementer, orders)); Task.Run(() => WorkerWorkAsync(implementer, orders));
} }
} }
/// Иммитация работы исполнителя
private async Task WorkerWorkAsync(ImplementerViewModel implementer, private async Task WorkerWorkAsync(ImplementerViewModel implementer, List<OrderViewModel> orders)
List<OrderViewModel> orders)
{ {
if (_orderLogic == null || implementer == null) if (_orderLogic == null || implementer == null)
{ {
return; return;
} }
await DeliverWaitingOrder(implementer);
await RunOrderInWork(implementer); await RunOrderInWork(implementer);
await Task.Run(() => await Task.Run(() =>
{ {
foreach (var order in orders) foreach (var order in orders)
{ {
try try
{ {
_logger.LogDebug("DoWork. Worker {Id} try get order { Order}", implementer.Id, order.Id); _logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id);
// пытаемся назначить заказ на исполнителя // пытаемся назначить заказ на исполнителя
_orderLogic.TakeOrderInWork(new OrderBindingModel _orderLogic.TakeOrderInWork(new OrderBindingModel
{ {
@ -71,16 +68,13 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
ImplementerId = implementer.Id ImplementerId = implementer.Id
}); });
// делаем работу // делаем работу
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count);
1000) * order.Count); _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id);
_logger.LogDebug("DoWork. Worker {Id} finish order { Order}", implementer.Id, order.Id);
_orderLogic.FinishOrder(new OrderBindingModel _orderLogic.FinishOrder(new OrderBindingModel
{ {
Id = order.Id Id = order.Id
}); });
// отдыхаем _orderLogic.DeliveryOrder(new OrderBindingModel { Id = order.Id });
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
} }
// кто-то мог уже перехватить заказ, игнорируем ошибку // кто-то мог уже перехватить заказ, игнорируем ошибку
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
@ -93,10 +87,12 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
_logger.LogError(ex, "Error while do work"); _logger.LogError(ex, "Error while do work");
throw; throw;
} }
// отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
} }
}); });
} }
/// Ищем заказ, которые уже в работе (вдруг исполнителя прервали)
private async Task RunOrderInWork(ImplementerViewModel implementer) private async Task RunOrderInWork(ImplementerViewModel implementer)
{ {
if (_orderLogic == null || implementer == null) if (_orderLogic == null || implementer == null)
@ -105,8 +101,7 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
} }
try try
{ {
var runOrder = await Task.Run(() => _orderLogic.ReadElement(new var runOrder = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel
OrderSearchModel
{ {
ImplementerId = implementer.Id, ImplementerId = implementer.Id,
Status = OrderStatus.Выполняется Status = OrderStatus.Выполняется
@ -115,17 +110,16 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
{ {
return; return;
} }
_logger.LogDebug("DoWork. Worker {Id} back to order {Order}",
implementer.Id, runOrder.Id); _logger.LogDebug("DoWork. Worker {Id} back to order {Order}", implementer.Id, runOrder.Id);
// доделываем работу // доделываем работу
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * runOrder.Count);
runOrder.Count); _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id);
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}",
implementer.Id, runOrder.Id);
_orderLogic.FinishOrder(new OrderBindingModel _orderLogic.FinishOrder(new OrderBindingModel
{ {
Id = runOrder.Id Id = runOrder.Id
}); });
_orderLogic.DeliveryOrder(new OrderBindingModel { Id = runOrder.Id });
// отдыхаем // отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
} }
@ -141,5 +135,47 @@ namespace TravelCompanyBusinessLogic.BusinessLogic
throw; throw;
} }
} }
private async Task DeliverWaitingOrder(ImplementerViewModel implementer)
{
if (_orderLogic == null || implementer == null)
{
return;
}
var waitingOrders = await Task.Run(() => _orderLogic.ReadList(new OrderSearchModel
{
ImplementerId = implementer.Id,
Status = OrderStatus.Ожидает
}));
if (waitingOrders == null || waitingOrders.Count == 0)
{
return;
}
_logger.LogInformation("DeliverWaitingOrder. Find some waitig order for implementer:{id}.Count:{count}", implementer.Id, waitingOrders.Count);
foreach (var waitingOrder in waitingOrders)
{
try
{
_logger.LogInformation("DeliverWaitingOrder. Trying to deliver order id:{id}", waitingOrder.Id);
var res = _orderLogic.DeliveryOrder(new OrderBindingModel
{
Id = waitingOrder.Id
});
}
catch (ArgumentException ex)
{
_logger.LogWarning(ex, "DeliverWaitingOrder. Fault");
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Error try deliver order");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while do work");
throw;
}
}
}
} }
} }

View File

@ -6,6 +6,7 @@
Принят = 0, Принят = 0,
Выполняется = 1, Выполняется = 1,
Готов = 2, Готов = 2,
Выдан = 3, Ожидает = 3,
Выдан = 4,
} }
} }

View File

@ -75,27 +75,32 @@ namespace TravelCompanyDatabaseImplement.Models
public void UpdateTravels(TravelCompanyDataBase context, ShopBindingModel model) public void UpdateTravels(TravelCompanyDataBase context, ShopBindingModel model)
{ {
var ShopTravels = context.ShopTravels.Where(rec => rec.ShopId == model.Id).ToList(); if (model.ShopTravels == null)
if (ShopTravels != null && ShopTravels.Count > 0) return;
var shopTravels = context.ShopTravels.Where(rec =>
rec.ShopId == model.Id).ToList();
if (shopTravels != null && shopTravels.Count > 0)
{ {
context.ShopTravels.RemoveRange(ShopTravels.Where(rec => !model.ShopTravels.ContainsKey(rec.TravelId))); context.ShopTravels.RemoveRange(shopTravels.Where(rec
=> !model.ShopTravels.ContainsKey(rec.TravelId)));
context.SaveChanges(); context.SaveChanges();
ShopTravels = context.ShopTravels.Where(rec => rec.ShopId == model.Id).ToList(); foreach (var updateTravel in shopTravels)
foreach (var updateTravel in ShopTravels)
{ {
updateTravel.Count = model.ShopTravels[updateTravel.TravelId].Item2; updateTravel.Count =
model.ShopTravels[updateTravel.TravelId].Item2;
model.ShopTravels.Remove(updateTravel.TravelId); model.ShopTravels.Remove(updateTravel.TravelId);
} }
context.SaveChanges(); context.SaveChanges();
} }
var shop = context.Shops.First(x => x.Id == Id); var shop = context.Shops.First(x => x.Id == Id);
foreach (var ar in model.ShopTravels) foreach (var pc in model.ShopTravels)
{ {
context.ShopTravels.Add(new ShopTravels context.ShopTravels.Add(new ShopTravels
{ {
Shop = shop, Shop = shop,
Travel = context.Travels.First(x => x.Id == ar.Key), Travel = context.Travels.First(x => x.Id == pc.Key),
Count = ar.Value.Item2 Count = pc.Value.Item2
}); });
context.SaveChanges(); context.SaveChanges();
} }