From 51a8119546cb00f10fce0baa48159baa1a8b4921 Mon Sep 17 00:00:00 2001 From: dasha Date: Thu, 6 Apr 2023 21:18:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D0=BD=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=20=D0=BE=D1=82=D1=87=D0=B5=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Storekeeper/ReportStorekeeperLogic.cs | 83 ++++++------------- ...proj => HardwareShopStorekeeperApp.csproj} | 0 HardwareShop/HardwareShopClientApp/Program.cs | 33 +------- .../BindingModels/ComponentBindingModel.cs | 2 + .../IReportStorekeeperLogic.cs | 3 +- .../SearchModels/ComponentSearchModel.cs | 2 + .../StoragesContracts/IComponentStorage.cs | 1 + .../ViewModels/ComponentViewModel.cs | 6 +- .../ViewModels/ReportComponentsViewModel.cs | 2 +- .../Models/IComponentModel.cs | 2 +- .../Storekeeper/ComponentStorage.cs | 24 ++++++ .../Models/Storekeeper/Component.cs | 11 ++- HardwareShop/HardwareShopView.sln | 2 +- 13 files changed, 72 insertions(+), 99 deletions(-) rename HardwareShop/HardwareShopClientApp/{HardwareShopClientApp.csproj => HardwareShopStorekeeperApp.csproj} (100%) diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs index 81ed199..6e26152 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs @@ -1,27 +1,17 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.BusinessLogicsContracts; -using HardwareShopContracts.SearchModels; using HardwareShopContracts.StoragesContracts; using HardwareShopContracts.ViewModels; -using HardwareShopDatabaseImplement.Models.Worker; -using HardwareShopDataModels.Models; -using System.Collections.Generic; namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper { public class ReportStorekeeperLogic : IReportStorekeeperLogic { private readonly IComponentStorage _componentStorage; - private readonly IGoodStorage _goodStorage; - private readonly IPurchaseStorage _purchaseStorage; - private readonly IBuildStorage _buildStorage; - public ReportStorekeeperLogic(IComponentStorage componentStorage, IGoodStorage goodStorage, IPurchaseStorage purchaseStorage, IBuildStorage buildStorage) + public ReportStorekeeperLogic(IComponentStorage componentStorage) { _componentStorage = componentStorage; - _goodStorage = goodStorage; - _purchaseStorage = purchaseStorage; - _buildStorage = buildStorage; } public List GetBuildGood(List goods) { @@ -46,60 +36,39 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper } return result; } - - public List GetComponents(ReportBindingModel model) + /// Получение сведений по комплектующим за период, + /// с указанием в каких товарах и сборках они использовались + public List GetComponents(UserBindingModel user, ReportBindingModel model) { - var list = new List(); - var purchases = _purchaseStorage - .GetFilteredList(new PurchaseSearchModel - { - DateFrom = model.DateFrom, - DateTo = model.DateTo - }); - var components = _componentStorage.GetFullList(); + var result = new List(); + var components = _componentStorage.GetFilteredList(new() + { + UserId = user.Id, + DateFrom = model.DateFrom, + DateTo = model.DateTo + }); + foreach (var component in components) { - var record = new ReportComponentsViewModel + var builds = component.ComponentBuilds + .Select(x => Tuple.Create(x.Value.Item1.BuildName, x.Value.Item2)) + .ToList(); + + var goods = component.ComponentBuilds + .SelectMany(x => + _componentStorage.GetComponentGoods(new() { Id = x.Key })) + .ToList(); + + ReportComponentsViewModel record = new() { ComponentName = component.ComponentName, - TotalCount = 0, - GoodOrBuilds = new List<(string GoodOrBuild, int Count)>() + TotalCount = builds.Sum(x => x.Item2) + goods.Sum(x => x.Item2), + GoodOrBuilds = builds.Concat(goods).ToList() }; - foreach (var purchase in purchases) - { - int sum = 0; - foreach (var build in purchase.PurchaseBuilds) - { - int countBuildsInPurchase = build.Value.Item2; - if (build.Value.Item1.BuildComponents.ContainsKey(component.Id)) - { - int countComponentsInBuild = build.Value.Item1 - .BuildComponents.First(x => x.Key == component.Id) - .Value.Item2; - sum += countComponentsInBuild * countBuildsInPurchase; - - record.GoodOrBuilds.Add((build.Value.Item1.BuildName, countComponentsInBuild)); - } - } - foreach (var good in purchase.PurchaseGoods) - { - int countGoodsInPurchase = good.Value.Item2; - if (good.Value.Item1.GoodComponents.ContainsKey(component.Id)) - { - int countComponentsInGood = good.Value.Item1 - .GoodComponents.First(x => x.Key == component.Id) - .Value.Item2; - sum += countComponentsInGood * countGoodsInPurchase; - - record.GoodOrBuilds.Add((good.Value.Item1.GoodName, countComponentsInGood)); - } - } - record.TotalCount = sum; - } + result.Add(record); } - - return list; + return result; } } } diff --git a/HardwareShop/HardwareShopClientApp/HardwareShopClientApp.csproj b/HardwareShop/HardwareShopClientApp/HardwareShopStorekeeperApp.csproj similarity index 100% rename from HardwareShop/HardwareShopClientApp/HardwareShopClientApp.csproj rename to HardwareShop/HardwareShopClientApp/HardwareShopStorekeeperApp.csproj diff --git a/HardwareShop/HardwareShopClientApp/Program.cs b/HardwareShop/HardwareShopClientApp/Program.cs index 197e9bb..9ff3bc8 100644 --- a/HardwareShop/HardwareShopClientApp/Program.cs +++ b/HardwareShop/HardwareShopClientApp/Program.cs @@ -1,9 +1,4 @@ using HardwareShopClientApp; -using HardwareShopBusinessLogic.BusinessLogics.Storekeeper; -using HardwareShopDatabaseImplement.Implements.Storekeeper; -using HardwareShopDatabaseImplement.Implements.Worker; -/* - var builder = WebApplication.CreateBuilder(args); @@ -31,30 +26,4 @@ app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); -//app.Run(); -*/ - - - -ComponentStorage _componentStorage = new(); -GoodStorage _goodStorage = new(); -PurchaseStorage _purchaseStorage = new(); -BuildStorage _buildStorage = new(); - -ReportStorekeeperLogic reportLogic = new(_componentStorage, _goodStorage, _purchaseStorage, _buildStorage); - -var goods = _goodStorage.GetFilteredList(new() { UserId = 1 }); -var reportRecords = reportLogic.GetBuildGood(goods); - -Console.WriteLine("start"); -foreach (var reportRecord in reportRecords) -{ - Console.WriteLine("good: " + reportRecord.GoodName); - Console.WriteLine("> builds:"); - for (int i = 0; i < reportRecord.Builds.Count; i++) - { - Console.WriteLine(i + 1 + ". " + reportRecord.Builds[i]); - } - Console.WriteLine(); -} - +app.Run(); diff --git a/HardwareShop/HardwareShopContracts/BindingModels/ComponentBindingModel.cs b/HardwareShop/HardwareShopContracts/BindingModels/ComponentBindingModel.cs index 6b2c2dd..e84a860 100644 --- a/HardwareShop/HardwareShopContracts/BindingModels/ComponentBindingModel.cs +++ b/HardwareShop/HardwareShopContracts/BindingModels/ComponentBindingModel.cs @@ -12,6 +12,8 @@ namespace HardwareShopContracts.BindingModels public int UserId { get; set; } + public DateTime DateCreate { get; set; } = DateTime.Now; + public Dictionary ComponentBuilds { get; diff --git a/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IReportStorekeeperLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IReportStorekeeperLogic.cs index d404576..ea93957 100644 --- a/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IReportStorekeeperLogic.cs +++ b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IReportStorekeeperLogic.cs @@ -16,7 +16,8 @@ namespace HardwareShopContracts.BusinessLogicsContracts /// с указанием в каких товарах и сборках они использовались /// /// + /// /// - List GetComponents(ReportBindingModel model); + List GetComponents(UserBindingModel user, ReportBindingModel model); } } diff --git a/HardwareShop/HardwareShopContracts/SearchModels/ComponentSearchModel.cs b/HardwareShop/HardwareShopContracts/SearchModels/ComponentSearchModel.cs index 2898ce0..17f5719 100644 --- a/HardwareShop/HardwareShopContracts/SearchModels/ComponentSearchModel.cs +++ b/HardwareShop/HardwareShopContracts/SearchModels/ComponentSearchModel.cs @@ -5,5 +5,7 @@ public int? Id { get; set; } public string? ComponentName { get; set; } public int? UserId { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } } } diff --git a/HardwareShop/HardwareShopContracts/StoragesContracts/IComponentStorage.cs b/HardwareShop/HardwareShopContracts/StoragesContracts/IComponentStorage.cs index e77aa04..8ad67c1 100644 --- a/HardwareShop/HardwareShopContracts/StoragesContracts/IComponentStorage.cs +++ b/HardwareShop/HardwareShopContracts/StoragesContracts/IComponentStorage.cs @@ -13,5 +13,6 @@ namespace HardwareShopContracts.StoragesContracts ComponentViewModel? Update(ComponentBindingModel model); ComponentViewModel? Delete(ComponentBindingModel model); List> GetComponentBuilds(ComponentSearchModel model); + List> GetComponentGoods(ComponentSearchModel model); } } diff --git a/HardwareShop/HardwareShopContracts/ViewModels/ComponentViewModel.cs b/HardwareShop/HardwareShopContracts/ViewModels/ComponentViewModel.cs index 81825dd..9fba828 100644 --- a/HardwareShop/HardwareShopContracts/ViewModels/ComponentViewModel.cs +++ b/HardwareShop/HardwareShopContracts/ViewModels/ComponentViewModel.cs @@ -8,10 +8,12 @@ namespace HardwareShopContracts.ViewModels public int Id { get; set; } [DisplayName("Компонент")] public string ComponentName { get; set; } = string.Empty; - [DisplayName("Цена")] + [DisplayName("Стоимость")] public double Cost { get; set; } + [DisplayName("Дата приобретения")] + public DateTime DateCreate { get; set; } = DateTime.Now; public int UserId { get; set; } - public Dictionary? ComponentBuilds + public Dictionary ComponentBuilds { get; set; diff --git a/HardwareShop/HardwareShopContracts/ViewModels/ReportComponentsViewModel.cs b/HardwareShop/HardwareShopContracts/ViewModels/ReportComponentsViewModel.cs index 094b415..634f0e0 100644 --- a/HardwareShop/HardwareShopContracts/ViewModels/ReportComponentsViewModel.cs +++ b/HardwareShop/HardwareShopContracts/ViewModels/ReportComponentsViewModel.cs @@ -6,6 +6,6 @@ public int TotalCount { get; set; } - public List<(string GoodOrBuild, int Count)> GoodOrBuilds { get; set; } = new(); + public List> GoodOrBuilds { get; set; } = new(); } } diff --git a/HardwareShop/HardwareShopDataModels/Models/IComponentModel.cs b/HardwareShop/HardwareShopDataModels/Models/IComponentModel.cs index 057a084..fa02c95 100644 --- a/HardwareShop/HardwareShopDataModels/Models/IComponentModel.cs +++ b/HardwareShop/HardwareShopDataModels/Models/IComponentModel.cs @@ -5,6 +5,6 @@ string ComponentName { get; } double Cost { get; } int UserId { get; } - Dictionary? ComponentBuilds { get; } + Dictionary ComponentBuilds { get; } } } diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/ComponentStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/ComponentStorage.cs index 5636352..add17ef 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/ComponentStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/ComponentStorage.cs @@ -45,6 +45,16 @@ namespace HardwareShopDatabaseImplement.Implements.Storekeeper public List GetFilteredList(ComponentSearchModel model) { using var context = new HardwareShopDatabase(); + if (model.UserId.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) + { + return context.Components + .Include(x => x.Builds) + .ThenInclude(x => x.Build) + .Where(x => x.UserId == model.UserId && x.DateCreate >= model.DateFrom + && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } if (model.UserId.HasValue) { return context.Components @@ -132,5 +142,19 @@ namespace HardwareShopDatabaseImplement.Implements.Storekeeper .ToList(); return builds; } + + public List> GetComponentGoods(ComponentSearchModel model) + { + if (model == null) + { + return new(); + } + using var context = new HardwareShopDatabase(); + var goods = context.GoodsComponents + .Where(x => x.ComponentId == model.Id) + .Select(x => Tuple.Create(x.Good.GoodName, x.Count)) + .ToList(); + return goods; + } } } diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs index a1c0cad..12fd9cb 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs @@ -1,7 +1,6 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.ViewModels; using HardwareShopDatabaseImplement.Models.ManyToMany; -using HardwareShopDatabaseImplement.Models.Worker; using HardwareShopDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -20,6 +19,8 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper [Required] public int UserId { get; set; } + [Required] + public DateTime DateCreate { get; set; } [ForeignKey("ComponentId")] public virtual List Goods { get; set; } = new(); @@ -30,7 +31,7 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper private Dictionary? _componentBuilds = null; [NotMapped] - public Dictionary? ComponentBuilds + public Dictionary ComponentBuilds { get { @@ -54,7 +55,8 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper Id = model.Id, ComponentName = model.ComponentName, Cost = model.Cost, - UserId = model.UserId + UserId = model.UserId, + DateCreate = model.DateCreate }; } @@ -74,7 +76,8 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper ComponentName = ComponentName, Cost = Cost, UserId = UserId, - ComponentBuilds = ComponentBuilds + ComponentBuilds = ComponentBuilds, + DateCreate = DateCreate }; public void UpdateBuilds(HardwareShopDatabase context, ComponentBindingModel model) diff --git a/HardwareShop/HardwareShopView.sln b/HardwareShop/HardwareShopView.sln index 8161f98..da831b4 100644 --- a/HardwareShop/HardwareShopView.sln +++ b/HardwareShop/HardwareShopView.sln @@ -11,7 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopBusinessLogic", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopDatabaseImplement", "HardwareShopDatabaseImplement\HardwareShopDatabaseImplement.csproj", "{1E5156F6-1F67-497C-A660-8AC61BC451BC}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopClientApp", "HardwareShopClientApp\HardwareShopClientApp.csproj", "{3DCD1BA3-4D72-4B0E-BB06-323E09B75EBB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopStorekeeperApp", "HardwareShopClientApp\HardwareShopStorekeeperApp.csproj", "{3DCD1BA3-4D72-4B0E-BB06-323E09B75EBB}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HardwareShopWorkerApp", "HardwareShopWorkerApp\HardwareShopWorkerApp.csproj", "{3C590713-871F-4D9B-A97A-3898D4E09506}" EndProject