Реализована логика пополнения и продажи изделий с магазинов

This commit is contained in:
Никита Потапов 2024-05-20 02:18:31 +04:00
parent 26bdec8665
commit 5902068043
7 changed files with 185 additions and 36 deletions

View File

@ -176,34 +176,44 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
return true;
}
public bool SupplySecures(ISecureModel secure, int count) => throw new NotImplementedException();
public bool SellSecures(ISecureModel model, int count) => throw new NotImplementedException();
public bool CheckSecuresCount(ISecureModel model, int count)
public bool SupplySecures(ISecureModel secure, int count)
{
int securesInShops = _shopStorage.GetFullList()
.Select(x => x.ShopSecures.Select(y => y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0).Sum()).Sum();
return securesInShops >= count;
}
public bool CheckSupplySecures(ShopSearchModel shopSearchModel, int count)
{
if (shopSearchModel == null)
throw new ArgumentNullException(nameof(shopSearchModel));
var shop = _shopStorage.GetElement(shopSearchModel);
if (shop == null)
if (!CheckSupplySecures(count))
{
_logger.LogWarning("Required shop element not found in storage");
return false;
throw new InvalidOperationException("Невозможно пополнить: в магазинах не хватает места");
}
int securesInShop = _shopStorage.GetFullList().Select(x => x.ShopSecures.Select(y => y.Value.Item2).Sum()).Sum();
var shops = _shopStorage.GetFullList();
foreach (var shop in shops)
{
int shopFreeSpace = GetFreeSpace(shop.Id);
if (shopFreeSpace >= 0)
{
int min = Math.Min(count, shopFreeSpace);
count -= min;
SupplySecures(new ShopSearchModel { Id = shop.Id }, secure, min);
}
}
return securesInShop + count <= shop.MaxSecuresCount;
return true;
}
public bool SellSecures(ISecureModel model, int count)
{
return _shopStorage.SellSecures(model, count);
}
public bool CheckSupplySecures(int count)
{
throw new NotImplementedException();
return GetFreeSpace() >= count;
}
private int GetFreeSpace()
{
var shops = _shopStorage.GetFullList();
return shops.Select(shop => shop.MaxSecuresCount - shop.ShopSecures.Select(shopSecure => shopSecure.Value.Item2).Sum()).Sum();
}
private int GetFreeSpace(int shopId)
{
var shop = _shopStorage.GetElement(new ShopSearchModel { Id = shopId });
return shop.MaxSecuresCount - shop.ShopSecures.Select(shopSecure => shopSecure.Value.Item2).Sum();
}
}
}

View File

@ -17,14 +17,6 @@ namespace SecuritySystemContracts.BusinessLogicsContracts
/// </summary>
bool SellSecures(ISecureModel model, int count);
/// <summary>
/// Проверяет наличие определенного количества продукта суммарно по всем магазинам
/// </summary>
bool CheckSecuresCount(ISecureModel model, int count);
/// <summary>
/// Проверяет можно ли пополнить конкретный магазин продукцией в указанном количестве
/// </summary>
bool CheckSupplySecures(ShopSearchModel shopSearchModel, int count);
/// <summary>
/// Проверяет можно ли распределить во все магазины продукты в указанном количестве
/// </summary>
bool CheckSupplySecures(int count);

View File

@ -13,5 +13,6 @@ namespace SecuritySystemContracts.StoragesContracts
ShopViewModel? Insert(ShopBindingModel model);
ShopViewModel? Update(ShopBindingModel model);
ShopViewModel? Delete(ShopBindingModel model);
bool SellSecures(ISecureModel secureModel, int securesCount);
}
}

View File

@ -1,7 +1,11 @@
using SecuritySystemContracts.BindingModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using SecuritySystemDatabaseImplement.Models;
using SecuritySystemDataModels.Models;
namespace SecuritySystemDatabaseImplement.Implements
{
@ -9,32 +13,116 @@ namespace SecuritySystemDatabaseImplement.Implements
{
public ShopViewModel? Delete(ShopBindingModel model)
{
throw new NotImplementedException();
using var context = new SecuritySystemDatabase();
var shop = context.Shops.Include(x => x.Secures).FirstOrDefault(x => x.Id == model.Id);
if (shop != null)
{
context.Shops.Remove(shop);
context.SaveChanges();
return shop.GetViewModel;
}
return null;
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
throw new NotImplementedException();
return GetFilteredList(model).FirstOrDefault();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
throw new NotImplementedException();
var elements = GetFullList();
if (model.Id != null)
{
elements = elements.Where(x => x.Id == model.Id).ToList();
}
if (!model.Name.IsNullOrEmpty())
{
elements = elements.Where(x => x.Name == model.Name).ToList();
}
return elements;
}
public List<ShopViewModel> GetFullList()
{
throw new NotImplementedException();
using var context = new SecuritySystemDatabase();
return context.Shops
.Include(x => x.Secures)
.ThenInclude(x => x.Secure)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
throw new NotImplementedException();
using var context = new SecuritySystemDatabase();
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
public bool SellSecures(ISecureModel secureModel, int securesCount)
{
using var context = new SecuritySystemDatabase();
using var transaction = context.Database.BeginTransaction();
foreach (var shopSecure in context.ShopSecures.Where(x => x.SecureId == secureModel.Id))
{
var min = Math.Min(securesCount, shopSecure.Count);
shopSecure.Count -= min;
securesCount -= min;
if (securesCount <= 0)
{
break;
}
}
if (securesCount == 0)
{
context.SaveChanges();
transaction.Commit();
}
else
{
transaction.Rollback();
}
if (securesCount > 0)
{
return false;
}
return true;
}
public ShopViewModel? Update(ShopBindingModel model)
{
throw new NotImplementedException();
using var context = new SecuritySystemDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
shop.UpdateSecures(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -66,7 +66,7 @@ namespace SecuritySystemDatabaseImplement.Models
ShopSecures = ShopSecures
};
public void UpdateComponents(SecuritySystemDatabase context, ShopBindingModel model)
public void UpdateSecures(SecuritySystemDatabase context, ShopBindingModel model)
{
var shopSecures = context.ShopSecures.Where(rec => rec.SecureId == model.Id).ToList();
if (shopSecures != null && shopSecures.Count > 0)

View File

@ -4,6 +4,7 @@ using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Models;
using SecuritySystemFileImplement.Models;
using System.Reflection;
namespace SecuritySystemFileImplement.Implements
{
@ -75,5 +76,56 @@ namespace SecuritySystemFileImplement.Implements
source.SaveShops();
return shop.GetViewModel;
}
public bool SellSecures(ISecureModel secureModel, int securesCount)
{
var secure = source.Secures.FirstOrDefault(x => x.Id == secureModel.Id);
if (secure == null)
{
return false;
}
var shopSecures = source.Shops.SelectMany(shop => shop.ShopSecures.Where(c => c.Value.Item1.Id == secure.Id));
int countStore = 0;
foreach (var it in shopSecures)
countStore += it.Value.Item2;
if (securesCount > countStore)
return false;
foreach (var shop in source.Shops)
{
var secures = shop.ShopSecures;
foreach (var c in secures.Where(x => x.Value.Item1.Id == secure.Id))
{
int min = Math.Min(c.Value.Item2, securesCount);
secures[c.Value.Item1.Id] = (c.Value.Item1, c.Value.Item2 - min);
securesCount -= min;
if (securesCount <= 0)
break;
}
shop.Update(new ShopBindingModel
{
Id = shop.Id,
Name = shop.Name,
Address = shop.Address,
MaxSecuresCount = shop.MaxSecuresCount,
OpeningDate = shop.OpeningDate,
ShopSecures = secures
});
source.SaveShops();
if (securesCount <= 0)
return true;
}
return true;
}
}
}

View File

@ -2,6 +2,7 @@
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Models;
using SecuritySystemListImplement.Models;
namespace SecuritySystemListImplement.Implements
@ -117,5 +118,10 @@ namespace SecuritySystemListImplement.Implements
return null;
}
public bool SellSecures(ISecureModel secureModel, int securesCount)
{
throw new NotImplementedException();
}
}
}