рабочая

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,8 +101,9 @@ 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);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element == null)
{ {
_logger.LogWarning("Read operation failed"); model = FillOrderBindingModel(model);
return false; if (model.Status != OrderStatus.Готов && model.Status != OrderStatus.Ожидает)
{
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-Выдан.", model.Status);
throw new InvalidOperationException($"Невозможно приствоить статус выдан заказу с текущим статусом {model.Status}");
}
if (!_shopStorage.RestockingShops(new SupplyBindingModel
{
TravelId = model.TravelId,
Count = model.Count
}))
{
if (model.Status == OrderStatus.Готов || model.Status == OrderStatus.Ожидает)
{
model.Status = OrderStatus.Ожидает;
return UpdateOrder(model);
}
}
model.Status = OrderStatus.Выдан;
return UpdateOrder(model);
} }
if (element.Status != orderStatus - 1)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
}
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,145 +1,181 @@
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 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;
}
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));
}
}
/// Иммитация работы исполнителя
private async Task WorkerWorkAsync(ImplementerViewModel implementer,
List<OrderViewModel> orders)
{
if (_orderLogic == null || implementer == null)
{
return;
}
await RunOrderInWork(implementer);
await Task.Run(() =>
{
foreach (var order in orders)
{
try
{
_logger.LogDebug("DoWork. Worker {Id} try get order { Order}", implementer.Id, order.Id);
// пытаемся назначить заказ на исполнителя
_orderLogic.TakeOrderInWork(new OrderBindingModel
{
Id = order.Id,
ImplementerId = implementer.Id
});
// делаем работу
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100,
1000) * order.Count);
_logger.LogDebug("DoWork. Worker {Id} finish order { Order}", implementer.Id, order.Id);
_orderLogic.FinishOrder(new OrderBindingModel private readonly Random _rnd;
{
Id = order.Id private IOrderLogic? _orderLogic;
});
// отдыхаем public WorkModeling(ILogger<WorkModeling> logger)
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); {
} _logger = logger;
// кто-то мог уже перехватить заказ, игнорируем ошибку _rnd = new Random(1000);
catch (InvalidOperationException ex) }
{
_logger.LogWarning(ex, "Error try get work"); public void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic)
} {
// заканчиваем выполнение имитации в случае иной ошибки _orderLogic = orderLogic;
catch (Exception ex) var implementers = implementerLogic.ReadList(null);
{ if (implementers == null)
_logger.LogError(ex, "Error while do work"); {
throw; _logger.LogWarning("DoWork. Implementers is null");
} return;
} }
}); var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Выдан });
} int count = _orderLogic.ReadList(null).Count;
/// Ищем заказ, которые уже в работе (вдруг исполнителя прервали) if (orders == null || count == orders.Count)
private async Task RunOrderInWork(ImplementerViewModel implementer) {
{ _logger.LogWarning("DoWork. Orders is null or empty");
if (_orderLogic == null || implementer == null) return;
{ }
return; orders = _orderLogic.ReadList(null);
} _logger.LogDebug("DoWork for {Count} orders", orders.Count);
try foreach (var implementer in implementers)
{ {
var runOrder = await Task.Run(() => _orderLogic.ReadElement(new Task.Run(() => WorkerWorkAsync(implementer, orders));
OrderSearchModel }
{ }
ImplementerId = implementer.Id,
Status = OrderStatus.Выполняется private async Task WorkerWorkAsync(ImplementerViewModel implementer, List<OrderViewModel> orders)
})); {
if (runOrder == null) if (_orderLogic == null || implementer == null)
{ {
return; return;
} }
_logger.LogDebug("DoWork. Worker {Id} back to order {Order}", await DeliverWaitingOrder(implementer);
implementer.Id, runOrder.Id); await RunOrderInWork(implementer);
// доделываем работу
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * await Task.Run(() =>
runOrder.Count); {
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", foreach (var order in orders)
implementer.Id, runOrder.Id); {
_orderLogic.FinishOrder(new OrderBindingModel try
{ {
Id = runOrder.Id _logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id);
}); // пытаемся назначить заказ на исполнителя
// отдыхаем _orderLogic.TakeOrderInWork(new OrderBindingModel
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); {
} Id = order.Id,
// заказа может не быть, просто игнорируем ошибку ImplementerId = implementer.Id
catch (InvalidOperationException ex) });
{ // делаем работу
_logger.LogWarning(ex, "Error try get work"); Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count);
} _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id);
// а может возникнуть иная ошибка, тогда просто заканчиваем выполнение имитации _orderLogic.FinishOrder(new OrderBindingModel
catch (Exception ex) {
{ Id = order.Id
_logger.LogError(ex, "Error while do work"); });
throw; _orderLogic.DeliveryOrder(new OrderBindingModel { Id = order.Id });
} }
} // кто-то мог уже перехватить заказ, игнорируем ошибку
} catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Error try get work");
}
// заканчиваем выполнение имитации в случае иной ошибки
catch (Exception ex)
{
_logger.LogError(ex, "Error while do work");
throw;
}
// отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
}
});
}
private async Task RunOrderInWork(ImplementerViewModel implementer)
{
if (_orderLogic == null || implementer == null)
{
return;
}
try
{
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);
// доделываем работу
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * runOrder.Count);
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id);
_orderLogic.FinishOrder(new OrderBindingModel
{
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;
}
}
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();
} }