From 3b0e3e475adce9bd67ff7f21212fd913dce90c89 Mon Sep 17 00:00:00 2001 From: Marselchi Date: Thu, 30 May 2024 03:32:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BE=D0=BC=D0=B5=D0=B3=D0=B0=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=87=D0=B8=D0=BD=D0=BA=D0=B0=20=D0=B2=203=20=D0=BD=D0=BE?= =?UTF-8?q?=D1=87=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/CarSearchModel.cs | 2 + .../Implements/BundlingStorage.cs | 36 +++++++-- .../Implements/CarStorage.cs | 14 ++++ .../Implements/OrderStorage.cs | 48 +++++++---- .../Implements/PresaleStorage.cs | 8 +- ...r.cs => 20240529194318_pochti.Designer.cs} | 4 +- ...8_predFin2.cs => 20240529194318_pochti.cs} | 2 +- .../Models/Bundling.cs | 60 ++++++++++---- .../CarCenterDatabaseImplement/Models/Car.cs | 5 +- .../Models/Order.cs | 40 +++++++-- .../Models/Presale.cs | 39 ++++++--- .../StorekeeperData.cs | 81 ++++++++++++++----- .../Views/Home/CreateBundling.cshtml | 26 +++--- .../Views/Home/CreateCar.cshtml | 6 +- .../Views/Home/CreateFeature.cshtml | 4 +- .../Controllers/HomeController.cs | 6 +- .../Views/Home/CreateOrder.cshtml | 4 +- .../Views/Home/CreatePresale.cshtml | 2 +- .../Views/Home/CreateRequest.cshtml | 2 +- 19 files changed, 280 insertions(+), 109 deletions(-) rename CarCenter/CarCenterDatabaseImplement/Migrations/{20240529170508_predFin2.Designer.cs => 20240529194318_pochti.Designer.cs} (99%) rename CarCenter/CarCenterDatabaseImplement/Migrations/{20240529170508_predFin2.cs => 20240529194318_pochti.cs} (99%) diff --git a/CarCenter/CarCenterContracts/SearchModels/CarSearchModel.cs b/CarCenter/CarCenterContracts/SearchModels/CarSearchModel.cs index bd280c0..251cd83 100644 --- a/CarCenter/CarCenterContracts/SearchModels/CarSearchModel.cs +++ b/CarCenter/CarCenterContracts/SearchModels/CarSearchModel.cs @@ -11,6 +11,8 @@ namespace CarCenterContracts.SearchModels public int? Id { get; set; } public long? VINnumber { get; set; } public int? StorekeeperId { get; set; } + public int? FeatureId { get; set; } + public int? OrderId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/CarCenter/CarCenterDatabaseImplement/Implements/BundlingStorage.cs b/CarCenter/CarCenterDatabaseImplement/Implements/BundlingStorage.cs index a35963d..0f08971 100644 --- a/CarCenter/CarCenterDatabaseImplement/Implements/BundlingStorage.cs +++ b/CarCenter/CarCenterDatabaseImplement/Implements/BundlingStorage.cs @@ -3,6 +3,7 @@ using CarCenterContracts.SearchModels; using CarCenterContracts.StoragesContracts; using CarCenterContracts.ViewModels; using CarCenterDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -17,7 +18,9 @@ namespace CarCenterDatabaseImplement.Implements { using var context = new CarCenterDatabase(); return context.Bundlings - .Select(x => x.GetViewModel) + .Include(x => x.PresaleBundling) + .ThenInclude(x => x.Presale) + .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(BundlingSearchModel model) @@ -28,9 +31,15 @@ namespace CarCenterDatabaseImplement.Implements } using var context = new CarCenterDatabase(); if (model.DateFrom.HasValue) - return context.Bundlings.Where(x => x.StorekeeperId == model.StorekeeperId).Where(x => x.DateCreate <= model.DateTo && x.DateCreate >= model.DateFrom).Select(x => x.GetViewModel).ToList(); + return context.Bundlings + .Include(x => x.PresaleBundling) + .ThenInclude(x => x.Presale) + .Where(x => x.StorekeeperId == model.StorekeeperId).Where(x => x.DateCreate <= model.DateTo && x.DateCreate >= model.DateFrom).Select(x => x.GetViewModel).ToList(); else - return context.Bundlings.Where(x => x.StorekeeperId == model.StorekeeperId).Select(x => x.GetViewModel).ToList(); + return context.Bundlings + .Include(x => x.PresaleBundling) + .ThenInclude(x => x.Presale) + .Where(x => x.StorekeeperId == model.StorekeeperId).Select(x => x.GetViewModel).ToList(); } public BundlingViewModel? GetElement(BundlingSearchModel model) { @@ -40,13 +49,15 @@ namespace CarCenterDatabaseImplement.Implements return null; } return context.Bundlings - .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + .Include(x => x.PresaleBundling) + .ThenInclude(x => x.Presale) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; } public BundlingViewModel? Insert(BundlingBindingModel model) { using var context = new CarCenterDatabase(); - var newBundling = Bundling.Create(model); + var newBundling = Bundling.Create(context, model); if (newBundling == null) { return null; @@ -54,7 +65,9 @@ namespace CarCenterDatabaseImplement.Implements context.Bundlings.Add(newBundling); context.SaveChanges(); return context.Bundlings - .FirstOrDefault(x => x.Id == newBundling.Id) + .Include(x => x.PresaleBundling) + .ThenInclude(x => x.Presale) + .FirstOrDefault(x => x.Id == newBundling.Id) ?.GetViewModel; } public BundlingViewModel? Update(BundlingBindingModel model) @@ -66,8 +79,11 @@ namespace CarCenterDatabaseImplement.Implements return null; } order.Update(model); + order.UpdatePresales(context, model); context.SaveChanges(); return context.Bundlings + .Include(x => x.PresaleBundling) + .ThenInclude(x => x.Presale) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; } @@ -75,11 +91,15 @@ namespace CarCenterDatabaseImplement.Implements { using var context = new CarCenterDatabase(); var element = context.Bundlings - .FirstOrDefault(rec => rec.Id == model.Id); + .Include(x => x.PresaleBundling) + .ThenInclude(x => x.Presale) + .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { var deletedElement = context.Bundlings - .FirstOrDefault(x => x.Id == model.Id) + .Include(x => x.PresaleBundling) + .ThenInclude(x => x.Presale) + .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; context.Bundlings.Remove(element); context.SaveChanges(); diff --git a/CarCenter/CarCenterDatabaseImplement/Implements/CarStorage.cs b/CarCenter/CarCenterDatabaseImplement/Implements/CarStorage.cs index 9cf950a..1c1856f 100644 --- a/CarCenter/CarCenterDatabaseImplement/Implements/CarStorage.cs +++ b/CarCenter/CarCenterDatabaseImplement/Implements/CarStorage.cs @@ -50,6 +50,19 @@ namespace CarCenterDatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } + else if (model.FeatureId.HasValue) + { + return context.Cars + .Include(x => x.Bundlings) + .ThenInclude(x => x.Bundling) + .Include(x => x.Feature) + .Where(x => x.FeatureID == model.FeatureId && x.OrderId == model.OrderId) + .Include(x => x.Order) + .Include(x => x.Storekeeper) + .Select(x => x.GetViewModel) + .ToList(); + + } return new(); } public CarViewModel? GetElement(CarSearchModel model) @@ -96,6 +109,7 @@ namespace CarCenterDatabaseImplement.Implements return null; } car.Update(model); + car.UpdateBundlings(context, model); context.SaveChanges(); return context.Cars .Include(x => x.Bundlings) diff --git a/CarCenter/CarCenterDatabaseImplement/Implements/OrderStorage.cs b/CarCenter/CarCenterDatabaseImplement/Implements/OrderStorage.cs index 1c3d50c..0a068a7 100644 --- a/CarCenter/CarCenterDatabaseImplement/Implements/OrderStorage.cs +++ b/CarCenter/CarCenterDatabaseImplement/Implements/OrderStorage.cs @@ -3,6 +3,7 @@ using CarCenterContracts.SearchModels; using CarCenterContracts.StoragesContracts; using CarCenterContracts.ViewModels; using CarCenterDatabaseImplement.Models; +using CarCenterDataModels.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -57,21 +58,36 @@ namespace CarCenterDatabaseImplement.Implements if (model.DateFrom.HasValue && model.DateTo.HasValue) //для списка Сагиров { - //будет применятся в ReportLogic - return context.Orders - .Where(x => x.WorkerId == model.WorkerId) - .Include(x => x.Worker) - .Include(x => x.Cars) - .Include(x => x.Presales) - .ThenInclude(x => x.Presale) - .ThenInclude(x => x.Requests) - .Include(x => x.Presales) - .ThenInclude(x => x.Presale) - .ThenInclude(x => x.Bundlings) - .ThenInclude(x => x.Bundling) - .Where(x => x.PaymentDate >= model.DateFrom && x.PaymentDate <= model.DateTo) - .Select(x => x.GetViewModel) - .ToList(); + if(model.WorkerId.HasValue) + { + return context.Orders + .Include(x => x.Worker) + .Where(x => x.WorkerId == model.WorkerId) + .Include(x => x.Cars) + .Include(x => x.Presales) + .ThenInclude(x => x.Presale) + .ThenInclude(x => x.Requests) + .Include(x => x.Presales) + //.Where(x => x.PaymentDate >= model.DateFrom && x.PaymentDate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return context.Orders + .Include(x => x.Worker) + //.Where(x => x.WorkerId == model.WorkerId) + .Include(x => x.Cars) + .ThenInclude(y => y.Bundlings.Where(b => b.BundlingId == model.BundlingId)) + .Include(x => x.Presales) + .ThenInclude(x => x.Presale) + .ThenInclude(x => x.Requests) + .Include(x => x.Presales) + //.Where(x => x.PaymentDate >= model.DateFrom && x.PaymentDate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + } else if (model.Presales.Count > 0) //для отчета Сагиров { @@ -152,6 +168,8 @@ namespace CarCenterDatabaseImplement.Implements return null; } order.Update(model); + order.UpdateCars(context, model); + order.UpdatePresales(context, model); context.SaveChanges(); return context.Orders .Include(x => x.Worker) diff --git a/CarCenter/CarCenterDatabaseImplement/Implements/PresaleStorage.cs b/CarCenter/CarCenterDatabaseImplement/Implements/PresaleStorage.cs index 046daed..c59814a 100644 --- a/CarCenter/CarCenterDatabaseImplement/Implements/PresaleStorage.cs +++ b/CarCenter/CarCenterDatabaseImplement/Implements/PresaleStorage.cs @@ -23,7 +23,6 @@ namespace CarCenterDatabaseImplement.Implements { var deletedElement = context.Presales .Include(x => x.Bundlings) - .ThenInclude(x => x.Bundling) .Include(x => x.Requests) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; @@ -43,7 +42,6 @@ namespace CarCenterDatabaseImplement.Implements } return context.Presales .Include(x => x.Bundlings) - .ThenInclude(x => x.Bundling) .Include(x => x.Requests) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; @@ -56,7 +54,6 @@ namespace CarCenterDatabaseImplement.Implements { return context.Presales .Include(x => x.Bundlings) - .ThenInclude(x => x.Bundling) .Include(x => x.Requests) .Where(x => x.WorkerId == model.WorkerId) .Select(x => x.GetViewModel) @@ -70,7 +67,6 @@ namespace CarCenterDatabaseImplement.Implements using var context = new CarCenterDatabase(); return context.Presales .Include(x => x.Bundlings) - .ThenInclude(x => x.Bundling) .Include(x => x.Requests) .Select(x => x.GetViewModel) .ToList(); @@ -88,7 +84,6 @@ namespace CarCenterDatabaseImplement.Implements context.SaveChanges(); return context.Presales .Include(x => x.Bundlings) - .ThenInclude(x => x.Bundling) .Include(x => x.Requests) .FirstOrDefault(x => x.Id == newPresale.Id) ?.GetViewModel; @@ -104,11 +99,10 @@ namespace CarCenterDatabaseImplement.Implements } order.Update(model); order.UpdateBundlings(context, model); - order.UpdateBundlings(context, model); + order.UpdateRequests(context, model); context.SaveChanges(); return context.Presales .Include(x => x.Bundlings) - .ThenInclude(x => x.Bundling) .Include(x => x.Requests) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; diff --git a/CarCenter/CarCenterDatabaseImplement/Migrations/20240529170508_predFin2.Designer.cs b/CarCenter/CarCenterDatabaseImplement/Migrations/20240529194318_pochti.Designer.cs similarity index 99% rename from CarCenter/CarCenterDatabaseImplement/Migrations/20240529170508_predFin2.Designer.cs rename to CarCenter/CarCenterDatabaseImplement/Migrations/20240529194318_pochti.Designer.cs index 22fc028..a1061c7 100644 --- a/CarCenter/CarCenterDatabaseImplement/Migrations/20240529170508_predFin2.Designer.cs +++ b/CarCenter/CarCenterDatabaseImplement/Migrations/20240529194318_pochti.Designer.cs @@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace CarCenterDatabaseImplement.Migrations { [DbContext(typeof(CarCenterDatabase))] - [Migration("20240529170508_predFin2")] - partial class predFin2 + [Migration("20240529194318_pochti")] + partial class pochti { protected override void BuildTargetModel(ModelBuilder modelBuilder) { diff --git a/CarCenter/CarCenterDatabaseImplement/Migrations/20240529170508_predFin2.cs b/CarCenter/CarCenterDatabaseImplement/Migrations/20240529194318_pochti.cs similarity index 99% rename from CarCenter/CarCenterDatabaseImplement/Migrations/20240529170508_predFin2.cs rename to CarCenter/CarCenterDatabaseImplement/Migrations/20240529194318_pochti.cs index 9554e73..9d58383 100644 --- a/CarCenter/CarCenterDatabaseImplement/Migrations/20240529170508_predFin2.cs +++ b/CarCenter/CarCenterDatabaseImplement/Migrations/20240529194318_pochti.cs @@ -6,7 +6,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace CarCenterDatabaseImplement.Migrations { - public partial class predFin2 : Migration + public partial class pochti : Migration { protected override void Up(MigrationBuilder migrationBuilder) { diff --git a/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs b/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs index 03c82e1..6276f3e 100644 --- a/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs +++ b/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs @@ -31,7 +31,21 @@ namespace CarCenterDatabaseImplement.Models public virtual List CarBundling { get; set; } = new(); [ForeignKey("BundlingId")] public virtual List PresaleBundling { get; set; } = new(); - public static Bundling? Create(BundlingBindingModel model) + private Dictionary? _bundlingPresales = null; + public Dictionary BundlingPresales + { + get + { + if (_bundlingPresales == null) + { + _bundlingPresales = PresaleBundling + .GroupBy(recPc => recPc.PresaleId) + .ToDictionary(g => g.Key, g => g.First().Presale as IPresaleModel); + } + return _bundlingPresales; + } + } + public static Bundling? Create(CarCenterDatabase context, BundlingBindingModel model) { if (model == null) { @@ -46,20 +60,11 @@ namespace CarCenterDatabaseImplement.Models ToolKit = model.ToolKit, Price = model.Price, DateCreate = model.DateCreate, - }; - } - public static Bundling Create(BundlingViewModel model) - { - return new Bundling - { - Id = model.Id, - StorekeeperId=model.StorekeeperId, - EquipmentPackage = model.EquipmentPackage, - TirePackage = model.TirePackage, - ToolKit = model.ToolKit, - Price = model.Price, - DateCreate = model.DateCreate, - }; + PresaleBundling = model.BundlingsPresale.Select(x => new PresaleBundling + { + Presale = context.Presales.First(y => y.Id == x.Value.Id) + }).ToList() + }; } public void Update(BundlingBindingModel model) { @@ -74,7 +79,29 @@ namespace CarCenterDatabaseImplement.Models ToolKit = model.ToolKit; Price = model.Price; } - public BundlingViewModel GetViewModel => new() + public void UpdatePresales(CarCenterDatabase context, BundlingBindingModel model) + { + var bundling = context.Bundlings.First(x => x.Id == Id); + var existingPresaleBundlings = context.PresaleBundlings.Where(pb => pb.BundlingId == bundling.Id).ToList(); + context.PresaleBundlings.RemoveRange(existingPresaleBundlings); + context.SaveChanges(); + foreach (var pc in model.BundlingsPresale) + { + var tmp = new PresaleBundling + { + Bundling = bundling, + Presale = context.Presales.First(x => x.Id == pc.Value.Id), + }; + if (context.PresaleBundlings.Contains(tmp)) + { + continue; + } + context.PresaleBundlings.Add(tmp); + context.SaveChanges(); + } + _bundlingPresales = null; + } + public BundlingViewModel GetViewModel => new() { Id = Id, StorekeeperId = StorekeeperId, @@ -83,6 +110,7 @@ namespace CarCenterDatabaseImplement.Models ToolKit = ToolKit, Price = Price, DateCreate = DateCreate, + BundlingsPresale = BundlingPresales, }; } } diff --git a/CarCenter/CarCenterDatabaseImplement/Models/Car.cs b/CarCenter/CarCenterDatabaseImplement/Models/Car.cs index 47816c9..137f200 100644 --- a/CarCenter/CarCenterDatabaseImplement/Models/Car.cs +++ b/CarCenter/CarCenterDatabaseImplement/Models/Car.cs @@ -85,7 +85,10 @@ namespace CarCenterDatabaseImplement.Models public void UpdateBundlings(CarCenterDatabase context, CarBindingModel model) { var car = context.Cars.First(x => x.Id == Id); - foreach (var pc in model.CarBundlings) + var existingBundling = context.CarBundlings.Where(pb => pb.CarId == car.Id).ToList(); + context.CarBundlings.RemoveRange(existingBundling); + context.SaveChanges(); + foreach (var pc in model.CarBundlings) { context.CarBundlings.Add(new CarBundling { diff --git a/CarCenter/CarCenterDatabaseImplement/Models/Order.cs b/CarCenter/CarCenterDatabaseImplement/Models/Order.cs index 1ac4db5..1ffbd2e 100644 --- a/CarCenter/CarCenterDatabaseImplement/Models/Order.cs +++ b/CarCenter/CarCenterDatabaseImplement/Models/Order.cs @@ -38,7 +38,9 @@ namespace CarCenterDatabaseImplement.Models { if(_orderPresales == null) { - _orderPresales = Presales.ToDictionary(recPc => recPc.PresaleId, recPc => recPc.Presale as IPresaleModel); + _orderPresales = Presales + .GroupBy(recPc => recPc.PresaleId) + .ToDictionary(g => g.Key, g => g.First().Presale as IPresaleModel); } return _orderPresales; } @@ -91,18 +93,44 @@ namespace CarCenterDatabaseImplement.Models public void UpdatePresales(CarCenterDatabase context, OrderBindingModel model) { var order = context.Orders.First(x => x.Id == Id); - foreach (var pc in model.OrderPresales) + var existingPresale = context.OrderPresales.Where(pb => pb.OrderId == order.Id).ToList(); + context.OrderPresales.RemoveRange(existingPresale); + context.SaveChanges(); + foreach (var pc in model.OrderPresales) { - context.OrderPresales.Add(new OrderPresale + var tmp = new OrderPresale { Order = order, - Presale = context.Presales.First(x => x.Id == pc.Key), - }); + Presale = context.Presales.First(x => x.Id == pc.Value.Id), + }; + if (context.OrderPresales.Contains(tmp)) + { + continue; + } + context.OrderPresales.Add(tmp); context.SaveChanges(); } _orderPresales = null; } - public OrderViewModel GetViewModel => new() + public void UpdateCars(CarCenterDatabase context, OrderBindingModel model) + { + var order = context.Orders.First(x => x.Id == Id); + order.Cars.Clear(); + foreach (var car in model.Cars) + { + var cartmp = context.Cars.FirstOrDefault(x => x.Id == car.Value.Id); + if (cartmp != null) + { + if (order.Cars.Contains(cartmp)) + { + continue; + } + order.Cars.Add(cartmp); + } + } + context.SaveChanges(); + } + public OrderViewModel GetViewModel => new() { Id = Id, WorkerId = WorkerId, diff --git a/CarCenter/CarCenterDatabaseImplement/Models/Presale.cs b/CarCenter/CarCenterDatabaseImplement/Models/Presale.cs index 3deefd2..15be166 100644 --- a/CarCenter/CarCenterDatabaseImplement/Models/Presale.cs +++ b/CarCenter/CarCenterDatabaseImplement/Models/Presale.cs @@ -31,15 +31,18 @@ namespace CarCenterDatabaseImplement.Models private Dictionary? _presaleBundlings = null; [ForeignKey("PresaleId")] - public virtual List Bundlings { get; set; } = new(); - public Dictionary PresaleBundlings + public virtual List? Bundlings { get; set; } = new(); + [NotMapped] + public Dictionary PresaleBundlings { get { if (_presaleBundlings == null) { - _presaleBundlings = Bundlings.ToDictionary(recPc => recPc.BundlingId, recPc => recPc.Bundling as IBundlingModel); - } + _presaleBundlings = Bundlings + .GroupBy(recPc => recPc.BundlingId) + .ToDictionary(g => g.Key, g => g.First().Bundling as IBundlingModel); + } return _presaleBundlings; } } @@ -59,13 +62,13 @@ namespace CarCenterDatabaseImplement.Models WorkerId = model.WorkerId, Bundlings = model.PresaleBundlings.Select(x => new PresaleBundling { - Bundling = context.Bundlings.First(y => y.Id == x.Value.Id) + Bundling = context.Bundlings.First(y => y.Id == x.Key) }).ToList() }; foreach (var request in model.Requests) { - var requesttmp = context.Requests.FirstOrDefault(x => x.Id == request.Value.Id); + var requesttmp = context.Requests.FirstOrDefault(x => x.Id == request.Key); if (requesttmp != null) { presale.Requests.Add(requesttmp); @@ -75,29 +78,41 @@ namespace CarCenterDatabaseImplement.Models return presale; } - public void UpdateBundlings(CarCenterDatabase context, PresaleBindingModel model) + public void UpdateRequests(CarCenterDatabase context, PresaleBindingModel model) { var presale = context.Presales.First(x => x.Id == Id); foreach (var request in model.Requests) { - var requesttmp = context.Requests.FirstOrDefault(x => x.Id == request.Value.Id); + var requesttmp = context.Requests.FirstOrDefault(x => x.Id == request.Key); if (requesttmp != null) { + if (presale.Requests.Contains(requesttmp)) + { + continue; + } presale.Requests.Add(requesttmp); } } } - public void UpdateRequests(CarCenterDatabase context, PresaleBindingModel model) + public void UpdateBundlings(CarCenterDatabase context, PresaleBindingModel model) { var presale = context.Presales.First(x => x.Id == Id); + var existingBundling = context.PresaleBundlings.Where(pb => pb.PresaleId == presale.Id).ToList(); + context.PresaleBundlings.RemoveRange(existingBundling); + context.SaveChanges(); foreach (var pc in model.PresaleBundlings) { - context.PresaleBundlings.Add(new PresaleBundling + var tmp = new PresaleBundling { Presale = presale, - Bundling = context.Bundlings.First(x => x.Id == pc.Value.Id), - }); + Bundling = context.Bundlings.First(x => x.Id == pc.Key), + }; + if (context.PresaleBundlings.Contains(tmp)) + { + continue; + } + context.PresaleBundlings.Add(tmp); context.SaveChanges(); } _presaleBundlings = null; diff --git a/CarCenter/CarCenterStorekeeperApp/StorekeeperData.cs b/CarCenter/CarCenterStorekeeperApp/StorekeeperData.cs index 61ff343..fc9b73f 100644 --- a/CarCenter/CarCenterStorekeeperApp/StorekeeperData.cs +++ b/CarCenter/CarCenterStorekeeperApp/StorekeeperData.cs @@ -5,6 +5,8 @@ using CarCenterContracts.StoragesContracts; using CarCenterContracts.SearchModels; using CarCenterBusinessLogic.MailWorker; using CarCenterBusinessLogic.OfficePackage; +using CarCenterDatabaseImplement.Models; +using DocumentFormat.OpenXml.Bibliography; namespace StorekeeperApp { @@ -120,24 +122,56 @@ namespace StorekeeperApp { return _carLogic.Delete(new() { Id = carId }); } - public List GetTimeReport(DateTime? startDate, DateTime? endDate, int UserId) + public List GetTimeReport(DateTime? startDate, DateTime? endDate, int userId) { - var bundlings = _bundlingLogic.ReadList(new() { DateFrom = startDate, DateTo = endDate, StorekeeperId = UserId }); + var bundlings = _bundlingLogic.ReadList(new BundlingSearchModel { StorekeeperId = userId }); if (bundlings == null) - return new(); + return new List(); + List bundlingTimeReports = new List(); + foreach (var bundling in bundlings) { - var report = new ReportBundlingViewModel(); - report.BundlingId = bundling.Id; - var features = _featureLogic.ReadList(new() { BundlingId = bundling.Id, StorekeeperId = UserId }); - if (features != null) - report.Features = features.Select(p => p.Id.ToString()).ToList(); - var orders = _orderLogic.ReadList(new() { BundlingId = bundling.Id, WorkerId = UserId }); - if (orders != null) - report.Orders = orders.Select(p => p.Id.ToString()).ToList(); + var report = new ReportBundlingViewModel + { + BundlingId = bundling.Id, + Features = new List(), + Orders = new List() + }; + + // Получение Orders + var orders = _orderLogic.ReadList(new OrderSearchModel { BundlingId = bundling.Id, DateFrom = startDate, DateTo = endDate }); + if (orders?.Count != 0 && orders != null) + { + // Получение Features из машин в заказах + var features = new List(); + foreach (var order in orders) + { + bool fnd = false; + var cars = order.Cars; + foreach (var car in cars) { + if(car.Value.CarBundlings.Count <= 0) + { + continue; + } + var carFeatures = _carLogic.ReadList(new CarSearchModel { FeatureId = car.Value.FeatureID, OrderId = order.Id }); + if (carFeatures?.Count != 0 && carFeatures != null) + { + fnd = true; + features.AddRange(carFeatures.Select(f => f.FeatureID.ToString())); + } + } + if (fnd) + { + report.Orders.Add(order.Id.ToString()); + } + } + report.Features = features.Distinct().ToList(); // Удаление дубликатов + } + bundlingTimeReports.Add(report); } + return bundlingTimeReports; } public List? GetPresales() @@ -149,18 +183,25 @@ namespace StorekeeperApp return _orderLogic.ReadList(null); } - public List? GetPresaleReports(List bundlings) + public List? GetPresaleReports(List cars) { List reports = new(); - foreach (int i in bundlings) + + var orders = _orderLogic.ReadList(null); + foreach (var order in orders) { - ReportCarViewModel report = new(); - var bundling = _carLogic.ReadElement(new() { Id = i }); - report.VINnumber = (int)bundling!.VINnumber; - var presales = _presaleLogic.ReadList(new() { CarId = i }); - if (presales != null) - report.Presales = presales.Select(w => w.Id.ToString()).ToList(); - reports.Add(report); + var Cars = order.Cars; + foreach (var car in Cars) + { + ReportCarViewModel report = new(); + report.VINnumber = (int)car.Value.VINnumber; + if (cars.Contains(car.Value.Id)) + { + report.Presales = order.OrderPresales.Select(p => p.Value.Id.ToString()).ToList(); + } + reports.Add(report); + } + } return reports; } diff --git a/CarCenter/CarCenterStorekeeperApp/Views/Home/CreateBundling.cshtml b/CarCenter/CarCenterStorekeeperApp/Views/Home/CreateBundling.cshtml index dceaf88..afb8dd8 100644 --- a/CarCenter/CarCenterStorekeeperApp/Views/Home/CreateBundling.cshtml +++ b/CarCenter/CarCenterStorekeeperApp/Views/Home/CreateBundling.cshtml @@ -16,7 +16,7 @@ @@ -28,7 +28,7 @@ @@ -40,7 +40,7 @@ @@ -99,6 +99,11 @@