В интерфес логик добавлен необходимый функционал, нужно реализовать
This commit is contained in:
parent
122db5c9af
commit
dcba13f0ed
@ -12,11 +12,13 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
private readonly IShopLogic _shopLogic;
|
||||
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic)
|
||||
{
|
||||
_logger = logger;
|
||||
_orderStorage = orderStorage;
|
||||
_shopLogic = shopLogic;
|
||||
}
|
||||
|
||||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||||
@ -45,7 +47,7 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
|
||||
public bool ChangeStatus(OrderBindingModel model, OrderStatus targetStatus)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
||||
@ -54,18 +56,20 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
_logger.LogWarning("Read operation failed");
|
||||
return false;
|
||||
}
|
||||
if (element.Status != status - 1)
|
||||
if (element.Status != targetStatus - 1)
|
||||
{
|
||||
_logger.LogWarning("Status change operation failed");
|
||||
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
|
||||
}
|
||||
OrderStatus oldStatus = model.Status;
|
||||
model.Status = status;
|
||||
if (targetStatus == OrderStatus.Выдан)
|
||||
{
|
||||
_shopLogic.SupplySecures();
|
||||
}
|
||||
model.Status = targetStatus;
|
||||
if (model.Status == OrderStatus.Выдан)
|
||||
model.DateImplement = DateTime.Now;
|
||||
if (_orderStorage.Update(model) == null)
|
||||
{
|
||||
model.Status = oldStatus;
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using SecuritySystemContracts.SearchModels;
|
||||
using SecuritySystemContracts.StoragesContracts;
|
||||
using SecuritySystemContracts.ViewModels;
|
||||
using SecuritySystemDataModels.Models;
|
||||
using System.Reflection;
|
||||
|
||||
namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
{
|
||||
@ -57,55 +58,6 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
return list;
|
||||
}
|
||||
|
||||
public bool SupplySecures(ShopSearchModel model, ISecureModel secure, int count)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
|
||||
if (secure == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(secure));
|
||||
}
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Count of secures in supply must be more than 0", nameof(count));
|
||||
}
|
||||
|
||||
var shopElement = _shopStorage.GetElement(model);
|
||||
if (shopElement == 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);
|
||||
|
||||
if (shopElement.ShopSecures.TryGetValue(secure.Id, out var sameSecure))
|
||||
{
|
||||
shopElement.ShopSecures[secure.Id] = (secure, sameSecure.Item2 + count);
|
||||
_logger.LogInformation("Same secure found by supply. Added {0} of {1} in {2} shop", count, secure.SecureName, shopElement.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
shopElement.ShopSecures[secure.Id] = (secure, count);
|
||||
_logger.LogInformation("New secure added by supply. Added {0} of {1} in {2} shop", count, secure.SecureName, shopElement.Name);
|
||||
}
|
||||
|
||||
_shopStorage.Update(new()
|
||||
{
|
||||
Id = shopElement.Id,
|
||||
Name = shopElement.Name,
|
||||
Address = shopElement.Address,
|
||||
OpeningDate = shopElement.OpeningDate,
|
||||
ShopSecures = shopElement.ShopSecures
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Create(ShopBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
@ -174,5 +126,89 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
throw new InvalidOperationException("Магазин с таким названием уже есть");
|
||||
}
|
||||
}
|
||||
public bool SupplySecures(ShopSearchModel model, ISecureModel secure, int count)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
|
||||
if (secure == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(secure));
|
||||
}
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Количество продукта для пополнения должно быть больше 0", nameof(count));
|
||||
}
|
||||
|
||||
var shopElement = _shopStorage.GetElement(model);
|
||||
|
||||
if (shopElement == 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);
|
||||
|
||||
if (shopElement.ShopSecures.TryGetValue(secure.Id, out var sameSecure))
|
||||
{
|
||||
shopElement.ShopSecures[secure.Id] = (secure, sameSecure.Item2 + count);
|
||||
_logger.LogInformation("Same secure found by supply. Added {0} of {1} in {2} shop", count, secure.SecureName, shopElement.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
shopElement.ShopSecures[secure.Id] = (secure, count);
|
||||
_logger.LogInformation("New secure added by supply. Added {0} of {1} in {2} shop", count, secure.SecureName, shopElement.Name);
|
||||
}
|
||||
|
||||
_shopStorage.Update(new()
|
||||
{
|
||||
Id = shopElement.Id,
|
||||
Name = shopElement.Name,
|
||||
Address = shopElement.Address,
|
||||
OpeningDate = shopElement.OpeningDate,
|
||||
ShopSecures = shopElement.ShopSecures,
|
||||
MaxSecuresCount = shopElement.MaxSecuresCount
|
||||
});
|
||||
|
||||
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)
|
||||
{
|
||||
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));
|
||||
|
||||
if (secure == null)
|
||||
throw new ArgumentNullException(nameof(secure));
|
||||
|
||||
var shop = _shopStorage.GetElement(shopSearchModel);
|
||||
|
||||
if (shop == null)
|
||||
{
|
||||
_logger.LogWarning("Required shop element not found in storage");
|
||||
return false;
|
||||
}
|
||||
|
||||
int securesInShop = _shopStorage.GetFullList().Select(x => x.ShopSecures.Select(y => y.Value.Item2).Sum()).Sum();
|
||||
|
||||
return securesInShop + count <= shop.MaxSecuresCount;
|
||||
}
|
||||
public bool CheckSupplySecures(int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,29 @@ namespace SecuritySystemContracts.BusinessLogicsContracts
|
||||
bool Create(ShopBindingModel model);
|
||||
bool Update(ShopBindingModel model);
|
||||
bool Delete(ShopBindingModel model);
|
||||
/// <summary>
|
||||
/// Продает продукт со всех магазинов в заданном количестве
|
||||
/// </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);
|
||||
/// <summary>
|
||||
/// Пополняет конкретный магазин продуктом в указанном количестве
|
||||
/// </summary>
|
||||
bool SupplySecures(ShopSearchModel model, ISecureModel secure, int count);
|
||||
/// <summary>
|
||||
/// Рапределяет по всем магазинам продукт в указанном количестве
|
||||
/// </summary>
|
||||
bool SupplySecures(ISecureModel secure, int count);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
using SecuritySystemContracts.BindingModels;
|
||||
using SecuritySystemContracts.SearchModels;
|
||||
using SecuritySystemContracts.ViewModels;
|
||||
using SecuritySystemDataModels.Models;
|
||||
|
||||
namespace SecuritySystemContracts.StoragesContracts
|
||||
{
|
||||
|
@ -75,100 +75,5 @@ namespace SecuritySystemFileImplement.Implements
|
||||
source.SaveShops();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
|
||||
public bool SellSecure(ISecureModel model, int count)
|
||||
{
|
||||
var secure = source.Secures.FirstOrDefault(x => x.Id == model.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 shopSec in shopSecures)
|
||||
countStore += shopSec.Value.Item2;
|
||||
|
||||
if (count > countStore)
|
||||
return false;
|
||||
|
||||
foreach (var shop in source.Shops)
|
||||
{
|
||||
var secures = shop.ShopSecures;
|
||||
|
||||
foreach (var sec in secures.Where(x => x.Value.Item1.Id == secure.Id))
|
||||
{
|
||||
int min = Math.Min(sec.Value.Item2, count);
|
||||
secures[sec.Value.Item1.Id] = (sec.Value.Item1, sec.Value.Item2 - min);
|
||||
count -= min;
|
||||
|
||||
if (count <= 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 (count <= 0)
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool CheckCountSecure(ISecureModel model, int count)
|
||||
{
|
||||
int store = source.Shops.Select(x => x.ShopSecures.Select(y => (y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)).Sum()).Sum();
|
||||
return store >= count;
|
||||
}
|
||||
|
||||
public bool SellSecures(ISecureModel model, int count)
|
||||
{
|
||||
var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id);
|
||||
|
||||
if (secure == null || !CheckCountSecure(model, count))
|
||||
{
|
||||
throw new ArgumentNullException("В магазинах нет такого количества товара");
|
||||
}
|
||||
|
||||
foreach (var shop in source.Shops)
|
||||
{
|
||||
var secures = shop.ShopSecures;
|
||||
foreach (var elem in secures.Where(x => x.Value.Item1.Id == secure.Id))
|
||||
{
|
||||
var selling = Math.Min(elem.Value.Item2, count);
|
||||
secures[elem.Value.Item1.Id] = (elem.Value.Item1, elem.Value.Item2 - selling);
|
||||
count -= selling;
|
||||
|
||||
if (count <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
shop.Update(new ShopBindingModel
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = shop.Name,
|
||||
Address = shop.Address,
|
||||
OpeningDate = shop.OpeningDate,
|
||||
ShopSecures = secures,
|
||||
MaxSecuresCount = shop.MaxSecuresCount
|
||||
});
|
||||
}
|
||||
|
||||
source.SaveShops();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user