Files
lux/Lux.UI/Components/Compo/OfferRowMan.razor.cs
T
Samuele Locatelli fd21093ee4 Refresh commenti
2025-09-24 18:27:43 +02:00

290 lines
8.7 KiB
C#

using EgwCoreLib.Lux.Core;
using EgwCoreLib.Lux.Core.RestPayload;
using EgwCoreLib.Lux.Data;
using EgwCoreLib.Lux.Data.DbModel.Sales;
using EgwCoreLib.Lux.Data.Services;
using EgwMultiEngineManager.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using static EgwCoreLib.Lux.Core.Enums;
namespace Lux.UI.Components.Compo
{
public partial class OfferRowMan : IDisposable
{
#region Public Properties
[Parameter]
public DisplayMode DisplayMode { get; set; } = DisplayMode.Standard;
[Parameter]
public EventCallback<bool> EC_Updated { get; set; }
[Parameter]
public int OfferID { get; set; } = 0;
#endregion Public Properties
#region Public Methods
/// <summary>
/// Dispose sottoscrizione canale
/// </summary>
public void Dispose()
{
DLService.UpdatePipe.EA_NewMessage -= UpdatePipe_EA_NewMessage;
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected IConfiguration Config { get; set; } = null!;
[Inject]
protected CalcRequestService CService { get; set; } = null!;
[Inject]
protected DataLayerServices DLService { get; set; } = null!;
[Inject]
protected ImageCacheService ICService { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected void ClosePopup()
{
showChangeMat = false;
EditBomRecord = null;
}
protected void DoEdit(OfferRowModel curRec)
{
EditRecord = curRec;
}
protected void DoSwapMat(OfferRowModel currRow)
{
showChangeMat = true;
EditBomRecord = currRow;
CurrBomList = DLService.OffertGetBomList(EditBomRecord);
}
/// <summary>
/// Calcolo URL immagine
/// </summary>
/// <param name="imgUid"></param>
/// <param name="env"></param>
/// <returns></returns>
protected string imgUrl(string imgUid, string env)
{
// cast string su env..
EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS envir = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW;
Enum.TryParse(env, out envir);
return ICService.ImageUrl($"{apiUrl}/{imgBasePath}", false, imgUid, envir);
}
/// <summary>
/// init obj
/// </summary>
protected override void OnInitialized()
{
apiUrl = Config.GetValue<string>("ServerConf:Prog.ApiUrl") ?? "";
imgBasePath = Config.GetValue<string>("ServerConf:ImageBaseUrl") ?? "";
DLService.UpdatePipe.EA_NewMessage += UpdatePipe_EA_NewMessage;
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
UpdateTable();
}
protected async Task RecalcOffer()
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler ricalcolare importi offerta?"))
return;
await DoRecalcOffer();
}
/// <summary>
/// Lancia la richiesta di ricaolo della BOM dal JWD (o equivalente)
/// </summary>
/// <returns></returns>
protected async Task RequestBom(OfferRowModel currRec)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler ricalcolare la BOM?"))
return;
// devo recuperare da ofgferta environment corrente, ora cablato
EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS currEnv = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW;
// preparo la domanda serializzata
Dictionary<string, string> DictExec = new Dictionary<string, string>();
// cablata la BOM
DictExec.Add("Mode", "2");
// UID cablato x ora...
DictExec.Add("UID", currRec.OfferRowUID);
DictExec.Add("Jwd", currRec.SerStruct);
CalcRequestDTO req = new CalcRequestDTO()
{
EnvType = currEnv,
DictExec = DictExec
};
// chiamo la chiamata POST alla API, che manda la richiesta via REDIS
await CService.CallRestPost($"{apiUrl}/{genBasePath}", $"{calcTag}/{currRec.OfferRowUID}", req);
#if false
isLoading = true;
// aspetta 1 sec
await Task.Delay(1000);
// ...e poi rilegge i dati, ricalcolando offerta...
await DoRecalcOffer();
#endif
}
#endregion Protected Methods
#region Private Fields
private List<OfferRowModel> AllRecords = new List<OfferRowModel>();
private string apiUrl = "";
private string calcTag = "calc";
private List<BomItemDTO>? CurrBomList = null;
private int currPage = 1;
private OfferRowModel? EditBomRecord = null;
private OfferRowModel? EditRecord = null;
private string genBasePath = "generic";
private string imgBasePath = "";
private bool isLoading = false;
private List<OfferRowModel> ListRecords = new List<OfferRowModel>();
private int numRecord = 10;
private bool showChangeMat = false;
private int totalCount = 0;
#endregion Private Fields
#region Private Methods
private async Task DoRecalcOffer()
{
isLoading = true;
await DLService.OffertUpdateCost(OfferID);
await Task.Delay(50);
await ReloadData();
UpdateTable();
isLoading = false;
await EC_Updated.InvokeAsync(true);
}
/// <summary>
/// Svuota cache corrente + rilegge dati
/// </summary>
private async Task ForceReloadData()
{
isLoading = true;
await Task.Delay(20);
await DLService.FlushCacheOffersAsync();
await Task.Delay(20);
await ReloadData();
UpdateTable();
isLoading = false;
}
/// <summary>
/// Legge i dati dei record completi
/// </summary>
private async Task ReloadData()
{
if (OfferID > 0)
{
AllRecords = await DLService.OfferRowGetByOffer(OfferID);
totalCount = AllRecords.Count();
}
}
/// <summary>
/// salva nel record corrente la BOM aggiornata e poi ricalcola importo...
/// </summary>
/// <param name="newBomList"></param>
/// <exception cref="NotImplementedException"></exception>
private async void UpdateBom(List<BomItemDTO> newBomList)
{
if (EditBomRecord != null)
{
// salvo BOM nel record corrente...
bool fatto = await DLService.OffertRowUpdateBom(EditBomRecord.OfferRowID, newBomList);
// ricalcolo offerta completa
await ReloadData();
UpdateTable();
}
}
/// <summary>
/// Task verifica update ricevuti
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <exception cref="NotImplementedException"></exception>
private async void UpdatePipe_EA_NewMessage(object? sender, EventArgs e)
{
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
// cerco se faccia aprte dei record correnti...
var recFound = AllRecords.Any(x => x.OfferRowUID == currArgs.newMessage);
if (recFound)
{
isLoading = true;
await Task.Delay(10);
#if false
if (currArgs.msgUid.Equals($"{subChannel}:{windowUid}"))
{
}
#endif
// se si tratta dell'UID corrente --> fa update
await DoRecalcOffer();
await InvokeAsync(StateHasChanged);
}
}
await Task.Delay(1);
}
/// <summary>
/// Filtro e paginazione
/// </summary>
private void UpdateTable()
{
// fix paginazione
ListRecords = AllRecords
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
isLoading = false;
}
#endregion Private Methods
}
}