Fixed lab 6 hard

This commit is contained in:
Viltskaa 2023-05-04 23:28:39 +04:00
parent fb3d255c3a
commit c69c8ba18c
5 changed files with 79 additions and 83 deletions

View File

@ -59,12 +59,13 @@ namespace SushiBarBusinessLogic.BusinessLogics
public bool DeliveryOrder(OrderBindingModel model) public bool DeliveryOrder(OrderBindingModel model)
{ {
return UpdateStatus(model, OrderStatus.Ready); return UpdateStatus(model, OrderStatus.Issued);
} }
public bool FinishOrder(OrderBindingModel model) public bool FinishOrder(OrderBindingModel model)
{ {
return UpdateStatus(model, OrderStatus.Issued); model.DateImplement = DateTime.Now;
return UpdateStatus(model, OrderStatus.Ready);
} }
public List<OrderViewModel>? ReadList(OrderSearchModel? model) public List<OrderViewModel>? ReadList(OrderSearchModel? model)
@ -93,11 +94,11 @@ namespace SushiBarBusinessLogic.BusinessLogics
{ {
throw new InvalidOperationException($"Error on change status"); throw new InvalidOperationException($"Error on change status");
} }
if (status == OrderStatus.Ready || status == OrderStatus.Waiting) if (status is OrderStatus.Ready or OrderStatus.Waiting)
{ {
var sushi = _sushiStorage.GetElement(new() { Id = model.SushiId }); var sushi = _sushiStorage.GetElement(new() { Id = order.SushiId });
if (sushi == null || !_storeLogic.CheckToSupply(sushi, model.Count)) if (sushi == null || !_storeLogic.CheckToSupply(sushi, order.Count))
{ {
_logger.LogWarning($"Failed to fill magazines with product " + _logger.LogWarning($"Failed to fill magazines with product " +
$"'{sushi?.SushiName ?? string.Empty}' " + $"'{sushi?.SushiName ?? string.Empty}' " +
@ -110,13 +111,14 @@ namespace SushiBarBusinessLogic.BusinessLogics
} }
} }
model.Status = status; model.Status = status;
model.DateCreate = model.DateCreate; model.DateCreate = order.DateCreate;
model.DateImplement ??= model.DateImplement; model.DateImplement ??= order.DateImplement;
if (model.ImplementerId.HasValue) if (order.ImplementerId.HasValue)
model.ImplementerId = model.ImplementerId; model.ImplementerId = order.ImplementerId;
model.SushiId = model.SushiId; model.ClientId = order.ClientId;
model.Sum = model.Sum; model.SushiId = order.SushiId;
model.Count = model.Count; model.Sum = order.Sum;
model.Count = order.Count;
if (_orderStorage.Update(model) != null) return true; if (_orderStorage.Update(model) != null) return true;
_logger.LogWarning("Update operation failed"); _logger.LogWarning("Update operation failed");
return false; return false;

View File

@ -108,95 +108,89 @@ namespace SushiBarBusinessLogic.BusinessLogics
return false; return false;
} }
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", store.Id, store.StoreName); _logger.LogInformation("Shop element found. ID: {0}, Name: {1}",
store.Id, store.StoreName);
var countSushi = store.Sushis.Sum(s => s.Value.Item2); var countSushi = store.Sushis.Sum(s => s.Value.Item2);
if (store.maxSushi - countSushi >= quantity) if (store.maxSushi - countSushi < quantity)
{
if (store.Sushis.TryGetValue(sushi.Id, out var pair))
{
store.Sushis[sushi.Id] = (sushi, quantity + pair.Item2);
_logger.LogInformation("AddPackageInStore. Has been added {quantity} {package} in {StoreName}", quantity, sushi.SushiName, store.StoreName);
}
else
{
store.Sushis[sushi.Id] = (sushi, quantity);
_logger.LogInformation("AddPastryInShop. Has been added {quantity} new Package {package} in {StoreName}", quantity, sushi.SushiName, store.StoreName);
}
_storeStorage.Update(new()
{
Id = store.Id,
StoreAddress = store.StoreAddress,
StoreName = store.StoreName,
OpeningDate = store.OpeningDate,
Sushis = store.Sushis,
maxSushi = store.maxSushi
});
}
else
{ {
_logger.LogWarning("Required shop is overflowed"); _logger.LogWarning("Required shop is overflowed");
return false; return false;
} }
if (store.Sushis.TryGetValue(sushi.Id, out var pair))
{
store.Sushis[sushi.Id] = (sushi, quantity + pair.Item2);
_logger.LogInformation("AddPackageInStore. Has been added {quantity} {package} in {StoreName}",
quantity, sushi.SushiName, store.StoreName);
}
else
{
store.Sushis[sushi.Id] = (sushi, quantity);
_logger.LogInformation(
"AddPastryInShop. Has been added {quantity} new Package {package} in {StoreName}",
quantity, sushi.SushiName, store.StoreName);
}
_storeStorage.Update(new()
{
Id = store.Id,
StoreAddress = store.StoreAddress,
StoreName = store.StoreName,
OpeningDate = store.OpeningDate,
Sushis = store.Sushis,
maxSushi = store.maxSushi
});
return true; return true;
} }
public bool CheckToSupply(ISushiModel sushi, int quantity) public bool CheckToSupply(ISushiModel sushi, int quantity)
{ {
if (sushi == null)
{
throw new ArgumentNullException(nameof(sushi));
}
if (quantity <= 0) if (quantity <= 0)
{ {
_logger.LogWarning("Check then supply operation error. Document count < 0."); throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity));
return false;
} }
int countSushi = 0; var freePlaces = _storeStorage.GetFullList()
foreach (var store in _storeStorage.GetFullList()) .Select(x => x.maxSushi - x.Sushis
{ .Select(p => p.Value.Item2).Sum()).Sum() - quantity;
countSushi += store.maxSushi;
countSushi = store.Sushis.Values.Aggregate(countSushi, (current, valueTuple) => current - valueTuple.Item2);
}
if (countSushi - quantity < 0) if (freePlaces < 0)
{ {
_logger.LogWarning("Check then supply operation error. There's no place for new docs in stores."); _logger.LogInformation("AddReinforced. Failed to add reinforced to shop. It's full.");
return false; return false;
} }
foreach (var store in _storeStorage.GetFullList()) foreach (var store in _storeStorage.GetFullList())
{ {
countSushi = store.Sushis.Values.Aggregate(store.maxSushi, (current, valueTuple) => current - valueTuple.Item2); var temp = Math
if (countSushi == 0) .Min(quantity, store.maxSushi - store.Sushis.Select(x => x.Value.Item2)
.Sum());
if (temp <= 0)
{ {
continue; continue;
} }
if (countSushi - quantity >= 0) if (!SupplySushi(new StoreSearchModel { Id = store.Id }, sushi, temp))
{ {
if (SupplySushi(new() { Id = store.Id }, sushi, countSushi)) _logger.LogWarning("An error occurred while adding reinforced to shops");
countSushi = 0; return false;
else
{
_logger.LogWarning("Supply error");
return false;
}
} }
if (countSushi - quantity < 0)
{ quantity -= temp;
if (SupplySushi(new StoreSearchModel { Id = store.Id }, sushi, countSushi))
quantity -= countSushi; if (quantity == 0)
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (countSushi <= 0)
{ {
return true; return true;
} }
} }
return true;
return false;
} }
public bool SellSushi(ISushiModel model, int quantity) public bool SellSushi(ISushiModel model, int quantity)

View File

@ -62,6 +62,7 @@ public class WorkModeling : IWorkProcess
try try
{ {
_orderLogic.FinishOrder(new OrderBindingModel { Id = order.Id }); _orderLogic.FinishOrder(new OrderBindingModel { Id = order.Id });
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
} }
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
{ {
@ -72,7 +73,6 @@ public class WorkModeling : IWorkProcess
_logger.LogError(ex, "Error while do work"); _logger.LogError(ex, "Error while do work");
throw; throw;
} }
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
} }
}); });
@ -80,7 +80,7 @@ public class WorkModeling : IWorkProcess
await Task.Run(() => await Task.Run(() =>
{ {
foreach (var order foreach (var order
in orders.Where(x => x.Status == OrderStatus.Accepted && x.ImplementerId == implementer.Id)) in orders.Where(x => x.Status == OrderStatus.Accepted))
{ {
try try
{ {
@ -90,13 +90,13 @@ public class WorkModeling : IWorkProcess
Id = order.Id, Id = order.Id,
ImplementerId = implementer.Id ImplementerId = implementer.Id
}); });
Thread.Sleep(implementer.WorkExperience * _rnd.Next(10, 100) * order.Count); //Thread.Sleep(implementer.WorkExperience * _rnd.Next(10, 100) * 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.DeliveryOrder(new OrderBindingModel _orderLogic.FinishOrder(new OrderBindingModel
{ {
Id = order.Id Id = order.Id
}); });
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); //Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
} }
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
{ {
@ -113,7 +113,7 @@ public class WorkModeling : IWorkProcess
private async Task RunOrderInWork(ImplementerViewModel implementer, List<OrderViewModel> allOrders) private async Task RunOrderInWork(ImplementerViewModel implementer, List<OrderViewModel> allOrders)
{ {
if (_orderLogic == null || implementer == null || allOrders == null || allOrders.Count == 0) if (_orderLogic == null || allOrders.Count == 0)
{ {
return; return;
} }
@ -129,7 +129,7 @@ public class WorkModeling : IWorkProcess
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * runOrder.Count); Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * 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.DeliveryOrder(new OrderBindingModel _orderLogic.FinishOrder(new OrderBindingModel
{ {
Id = runOrder.Id Id = runOrder.Id
}); });

View File

@ -211,7 +211,7 @@ namespace SushiBarDatabaseImplement.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Sushi"); b.ToTable("Sushis");
}); });
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b =>

View File

@ -80,19 +80,19 @@ public class Store : IStoreModel
public void UpdateSushi(SushiBarDatabase context, StoreBindingModel model) public void UpdateSushi(SushiBarDatabase context, StoreBindingModel model)
{ {
var StoreSushi = context.StoreSushis var storeSushi = context.StoreSushis
.Where(rec => rec.StoreId == model.Id) .Where(rec => rec.StoreId == model.Id)
.ToList(); .ToList();
if (StoreSushi.Count > 0) if (storeSushi.Count > 0)
{ {
context.StoreSushis context.StoreSushis
.RemoveRange(StoreSushi .RemoveRange(storeSushi
.Where(rec => !model.Sushis .Where(rec => !model.Sushis
.ContainsKey(rec.SushiId))); .ContainsKey(rec.SushiId)));
foreach (var updateSushi in foreach (var updateSushi in
StoreSushi.Where(x => model.Sushis.ContainsKey(x.SushiId))) storeSushi.Where(x => model.Sushis.ContainsKey(x.SushiId)))
{ {
updateSushi.Count = model.Sushis[updateSushi.SushiId].Item2; updateSushi.Count = model.Sushis[updateSushi.SushiId].Item2;
model.Sushis.Remove(updateSushi.SushiId); model.Sushis.Remove(updateSushi.SushiId);
@ -103,7 +103,7 @@ public class Store : IStoreModel
{ {
Sushi = context.Sushis.First(y => y.Id == x.Key), Sushi = context.Sushis.First(y => y.Id == x.Key),
Count = x.Value.Item2, Count = x.Value.Item2,
}).Except(StoreSushi)); }).Except(storeSushi));
context.SaveChanges(); context.SaveChanges();
_sushis = null; _sushis = null;
} }