Mango.Nop.Plugins/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs

89 lines
3.6 KiB
C#

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<IAcLogWriterBase> logWriters) :
MgEventConsumer(httpContextAccessor, logWriters),
IConsumer<EntityUpdatedEvent<ShippingItem>>,
IConsumer<EntityUpdatedEvent<ShippingDocument>>
{
public override Task HandleEventAsync(EntityUpdatedEvent<Product> eventMessage)
{
return base.HandleEventAsync(eventMessage);
}
public async Task HandleEventAsync(EntityInsertedEvent<ShippingItem> eventMessage)
{
Logger.Info($"HandleEventAsync EntityInsertedEvent<ShippingItem>; id: {eventMessage.Entity.Id}");
await UpdateShippingDocumentIsAllMeasuredAsync(eventMessage.Entity);
}
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: && 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<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);
}
}
}