diff --git a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs index 7ef202d..4c98beb 100644 --- a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs @@ -8,6 +8,7 @@ using FruitBank.Common.Server; using FruitBank.Common.SignalRs; using LinqToDB; using Mango.Nop.Core.Dtos; +using Mango.Nop.Core.Loggers; using Mango.Nop.Core.Models; using Nop.Core; using Nop.Core.Domain.Customers; @@ -137,7 +138,22 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers _logger.Detail($"UpdateShippingItem invoked; id: {shippingItem.Id}"); - await ctx.ShippingItems.UpdateAsync(shippingItem); + if (!await ctx.UpdateShippingItemAsync(shippingItem)) + return null; //await ctx.ShippingItems.GetByIdAsync(shippingItem.Id); + + return shippingItem; + } + + [SignalR(SignalRTags.UpdateMeasuredShippingItem)] + public async Task UpdateMeasuredShippingItem(ShippingItem shippingItem) + { + ArgumentNullException.ThrowIfNull(shippingItem); + + _logger.Detail($"UpdateMeasuredShippingItem invoked; id: {shippingItem.Id}"); + + if (!await ctx.UpdateMeasuredShippingItemAsync(shippingItem)) + return null; //await ctx.ShippingItems.GetByIdAsync(shippingItem.Id); + return shippingItem; } diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs index b31081a..00b732a 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs @@ -1,4 +1,8 @@ -using FruitBank.Common.Models; +using AyCode.Core.Loggers; +using FruitBank.Common.Entities; +using FruitBank.Common.Interfaces; +using FruitBank.Common.Models; +using Mango.Nop.Core.Loggers; using Mango.Nop.Core.Repositories; using Nop.Core.Caching; using Nop.Core.Domain.Catalog; @@ -6,8 +10,8 @@ using Nop.Core.Domain.Customers; using Nop.Data; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces; using Nop.Services.Catalog; -using Nop.Services.Logging; using NUglify.Helpers; +using System.Transactions; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; @@ -33,7 +37,7 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet IRepository customerRepository, IRepository customerCustomerRoleMappingRepository, IRepository customerRoleRepository, - ILogger logger) : base(dataProvider, logger) + IEnumerable logWriters) : base(dataProvider, logWriters) { _productService = productService; _staticCacheManager = staticCacheManager; @@ -76,4 +80,132 @@ public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet public IQueryable GetAllProducts() => Products.Table.Where(p => !p.Deleted).OrderBy(o => o.Name); + + public Task UpdateMeasuredShippingItemAsync(ShippingItem shippingItem) + { + if (!shippingItem.IsMeasurable || shippingItem.IsValidMeasuringValues()) return UpdateShippingItemAsync(shippingItem); + + Logger.Error("shippingItem.IsMeasurable && !shippingItem.IsValidMeasuringValues()"); + return Task.FromResult(false); + } + + public async Task UpdateShippingItemAsync(ShippingItem shippingItem) + { + if (shippingItem == null) + { + Logger.Error("shippingItem == null"); + return await Task.FromResult(false); + } + + using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + try + { + //Mi van ha nem jött meg a termék? Nem fogják tudni menteni... - J. + + if (shippingItem.MeasuredQuantity <= 0) shippingItem.MeasuredQuantity = null; + + if (!shippingItem.IsMeasurable || shippingItem.MeasuredNetWeight <= 0) shippingItem.MeasuredNetWeight = null; + if (!shippingItem.IsMeasurable || shippingItem.MeasuredGrossWeight <= 0) shippingItem.MeasuredGrossWeight = null; + + //Update előtt kivesszük a korábbi ShippingItem-et a db-ből! - J. + var dbShippingItem = await ShippingItems.GetByIdAsync(shippingItem.Id); + if (dbShippingItem == null) + { + Logger.Error("dbShippingItem == null"); + return await Task.FromResult(false); + } + + Product product = null; + if (shippingItem.ProductId > 0) + { + product = await Products.GetByIdAsync(shippingItem.ProductId); + + if (product == null) + { + Logger.Error($"shippingItem.ProductId > 0 && product == null; shippingItem.ProductId: {shippingItem.ProductId}"); + return await Task.FromResult(false); + } + } + + shippingItem.IsMeasured = product != null && shippingItem.IsValidMeasuringValues(); + + await ShippingItems.UpdateAsync(shippingItem); + + //TODO: a measuredweight-eket is! - J. + if (shippingItem.ProductId == dbShippingItem.ProductId && + shippingItem.IsMeasured == dbShippingItem.IsMeasured && shippingItem.IsMeasurable == dbShippingItem.IsMeasurable && + shippingItem.MeasuredQuantity == dbShippingItem.MeasuredQuantity && + shippingItem.MeasuredNetWeight == dbShippingItem.MeasuredNetWeight && shippingItem.MeasuredGrossWeight == dbShippingItem.MeasuredGrossWeight) + { + return await Task.FromResult(true); + } + + //TODO: productIdUnchanged-et lekezelni! - J. + var productIdUnchanged = shippingItem.ProductId == dbShippingItem.ProductId; + + if (shippingItem.IsMeasured) + { + product!.StockQuantity += shippingItem.MeasuredQuantity.GetValueOrDefault(0); + + if (!await UpdateProductStockQuantityAsync(product)) + { + Logger.Error($"UpdateProductStockQuantity() == false; shippingItem! id: {product.Id}"); + return await Task.FromResult(false); + } + } + + if (dbShippingItem.IsMeasured) + { + product = await Products.GetByIdAsync(dbShippingItem.ProductId); + + if (product != null) + { + product.StockQuantity -= dbShippingItem.MeasuredQuantity.GetValueOrDefault(0); + + if (!await UpdateProductStockQuantityAsync(product)) + { + Logger.Error($"UpdateProductStockQuantity() == false; dbShippingItem! id: {product.Id}"); + return await Task.FromResult(false); + } + } + else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}"); + } + } + catch (Exception ex) + { + Logger.Error($"UpdateShippingItemAsync Transaction ERROR! Id: {shippingItem?.Id}", ex); + return await Task.FromResult(false); + } + + transaction.Complete(); + } + + return await Task.FromResult(true); + } + + private async Task UpdateProductStockQuantityAsync(int productId) + { + var product = await Products.GetByIdAsync(productId); + if (product != null) return await UpdateProductStockQuantityAsync(product); + + Logger.Error($"product == null; id: {productId}"); + return await Task.FromResult(false); + } + + private async Task UpdateProductStockQuantityAsync(Product product) + { + //Itt mi legyen? RollBack? - J. + if (product.StockQuantity < 0) + Logger.Error($"product.StockQuantity < 0; Id: {product.Id}; StockQuantity: {product.StockQuantity}"); + + await Products.UpdateAsync(product, true); + return await Task.FromResult(true); + + //var updatedRowsCount = await DataProvider.ExecuteNonQueryAsync($"update product set {nameof(Product.StockQuantity)} = {product.StockQuantity} where {nameof(Product.Id)} = {product.Id}"); + //if (updatedRowsCount == 1) return await Task.FromResult(true); + + //Logger.Error($"Product updatedRowsCount != 1; id: {product.Id}"); + //return await Task.FromResult(false); + } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemDbTable.cs index 1889b3f..e06e2a1 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemDbTable.cs @@ -23,8 +23,9 @@ public class ShippingItemDbTable : MgDbTableBase { return loadRelations ? GetAll() - .LoadWith(sd => sd.ShippingDocument).ThenLoad(s => s.Shipping) - .LoadWith(sd => sd.ShippingDocument).ThenLoad(p => p.Partner) + .LoadWith(si => si.ShippingDocument).ThenLoad(s => s.Shipping) + .LoadWith(si => si.ShippingDocument).ThenLoad(p => p.Partner) + .LoadWith(si => si.Product) : GetAll(); } diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs new file mode 100644 index 0000000..1ee6b79 --- /dev/null +++ b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs @@ -0,0 +1,89 @@ +using AyCode.Core.Loggers; +using FruitBank.Common.Entities; +using FruitBank.Common.Interfaces; +using FruitBank.Common.Loggers; +using Mango.Nop.Core.Loggers; +using Mango.Nop.Services; +using Microsoft.AspNetCore.Http; +using Nop.Core.Domain.Catalog; +using Nop.Core.Events; +using Nop.Plugin.Misc.FruitBankPlugin.Controllers; +using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; +using Nop.Services.Events; + +namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.EventConsumers; + +public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, FruitBankDbContext ctx, IEnumerable logWriters) : + MgEventConsumer(httpContextAccessor, logWriters), + IConsumer>, + IConsumer> +{ + public override Task HandleEventAsync(EntityUpdatedEvent eventMessage) + { + + return base.HandleEventAsync(eventMessage); + } + + public async Task HandleEventAsync(EntityInsertedEvent eventMessage) + { + Logger.Info($"HandleEventAsync EntityInsertedEvent; id: {eventMessage.Entity.Id}"); + + await UpdateShippingDocumentIsAllMeasuredAsync(eventMessage.Entity); + } + + public async Task HandleEventAsync(EntityUpdatedEvent eventMessage) + { + Logger.Info($"HandleEventAsync EntityUpdatedEvent; id: {eventMessage.Entity.Id}"); + + var shippingItem = eventMessage.Entity; + var isMeasured = shippingItem.IsValidMeasuringValues(); + + if (shippingItem.IsMeasured != isMeasured) + { + shippingItem.IsMeasured = isMeasured; + await ctx.ShippingItems.UpdateAsync(shippingItem, false); + } + + await UpdateShippingDocumentIsAllMeasuredAsync(shippingItem); + } + + private async Task UpdateShippingDocumentIsAllMeasuredAsync(ShippingItem shippingItem) + { + //TODO: where: && IsMeasureable!!!! - J. + var isAllShippingItemMeasured = ctx.ShippingItems.GetAll(false).Where(si => si.ShippingDocumentId == shippingItem.ShippingDocumentId).All(si => si.IsMeasured); + var shippingDocument = await ctx.ShippingDocuments.GetByIdAsync(shippingItem.ShippingDocumentId); + + if (shippingDocument != null && shippingDocument.IsAllMeasured != isAllShippingItemMeasured) + { + shippingDocument.IsAllMeasured = isAllShippingItemMeasured; + await ctx.ShippingDocuments.UpdateAsync(shippingDocument); + } + } + + public async Task HandleEventAsync(EntityInsertedEvent eventMessage) + { + Logger.Info($"HandleEventAsync EntityInsertedEvent; id: {eventMessage.Entity.Id}"); + + await UpdateShippingIsAllMeasuredAsync(eventMessage.Entity); + } + + public async Task HandleEventAsync(EntityUpdatedEvent eventMessage) + { + Logger.Info($"HandleEventAsync EntityUpdatedEvent; id: {eventMessage.Entity.Id}"); + + await UpdateShippingIsAllMeasuredAsync(eventMessage.Entity); + } + + private async Task UpdateShippingIsAllMeasuredAsync(ShippingDocument shippingDocument) + { + var isAllShippingDocumentMeasured = ctx.ShippingDocuments.GetAll(false).Where(si => si.ShippingId == shippingDocument.ShippingId).All(si => si.IsAllMeasured); + var shipping = await ctx.Shippings.GetByIdAsync(shippingDocument.ShippingId); + + if (shipping != null && shipping.IsAllMeasured != isAllShippingDocumentMeasured) + { + shipping.IsAllMeasured = isAllShippingDocumentMeasured; + await ctx.Shippings.UpdateAsync(shipping); + } + } + +} \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs b/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs index 891baa7..de6089e 100644 --- a/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs +++ b/Nop.Plugin.Misc.AIPlugin/Infrastructure/PluginNopStartup.cs @@ -37,9 +37,9 @@ public class PluginNopStartup : INopStartup //register services and interfaces - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); + services.AddScoped(); + //services.AddScoped(); + services.AddScoped(); //services.AddSingleton(); services.AddScoped(); diff --git a/Nop.Plugin.Misc.AIPlugin/Services/LoggerToLoggerApiController2.cs b/Nop.Plugin.Misc.AIPlugin/Services/LoggerToLoggerApiController2.cs index b8d7e30..e088b1d 100644 --- a/Nop.Plugin.Misc.AIPlugin/Services/LoggerToLoggerApiController2.cs +++ b/Nop.Plugin.Misc.AIPlugin/Services/LoggerToLoggerApiController2.cs @@ -1,6 +1,7 @@ using AyCode.Core.Enums; using AyCode.Core.Loggers; using FruitBank.Common.Loggers; +using Mango.Nop.Core.Loggers; namespace Nop.Plugin.Misc.FruitBankPlugin.Services;