Fixed lab 6 hard
This commit is contained in:
parent
fb3d255c3a
commit
c69c8ba18c
@ -59,12 +59,13 @@ namespace SushiBarBusinessLogic.BusinessLogics
|
||||
|
||||
public bool DeliveryOrder(OrderBindingModel model)
|
||||
{
|
||||
return UpdateStatus(model, OrderStatus.Ready);
|
||||
return UpdateStatus(model, OrderStatus.Issued);
|
||||
}
|
||||
|
||||
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)
|
||||
@ -93,11 +94,11 @@ namespace SushiBarBusinessLogic.BusinessLogics
|
||||
{
|
||||
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 " +
|
||||
$"'{sushi?.SushiName ?? string.Empty}' " +
|
||||
@ -110,13 +111,14 @@ namespace SushiBarBusinessLogic.BusinessLogics
|
||||
}
|
||||
}
|
||||
model.Status = status;
|
||||
model.DateCreate = model.DateCreate;
|
||||
model.DateImplement ??= model.DateImplement;
|
||||
if (model.ImplementerId.HasValue)
|
||||
model.ImplementerId = model.ImplementerId;
|
||||
model.SushiId = model.SushiId;
|
||||
model.Sum = model.Sum;
|
||||
model.Count = model.Count;
|
||||
model.DateCreate = order.DateCreate;
|
||||
model.DateImplement ??= order.DateImplement;
|
||||
if (order.ImplementerId.HasValue)
|
||||
model.ImplementerId = order.ImplementerId;
|
||||
model.ClientId = order.ClientId;
|
||||
model.SushiId = order.SushiId;
|
||||
model.Sum = order.Sum;
|
||||
model.Count = order.Count;
|
||||
if (_orderStorage.Update(model) != null) return true;
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
|
@ -108,95 +108,89 @@ namespace SushiBarBusinessLogic.BusinessLogics
|
||||
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);
|
||||
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
|
||||
if (store.maxSushi - countSushi < quantity)
|
||||
{
|
||||
_logger.LogWarning("Required shop is overflowed");
|
||||
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;
|
||||
}
|
||||
|
||||
public bool CheckToSupply(ISushiModel sushi, int quantity)
|
||||
{
|
||||
if (sushi == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(sushi));
|
||||
}
|
||||
|
||||
if (quantity <= 0)
|
||||
{
|
||||
_logger.LogWarning("Check then supply operation error. Document count < 0.");
|
||||
return false;
|
||||
throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity));
|
||||
}
|
||||
|
||||
int countSushi = 0;
|
||||
foreach (var store in _storeStorage.GetFullList())
|
||||
{
|
||||
countSushi += store.maxSushi;
|
||||
countSushi = store.Sushis.Values.Aggregate(countSushi, (current, valueTuple) => current - valueTuple.Item2);
|
||||
}
|
||||
var freePlaces = _storeStorage.GetFullList()
|
||||
.Select(x => x.maxSushi - x.Sushis
|
||||
.Select(p => p.Value.Item2).Sum()).Sum() - quantity;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
foreach (var store in _storeStorage.GetFullList())
|
||||
{
|
||||
countSushi = store.Sushis.Values.Aggregate(store.maxSushi, (current, valueTuple) => current - valueTuple.Item2);
|
||||
if (countSushi == 0)
|
||||
var temp = Math
|
||||
.Min(quantity, store.maxSushi - store.Sushis.Select(x => x.Value.Item2)
|
||||
.Sum());
|
||||
|
||||
if (temp <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (countSushi - quantity >= 0)
|
||||
if (!SupplySushi(new StoreSearchModel { Id = store.Id }, sushi, temp))
|
||||
{
|
||||
if (SupplySushi(new() { Id = store.Id }, sushi, countSushi))
|
||||
countSushi = 0;
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Supply error");
|
||||
return false;
|
||||
}
|
||||
_logger.LogWarning("An error occurred while adding reinforced to shops");
|
||||
return false;
|
||||
}
|
||||
if (countSushi - quantity < 0)
|
||||
{
|
||||
if (SupplySushi(new StoreSearchModel { Id = store.Id }, sushi, countSushi))
|
||||
quantity -= countSushi;
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Supply error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (countSushi <= 0)
|
||||
|
||||
quantity -= temp;
|
||||
|
||||
if (quantity == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool SellSushi(ISushiModel model, int quantity)
|
||||
|
@ -62,6 +62,7 @@ public class WorkModeling : IWorkProcess
|
||||
try
|
||||
{
|
||||
_orderLogic.FinishOrder(new OrderBindingModel { Id = order.Id });
|
||||
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
@ -72,7 +73,6 @@ public class WorkModeling : IWorkProcess
|
||||
_logger.LogError(ex, "Error while do work");
|
||||
throw;
|
||||
}
|
||||
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
|
||||
}
|
||||
});
|
||||
|
||||
@ -80,7 +80,7 @@ public class WorkModeling : IWorkProcess
|
||||
await Task.Run(() =>
|
||||
{
|
||||
foreach (var order
|
||||
in orders.Where(x => x.Status == OrderStatus.Accepted && x.ImplementerId == implementer.Id))
|
||||
in orders.Where(x => x.Status == OrderStatus.Accepted))
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -90,13 +90,13 @@ public class WorkModeling : IWorkProcess
|
||||
Id = order.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);
|
||||
_orderLogic.DeliveryOrder(new OrderBindingModel
|
||||
_orderLogic.FinishOrder(new OrderBindingModel
|
||||
{
|
||||
Id = order.Id
|
||||
});
|
||||
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
|
||||
//Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
@ -113,7 +113,7 @@ public class WorkModeling : IWorkProcess
|
||||
|
||||
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;
|
||||
}
|
||||
@ -129,7 +129,7 @@ public class WorkModeling : IWorkProcess
|
||||
|
||||
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * runOrder.Count);
|
||||
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id);
|
||||
_orderLogic.DeliveryOrder(new OrderBindingModel
|
||||
_orderLogic.FinishOrder(new OrderBindingModel
|
||||
{
|
||||
Id = runOrder.Id
|
||||
});
|
||||
|
@ -211,7 +211,7 @@ namespace SushiBarDatabaseImplement.Migrations
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Sushi");
|
||||
b.ToTable("Sushis");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b =>
|
||||
|
@ -80,19 +80,19 @@ public class Store : IStoreModel
|
||||
|
||||
public void UpdateSushi(SushiBarDatabase context, StoreBindingModel model)
|
||||
{
|
||||
var StoreSushi = context.StoreSushis
|
||||
var storeSushi = context.StoreSushis
|
||||
.Where(rec => rec.StoreId == model.Id)
|
||||
.ToList();
|
||||
|
||||
if (StoreSushi.Count > 0)
|
||||
if (storeSushi.Count > 0)
|
||||
{
|
||||
context.StoreSushis
|
||||
.RemoveRange(StoreSushi
|
||||
.RemoveRange(storeSushi
|
||||
.Where(rec => !model.Sushis
|
||||
.ContainsKey(rec.SushiId)));
|
||||
|
||||
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;
|
||||
model.Sushis.Remove(updateSushi.SushiId);
|
||||
@ -103,7 +103,7 @@ public class Store : IStoreModel
|
||||
{
|
||||
Sushi = context.Sushis.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2,
|
||||
}).Except(StoreSushi));
|
||||
}).Except(storeSushi));
|
||||
context.SaveChanges();
|
||||
_sushis = null;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user