|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Nop.Core; |
| 7 | +using Nop.Core.Domain.Catalog; |
| 8 | +using Nop.Plugin.Widgets.AzPersonalizer.Controllers; |
| 9 | +using Nop.Plugin.Widgets.AzPersonalizer.Models; |
| 10 | +using Nop.Plugin.Widgets.AzPersonalizer.Services; |
| 11 | +using Nop.Services.Catalog; |
| 12 | +using Nop.Services.Configuration; |
| 13 | +using Nop.Services.Logging; |
| 14 | +using Nop.Web.Factories; |
| 15 | +using Nop.Web.Framework.Components; |
| 16 | +using Nop.Web.Framework.Infrastructure; |
| 17 | +using Nop.Web.Models.Catalog; |
| 18 | + |
| 19 | +namespace Nop.Plugin.Widgets.AzPersonalizer.Components |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Represents the view component to place a widget into pages |
| 23 | + /// </summary> |
| 24 | + [ViewComponent(Name = "WidgetsAzPersonalizer")] |
| 25 | + public class WidgetsAzPersonalizerViewComponent : NopViewComponent |
| 26 | + { |
| 27 | + private readonly IStoreContext _storeContext; |
| 28 | + private readonly ISettingService _settingService; |
| 29 | + private readonly IWebHelper _webHelper; |
| 30 | + private readonly IProductService _productService; |
| 31 | + private readonly IProductModelFactory _productModelFactory; |
| 32 | + private readonly ILogger _logger; |
| 33 | + private readonly PersonalizerClientService _personalizerClientService; |
| 34 | + private static bool debugging = true; |
| 35 | + |
| 36 | + |
| 37 | + public WidgetsAzPersonalizerViewComponent(IStoreContext storeContext, |
| 38 | + ISettingService settingService, |
| 39 | + PersonalizerClientService personalizerClientService, |
| 40 | + IProductService productService, |
| 41 | + IProductModelFactory productModelFactory, |
| 42 | + IWebHelper webHelper, |
| 43 | + ILogger logger) |
| 44 | + { |
| 45 | + _logger = logger; |
| 46 | + _storeContext = storeContext; |
| 47 | + _settingService = settingService; |
| 48 | + _webHelper = webHelper; |
| 49 | + _personalizerClientService = personalizerClientService; |
| 50 | + _productService = productService; |
| 51 | + _productModelFactory = productModelFactory; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Invoke view component |
| 56 | + /// </summary> |
| 57 | + /// <param name="widgetZone">Widget zone name</param> |
| 58 | + /// <param name="additionalData">Additional data</param> |
| 59 | + /// <returns> |
| 60 | + /// A task that represents the asynchronous operation |
| 61 | + /// The task result contains the view component result |
| 62 | + /// </returns> |
| 63 | + public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData) |
| 64 | + { |
| 65 | + var azPersonalizerSettings = await _settingService.LoadSettingAsync<AzPersonalizerSettings>((await _storeContext.GetCurrentStoreAsync()).Id); |
| 66 | + if (widgetZone.Equals(PublicWidgetZones.ProductDetailsEssentialBottom)) |
| 67 | + { |
| 68 | + string[] url = (_webHelper.GetThisPageUrl(true))?.Split("?"); |
| 69 | + if (TempData["RewardID"] != null |
| 70 | + && TempData["OrderedIDs"] != null |
| 71 | + && url.Length > 1 && url[1].Contains("azrec")) // TODO remover url? |
| 72 | + { |
| 73 | + string id = TempData["RewardID"] is string rewardID ? rewardID : null; |
| 74 | + string pos = TempData["OrderedIDs"] is string position ? position : null; |
| 75 | + |
| 76 | + await ProcessRewardAsync(id, pos, additionalData); |
| 77 | + } |
| 78 | + return await ReccomendedList(azPersonalizerSettings, additionalData); |
| 79 | + |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + // While there is only one widget zone with reccomendations doesn't return anything. But to add |
| 84 | + // recomendations to multiple widget zones should add else if's. |
| 85 | + return View(); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Prepares the view component with a list of Ranked Actions |
| 93 | + /// </summary> |
| 94 | + /// <param name="azPersonalizerSettings"></param> |
| 95 | + /// <param name="additionalData"></param> |
| 96 | + /// <returns> |
| 97 | + /// A task that represents the asynchronous operation |
| 98 | + /// The task result contains the view component result |
| 99 | + /// </returns> |
| 100 | + private async Task<IViewComponentResult> ReccomendedList(AzPersonalizerSettings azPersonalizerSettings, object additionalData) |
| 101 | + { |
| 102 | + |
| 103 | + try |
| 104 | + { |
| 105 | + var a = await _personalizerClientService?.getRankedActions(additionalData); |
| 106 | + var model = a.Ranking; |
| 107 | + TempDataTokenController tdt = new TempDataTokenController(a.EventId); // Design mau? |
| 108 | + TempData["RewardID"] = a.EventId; |
| 109 | + IList<Product> products = new List<Product>(model.Count); |
| 110 | + string odds = a.EventId + ": "; |
| 111 | + for(int i = 0; i< model.Count; i++) |
| 112 | + { |
| 113 | + odds +=("(" + model[i]?.Id + "," + model[i].Probability +");"); |
| 114 | + products.Insert(i,await _productService?.GetProductByIdAsync(int.Parse(model[i]?.Id))); |
| 115 | + tdt.AddId(model[i]?.Id); |
| 116 | + } |
| 117 | + TempData["OrderedIDs"] = tdt.OrderedIDs; |
| 118 | + if(debugging) await _logger.WarningAsync(odds); |
| 119 | + |
| 120 | + return View("~/Plugins/Widgets.AzPersonalizer/Views/Reccomendations.cshtml", new ProductOverviewToRankingList( (await _productModelFactory.PrepareProductOverviewModelsAsync(products)).ToList(), a.EventId)); |
| 121 | + |
| 122 | + }catch(Exception e) |
| 123 | + { |
| 124 | + await _logger.ErrorAsync("Failed to get reccomendations"); |
| 125 | + return View(); |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Calculates and sends a reward to a given Event. |
| 132 | + /// </summary> |
| 133 | + /// <param name="ID"></param> |
| 134 | + /// <param name="OrderIDs"></param> |
| 135 | + /// <param name="additionalData"></param> |
| 136 | + /// <returns>An empty task.</returns> |
| 137 | + private async Task ProcessRewardAsync(string eventID, string OrderIDs, object additionalData) |
| 138 | + { |
| 139 | + var product = additionalData is ProductDetailsModel model ? model : null; |
| 140 | + if (string.IsNullOrEmpty(eventID) || string.IsNullOrEmpty(OrderIDs) || product == null) |
| 141 | + return; |
| 142 | + string[] products = OrderIDs.Split("-"); |
| 143 | + for(int i = 0;i<products.Length; i++) |
| 144 | + { |
| 145 | + if(products[i] == product.Id.ToString()) // TODO mudar para .Contains? |
| 146 | + { |
| 147 | + await _personalizerClientService.RewardAnAction(eventID, i); |
| 148 | + return; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | +} |
0 commit comments