using AyCode.Core.Loggers; using FruitBank.Common.Entities; using FruitBank.Common.Interfaces; using Mango.Nop.Services; using Microsoft.AspNetCore.Http; using Nop.Core; using Nop.Core.Domain.Catalog; using Nop.Core.Events; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; using Nop.Plugin.Misc.FruitBankPlugin.Services; using Nop.Services.Events; using Mango.Nop.Core.Extensions; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.EventConsumers; public class FruitBankEventConsumer(IHttpContextAccessor httpContextAcc, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IEnumerable logWriters) : MgEventConsumer(httpContextAcc, logWriters), IConsumer>, IConsumer>, IConsumer>, IConsumer>, IConsumer>, IConsumer>, IConsumer>, IConsumer>, IConsumer>, IConsumer> { public override async Task HandleEventAsync(EntityUpdatedEvent eventMessage) { var product = eventMessage.Entity; var saveProductCustomAttributesResult = await SaveProductCustomAttributesAsync(eventMessage.Entity); //var isMeasurableProduct = await fruitBankAttributeService.IsMeasurableEntityAsync(product.Id); if (saveProductCustomAttributesResult is { IsMeasurableChanged: true, IsMeasurable: not null }) { var shippingItems = await ctx.ShippingItems.Table .Where(si => si.ProductId == product.Id && !si.IsMeasured && si.IsMeasurable != saveProductCustomAttributesResult.IsMeasurable.Value) .ToListAsync(); if (shippingItems.Count > 0) { foreach (var shippingItem in shippingItems) shippingItem.IsMeasurable = saveProductCustomAttributesResult.IsMeasurable.Value; await ctx.ShippingItems.UpdateAsync(shippingItems, false); } } await base.HandleEventAsync(eventMessage); } public override async Task HandleEventAsync(EntityInsertedEvent eventMessage) { await SaveProductCustomAttributesAsync(eventMessage.Entity); //TODO: ez ide miért kell? - J. await base.HandleEventAsync(eventMessage); } /// /// /// /// /// IsMeasureable /// private async Task<(bool IsMeasurableChanged, bool? IsMeasurable)> SaveProductCustomAttributesAsync(Product product) { if (product == null) return (false, null); var hasForm = HttpContextAccessor.HttpContext?.Request?.HasFormContentType ?? false; var form = hasForm ? HttpContextAccessor.HttpContext.Request.Form : null; if (form == null || form.Count == 0 || !form.ContainsKey(nameof(IMeasurable.IsMeasurable)) || !form.ContainsKey(nameof(IMeasuringNetWeight.NetWeight)) || !form.ContainsKey(nameof(IIncomingQuantity.IncomingQuantity)) || !form.ContainsKey(nameof(ITare.Tare))) return (false, null); bool? isMeasurable = null; var isMeasurableChanged = false; try { var productDto = product.Id > 0 ? await ctx.ProductDtos.GetByIdAsync(product.Id, false) : null; //IsMeasurable isMeasurable = form[nameof(IMeasurable.IsMeasurable)].ToString().Contains("true"); var productDtoIsMeasurable = productDto?.GenericAttributes.GetValueOrNull(nameof(IMeasurable.IsMeasurable)); if (productDtoIsMeasurable == null || productDtoIsMeasurable.Value != isMeasurable.Value) { await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(product.Id, nameof(IMeasurable.IsMeasurable), isMeasurable.Value); isMeasurableChanged = true; } //NetWeight var netWeight = double.Round(CommonHelper.To(form[nameof(IMeasuringNetWeight.NetWeight)].ToString()), 1); var productDtoNetWeight = productDto?.GenericAttributes.GetValueOrNull(nameof(IMeasuringNetWeight.NetWeight)); if (productDtoNetWeight == null || double.Round(productDtoNetWeight.Value, 1) != netWeight) await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight); //Tára var tare = double.Round(CommonHelper.To(form[nameof(ITare.Tare)].ToString()), 1); if (tare < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (tare < 0); productId: {product.Id}; tare: {tare}"); if (productDto == null || productDto.Tare != tare) await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(product.Id, nameof(ITare.Tare), tare); //IncomingQuantity var incomingQuantity = CommonHelper.To(form[nameof(IIncomingQuantity.IncomingQuantity)].ToString()); if (incomingQuantity < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (incomingQuantity < 0); productId: {product.Id}; incomingQuantity: {incomingQuantity}"); if (productDto == null || productDto.IncomingQuantity != incomingQuantity) await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(product.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity); } catch (Exception ex) { Logger.Error($"FruitBankEventConsumer->SaveProductCustomAttributesAsync; {ex.Message}", ex); } return (isMeasurableChanged, isMeasurable); } public async Task HandleEventAsync(EntityInsertedEvent eventMessage) { return; Logger.Info($"HandleEventAsync EntityInsertedEvent; id: {eventMessage.Entity.Id}"); await UpdateShippingItemMeasuringValuesAsync(eventMessage.Entity); } public async Task HandleEventAsync(EntityUpdatedEvent eventMessage) { return; Logger.Info($"HandleEventAsync EntityUpdatedEvent; id: {eventMessage.Entity.Id}"); await UpdateShippingItemMeasuringValuesAsync(eventMessage.Entity); } public async Task HandleEventAsync(EntityDeletedEvent eventMessage) { Logger.Info($"HandleEventAsync EntityDeletedEvent; id: {eventMessage.Entity.Id}"); await UpdateShippingItemMeasuringValuesAsync(eventMessage.Entity); } private async Task UpdateShippingItemMeasuringValuesAsync(ShippingItemPallet shippingItemPallet) { var shippingItem = await ctx.ShippingItems.GetByIdAsync(shippingItemPallet.ShippingItemId, false); await ctx.UpdateShippingItemAsync(shippingItem); } public async Task HandleEventAsync(EntityInsertedEvent eventMessage) { Logger.Info($"HandleEventAsync EntityInsertedEvent; id: {eventMessage.Entity.Id}"); await UpdateShippingDocumentIsAllMeasuredAsync(eventMessage.Entity); } #region Update 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: && IsMeasurable!!!! - 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); } } #endregion Update #region Delete public async Task HandleEventAsync(EntityDeletedEvent eventMessage) { Logger.Info($"HandleEventAsync EntityDeletedEvent; id: {eventMessage.Entity.Id}"); await ctx.ShippingDocuments.DeleteAsync(sd => sd.ShippingId == eventMessage.Entity.Id, true); } public async Task HandleEventAsync(EntityDeletedEvent eventMessage) { Logger.Info($"HandleEventAsync EntityDeletedEvent; id: {eventMessage.Entity.Id}"); await ctx.ShippingItems.DeleteAsync(si => si.ShippingDocumentId == eventMessage.Entity.Id, true); } public async Task HandleEventAsync(EntityDeletedEvent eventMessage) { Logger.Info($"HandleEventAsync EntityDeletedEvent; id: {eventMessage.Entity.Id}"); await ctx.ShippingItemPallets.DeleteAsync(sp => sp.ShippingItemId == eventMessage.Entity.Id, false); } #endregion Delete }