From 5069b9be8007c04b04eb88159ef8576856d889d1 Mon Sep 17 00:00:00 2001 From: Loretta Date: Wed, 15 Oct 2025 07:57:03 +0200 Subject: [PATCH] ... --- .../Controllers/CustomOrderController.cs | 5 +- .../Controllers/CustomOrderSignalREndpoint.cs | 88 +++++++++- .../Domains/DataLayer/FruitBankDbContext.cs | 153 +++++++++++------- .../Interfaces/IOrderItemDtoDbSet.cs | 10 ++ .../MeasuringItemPalletBaseDbTable.cs | 14 +- .../Domains/DataLayer/OrderDtoDbTable.cs | 35 ++-- .../DataLayer/OrderItemPalletDbTable.cs | 15 +- .../DataLayer/ShippingItemPalletDbTable.cs | 12 -- .../Factories/CustomOrderModelFactory.cs | 8 +- .../Infrastructure/PluginNopStartup.cs | 3 + .../Mapping/NameCompatibility.cs | 10 +- 11 files changed, 248 insertions(+), 105 deletions(-) create mode 100644 Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/Interfaces/IOrderItemDtoDbSet.cs diff --git a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs index c5422e4..878a56e 100644 --- a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs @@ -1,5 +1,6 @@ using AyCode.Services.SignalRs; using FruitBank.Common.Dtos; +using FruitBank.Common.Entities; using FruitBank.Common.Interfaces; using FruitBank.Common.Server.Interfaces; using FruitBank.Common.SignalRs; @@ -41,7 +42,9 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers [NonAction] public Task> GetAllOrderDtos() => _customOrderSignalREndpoint.GetAllOrderDtos(); [NonAction]public Task GetOrderDtoById(int orderId) => _customOrderSignalREndpoint.GetOrderDtoById(orderId); [NonAction]public Task> GetPendingOrderDtos() => _customOrderSignalREndpoint.GetPendingOrderDtos(); - [NonAction] public Task> GetAllByIds(int[] orderIds) => _customOrderSignalREndpoint.GetAllByIds(orderIds); + [NonAction]public Task SetOrderStatusToComplete(int orderId) => _customOrderSignalREndpoint.SetOrderStatusToComplete(orderId); + [NonAction] public Task> GetAllOrderDtoByIds(int[] orderIds) => _customOrderSignalREndpoint.GetAllOrderDtoByIds(orderIds); + [NonAction] public Task AddOrUpdateMeasuredOrderItemPallet(OrderItemPallet orderItemPallet) => _customOrderSignalREndpoint.AddOrUpdateMeasuredOrderItemPallet(orderItemPallet); #endregion CustomOrderSignalREndpoint [CheckPermission(StandardPermission.Orders.ORDERS_VIEW)] diff --git a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderSignalREndpoint.cs b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderSignalREndpoint.cs index d4b9657..97209ae 100644 --- a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderSignalREndpoint.cs +++ b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderSignalREndpoint.cs @@ -1,14 +1,21 @@ -using AyCode.Services.SignalRs; +using AyCode.Core.Loggers; +using AyCode.Services.SignalRs; using FruitBank.Common.Dtos; +using FruitBank.Common.Entities; +using FruitBank.Common.Interfaces; using FruitBank.Common.Server.Interfaces; using FruitBank.Common.SignalRs; +using Mango.Nop.Core.Loggers; using Nop.Core.Domain.Orders; +using Nop.Plugin.Misc.FruitBankPlugin.Controllers; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers; -public class CustomOrderSignalREndpoint(FruitBankDbContext ctx) : ICustomOrderSignalREndpointServer +public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, IEnumerable logWriters) : ICustomOrderSignalREndpointServer { + private readonly ILogger _logger = new Logger(logWriters.ToArray()); + [SignalR(SignalRTags.GetAllOrderDtos)] public async Task> GetAllOrderDtos() { @@ -27,9 +34,82 @@ public class CustomOrderSignalREndpoint(FruitBankDbContext ctx) : ICustomOrderSi return await ctx.OrderDtos.GetAllByOrderStatus(OrderStatus.Pending).ToListAsync(); } - [SignalR(SignalRTags.GetAllByIdList)] - public async Task> GetAllByIds(int[] orderIds) + [SignalR(SignalRTags.GetAllOrderDtoByIds)] + public async Task> GetAllOrderDtoByIds(int[] orderIds) { return await ctx.OrderDtos.GetAllByIds(orderIds).ToListAsync(); } + + [SignalR(SignalRTags.SetOrderStatusToComplete)] + public async Task SetOrderStatusToComplete(int orderId) + { + _logger.Detail($"SetOrderStatusToComplete invoked; orderId: {orderId}"); + + if (!await ctx.SetOrderStatusToCompleteSafe(orderId)) return null; + return await ctx.OrderDtos.GetByIdAsync(orderId); + } + + [SignalR(SignalRTags.AddOrUpdateMeasuredOrderItemPallet)] + public async Task AddOrUpdateMeasuredOrderItemPallet(OrderItemPallet orderItemPallet) + { + ArgumentNullException.ThrowIfNull(orderItemPallet); + + _logger.Detail($"AddOrUpdateMeasuredOrderItemPallet invoked; {orderItemPallet}"); + + if (!await ctx.AddOrUpdateOrderItemPalletSafeAsync(orderItemPallet)) return null; + return await ctx.OrderItemPallets.GetByIdAsync(orderItemPallet.Id, false); + } + + //[SignalR(SignalRTags.AddOrderItemPallet)] + //public async Task AddOrderItemPallet(OrderItemPallet orderItemPallet) + //{ + // ArgumentNullException.ThrowIfNull(orderItemPallet); + + // _logger.Detail($"AddOrderItemPallet invoked; {orderItemPallet}"); + + // if (!await ctx.AddOrderItemPalletSafeAsync(orderItemPallet)) return null; + // return await ctx.OrderItemPallets.GetByIdAsync(orderItemPallet.Id, false); + //} + + //[SignalR(SignalRTags.UpdateOrderItemPallet)] + //public async Task UpdateOrderItemPallet(OrderItemPallet orderItemPallet) + //{ + // ArgumentNullException.ThrowIfNull(orderItemPallet); + + // _logger.Detail($"UpdateOrderItemPallet invoked; {orderItemPallet}"); + + // if (!await ctx.UpdateOrderItemPalletSafeAsync(orderItemPallet)) return null; + // return await ctx.OrderItemPallets.GetByIdAsync(orderItemPallet.Id, false); + //} + + //[SignalR(SignalRTags.AddOrUpdateMeasuredOrderItemPallets)] + //public async Task AddOrUpdateMeasuredOrderItemPallets(List orderItemPallets) + //{ + // // ArgumentNullException.ThrowIfNull(orderItemPallets); + + // // _logger.Detail($"AddOrUpdateMeasuredOrderItemPallets invoked; count: {orderItemPallets.Count}"); + + // // if (orderItemPallets.Count == 0) return null; + + // // var shippingItemId = orderItemPallets.FirstOrDefault()!.ShippingItemId; + // // if (shippingItemId <= 0 || orderItemPallets.Any(x => x.ShippingItemId != shippingItemId)) return null; + + // // var shippingItem = await ctx.ShippingItems.GetByIdAsync(shippingItemId, false); + // // shippingItem.orderItemPallets = orderItemPallets.Where(sip => sip.IsValidMeasuringValues(shippingItem.IsMeasurable)).ToList(); + + // // return await UpdateMeasuredShippingItem(shippingItem); + + // return null; + //} + + //[SignalR(SignalRTags.AddOrUpdateMeasuredOrderItemPallet)] + //public async Task AddOrUpdateMeasuredOrderItemPallet(OrderItemPallet orderItemPallet) + //{ + // ArgumentNullException.ThrowIfNull(orderItemPallet); + + // _logger.Detail($"AddOrUpdateMeasuredOrderItemPallet invoked; {orderItemPallet}"); + + // if (!await ctx.AddOrUpdateOrderItemPalletSafeAsync(orderItemPallet)) return null; + // return await ctx.OrderItemPallets.GetByIdAsync(orderItemPallet.Id, false); + //} } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs index edae232..29907ee 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs @@ -20,6 +20,7 @@ using Nop.Plugin.Misc.FruitBankPlugin.Services; using Nop.Services.Catalog; using Nop.Services.Common; using System.Transactions; +using DevExpress.XtraExport.Helpers; using FruitBank.Common.Dtos; using Mango.Nop.Core.Dtos; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -27,12 +28,13 @@ using Nop.Core.Domain.Orders; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; -public class FruitBankDbContext : MgDbContextBase, +public class FruitBankDbContext : MgDbContextBase, IOrderDtoDbSet, - IPartnerDbSet, - IShippingDbSet, - IShippingDocumentDbSet, - IShippingItemDbSet, + IOrderItemDtoDbSet, + IPartnerDbSet, + IShippingDbSet, + IShippingDocumentDbSet, + IShippingItemDbSet, IShippingItemPalletDbSet, IOrderItemPalletDbSet, IShippingDocumentToFilesDbSet, @@ -42,8 +44,9 @@ public class FruitBankDbContext : MgDbContextBase, private readonly IStoreContext _storeContext; private readonly IProductService _productService; private readonly IStaticCacheManager _staticCacheManager; - + public OrderDtoDbTable OrderDtos { get; set; } + public OrderItemDtoDbTable OrderItemDtos { get; set; } public PartnerDbTable Partners { get; set; } public ShippingDbTable Shippings { get; set; } @@ -60,11 +63,11 @@ public class FruitBankDbContext : MgDbContextBase, public IRepository Customers { get; set; } public IRepository CustomerRoles { get; set; } public IRepository CustomerRoleMappings { get; set; } - + public FruitBankDbContext(INopDataProvider dataProvider, ILockService lockService, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext, - PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, ShippingItemDbTable shippingItemDbTable, + PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, ShippingItemDbTable shippingItemDbTable, ShippingItemPalletDbTable shippingItemPalletDbTable, FilesDbTable filesDbTable, ShippingDocumentToFilesDbTable shippingDocumentToFilesDbTable, - OrderDtoDbTable orderDtoDbTable, + OrderDtoDbTable orderDtoDbTable, OrderItemDtoDbTable orderItemDtoDbTable, OrderItemPalletDbTable orderItemPalletDbTable, IProductService productService, IStaticCacheManager staticCacheManager, IRepository productRepository, IRepository customerRepository, @@ -78,15 +81,19 @@ public class FruitBankDbContext : MgDbContextBase, _fruitBankAttributeService = fruitBankAttributeService; Files = filesDbTable; - OrderDtos = orderDtoDbTable; Partners = partnerDbTable; + Products = productRepository; + + OrderDtos = orderDtoDbTable; + OrderItemDtos = orderItemDtoDbTable; + OrderItemPallets = orderItemPalletDbTable; + Shippings = shippingDbTable; ShippingDocuments = shippingDocumentDbTable; ShippingItems = shippingItemDbTable; ShippingItemPallets = shippingItemPalletDbTable; ShippingDocumentToFiles = shippingDocumentToFilesDbTable; - Products = productRepository; Customers = customerRepository; CustomerRoles = customerRoleRepository; CustomerRoleMappings = customerCustomerRoleMappingRepository; @@ -169,7 +176,7 @@ public class FruitBankDbContext : MgDbContextBase, public Task UpdateShippingItemSafeAsync(ShippingItem shippingItem) => TransactionSafeAsync(async _ => await UpdateShippingItemAsync(shippingItem)); - + public async Task UpdateShippingItemAsync(ShippingItem shippingItem) { try @@ -192,7 +199,7 @@ public class FruitBankDbContext : MgDbContextBase, //if (shippingItem.ShippingItemPallets is { Count: > 0 }) await AddOrUpdateShippingItemPalletAsync(shippingItem); //await AddOrUpdateShippingItemPalletAsync(shippingItem.ShippingItemPallets.Where(x => x.IsValidMeasuringValues(shippingItem.IsMeasurable)), shippingItem); - + //Nem tudhatjuk, h minden Pallet-et tartalmaz-e a shippingItem paraméter! A Biztonság kedvéért lekérjük db-ből! - J. shippingItem.ShippingItemPallets = await ShippingItemPallets.GetAllByShippingItemIdAsync(shippingItem.Id, false).ToListAsync(); @@ -293,25 +300,14 @@ public class FruitBankDbContext : MgDbContextBase, public async Task AddShippingItemPalletAsync(ShippingItemPallet shippingItemPallet) { if (!await SetupShippingItemPalletMeauringValues(shippingItemPallet)) return null; - + await ShippingItemPallets.InsertAsync(shippingItemPallet); return shippingItemPallet; } public Task AddShippingItemPalletSafeAsync(ShippingItemPallet shippingItemPallet) - { - return TransactionSafeAsync(async tr => - { - try - { - return (await AddShippingItemPalletAsync(shippingItemPallet)) != null; - } - catch (Exception ex) - { - throw new Exception($"AddShippingItemPalletSafeAsync->TransactionSafeAsync error! shippingItemPallet: {shippingItemPallet}; ex: {ex.Message}", ex); - } - }); - } + => TransactionSafeAsync(async tr => await AddShippingItemPalletAsync(shippingItemPallet) != null); + public async Task UpdateShippingItemPalletAsync(ShippingItemPallet shippingItemPallet) { if (!await SetupShippingItemPalletMeauringValues(shippingItemPallet)) return null; @@ -321,38 +317,21 @@ public class FruitBankDbContext : MgDbContextBase, } public Task UpdateShippingItemPalletSafeAsync(ShippingItemPallet shippingItemPallet) - { - return TransactionSafeAsync(async tr => - { - try - { - return (await UpdateShippingItemPalletAsync(shippingItemPallet)) != null; - } - catch (Exception ex) - { - throw new Exception($"UpdateShippingItemPalletSafeAsync->TransactionSafeAsync error! shippingItemPallet: {shippingItemPallet}; ex: {ex.Message}", ex); - } - }); - } + => TransactionSafeAsync(async tr => await UpdateShippingItemPalletAsync(shippingItemPallet) != null); public Task AddOrUpdateShippingItemPalletsSafeAsync(ShippingItem shippingItem) => AddOrUpdateShippingItemPalletsSafeAsync(shippingItem.ShippingItemPallets!, shippingItem); + public Task AddOrUpdateShippingItemPalletsSafeAsync(IEnumerable shippingItemPallets, ShippingItem parentShippingItem) { return TransactionSafeAsync(async tr => { - try - { - await AddOrUpdateShippingItemPalletAsync(shippingItemPallets, parentShippingItem); - return true; - } - catch (Exception ex) - { - throw new Exception($"AddOrUpdateShippingItemPalletsSafeAsync->TransactionSafeAsync error! ex: {ex.Message}", ex); - } + await AddOrUpdateShippingItemPalletAsync(shippingItemPallets, parentShippingItem); + return true; }); } public Task AddOrUpdateShippingItemPalletAsync(ShippingItem shippingItem) => AddOrUpdateShippingItemPalletAsync(shippingItem.ShippingItemPallets!, shippingItem); + public async Task AddOrUpdateShippingItemPalletAsync(IEnumerable shippingItemPallets, ShippingItem parentShippingItem) { foreach (var shippingItemPallet in shippingItemPallets) @@ -363,38 +342,96 @@ public class FruitBankDbContext : MgDbContextBase, throw new Exception($"AddOrUpdateShippingItemPalletAsync->AddOrUpdateShippingItemPalletAsync() == null"); } } + public async Task AddOrUpdateShippingItemPalletSafeAsync(ShippingItemPallet shippingItemPallet) => await TransactionSafeAsync(async tr => await AddOrUpdateShippingItemPalletAsync(shippingItemPallet) != null); public async Task AddOrUpdateShippingItemPalletAsync(ShippingItemPallet shippingItemPallet) { if (shippingItemPallet.Id <= 0) return await AddShippingItemPalletAsync(shippingItemPallet); - + return await UpdateShippingItemPalletAsync(shippingItemPallet); } public async Task DeleteShippingItemPalletSafeAsync(ShippingItemPallet shippingItemPallet) { - await TransactionSafeAsync(async tr => + await TransactionSafeAsync(async tr => { await ShippingItemPallets.DeleteAsync(shippingItemPallet, false); return true; }); } + public async Task SetOrderStatusToCompleteSafe(int orderId) + => await TransactionSafeAsync(async tr => await SetOrderStatusToComplete(orderId) != null); + + public async Task SetOrderStatusToComplete(int orderId) + { + var orderDto = await OrderDtos.GetByIdAsync(orderId); + if (orderDto == null) return null; + + if (!orderDto.IsMeasured || orderDto.OrderStatus == OrderStatus.Complete) return null; //throw new Exception($"SetOrderDtoToComplete; orderDto.IsMeasured == false; {orderDto}"); + orderDto.OrderStatus = OrderStatus.Complete; + + await OrderDtos.UpdateAsync(orderDto); + + foreach (var orderItemDto in orderDto.OrderItemDtos) + { + if (!orderItemDto.IsMeasurable) continue; + + var gaNetWeight = CommonHelper.To(orderItemDto.GenericAttributes.FirstOrDefault(x => x.Key == nameof(IMeasuringNetWeight.NetWeight))?.Value ?? "0"); + + await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync + (orderItemDto.Id, nameof(IMeasuringNetWeight.NetWeight), orderItemDto.NetWeight); + + await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync + (orderItemDto.ProductId, -(orderItemDto.NetWeight-gaNetWeight), 0, orderItemDto.IsMeasurable, true); + } + + return orderDto; + } + + public async Task AddOrderItemPalletAsync(OrderItemPallet orderItemPallet) + { + if (!await SetupOrderItemPalletMeauringValues(orderItemPallet)) return null; + + await OrderItemPallets.InsertAsync(orderItemPallet); + return orderItemPallet; + } + + public async Task UpdateOrderItemPalletAsync(OrderItemPallet orderItemPallet) + { + if (!await SetupOrderItemPalletMeauringValues(orderItemPallet)) return null; + + await OrderItemPallets.UpdateAsync(orderItemPallet); + return orderItemPallet; + } + + public async Task AddOrUpdateOrderItemPalletSafeAsync(OrderItemPallet orderItemPallet) + => await TransactionSafeAsync(async tr => await AddOrUpdateOrderItemPalletAsync(orderItemPallet) != null); + + public async Task AddOrUpdateOrderItemPalletAsync(OrderItemPallet orderItemPallet) + { + if (orderItemPallet.Id <= 0) return await AddOrderItemPalletAsync(orderItemPallet); + + return await UpdateOrderItemPalletAsync(orderItemPallet); + } + private async Task SetupShippingItemPalletMeauringValues(ShippingItemPallet shippingItemPallet) { var shippingItem = shippingItemPallet.ShippingItem ?? await ShippingItems.GetByIdAsync(shippingItemPallet.ShippingItemId, false); if (shippingItem == null || shippingItemPallet.ShippingItemId != shippingItem.Id) return false; - if (!shippingItem.IsMeasurable) - { - shippingItemPallet.TareWeight = 0; - shippingItemPallet.PalletWeight = 0; - shippingItemPallet.GrossWeight = 0; - } + shippingItemPallet.SetupCustomItemPalletMeauringValues(shippingItem.IsMeasurable); + return true; + } - shippingItemPallet.IsMeasured = shippingItemPallet.IsValidMeasuringValues(shippingItem.IsMeasurable); + private async Task SetupOrderItemPalletMeauringValues(OrderItemPallet orderItemPallet) + { + var orderItemDto = orderItemPallet.OrderItemDto ?? await OrderItemDtos.GetByIdAsync(orderItemPallet.OrderItemId); + if (orderItemDto == null || orderItemPallet.OrderItemId != orderItemDto.Id) return false; + + orderItemPallet.SetupCustomItemPalletMeauringValues(orderItemDto.IsMeasurable); return true; } diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/Interfaces/IOrderItemDtoDbSet.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/Interfaces/IOrderItemDtoDbSet.cs new file mode 100644 index 0000000..1f645c0 --- /dev/null +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/Interfaces/IOrderItemDtoDbSet.cs @@ -0,0 +1,10 @@ +using FruitBank.Common.Dtos; +using Mango.Nop.Core.Interfaces; +using Nop.Data; + +namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces; + +public interface IOrderItemDtoDbSet : IMgDbTableBase where TDbTable : IRepository +{ + public TDbTable OrderItemDtos { get; set; } +} \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/MeasuringItemPalletBaseDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/MeasuringItemPalletBaseDbTable.cs index ffa1279..f2d6746 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/MeasuringItemPalletBaseDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/MeasuringItemPalletBaseDbTable.cs @@ -12,7 +12,19 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; public class MeasuringItemPalletBaseDbTable(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings, ILogger logger) : MgDbTableBase(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings, logger) where TEntity : MgEntityBase, IMeasuringItemPalletBase { - protected virtual void BeforeInsertOrUpdate(IMeasuringItemPalletBase measuringItemPalletBase) + protected override void OnUpdate(TEntity entity) + { + BeforeInsertOrUpdate(entity); + base.OnUpdate(entity); + } + + protected override void OnInsert(TEntity entity) + { + BeforeInsertOrUpdate(entity); + base.OnInsert(entity); + } + + protected virtual void BeforeInsertOrUpdate(TEntity measuringItemPalletBase) { if (measuringItemPalletBase.IsValidSafeMeasuringValues()) return; diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderDtoDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderDtoDbTable.cs index 96e4e10..ae8e980 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderDtoDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderDtoDbTable.cs @@ -11,6 +11,26 @@ using Nop.Services.Logging; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; +public class OrderItemDtoDbTable : MgDbTableBase +{ + public OrderItemDtoDbTable(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings, ILogger logger) + : base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings, logger) + { + } + + public IQueryable GetAll(bool loadRelations) + { + return GetAll() + .LoadWith(oi => oi.OrderDto) + .LoadWith(oi => oi.OrderItemPallets) + .LoadWith(oi => oi.GenericAttributes) + .LoadWith(oi => oi.ProductDto).ThenLoad(prod => prod.GenericAttributes); + } + + public Task GetByIdAsync(int orderItemId) => GetAll(true).Where(x => x.Id == orderItemId).FirstOrDefaultAsync(null); + + public IQueryable GetAllByOrderId(int orderId)=> GetAll(true).Where(o => o.OrderId == orderId); +} public class OrderDtoDbTable : MgDbTableBase { @@ -23,21 +43,14 @@ public class OrderDtoDbTable : MgDbTableBase { return GetAll() .LoadWith(o => o.GenericAttributes) - .LoadWith(o => o.OrderItemDtos).ThenLoad(oi => oi.ProductDto).ThenLoad(prod=>prod.GenericAttributes) + .LoadWith(o => o.OrderItemDtos).ThenLoad(oi => oi.ProductDto).ThenLoad(prod => prod.GenericAttributes) .LoadWith(o => o.OrderItemDtos).ThenLoad(oi => oi.GenericAttributes) .LoadWith(o => o.OrderItemDtos).ThenLoad(oi => oi.OrderItemPallets); } - public Task GetByIdAsync(int orderId) - { - return GetAll(true).Where(x => x.Id == orderId).FirstOrDefaultAsync(null); - } + public Task GetByIdAsync(int orderId) => GetAll(true).Where(x => x.Id == orderId).FirstOrDefaultAsync(null); - public IQueryable GetAllByOrderStatus(OrderStatus orderStatus) - => GetAll(true).Where(o => o.OrderStatusId == (int)orderStatus); + public IQueryable GetAllByOrderStatus(OrderStatus orderStatus) => GetAll(true).Where(o => o.OrderStatusId == (int)orderStatus); - public IQueryable GetAllByIds(IEnumerable orderIds) - { - return GetAll(true).Where(o => orderIds.Contains(o.Id)); - } + public IQueryable GetAllByIds(IEnumerable orderIds) => GetAll(true).Where(o => orderIds.Contains(o.Id)); } diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemPalletDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemPalletDbTable.cs index 7d32143..9308180 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemPalletDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemPalletDbTable.cs @@ -15,24 +15,13 @@ public class OrderItemPalletDbTable : MeasuringItemPalletBaseDbTable GetAll() => base.GetAll(); public IQueryable GetAll(bool loadRelations) { return loadRelations - ? GetAll().LoadWith(oip => oip.OrderItemDto).ThenLoad(oi => oi.ProductDto) + ? GetAll() + .LoadWith(oip => oip.OrderItemDto).ThenLoad(oi => oi.ProductDto) : GetAll(); } diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemPalletDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemPalletDbTable.cs index a99e61e..ca5f279 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemPalletDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemPalletDbTable.cs @@ -16,18 +16,6 @@ public class ShippingItemPalletDbTable : MeasuringItemPalletBaseDbTable GetAll() => base.GetAll(); public IQueryable GetAll(bool loadRelations) diff --git a/Nop.Plugin.Misc.AIPlugin/Factories/CustomOrderModelFactory.cs b/Nop.Plugin.Misc.AIPlugin/Factories/CustomOrderModelFactory.cs index 3783369..7ddf2ad 100644 --- a/Nop.Plugin.Misc.AIPlugin/Factories/CustomOrderModelFactory.cs +++ b/Nop.Plugin.Misc.AIPlugin/Factories/CustomOrderModelFactory.cs @@ -35,14 +35,17 @@ using Nop.Web.Areas.Admin.Models.Orders; using System.Collections; using System.Reflection; using System.Runtime.Intrinsics.Arm; +using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; namespace Nop.Plugin.Misc.FruitBankPlugin.Factories { public class CustomOrderModelFactory : OrderModelFactory { + private FruitBankDbContext _ctx; private readonly IOrderMeasurementService _orderMeasurementService; public CustomOrderModelFactory( + FruitBankDbContext ctx, IOrderMeasurementService orderMeasurementService, AddressSettings addressSettings, CatalogSettings catalogSettings, @@ -136,6 +139,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Factories taxSettings ) { + _ctx = ctx; _orderMeasurementService = orderMeasurementService; } @@ -175,13 +179,15 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Factories { var orderListModel = await PrepareOrderListModelAsync(searchModel); var extendedRows = new List(orderListModel.RecordsFiltered); + + var orderDtosById = await _ctx.OrderDtos.GetAllByIds(orderListModel.Data.Select(x => x.Id)).ToDictionaryAsync(k => k.Id, v => v); foreach (var orderModel in orderListModel.Data) { var orderModelExtended = new OrderModelExtended(); PropertyHelper.CopyPublicValueTypeProperties(orderModel, orderModelExtended); - orderModelExtended.IsMeasurable = await ShouldMarkAsNeedsMeasurementAsync(orderModel); + orderModelExtended.IsMeasurable = orderDtosById[orderModel.Id].IsMeasurable;// await ShouldMarkAsNeedsMeasurementAsync(orderModel); Console.WriteLine(orderModelExtended.Id); extendedRows.Add(orderModelExtended); diff --git a/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs b/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs index b1b5ab4..e478105 100644 --- a/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs +++ b/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs @@ -59,6 +59,9 @@ public class PluginNopStartup : INopStartup services.AddScoped(); services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/Nop.Plugin.Misc.AIPlugin/Mapping/NameCompatibility.cs b/Nop.Plugin.Misc.AIPlugin/Mapping/NameCompatibility.cs index 487ab3d..9425e13 100644 --- a/Nop.Plugin.Misc.AIPlugin/Mapping/NameCompatibility.cs +++ b/Nop.Plugin.Misc.AIPlugin/Mapping/NameCompatibility.cs @@ -14,18 +14,20 @@ public partial class NameCompatibility : INameCompatibility /// public Dictionary TableNames => new Dictionary { + { typeof(Files), FruitBankConstClient.FilesDbTableName}, + { typeof(Pallet), FruitBankConstClient.PalletDbTableName}, + { typeof(Partner), FruitBankConstClient.PartnerDbTableName }, { typeof(ProductDto), nameof(Product)}, + { typeof(OrderDto), nameof(Order)}, { typeof(OrderItemDto), nameof(OrderItem)}, + { typeof(OrderItemPallet), FruitBankConstClient.OrderItemPalletDbTableName}, - { typeof(Pallet), FruitBankConstClient.PalletDbTableName}, - { typeof(Files), FruitBankConstClient.FilesDbTableName}, - { typeof(Partner), FruitBankConstClient.PartnerDbTableName }, { typeof(Shipping), FruitBankConstClient.ShippingDbTableName }, { typeof(ShippingDocument), FruitBankConstClient.ShippingDocumentDbTableName }, { typeof(ShippingItem), FruitBankConstClient.ShippingItemDbTableName}, { typeof(ShippingItemPallet), FruitBankConstClient.ShippingItemPalletDbTableName}, - { typeof(OrderItemPallet), FruitBankConstClient.OrderItemPalletDbTableName}, + { typeof(ShippingDocumentToFiles), FruitBankConstClient.ShippingDocumentToFilesDbTableName}, };