доработки
This commit is contained in:
parent
9923cec609
commit
f1a8f95f9f
@ -140,7 +140,7 @@ namespace IceCreamBusinessLogic.BusinessLogics
|
||||
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Document not found.");
|
||||
return false;
|
||||
}
|
||||
if (CheckSupply(icecream, model.Count) == false)
|
||||
if (!_shopLogic.CheckSupply(icecream, model.Count))
|
||||
{
|
||||
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error.");
|
||||
return false;
|
||||
@ -157,66 +157,5 @@ namespace IceCreamBusinessLogic.BusinessLogics
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool CheckSupply(IIceCreamModel iceCream, int count)
|
||||
{
|
||||
if (count <= 0)
|
||||
{
|
||||
_logger.LogWarning("Check then supply operation error. IceCream count < 0.");
|
||||
return false;
|
||||
}
|
||||
|
||||
int freeSpace = 0;
|
||||
foreach (var shop in _shopStorage.GetFullList())
|
||||
{
|
||||
freeSpace += shop.IceCreamMaxCount;
|
||||
foreach (var doc in shop.ShopIceCreams)
|
||||
{
|
||||
freeSpace -= doc.Value.Item2;
|
||||
}
|
||||
}
|
||||
|
||||
if (freeSpace - count < 0)
|
||||
{
|
||||
_logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops.");
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var shop in _shopStorage.GetFullList())
|
||||
{
|
||||
freeSpace = shop.IceCreamMaxCount;
|
||||
foreach (var doc in shop.ShopIceCreams)
|
||||
{
|
||||
freeSpace -= doc.Value.Item2;
|
||||
}
|
||||
if (freeSpace == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (freeSpace - count >= 0)
|
||||
{
|
||||
if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, count)) count = 0;
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Supply error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (freeSpace - count < 0)
|
||||
{
|
||||
if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, iceCream, freeSpace)) count -= freeSpace;
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Supply error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (count <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Xml.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace IceCreamBusinessLogic.BusinessLogics
|
||||
{
|
||||
@ -73,50 +74,95 @@ namespace IceCreamBusinessLogic.BusinessLogics
|
||||
if (count <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Count of icecreams in supply must be more than 0", nameof(count));
|
||||
|
||||
}
|
||||
_logger.LogInformation("SupplyIceCreams. ShopName:{0}.Id:{ 1}", model.Name, model.Id);
|
||||
var shopElement = _shopStorage.GetElement(model);
|
||||
if (shopElement == null)
|
||||
|
||||
var element = _shopStorage.GetElement(model);
|
||||
if (element == null)
|
||||
{
|
||||
_logger.LogWarning("Required shop element not found in storage");
|
||||
return false;
|
||||
}
|
||||
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name);
|
||||
|
||||
var countIceCreams = 0;
|
||||
foreach (var icecream in shopElement.ShopIceCreams)
|
||||
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", element.Id, element.Name);
|
||||
|
||||
if(element.IceCreamMaxCount - element.ShopIceCreams.Select(x => x.Value.Item2).Sum() < count)
|
||||
{
|
||||
countIceCreams += icecream.Value.Item2;
|
||||
throw new ArgumentNullException("Shop is full", nameof(count));
|
||||
}
|
||||
if (shopElement.IceCreamMaxCount - countIceCreams >= count)
|
||||
|
||||
_logger.LogWarning("SupplyIceCreams find. Id:{Id}", element.Id);
|
||||
|
||||
if(element.ShopIceCreams.TryGetValue(iceCream.Id, out var pair))
|
||||
{
|
||||
if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameIceCream))
|
||||
element.ShopIceCreams[iceCream.Id] = (iceCream, count + pair.Item2);
|
||||
_logger.LogInformation(
|
||||
"SupplyIceCreams. Added {count} {iceCream} to '{ShopName}' shop",
|
||||
count, iceCream.IceCreamName, element.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
element.ShopIceCreams[iceCream.Id] = (iceCream, count);
|
||||
_logger.LogInformation("SupplyIceCreams. Added {count} {iceCream} to '{ShopName}' shop", count, iceCream.IceCreamName, element.Name);
|
||||
}
|
||||
|
||||
_shopStorage.Update(new()
|
||||
{
|
||||
Id = element.Id,
|
||||
Name = element.Name,
|
||||
Adress = element.Adress,
|
||||
OpeningDate = element.OpeningDate,
|
||||
ShopIceCreams = element.ShopIceCreams,
|
||||
IceCreamMaxCount = element.IceCreamMaxCount
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool CheckSupply(IIceCreamModel iceCream, int count)
|
||||
{
|
||||
if (iceCream == null) throw new ArgumentNullException(nameof(iceCream));
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
throw new ArgumentException("count should be more then 0", nameof(count));
|
||||
}
|
||||
|
||||
int freeSpace = _shopStorage.GetFullList().Select(x => x.IceCreamMaxCount - x.ShopIceCreams.Select(p => p.Value.Item2).Sum()).Sum() - count;
|
||||
|
||||
if(freeSpace < 0)
|
||||
{
|
||||
_logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops.");
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var shop in _shopStorage.GetFullList())
|
||||
{
|
||||
int countFree = shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum();
|
||||
if (countFree < count)
|
||||
{
|
||||
shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameIceCream.Item2 + count);
|
||||
_logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name);
|
||||
if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, countFree))
|
||||
{
|
||||
_logger.LogWarning("SupplyIceCreams operation failed.");
|
||||
return false;
|
||||
}
|
||||
count -= countFree;
|
||||
}
|
||||
else
|
||||
{
|
||||
shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count);
|
||||
_logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name);
|
||||
if (!SupplyIceCreams(new() { Id = shop.Id }, iceCream, count))
|
||||
{
|
||||
_logger.LogWarning("SupplyIceCreams operation failed.");
|
||||
return false;
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
_shopStorage.Update(new()
|
||||
if (count == 0)
|
||||
{
|
||||
Id = shopElement.Id,
|
||||
Name = shopElement.Name,
|
||||
Adress = shopElement.Adress,
|
||||
OpeningDate = shopElement.OpeningDate,
|
||||
ShopIceCreams = shopElement.ShopIceCreams,
|
||||
IceCreamMaxCount = shopElement.IceCreamMaxCount
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Required shop is overflowed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool SellIceCreams(IIceCreamModel iceCream, int count)
|
||||
|
@ -19,5 +19,6 @@ namespace IceCreamShopContracts.BusinessLogicsContracts
|
||||
bool Delete(ShopBindingModel model);
|
||||
bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count);
|
||||
bool SellIceCreams(IIceCreamModel iceCream, int count);
|
||||
bool CheckSupply(IIceCreamModel iceCream, int count);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using IceCreamShopContracts.ViewModels;
|
||||
using AbstractIceCreamShopDataModels.Models;
|
||||
using IceCreamShopFileImplement.Models;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Reflection;
|
||||
|
||||
namespace IceCreamShopFileImplement.Implements
|
||||
{
|
||||
@ -82,43 +83,12 @@ namespace IceCreamShopFileImplement.Implements
|
||||
|
||||
public bool SellIceCreams(IIceCreamModel model, int count)
|
||||
{
|
||||
var iceCream = source.IceCreams.FirstOrDefault(x => x.Id == model.Id);
|
||||
|
||||
var countStore = count;
|
||||
|
||||
if (iceCream == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var shop in source.Shops)
|
||||
{
|
||||
foreach (var icecream in shop.ShopIceCreams)
|
||||
{
|
||||
if (icecream.Value.Item1.Id == iceCream.Id)
|
||||
{
|
||||
count -= icecream.Value.Item2;
|
||||
}
|
||||
if (count <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
count = countStore;
|
||||
|
||||
for (int i = 0; i < source.Shops.Count; i++)
|
||||
{
|
||||
var shop = source.Shops[i];
|
||||
var icecreams = shop.ShopIceCreams;
|
||||
|
||||
foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == iceCream.Id))
|
||||
foreach (var icecream in icecreams.Where(x => x.Value.Item1.Id == model.Id))
|
||||
{
|
||||
var min = Math.Min(icecream.Value.Item2, count);
|
||||
icecreams[icecream.Value.Item1.Id] = (icecream.Value.Item1, icecream.Value.Item2 - min);
|
||||
|
Loading…
Reference in New Issue
Block a user