210 lines
8.9 KiB
C#
210 lines
8.9 KiB
C#
using AyCode.Core.Loggers;
|
|
using AyCode.Interfaces.Entities;
|
|
using FruitBank.Common.Entities;
|
|
using FruitBank.Common.Interfaces;
|
|
using FruitBank.Common.Loggers;
|
|
using FruitBank.Common.Server;
|
|
using Mango.Nop.Core.Loggers;
|
|
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.Controllers;
|
|
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
|
|
using Nop.Plugin.Misc.FruitBankPlugin.Services;
|
|
using Nop.Services.Common;
|
|
using Nop.Services.Events;
|
|
using System.Globalization;
|
|
|
|
namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.EventConsumers;
|
|
|
|
public class FruitBankEventConsumer(IHttpContextAccessor httpContextAcc, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IEnumerable<IAcLogWriterBase> logWriters) :
|
|
MgEventConsumer(httpContextAcc, logWriters),
|
|
IConsumer<EntityDeletedEvent<Shipping>>,
|
|
IConsumer<EntityInsertedEvent<ShippingItem>>,
|
|
IConsumer<EntityUpdatedEvent<ShippingItem>>,
|
|
IConsumer<EntityDeletedEvent<ShippingItem>>,
|
|
IConsumer<EntityInsertedEvent<ShippingDocument>>,
|
|
IConsumer<EntityUpdatedEvent<ShippingDocument>>,
|
|
IConsumer<EntityDeletedEvent<ShippingDocument>>,
|
|
IConsumer<EntityInsertedEvent<ShippingItemPallet>>,
|
|
IConsumer<EntityUpdatedEvent<ShippingItemPallet>>,
|
|
IConsumer<EntityDeletedEvent<ShippingItemPallet>>
|
|
{
|
|
public override async Task HandleEventAsync(EntityUpdatedEvent<Product> eventMessage)
|
|
{
|
|
var product = eventMessage.Entity;
|
|
|
|
await SaveProductCustomAttributesAsync(eventMessage.Entity);
|
|
|
|
var isMeasurableProduct = await fruitBankAttributeService.IsMeasurableEntityAsync<Product>(product.Id);
|
|
|
|
var shippingItems = await ctx.ShippingItems.Table
|
|
.Where(si => si.ProductId == product.Id && !si.IsMeasured && si.IsMeasurable != isMeasurableProduct)
|
|
.ToListAsync();
|
|
|
|
foreach (var shippingItem in shippingItems)
|
|
shippingItem.IsMeasurable = isMeasurableProduct;
|
|
|
|
await ctx.ShippingItems.UpdateAsync(shippingItems, false);
|
|
await base.HandleEventAsync(eventMessage);
|
|
}
|
|
|
|
public override async Task HandleEventAsync(EntityInsertedEvent<Product> eventMessage)
|
|
{
|
|
await SaveProductCustomAttributesAsync(eventMessage.Entity);
|
|
await base.HandleEventAsync(eventMessage);
|
|
}
|
|
|
|
private async Task SaveProductCustomAttributesAsync(Product product)
|
|
{
|
|
if (product == null) return;
|
|
|
|
var form = HttpContextAccessor.HttpContext?.Request?.Form;
|
|
if (form == null || form.Count == 0) return;
|
|
|
|
if (form.ContainsKey(nameof(IMeasurable.IsMeasurable)) && form.ContainsKey(nameof(IMeasuringNetWeight.NetWeight)))
|
|
{
|
|
var isMeasurable = form[nameof(IMeasurable.IsMeasurable)].ToString().Contains("true");
|
|
//var isMeasurable = CommonHelper.To<bool>(form[nameof(IMeasurable.IsMeasurable)].ToString());
|
|
var netWeight = CommonHelper.To<double>(form[nameof(IMeasuringNetWeight.NetWeight)].ToString());
|
|
|
|
await fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(product.Id, netWeight, 0, isMeasurable, false);
|
|
}
|
|
|
|
if (form.ContainsKey(nameof(ITare.Tare)))
|
|
{
|
|
var tare = CommonHelper.To<double>(form[nameof(ITare.Tare)].ToString());
|
|
if (tare < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (tare < 0); productId: {product.Id}; tare: {tare}");
|
|
|
|
await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(product.Id, nameof(ITare.Tare), tare);
|
|
}
|
|
|
|
if (form.ContainsKey("IncomingQuantity"))
|
|
{
|
|
var incomingQuantity = CommonHelper.To<int>(form["IncomingQuantity"].ToString());
|
|
if (incomingQuantity < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (incomingQuantity < 0); productId: {product.Id}; incomingQuantity: {incomingQuantity}");
|
|
|
|
await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, int>(product.Id, "IncomingQuantity", incomingQuantity);
|
|
}
|
|
}
|
|
|
|
public async Task HandleEventAsync(EntityInsertedEvent<ShippingItemPallet> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityInsertedEvent<ShippingItemPallet>; id: {eventMessage.Entity.Id}");
|
|
|
|
await UpdateShippingItemMeasuringValuesAsync(eventMessage.Entity);
|
|
}
|
|
|
|
public async Task HandleEventAsync(EntityUpdatedEvent<ShippingItemPallet> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityUpdatedEvent<ShippingItemPallet>; id: {eventMessage.Entity.Id}");
|
|
|
|
await UpdateShippingItemMeasuringValuesAsync(eventMessage.Entity);
|
|
}
|
|
|
|
public async Task HandleEventAsync(EntityDeletedEvent<ShippingItemPallet> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityDeletedEvent<ShippingItemPallet>; 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<ShippingItem> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityInsertedEvent<ShippingItemPallet>; id: {eventMessage.Entity.Id}");
|
|
|
|
await UpdateShippingDocumentIsAllMeasuredAsync(eventMessage.Entity);
|
|
}
|
|
|
|
#region Update
|
|
|
|
public async Task HandleEventAsync(EntityUpdatedEvent<ShippingItem> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityUpdatedEvent<ShippingItem>; 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<ShippingDocument> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityInsertedEvent<ShippingDocument>; id: {eventMessage.Entity.Id}");
|
|
|
|
await UpdateShippingIsAllMeasuredAsync(eventMessage.Entity);
|
|
}
|
|
|
|
public async Task HandleEventAsync(EntityUpdatedEvent<ShippingDocument> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityUpdatedEvent<ShippingDocument>; 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<Shipping> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityDeletedEvent<Shipping>; id: {eventMessage.Entity.Id}");
|
|
|
|
await ctx.ShippingDocuments.DeleteAsync(sd => sd.ShippingId == eventMessage.Entity.Id, true);
|
|
}
|
|
|
|
public async Task HandleEventAsync(EntityDeletedEvent<ShippingDocument> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityDeletedEvent<ShippingDocument>; id: {eventMessage.Entity.Id}");
|
|
|
|
await ctx.ShippingItems.DeleteAsync(si => si.ShippingDocumentId == eventMessage.Entity.Id, true);
|
|
}
|
|
|
|
public async Task HandleEventAsync(EntityDeletedEvent<ShippingItem> eventMessage)
|
|
{
|
|
Logger.Info($"HandleEventAsync EntityDeletedEvent<ShippingItem>; id: {eventMessage.Entity.Id}");
|
|
|
|
await ctx.ShippingItemPallets.DeleteAsync(sp => sp.ShippingItemId == eventMessage.Entity.Id, false);
|
|
}
|
|
|
|
#endregion Delete
|
|
} |