Files
mapo-core/MP.Land/Components/CompareVocabolario.razor.cs
T
2025-06-28 09:24:16 +02:00

296 lines
9.3 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.AppAuth.DTO;
using MP.AppAuth.Models;
using MP.AppAuth.Services;
using MP.Land.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Land.Components
{
public partial class CompareVocabolario
{
#region Public Properties
[Parameter]
public EventCallback<string> EC_reqAct { get; set; }
[Parameter]
public EventCallback<string> EC_reqReload { get; set; }
[Parameter]
public List<VocabolarioModel> ListVocabolarioLoc { get; set; } = new List<VocabolarioModel>();
[Parameter]
public List<VocabolarioModel> ListVocabolarioRem { get; set; } = new List<VocabolarioModel>();
[Parameter]
public bool ShowDetail { get; set; } = false;
#endregion Public Properties
#region Protected Fields
protected List<CompVocabolario> ListCompare = new List<CompVocabolario>();
protected List<CompVocabolario> ListCompareAll = new List<CompVocabolario>();
#endregion Protected Fields
#region Protected Properties
protected CmpCompType.Display currFilt { get; set; } = new CmpCompType.Display();
[Inject]
protected AppAuthService DataService { get; set; }
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected LMessageService AppMServ { get; set; } = null!;
public string searchVal
{
get => AppMServ.SearchVal;
}
protected int numLoc { get; set; } = 0;
protected int numRem { get; set; } = 0;
#endregion Protected Properties
#region Protected Methods
protected string genClass(CompVocabolario currItem)
{
string answ = currItem.IsEqual ? "text-dark" : currItem.SrcExist && currItem.DestExist ? "text-warning" : currItem.SrcExist ? "text-success" : "text-danger";
return answ;
}
protected async Task LocalAdd(CompVocabolario currItem)
{
//if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler aggiungere il record?"))
// return;
bool fatto = false;
if (currItem != null)
{
fatto = await DataService.VocabolarioAdd(currItem.Source);
}
if (fatto)
{
await ReqReload();
}
}
protected async Task LocalRem(CompVocabolario currItem)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler eliminare il record?"))
return;
bool fatto = false;
if (currItem != null)
{
fatto = await DataService.VocabolarioDelete(currItem.Dest);
}
if (fatto)
{
await ReqReload();
}
}
protected async Task LocalUpd(CompVocabolario currItem)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler aggiungere il record?"))
return;
bool fatto = false;
if (currItem != null)
{
fatto = await DataService.VocabolarioUpd(currItem.Source);
}
if (fatto)
{
await ReqReload();
}
}
protected override void OnParametersSet()
{
ForceReload();
}
/// <summary>
/// Report richiesta confronto x Vocabolario
/// </summary>
protected async Task ReqCompare()
{
await EC_reqAct.InvokeAsync("Vocabolario");
}
/// <summary>
/// Report evento richiesta confronto Vocabolario
/// </summary>
protected async Task ReqReload()
{
await EC_reqReload.InvokeAsync("Vocabolario");
}
/// <summary>
/// Report evento richiesta reset confronto
/// </summary>
protected async Task ResetCompare()
{
await EC_reqAct.InvokeAsync("");
}
protected void SetFilter(CmpCompType.Display filtDisplay)
{
currFilt = filtDisplay;
ForceReload();
}
protected async Task SetNumRec(int newNum)
{
numRecord = newNum;
currPage = 1;
await InvokeAsync(UpdPaging);
await Task.Delay(1);
isLoading = false;
}
protected async Task SetPage(int newNum)
{
isLoading = true;
currPage = newNum;
await InvokeAsync(UpdPaging);
await Task.Delay(1);
isLoading = false;
}
#endregion Protected Methods
#region Private Properties
private int currPage { get; set; } = 1;
private bool isLoading { get; set; } = false;
private int numRecord { get; set; } = 10;
private int totalCount { get; set; } = 0;
#endregion Private Properties
#region Private Methods
private void ForceReload()
{
numLoc = ListVocabolarioLoc != null ? ListVocabolarioLoc.Count : 0;
numRem = ListVocabolarioRem != null ? ListVocabolarioRem.Count : 0;
// predispongo lista comparazione...
ListCompareAll = new List<CompVocabolario>();
int cIdx = 0;
// filtro EQUAL
if (currFilt.Equal)
{
foreach (var item in ListVocabolarioRem)
{
CompVocabolario newRec = new CompVocabolario()
{
Idx = cIdx++,
NomeVar = $"{item.Lemma}_{item.Lingua}",
Source = item,
Dest = ListVocabolarioLoc.Find(x => x.Lingua == item.Lingua && x.Lemma == item.Lemma)
};
if (newRec.IsEqual)
{
ListCompareAll.Add(newRec);
}
}
}
// ciclo valori src...
if (currFilt.Diff)
{
foreach (var item in ListVocabolarioRem)
{
CompVocabolario newRec = new CompVocabolario()
{
Idx = cIdx++,
NomeVar = $"{item.Lemma}_{item.Lingua}",
Source = item,
Dest = ListVocabolarioLoc.Find(x => x.Lingua == item.Lingua && x.Lemma == item.Lemma)
};
if (!newRec.IsEqual && currFilt.Diff && newRec.SrcExist && newRec.DestExist)
{
ListCompareAll.Add(newRec);
}
}
}
if (currFilt.Sx)
{
foreach (var item in ListVocabolarioRem)
{
// se non c'è...
if (ListVocabolarioLoc.Where(x => x.Lingua == item.Lingua && x.Lemma == item.Lemma).Count() == 0)
{
CompVocabolario newRec = new CompVocabolario()
{
Idx = cIdx++,
NomeVar = $"{item.Lemma}_{item.Lingua}",
Source = item,
Dest = ListVocabolarioLoc.Find(x => x.Lingua == item.Lingua && x.Lemma == item.Lemma)
};
if (!newRec.IsEqual)
{
ListCompareAll.Add(newRec);
}
}
}
}
if (currFilt.Dx)
{
// cerco eventuali record SOLO locali...
foreach (var item in ListVocabolarioRem)
{
// se non c'è...
if (ListVocabolarioRem.Where(x => x.Lingua == item.Lingua && x.Lemma == item.Lemma).Count() == 0)
{
CompVocabolario newRec = new CompVocabolario()
{
Idx = cIdx++,
NomeVar = $"{item.Lemma}_{item.Lingua}",
Source = null,
Dest = item
};
ListCompareAll.Add(newRec);
}
}
}
// filtro sui dati chiave e note
if (!string.IsNullOrEmpty(searchVal))
{
ListCompareAll = ListCompareAll
.Where(x => x.Source.Lemma.Contains(searchVal, StringComparison.InvariantCultureIgnoreCase) ||
x.Source.Traduzione.Contains(searchVal, StringComparison.InvariantCultureIgnoreCase))
.ToList();
}
totalCount = ListCompareAll.Count;
// riordino...
ListCompareAll = ListCompareAll.OrderBy(x => x.NomeVar).ToList();
// ciclo eventuali record locali rimasti
UpdPaging();
}
private void UpdPaging()
{
ListCompare = ListCompareAll.Skip((currPage - 1) * numRecord).Take(numRecord).ToList();
}
#endregion Private Methods
}
}