272 lines
11 KiB
C#
Raw Normal View History

using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.BusinessLogicsContracts;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.StoragesContracts;
using IceCreamShopContracts.ViewModels;
using AbstractIceCreamShopDataModels.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection.Metadata;
2023-03-13 13:25:55 +04:00
using System.Xml.Linq;
2023-03-26 21:00:37 +04:00
using System.Reflection;
namespace IceCreamBusinessLogic.BusinessLogics
{
public class ShopLogic : IShopLogic
{
private readonly ILogger _logger;
private readonly IShopStorage _shopStorage;
public ShopLogic(ILogger<ShopLogic> logger, IShopStorage shopStorage)
{
_logger = logger;
_shopStorage = shopStorage;
}
public ShopViewModel? ReadElement(ShopSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Shop Name:{0}.ID:{1}", model.Name, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<ShopViewModel>? ReadList(ShopSearchModel? model)
{
_logger.LogInformation("ReadList. Shop Name:{0}.ID:{1} ", model?.Name, model?.Id);
var list = (model == null) ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
2023-02-14 09:24:06 +04:00
public bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2023-02-14 09:24:06 +04:00
if (iceCream == null)
{
2023-02-14 09:24:06 +04:00
throw new ArgumentNullException(nameof(iceCream));
}
if (count <= 0)
{
2023-03-28 10:02:32 +04:00
throw new ArgumentNullException("Count of iceCreams in supply myst be more than 0", nameof(count));
}
2023-03-26 21:00:37 +04:00
2023-04-25 08:54:06 +04:00
_logger.LogInformation("AddPastryInShop. ShopName:{ShopName}.Id:{ Id}", model.Name, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
2023-04-25 08:54:06 +04:00
_logger.LogWarning("AddPastryInShop element not found");
return false;
}
2023-04-25 08:54:06 +04:00
_logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id);
2023-04-25 08:54:06 +04:00
if (element.ShopIceCreams.TryGetValue(iceCream.Id, out var sameIcecream))
{
element.ShopIceCreams[iceCream.Id] = (iceCream, sameIcecream.Item2 + count);
_logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, element.Name);
}
else
{
2023-04-25 08:54:06 +04:00
element.ShopIceCreams[iceCream.Id] = (iceCream, count);
_logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, element.Name);
}
2023-04-25 08:54:06 +04:00
/* var prevCount = element.ShopIceCreams.GetValueOrDefault(iceCream.Id, (iceCream, 0)).Item2;
element.ShopIceCreams[iceCream.Id] = (iceCream, prevCount + count);
_logger.LogInformation(
"AddPastryInShop. Has been added {count} {pastry} in {ShopName}",
count, iceCream.IceCreamName, element.Name);
*/
_shopStorage.Update(new()
{
2023-04-25 08:54:06 +04:00
Id = element.Id,
Adress = element.Adress,
Name = element.Name,
OpeningDate = element.OpeningDate,
ShopIceCreams = element.ShopIceCreams
});
return true;
/* 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);
var countIcecreams = 0;
foreach (var icecream in shopElement.ShopIceCreams)
{
countIcecreams += icecream.Value.Item2;
}
if (shopElement.IceCreamMaxCount - countIcecreams >= count)
{
if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameIcecream))
{
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);
}
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);
}
_shopStorage.Update(new()
{
Id = shopElement.Id,
Name = shopElement.Name,
Adress = shopElement.Adress,
OpeningDate = shopElement.OpeningDate,
ShopIceCreams = shopElement.ShopIceCreams,
IceCreamMaxCount = shopElement.IceCreamMaxCount
});
}
else
{
_logger.LogWarning("Required shop is overflowed");
return false;
}
return true;*/
}
public bool AddIceCream(IIceCreamModel icecream, int count)
{
if (count <= 0)
{
_logger.LogWarning("AddPastriesInShops. Количество добавляемых изделий должно быть больше 0. Количество - {count}", count);
return false;
}
var freePlaces = GetFreePlaces(count);
if (freePlaces < 0)
{
_logger.LogInformation("AddPastriesInShops. Не удалось добавить изделия в магазины, поскольку они переполнены." +
"Освободите магазины на {places} изделий", -freePlaces);
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
/* int freeInShop = shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum();
if (freeInShop < count) continue;*/
var cnt = Math.Min(count, shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum());
if (cnt <= 0)
{
2023-04-25 08:54:06 +04:00
continue;
}
2023-04-25 08:54:06 +04:00
if (!SupplyIceCreams(new() { Id = shop.Id }, icecream, cnt))
{
2023-04-25 08:54:06 +04:00
_logger.LogWarning("При добавления изделий во все магазины произошла ошибка");
return false;
}
2023-04-25 08:54:06 +04:00
count -= cnt;
if (count == 0)
{
2023-04-25 08:54:06 +04:00
return true;
}
}
2023-03-28 10:02:32 +04:00
return true;
}
2023-04-25 08:54:06 +04:00
//свободные места со всех магазинов
public int GetFreePlaces(int countIceCreams)
{
// Сумма разностей между максимальный кол-вом изделий и суммой всех изделий в магазине
return _shopStorage.GetFullList()
.Select(x => x.IceCreamMaxCount - x.ShopIceCreams.Select(p => p.Value.Item2).Sum())
.Sum() - countIceCreams;
}
2023-03-13 13:25:55 +04:00
public bool SellIceCreams(IIceCreamModel iceCream, int count)
{
2023-03-13 13:25:55 +04:00
return _shopStorage.SellIceCreams(iceCream, count);
}
public bool Create(ShopBindingModel model)
{
CheckModel(model);
if (_shopStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ShopBindingModel model)
{
CheckModel(model);
if (_shopStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ShopBindingModel model)
{
CheckModel(model, false);
if (_shopStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ShopBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия магазина!", nameof(model.Name));
}
_logger.LogInformation("Shop. Name: {0}, Adress: {1}, ID: {2}", model.Name, model.Adress, model.Id);
var element = _shopStorage.GetElement(new ShopSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
}
}