done hard6

This commit is contained in:
Вячеслав Иванов 2024-03-28 22:09:40 +04:00
parent d8b075f8eb
commit 3bd5643b10
5 changed files with 112 additions and 50 deletions

View File

@ -13,7 +13,6 @@ namespace PizzeriaBusinessLogic.BusinessLogics
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
static readonly object _locker = new object(); static readonly object _locker = new object();
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
private readonly IShopStorage _shopStorage; private readonly IShopStorage _shopStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage) public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage)
@ -84,24 +83,29 @@ namespace PizzeriaBusinessLogic.BusinessLogics
public bool DeliveryOrder(OrderBindingModel model) public bool DeliveryOrder(OrderBindingModel model)
{ {
var order = _orderStorage.GetElement(new OrderSearchModel lock (_locker)
{ {
Id = model.Id, model = FillOrderBindingModel(model);
}); if (model.Status != OrderStatus.Готов && model.Status != OrderStatus.Ожидает)
if (order == null)
{ {
throw new ArgumentNullException(nameof(order)); _logger.LogWarning("Changing status operation faled: Current-{Status}:required-Выдан.", model.Status);
throw new InvalidOperationException($"Невозможно приствоить статус выдан заказу с текущим статусом {model.Status}");
} }
if (!_shopStorage.RestockingShops(new SupplyBindingModel if (!_shopStorage.RestockingShops(new SupplyBindingModel
{ {
PizzaId = order.PizzaId, PizzaId = model.PizzaId,
Count = order.Count Count = model.Count
})) }))
{ {
throw new ArgumentException("Недостаточно места"); if (model.Status == OrderStatus.Готов || model.Status == OrderStatus.Ожидает)
{
model.Status = OrderStatus.Ожидает;
return UpdateOrder(model);
}
}
model.Status = OrderStatus.Выдан;
return UpdateOrder(model);
} }
return ChangeStatus(model, OrderStatus.Выдан);
} }
private void CheckModel(OrderBindingModel model, bool withParams = true) private void CheckModel(OrderBindingModel model, bool withParams = true)
@ -129,8 +133,22 @@ namespace PizzeriaBusinessLogic.BusinessLogics
_logger.LogInformation("Pizza. PizzaId:{PizzaId}.Count:{Count}.Sum:{Sum}Id:{Id}", _logger.LogInformation("Pizza. PizzaId:{PizzaId}.Count:{Count}.Sum:{Sum}Id:{Id}",
model.PizzaId, model.Count, model.Sum, model.Id); model.PizzaId, model.Count, model.Sum, model.Id);
} }
private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus) 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); CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel() var element = _orderStorage.GetElement(new OrderSearchModel()
@ -141,33 +159,30 @@ namespace PizzeriaBusinessLogic.BusinessLogics
{ {
throw new InvalidOperationException(nameof(element)); throw new InvalidOperationException(nameof(element));
} }
model.Id = element.Id;
model.DateCreate = element.DateCreate; model.DateCreate = element.DateCreate;
model.PizzaId = element.PizzaId; model.PizzaId = element.PizzaId;
model.DateImplement = element.DateImplement; model.DateImplement = element.DateImplement;
model.ClientId = element.ClientId; model.ClientId = element.ClientId;
model.Status = element.Status;
model.Count = element.Count;
model.Sum = element.Sum;
if (!model.ImplementerId.HasValue) if (!model.ImplementerId.HasValue)
{ {
model.ImplementerId = element.ImplementerId; model.ImplementerId = element.ImplementerId;
} }
model.Status = element.Status; return model;
model.Count = element.Count;
model.Sum = element.Sum;
if (requiredStatus - model.Status == 1)
{
model.Status = requiredStatus;
if (model.Status == OrderStatus.Готов)
{
model.DateImplement = DateTime.Now;
} }
private bool UpdateOrder(OrderBindingModel model)
{
if (_orderStorage.Update(model) == null) if (_orderStorage.Update(model) == null)
{ {
_logger.LogWarning("Update operation failed"); _logger.LogWarning("Update operation failed");
return false; return false;
} }
_logger.LogWarning("Update operation sucsess");
return true; return true;
} }
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus);
throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}");
}
} }
} }

View File

@ -30,12 +30,14 @@ namespace PizzeriaBusinessLogic.BusinessLogics
_logger.LogWarning("DoWork. Implementers is null"); _logger.LogWarning("DoWork. Implementers is null");
return; return;
} }
var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят }); var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Выдан });
if (orders == null || orders.Count == 0) int count = _orderLogic.ReadList(null).Count;
if (orders == null || count == orders.Count)
{ {
_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)
{ {
@ -49,6 +51,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics
{ {
return; return;
} }
await DeliverWaitingOrder(implementer);
await RunOrderInWork(implementer); await RunOrderInWork(implementer);
await Task.Run(() => await Task.Run(() =>
@ -71,6 +74,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics
{ {
Id = order.Id Id = order.Id
}); });
_orderLogic.DeliveryOrder(new OrderBindingModel { Id = order.Id });
} }
// кто-то мог уже перехватить заказ, игнорируем ошибку // кто-то мог уже перехватить заказ, игнорируем ошибку
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
@ -115,6 +119,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics
{ {
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));
} }
@ -130,5 +135,47 @@ namespace PizzeriaBusinessLogic.BusinessLogics
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

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

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore; using PizzeriaDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace PizzeriaDatabaseImplement namespace PizzeriaDatabaseImplement
{ {

View File

@ -39,10 +39,6 @@
this.transactionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.transactionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.продажаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.продажаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.отчётыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.отчётыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.componentsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.componentPizzaToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.ordersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.запускРаботToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.изделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.изделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.списокИзделийToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.списокИзделийToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.пиццаСИнгридиентамиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.пиццаСИнгридиентамиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@ -52,6 +48,10 @@
this.заказыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.заказыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.заказыToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.заказыToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.заказыПоГруппамToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.заказыПоГруппамToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.componentsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.componentPizzaToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.ordersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.запускРаботToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.dataGridView = new System.Windows.Forms.DataGridView(); this.dataGridView = new System.Windows.Forms.DataGridView();
this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonCreateOrder = new System.Windows.Forms.Button();
this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); this.buttonTakeOrderInWork = new System.Windows.Forms.Button();
@ -67,8 +67,7 @@
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.bookToolStripMenuItem, this.bookToolStripMenuItem,
this.отчётыToolStripMenuItem, this.запускРаботToolStripMenuItem,
this.запускРаботToolStripMenuItem});
this.operationToolStripMenuItem, this.operationToolStripMenuItem,
this.отчётыToolStripMenuItem}); this.отчётыToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Location = new System.Drawing.Point(0, 0);
@ -83,8 +82,7 @@
this.bookToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.bookToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ingridientsToolStripMenuItem, this.ingridientsToolStripMenuItem,
this.pizzasToolStripMenuItem, this.pizzasToolStripMenuItem,
this.клиентToolStripMenuItem, this.исполнителиToolStripMenuItem,
this.исполнителиToolStripMenuItem});
this.shopsToolStripMenuItem, this.shopsToolStripMenuItem,
this.клиентToolStripMenuItem}); this.клиентToolStripMenuItem});
this.bookToolStripMenuItem.Name = "bookToolStripMenuItem"; this.bookToolStripMenuItem.Name = "bookToolStripMenuItem";
@ -346,8 +344,6 @@
private ToolStripMenuItem componentPizzaToolStripMenuItem1; private ToolStripMenuItem componentPizzaToolStripMenuItem1;
private ToolStripMenuItem ordersToolStripMenuItem; private ToolStripMenuItem ordersToolStripMenuItem;
private ToolStripMenuItem клиентToolStripMenuItem; private ToolStripMenuItem клиентToolStripMenuItem;
private ToolStripMenuItem исполнителиToolStripMenuItem;
private ToolStripMenuItem запускРаботToolStripMenuItem;
private ToolStripMenuItem shopsToolStripMenuItem; private ToolStripMenuItem shopsToolStripMenuItem;
private ToolStripMenuItem operationToolStripMenuItem; private ToolStripMenuItem operationToolStripMenuItem;
private ToolStripMenuItem transactionToolStripMenuItem; private ToolStripMenuItem transactionToolStripMenuItem;
@ -361,5 +357,7 @@
private ToolStripMenuItem заказыToolStripMenuItem; private ToolStripMenuItem заказыToolStripMenuItem;
private ToolStripMenuItem заказыToolStripMenuItem1; private ToolStripMenuItem заказыToolStripMenuItem1;
private ToolStripMenuItem заказыПоГруппамToolStripMenuItem; private ToolStripMenuItem заказыПоГруппамToolStripMenuItem;
private ToolStripMenuItem исполнителиToolStripMenuItem;
private ToolStripMenuItem запускРаботToolStripMenuItem;
} }
} }