Mango.Nop.Plugins/Nop.Plugin.Misc.AIPlugin/Services/EventConsumer.cs

224 lines
9.9 KiB
C#

using System.Linq;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Orders;
using Nop.Services.Catalog;
using Nop.Services.Common;
using Nop.Services.Events;
using Nop.Services.Localization;
using Nop.Services.Orders;
using Nop.Services.Plugins;
using Nop.Web.Framework.Events;
using Nop.Web.Framework.Menu;
using Nop.Web.Models.Sitemap;
namespace Nop.Plugin.Misc.FruitBankPlugin.Services
{
public class EventConsumer : BaseAdminMenuCreatedEventConsumer, IConsumer<OrderPlacedEvent>, IConsumer<AdminMenuCreatedEvent>
{
private readonly IGenericAttributeService _genericAttributeService;
private readonly IProductService _productService;
private readonly ISpecificationAttributeService _specificationAttributeService;
private readonly IOrderService _orderService;
private readonly IProductAttributeService _productAttributeService;
private readonly IWorkContext _workContext;
private readonly IStoreContext _storeContext;
private readonly IAdminMenu _adminMenu;
private readonly ILocalizationService _localizationService;
public EventConsumer(
IGenericAttributeService genericAttributeService,
IProductService productService,
ISpecificationAttributeService specificationAttributeService,
IOrderService orderService,
IProductAttributeService productAttributeService,
IPluginManager<IPlugin> pluginManager,
IWorkContext workContext,
IStoreContext storeContext,
IAdminMenu adminMenu,
ILocalizationService localizationService) : base(pluginManager)
{
_genericAttributeService = genericAttributeService;
_productService = productService;
_specificationAttributeService = specificationAttributeService;
_orderService = orderService;
_productAttributeService = productAttributeService;
_workContext = workContext;
_storeContext = storeContext;
_adminMenu = adminMenu;
_localizationService = localizationService;
}
protected override string PluginSystemName => "Misc.FruitBankPlugin";
public async Task HandleEventAsync(OrderPlacedEvent eventMessage)
{
var order = eventMessage.Order;
var orderItems = await _orderService.GetOrderItemsAsync(order.Id);
bool requiresMeasurement = false;
foreach (var item in orderItems)
{
var product = await _productService.GetProductByIdAsync(item.ProductId);
// itt pl. egy custom flag a producton, ami jelzi, hogy mérés kell hozzá
// akár egy product attribute is lehet, vagy egy saját extension metódus
if (product != null)
{
//var productAttributeMappings = await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(product.Id);
////Product Attributes
//foreach (var pam in productAttributeMappings)
//{
// var attributes = await _productAttributeService.GetProductAttributeValuesAsync(pam.Id);
// foreach (var attr in attributes)
// {
// // you can check for specific attribute by its name or id
// if (attr.Name == "NeedsToBeMeasured" && attr.IsPreSelected)
// {
// requiresMeasurement = true;
// break;
// }
// }
//}
var productSpecAttributes = await _specificationAttributeService.GetProductSpecificationAttributesAsync(product.Id);
foreach (var specAttribute in productSpecAttributes)
{
// Get the specification attribute
var specificationAttribute = await _specificationAttributeService
.GetSpecificationAttributeByIdAsync(specAttribute.Id);
// Get the specification attribute option
var specificationAttributeOption = await _specificationAttributeService
.GetSpecificationAttributeOptionByIdAsync(specAttribute.SpecificationAttributeOptionId);
System.Diagnostics.Debug.WriteLine($"Spec Attribute: {specificationAttribute.Name}, Option: {specificationAttributeOption.Name}");
// Check if this is your "NeedsToBeMeasured" specification attribute
if (specificationAttribute.Name == "Measureable" &&
specificationAttributeOption.Name == "Yes") // or whatever value you set
{
requiresMeasurement = true;
break;
}
}
}
}
if (requiresMeasurement)
{
var store = await _storeContext.GetCurrentStoreAsync();
// itt adjuk hozzá a GenericAttribute flag-et az orderhez
await _genericAttributeService.SaveAttributeAsync(order,
"PendingMeasurement", true, store.Id);
// status pending
// paymentstatus pending
}
}
public async Task HandleEventAsync(AdminMenuCreatedEvent eventMessage)
{
var rootNode = eventMessage.RootMenuItem;
var shipmentsListMenuItem = new AdminMenuItem
{
Visible = true,
SystemName = "FruitBank",
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.ShipmentsList"), // You can localize this with await _localizationService.GetResourceAsync("...")
IconClass = "fas fa-shipping-fast",
Url = _adminMenu.GetMenuItemUrl("Shipment", "List")
};
var createShipmentMenuItem = new AdminMenuItem
{
Visible = true,
SystemName = "Shipments.Create",
Title = "Create Shipment",
IconClass = "far fa-circle",
Url = _adminMenu.GetMenuItemUrl("Shipment", "Create")
};
// Create a new top-level menu item
var shipmentsMenuItem = new AdminMenuItem
{
Visible = true,
SystemName = "FruitBank",
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.Shipments"), // You can localize this with await _localizationService.GetResourceAsync("...")
IconClass = "fas fa-shipping-fast",
//Url = _adminMenu.GetMenuItemUrl("Shipment", "List")
ChildNodes = new[] { shipmentsListMenuItem, createShipmentMenuItem }
};
var shipmentConfigurationItem = rootNode;
shipmentConfigurationItem.ChildNodes.Insert(2, shipmentsMenuItem);
var invoiceListMenuItem = new AdminMenuItem
{
Visible = true,
SystemName = "FruitBank",
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.InvoicesList"), // You can localize this with await _localizationService.GetResourceAsync("...")
IconClass = "fas fa-file-invoice",
Url = _adminMenu.GetMenuItemUrl("Invoices", "List")
};
// Create a new top-level menu item
var invoicesMenuItem = new AdminMenuItem
{
Visible = true,
SystemName = "FruitBank",
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.Invoices"), // You can localize this with await _localizationService.GetResourceAsync("...")
IconClass = "fas fa-file-invoice",
//Url = _adminMenu.GetMenuItemUrl("Shipment", "List")
ChildNodes = new[] { invoiceListMenuItem }
};
var invoiceConfigurationItem = rootNode;
invoiceConfigurationItem.ChildNodes.Insert(2, invoicesMenuItem);
var configurationItem = rootNode.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Configuration"));
if (configurationItem is null)
return;
var shippingItem = configurationItem.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Shipping"));
var widgetsItem = configurationItem.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Widgets"));
if (shippingItem is null && widgetsItem is null)
return;
var index = shippingItem is not null ? configurationItem.ChildNodes.IndexOf(shippingItem) : -1;
if (index < 0)
index = widgetsItem is not null ? configurationItem.ChildNodes.IndexOf(widgetsItem) : -1;
if (index < 0)
return;
configurationItem.ChildNodes.Insert(index + 1, new AdminMenuItem
{
Visible = true,
SystemName = "FruitBank",
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.AI"),
IconClass = "far fa-dot-circle",
ChildNodes = new List<AdminMenuItem>
{
new()
{
Visible = true,
SystemName = "FruitBank",
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.Configure"),
IconClass = "far fa-circle",
Url = _adminMenu.GetMenuItemUrl("FruitBankPlugin", "Configure"),
}
}
});
}
}
}