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

View File

@ -30,12 +30,14 @@ namespace PizzeriaBusinessLogic.BusinessLogics
_logger.LogWarning("DoWork. Implementers is null");
return;
}
var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят });
if (orders == null || orders.Count == 0)
var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Выдан });
int count = _orderLogic.ReadList(null).Count;
if (orders == null || count == orders.Count)
{
_logger.LogWarning("DoWork. Orders is null or empty");
return;
}
orders = _orderLogic.ReadList(null);
_logger.LogDebug("DoWork for {Count} orders", orders.Count);
foreach (var implementer in implementers)
{
@ -49,6 +51,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics
{
return;
}
await DeliverWaitingOrder(implementer);
await RunOrderInWork(implementer);
await Task.Run(() =>
@ -71,6 +74,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics
{
Id = order.Id
});
_orderLogic.DeliveryOrder(new OrderBindingModel { Id = order.Id });
}
// кто-то мог уже перехватить заказ, игнорируем ошибку
catch (InvalidOperationException ex)
@ -115,6 +119,7 @@ namespace PizzeriaBusinessLogic.BusinessLogics
{
Id = runOrder.Id
});
_orderLogic.DeliveryOrder(new OrderBindingModel { Id = runOrder.Id });
// отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
}
@ -130,5 +135,47 @@ namespace PizzeriaBusinessLogic.BusinessLogics
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,
Выполняется = 1,
Готов = 2,
Выдан = 3
Ожидает = 3,
Выдан = 4
}
}

View File

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

View File

@ -39,10 +39,6 @@
this.transactionToolStripMenuItem = 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();
@ -52,6 +48,10 @@
this.заказыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.заказыToolStripMenuItem1 = 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.buttonCreateOrder = 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.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.bookToolStripMenuItem,
this.отчётыToolStripMenuItem,
this.запускРаботToolStripMenuItem});
this.запускРаботToolStripMenuItem,
this.operationToolStripMenuItem,
this.отчётыToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
@ -83,8 +82,7 @@
this.bookToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ingridientsToolStripMenuItem,
this.pizzasToolStripMenuItem,
this.клиентToolStripMenuItem,
this.исполнителиToolStripMenuItem});
this.исполнителиToolStripMenuItem,
this.shopsToolStripMenuItem,
this.клиентToolStripMenuItem});
this.bookToolStripMenuItem.Name = "bookToolStripMenuItem";
@ -346,8 +344,6 @@
private ToolStripMenuItem componentPizzaToolStripMenuItem1;
private ToolStripMenuItem ordersToolStripMenuItem;
private ToolStripMenuItem клиентToolStripMenuItem;
private ToolStripMenuItem исполнителиToolStripMenuItem;
private ToolStripMenuItem запускРаботToolStripMenuItem;
private ToolStripMenuItem shopsToolStripMenuItem;
private ToolStripMenuItem operationToolStripMenuItem;
private ToolStripMenuItem transactionToolStripMenuItem;
@ -361,5 +357,7 @@
private ToolStripMenuItem заказыToolStripMenuItem;
private ToolStripMenuItem заказыToolStripMenuItem1;
private ToolStripMenuItem заказыПоГруппамToolStripMenuItem;
private ToolStripMenuItem исполнителиToolStripMenuItem;
private ToolStripMenuItem запускРаботToolStripMenuItem;
}
}