From 590206804338f4d9f948bc4ce4b317c4e20fa39b Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Mon, 20 May 2024 02:18:31 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0=D0=B6=D0=B8=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=B9=20=D1=81=20=D0=BC=D0=B0?= =?UTF-8?q?=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ShopLogic.cs | 50 +++++---- .../BusinessLogicsContracts/IShopLogic.cs | 8 -- .../StoragesContracts/IShopStorage.cs | 1 + .../Implements/ShopStorage.cs | 102 ++++++++++++++++-- .../Models/Shop.cs | 2 +- .../Implements/ShopStorage.cs | 52 +++++++++ .../Implements/ShopStorage.cs | 6 ++ 7 files changed, 185 insertions(+), 36 deletions(-) diff --git a/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ShopLogic.cs b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ShopLogic.cs index 5f8ad8b..7d1e93d 100644 --- a/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ShopLogic.cs @@ -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(); } } } diff --git a/SecuritySystem/SecuritySystemContracts/BusinessLogicsContracts/IShopLogic.cs b/SecuritySystem/SecuritySystemContracts/BusinessLogicsContracts/IShopLogic.cs index e4a6503..fc8c42b 100644 --- a/SecuritySystem/SecuritySystemContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/SecuritySystem/SecuritySystemContracts/BusinessLogicsContracts/IShopLogic.cs @@ -17,14 +17,6 @@ namespace SecuritySystemContracts.BusinessLogicsContracts /// bool SellSecures(ISecureModel model, int count); /// - /// Проверяет наличие определенного количества продукта суммарно по всем магазинам - /// - bool CheckSecuresCount(ISecureModel model, int count); - /// - /// Проверяет можно ли пополнить конкретный магазин продукцией в указанном количестве - /// - bool CheckSupplySecures(ShopSearchModel shopSearchModel, int count); - /// /// Проверяет можно ли распределить во все магазины продукты в указанном количестве /// bool CheckSupplySecures(int count); diff --git a/SecuritySystem/SecuritySystemContracts/StoragesContracts/IShopStorage.cs b/SecuritySystem/SecuritySystemContracts/StoragesContracts/IShopStorage.cs index e7e624a..112a8ff 100644 --- a/SecuritySystem/SecuritySystemContracts/StoragesContracts/IShopStorage.cs +++ b/SecuritySystem/SecuritySystemContracts/StoragesContracts/IShopStorage.cs @@ -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); } } diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ShopStorage.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ShopStorage.cs index b10383f..4e7adb6 100644 --- a/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ShopStorage.cs +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ShopStorage.cs @@ -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 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 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; + } } } } diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs index 3b2e23b..3f736aa 100644 --- a/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs @@ -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) diff --git a/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs b/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs index 7a12271..23df450 100644 --- a/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs +++ b/SecuritySystem/SecuritySystemFileImplement/Implements/ShopStorage.cs @@ -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; + } } } diff --git a/SecuritySystem/SecuritySystemListImplement/Implements/ShopStorage.cs b/SecuritySystem/SecuritySystemListImplement/Implements/ShopStorage.cs index a46bcb6..81ef1a4 100644 --- a/SecuritySystem/SecuritySystemListImplement/Implements/ShopStorage.cs +++ b/SecuritySystem/SecuritySystemListImplement/Implements/ShopStorage.cs @@ -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(); + } } }