Files
2024-06-20 13:15:12 +02:00

453 lines
15 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Newtonsoft.Json;
using NLog;
using System;
using WebDoorCreator.Data.DbModels;
using WebDoorCreator.Data.Services;
using WebDoorCreator.UI.Data;
namespace WebDoorCreator.UI.Components.Hardware
{
public partial class HwSingleInstance
{
#region Public Properties
[Parameter]
public DoorOpModel DoorOpInst { get; set; } = null!;
[Parameter]
public EventCallback<bool> E_act2Rel { get; set; }
[Parameter]
public EventCallback<DoorOpModel> E_recordUpdate { get; set; }
[Parameter]
public string HwCode { get; set; } = "";
[Parameter]
public int instanceN { get; set; } = 0;
[Parameter]
public string Lingua { get; set; } = "EN";
[CascadingParameter(Name = "orderStatus")]
public int orderStatus { get; set; } = 10;
[CascadingParameter]
public string userId { get; set; } = "";
#endregion Public Properties
#region Protected Properties
[Inject]
protected IConfiguration config { get; set; } = null!;
protected WebDoorCreator.Data.DDF.Converter currDdfConv { get; set; } = null!;
protected string currTempOrSh { get; set; } = "";
/// <summary>
/// Array dati
/// </summary>
protected Dictionary<string, string>? dictValCurr { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Array dati originali all'apertura
/// </summary>
protected Dictionary<string, string>? dictValOrig { get; set; } = new Dictionary<string, string>();
protected DoorOpModel DoorOpOrig { get; set; } = null!;
protected string folderName
{
get => dictValCurr != null ? tryGetCurrVal("Folder") : "";
set
{
if (dictValCurr != null)
{
if (dictValCurr["Folder"] != value)
{
dictValCurr["Folder"] = value;
var pUpd = Task.Run(async () =>
{
templateName = "";
await SetSelectedData();
});
}
}
}
}
protected Dictionary<string, List<string>> graphicParams { get; set; } = null!;
protected bool hasChanged { get; set; } = false;
protected bool isLoading { get; set; } = false;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected List<ListValuesModel>? listValuesFiles { get; set; } = null!;
protected List<ListValuesModel>? listValuesFname { get; set; } = null!;
protected List<ListValuesModel>? listValuesHW { get; set; } = null!;
[Inject]
protected NavigationManager NavMan { get; set; } = null!;
protected List<string> ordListVal { get; set; } = new List<string>();
[Inject]
protected QueueDataService QDataServ { get; set; } = null!;
protected string templateName
{
get
{
string answ = "";
if (dictValCurr != null)
{
if (dictValCurr[currTempOrSh] != null)
{
answ = tryGetCurrVal(currTempOrSh);
}
}
return answ;
}
set
{
if (dictValCurr != null)
{
if (dictValCurr[currTempOrSh] != null)
{
if (dictValCurr[currTempOrSh] != value)
{
dictValCurr[currTempOrSh] = value;
DoorOpInst.userConfirm = "";
DoorOpInst.DtConfirm = null;
if (!string.IsNullOrEmpty(value))
{
var pUpd = Task.Run(async () =>
{
// aggiorno direttamente i graphics parameters...
var newData = await WDCService.DoorOpGetUpdatedVals(DoorOpInst.ObjectId, dictValCurr, graphicParams, currTempOrSh);
dictValCurr = newData.Item1;
graphicParams = newData.Item2;
await doSave();
await SetSelectedData();
await E_recordUpdate.InvokeAsync(DoorOpInst);
});
}
}
}
}
}
}
[Inject]
protected WebDoorCreatorService WDCService { get; set; } = null!;
[Inject]
protected WDCUserService WDCUService { get; set; } = null!;
[Inject]
protected WDCVocabularyService WDVService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected List<string> comboSet(string pKey)
{
List<string> answ = new List<string>();
if (graphicParams.ContainsKey(pKey))
{
answ = graphicParams[pKey].ToList();
}
return answ;
}
protected async Task doCancel()
{
await FullReloadData();
await InvokeAsync(StateHasChanged);
}
protected async Task doClone()
{
var doorOpToAdd = DoorOpInst.ObjClone(userId, 0);
List<DoorOpModel> doorOpAddList = new List<DoorOpModel>() { doorOpToAdd };
// salvo!
var done = await WDCService.DoorOpInsert(DoorOpInst.DoorId, doorOpAddList);
// richiedo aggiornamento SVG
await refreshSVG();
await E_act2Rel.InvokeAsync(done);
await FullReloadData();
}
protected async Task doDeleteInst()
{
var delConf = await JSRuntime.InvokeAsync<bool>("confirm", "Delete PERMANENTLY the current record?");
if (delConf)
{
var done = await WDCService.DoorOpDelete(DoorOpInst);
// richiedo aggiornamento SVG
await refreshSVG();
await E_act2Rel.InvokeAsync(done);
await FullReloadData();
}
}
protected async Task doSave()
{
if (dictValCurr != null)
{
// salvo i NUOVI valori...
DoorOpInst.CurrVals = dictValCurr;
DoorOpInst.GraphicParams = graphicParams;
// se debug + superadmin --> auto confirm
if (autoApprove && string.IsNullOrEmpty(DoorOpInst.userConfirm))
{
DoorOpInst.userConfirm = WDCUService.userId;
DoorOpInst.DtConfirm = DateTime.Now;
}
// salvo!
List<DoorOpModel> doorOpUpd = new List<DoorOpModel>() { DoorOpInst };
await WDCService.DoorOpUpdate(doorOpUpd);
// richiedo aggiornamento SVG
await refreshSVG();
// aggiorno oggetto clonato
DoorOpOrig = DoorOpInst.ObjClone(userId, 0);
// invio msg dato "salvabile" rileggo
await FullReloadData();
}
}
/// <summary>
/// Blazor: get query parm from the URL
/// </summary>
/// <param name="parmName"></param>
/// <returns></returns>
protected string GetQueryParm(string parmName)
{
var uriBuilder = new UriBuilder(NavMan.Uri);
var q = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
return q[parmName] ?? "";
}
protected string getTrad(string objId)
{
string answ = "";
if (listValuesHW != null)
{
var currHw2Trad = listValuesHW.Where(x => x.TableName == objId).FirstOrDefault();
if (currHw2Trad != null)
{
answ = translate(currHw2Trad.Label);
}
}
return answ;
}
protected void isChangedVal()
{
//hasChanged = !DoorOpInst.JsonActValEquals(currVal);
var currSer = JsonConvert.SerializeObject(dictValCurr);
var origSer = JsonConvert.SerializeObject(dictValOrig);
hasChanged = currSer != origSer;
if (hasChanged)
{
// se debug + superadmin --> auto confirm
if (autoApprove && string.IsNullOrEmpty(DoorOpInst.userConfirm))
{
DoorOpInst.userConfirm = WDCUService.userId;
DoorOpInst.DtConfirm = DateTime.Now;
}
DoorOpInst.JsoncActVal = JsonConvert.SerializeObject(dictValCurr);
Task.Run(async () =>
{
await E_recordUpdate.InvokeAsync(DoorOpInst);
});
}
}
protected bool isCombo(string pKey)
{
bool answ = false;
if (graphicParams.ContainsKey(pKey))
{
answ = graphicParams[pKey].Count > 1;
}
return answ;
}
protected override void OnInitialized()
{
base.OnInitialized();
var currMode = GetQueryParm("currMode");
if (!string.IsNullOrEmpty(currMode) && WDCUService.userRole == "SuperAdmin")
{
autoApprove = currMode.Equals("debug");
}
string vers = config.GetValue<string>("ConfDDF:VersNumber");
bool remDoorOp = config.GetValue<bool>("ConfDDF:RemoveDoorOps");
var headRows = config.GetSection("ConfDDF:Header").Get<List<string>>();
var footRows = config.GetSection("ConfDDF:Footer").Get<List<string>>();
currDdfConv = new WebDoorCreator.Data.DDF.Converter(vers, remDoorOp, headRows, footRows);
}
protected override async Task OnParametersSetAsync()
{
// copio valori orig...
DoorOpOrig = DoorOpInst.ObjClone(userId, 0);
await FullReloadData();
}
/// <summary>
/// Effettua generazione DDF + refresh SVG
/// </summary>
/// <returns></returns>
protected async Task refreshSVG()
{
List<DoorOpModel>? listOpAll = await WDCService.DoorOpGetByDoorId(DoorOpInst.DoorId);
if (listOpAll != null)
{
List<DoorOpModel> listOp = listOpAll.Where(x => x.userConfirm != "" && x.DtConfirm != null).ToList();
if (listOp != null)
{
// chiamo metodo x avere DDF serializzato
var CurrentCompoOrder = await WDCService.ListValuesGetAll("*", "Hardware");
if (CurrentCompoOrder != null)
{
foreach (var item in CurrentCompoOrder.OrderBy(x => x.Ordinal).ToList())
{
ordListVal.Add(item.TableName);
}
}
listOp = listOp.OrderBy(d => ordListVal.IndexOf(d.ObjectId)).ToList();
string currDdf = currDdfConv.GetSerialized(listOp);
// versione corrente del DDF generato
int currVers = await QDataServ.SendCalcReq(DoorOpInst.DoorId, currDdf);
}
}
}
protected string translate(string lemma)
{
string answ = "";
answ = WDVService.Traduci(Lingua, lemma);
return answ;
}
protected string tryGetCurrVal(string key)
{
string answ = "";
if (dictValCurr != null)
{
if (dictValCurr.ContainsKey(key))
{
answ = dictValCurr[key];
}
}
return answ;
}
#endregion Protected Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
private bool autoApprove = false;
#endregion Private Fields
#region Private Methods
private async Task FullReloadData()
{
dictValCurr = null;
await Task.Delay(1);
// genero 2 array x copia valori originali
dictValCurr = new Dictionary<string, string>();
dictValOrig = new Dictionary<string, string>();
foreach (var item in DoorOpOrig.CurrVals)
{
dictValCurr.Add(item.Key, item.Value);
dictValOrig.Add(item.Key, item.Value);
}
graphicParams = DoorOpInst.GraphicParams;
isChangedVal();
await SetSelectedData();
}
private string pathFinder(int id, bool reqConf)
{
string answ = $"HwPdfConfirm?doorOpId={id}&doorId={DoorOpInst.DoorId}&objectId={DoorOpInst.ObjectId}&reqConf={reqConf}";
return answ;
}
private async Task SetSelectedData()
{
isLoading = true;
await Task.Delay(1);
// fix folder
listValuesFiles = null;
listValuesHW = null;
listValuesHW = await WDCService.ListValuesGetAll(HwCode, "Hardware");
var tempListVal = await WDCService.ListValuesGetAll(HwCode, "Folder");
if (tempListVal != null)
{
listValuesFname = tempListVal;
}
if (string.IsNullOrEmpty(folderName))
{
if (listValuesFname != null)
{
folderName = listValuesFname.FirstOrDefault()?.Label!;
}
}
// fix template
var tempListFilesTempl = await WDCService.ListValuesGetAll(HwCode, "template");
var tempListFilesSh = await WDCService.ListValuesGetAll(HwCode, "shape");
if (tempListFilesTempl!.Count != 0)
{
listValuesFiles = tempListFilesTempl.Where(x => x.Value.Contains($"{folderName}\\")).ToList();
}
else if (tempListFilesSh!.Count != 0)
{
listValuesFiles = tempListFilesSh!.Where(x => x.Value.Contains($"{folderName}\\")).ToList();
}
try
{
if (listValuesFiles != null && listValuesFiles.Count > 0)
{
currTempOrSh = listValuesFiles.FirstOrDefault()?.FieldName!;
if (string.IsNullOrEmpty(templateName))
{
templateName = listValuesFiles.FirstOrDefault()?.Label!;
}
}
}
catch (Exception exc)
{
Log.Error($"Exc in SetSelectedData:{Environment.NewLine}{exc}");
}
isLoading = false;
await InvokeAsync(StateHasChanged);
}
#endregion Private Methods
}
}