Готовая 6 лабораторная (Усложненная)

This commit is contained in:
Илья 2024-05-19 21:56:44 +04:00
parent 154ca08301
commit af101ac4d8
5 changed files with 101 additions and 38 deletions

View File

@ -6,6 +6,7 @@ using IceCreamShopContracts.ViewModels;
using IceCreamShopDataModels.Enums;
using IceCreamShopDataModels.Models;
using Microsoft.Extensions.Logging;
using System.Xml.Linq;
namespace IceCreamShopBusinessLogic.BusinessLogics
{
@ -137,12 +138,15 @@ namespace IceCreamShopBusinessLogic.BusinessLogics
_logger.LogWarning("Change status operation failed. Order not found");
return false;
}
if (order.Status + 1 != newStatus)
if ((order.Status + 1 != newStatus && order.Status != OrderStatus.Ожидание) || (order.Status == OrderStatus.Ожидание && newStatus != OrderStatus.Выдан))
{
_logger.LogWarning("Change status operation failed. Incorrect new status: {newStatus}. Current status: {currStatus}",
newStatus, order.Status);
_logger.LogWarning("Change status operation failed");
return false;
}
if (order.ImplementerId.HasValue)
{
model.ImplementerId = order.ImplementerId;
}
if (newStatus == OrderStatus.Выдан)
{
var iceCream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = order.IceCreamId });
@ -154,14 +158,15 @@ namespace IceCreamShopBusinessLogic.BusinessLogics
if (!DeliverIceCreams(iceCream, order.Count))
{
_logger.LogWarning("Change status operation failed. Ice creams delivery operation failed");
model.Status = OrderStatus.Ожидание;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
}
return false;
}
}
model.Status = newStatus;
if (order.ImplementerId.HasValue)
{
model.ImplementerId = order.ImplementerId;
}
if (model.Status == OrderStatus.Готов)
{
model.DateImplement = DateTime.Now;

View File

@ -30,10 +30,13 @@ namespace IceCreamShopBusinessLogic.BusinessLogics
_logger.LogWarning("DoWork. Implementers is null");
return;
}
var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят });
if (orders == null || orders.Count == 0)
List<OrderViewModel>? orders = new();
orders.AddRange(_orderLogic.ReadList(new() { Status = OrderStatus.Принят }) ?? new());
orders.AddRange(_orderLogic.ReadList(new() { Status = OrderStatus.Выполняется }) ?? new());
orders.AddRange(_orderLogic.ReadList(new() { Status = OrderStatus.Ожидание }) ?? new());
if (orders.Count == 0)
{
_logger.LogWarning("DoWork. Orders is null or empty");
_logger.LogWarning("DoWork. Orders is empty");
return;
}
_logger.LogDebug("DoWork for {Count} orders", orders.Count);
@ -54,6 +57,8 @@ namespace IceCreamShopBusinessLogic.BusinessLogics
{
return;
}
await RunWaitingOrders(implementer);
await RunOrderInWork(implementer);
await Task.Run(() =>
@ -76,6 +81,11 @@ namespace IceCreamShopBusinessLogic.BusinessLogics
{
Id = order.Id
});
_logger.LogDebug("DoWork. Order {Order} delivery", order.Id);
_orderLogic.DeliveryOrder(new OrderBindingModel
{
Id = order.Id
});
}
// кто-то мог уже перехватить заказ, игнорируем ошибку
catch (InvalidOperationException ex)
@ -125,6 +135,13 @@ namespace IceCreamShopBusinessLogic.BusinessLogics
{
Id = runOrder.Id
});
// доставляем
_logger.LogDebug("DoWork. Order {Order} delivery", runOrder.Id);
_orderLogic.DeliveryOrder(new OrderBindingModel
{
Id = runOrder.Id
});
// отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
}
@ -140,5 +157,43 @@ namespace IceCreamShopBusinessLogic.BusinessLogics
throw;
}
}
private async Task RunWaitingOrders(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.LogDebug("RunWaitingOrders for {Count} orders. Implementer: {id}.", waitingOrders.Count, implementer.Id);
foreach (var waitingOrder in waitingOrders)
{
try
{
_logger.LogInformation("RunWaitingOrders. Order {Order} delivery", waitingOrder.Id);
_orderLogic.DeliveryOrder(new OrderBindingModel
{
Id = waitingOrder.Id
});
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Error try get work");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while do work");
throw;
}
}
}
}
}

View File

@ -10,6 +10,8 @@
Готов = 2,
Выдан = 3
Выдан = 3,
Ожидание = 4
}
}

View File

@ -149,8 +149,9 @@
Controls.Add(buttonSave);
Controls.Add(textBoxFIO);
Controls.Add(labelFIO);
Margin = new Padding(2, 2, 2, 2);
Margin = new Padding(2);
Name = "FormImplementer";
StartPosition = FormStartPosition.CenterScreen;
Text = "Исполнитель";
Load += FormImplementer_Load;
ResumeLayout(false);