- Ottimizzazione codice

This commit is contained in:
2026-02-05 18:03:25 +01:00
parent 4ddf71f465
commit 3f474c4d39
6 changed files with 146 additions and 85 deletions

View File

@ -48,7 +48,7 @@
<div class="row">
<div class="col-4 mb-3">
<RadzenFormField Text="Provincia" Variant="Variant.Flat" Style="width: 100%;">
<RadzenDropDown TValue="Guid ?" @bind-bind-Value="@iscrizione.ProvinciaId" Change="@(args => onProvinciaChanged(args))" Style="width: 100%" TextProperty="Info" ValueProperty="Id" Placeholder="Seleziona la provincia"
<RadzenDropDown TValue="Guid ?" @bind-Value="@iscrizione.ProvinciaId" Change="@(args => onProvinciaChanged(args))" Style="width: 100%" TextProperty="Info" ValueProperty="Id" Placeholder="Seleziona la provincia"
Data="@provList" Size="ButtonSize.Small" />
</RadzenFormField>
<ValidationMessage For="@(() => iscrizione.ProvinciaId)" />
@ -200,10 +200,19 @@
? await _managerService.EventoService.RicercaPer(x => x.Id == invito.EventoId)
: (await _managerService.EventoService.RicercaQueryable(x => x.DataA > DateTime.Now,
ordinamento: y => y.OrderBy(z => z.DataA))).FirstOrDefault();
inizioEvento = evento?.DataDa ?? DateTime.Now.AddDays(-7);
fineEvento = evento?.DataA ?? DateTime.Now.AddDays(7);
if (!hasCliente)
NoCliente();
else
HasCliente();
}
/// <summary>
/// Funzione di salvataggio, mappa i dati e aggiunge gli ids per le FK
/// </summary>
private async Task onIscrizioneSave()
{
var userId = await MembershipUtils.GetUserId(_auth);
@ -228,6 +237,9 @@
_navManager.NavigateTo($"/Grazie");
}
/// <summary>
/// Imposta i dati cliente al cambio della Partita IVA
/// </summary>
public async Task SetDatiCliente()
{
iscrizione.DestinazioneId = invito.DestinazioneId;
@ -245,6 +257,7 @@
{
destinazioniList = new List<DestinazioneViewModel>() { new() { RagioneSociale = "--Nessuna" } };
iscrizione.EsperienzaConDAC = "No";
}
public void HasCliente()
@ -252,6 +265,9 @@
iscrizione.EsperienzaConDAC = "Si";
}
/// <summary>
/// Aggiorna i comuni in base alla provincia selezionata
/// </summary>
private async Task onProvinciaChanged(object args)
{
Guid parsed;
@ -271,6 +287,10 @@
.Select(x => (ComuneIstatViewModel)x).ToList();
}
/// <summary>
/// Serve per aggiornare i dati del form se ci troviamo nella situazione in cui il cliente è salvato a db
/// Attiva la compilazione solo se siamo nel form e da UI chi compila ci dice che è già stato cliente
/// </summary>
private async Task onCodiceFornitoChanged(string text)
{
if (!hasCliente) return;

View File

@ -0,0 +1,94 @@
@using System.Globalization
@using System.IO
@using Microsoft.EntityFrameworkCore
@using StandManager.Model
@attribute [Authorize]
@rendermode InteractiveServer
@inject AuthenticationStateProvider auth
<RadzenStack Gap="1rem" class="rz-m-12">
<RadzenProgressBar Value="@counter" Max="@utentiTotali" Unit="@counterLabel" AriaLabel="" />
</RadzenStack>
@code {
[CascadingParameter] private Dialog _dialog { get; set; }
[Parameter] public UploadChangeEventArgs args { get; set; }
private int utentiTotali { get; set; } = 0;
private int counter { get; set; } = 0;
private string counterLabel { get; set; } = string.Empty;
protected override async Task OnInitializedAsync()
{
base.OnInitializedAsync();
var file = args.Files.FirstOrDefault();
await using var uploadStream = file.OpenReadStream(10_000_000);
var idClaim = await MembershipUtils.GetUserId(auth);
await using var ms = new MemoryStream();
await uploadStream.CopyToAsync(ms);
ms.Position = 0;
using var reader = new StreamReader(ms, System.Text.Encoding.UTF8);
var mailDictionary = new Dictionary<string, string>();
bool isHeader = true;
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
if (string.IsNullOrWhiteSpace(line)) continue;
if (isHeader)
{
isHeader = false;
continue;
}
var parts = line.Split(',');
if (parts.Length >= 2)
{
var code = parts[0];
var mail = parts[1];
if (!string.IsNullOrEmpty(code) && !mailDictionary.ContainsKey(code))
{
mailDictionary.Add(code, mail);
}
}
}
var listaUtenti = await (await _managerService.UtenteService.RicercaQueryable(x => !x.Eliminato, solaLettura: false)).ToListAsync();
utentiTotali = listaUtenti.Count;
counterLabel = " utenti analizzati";
foreach (var utente in listaUtenti)
{
if (!string.IsNullOrWhiteSpace(utente.CodiceAgente) && mailDictionary.TryGetValue(utente.CodiceAgente, out string nuovaMail) && !string.IsNullOrWhiteSpace(nuovaMail) && utente.Email != nuovaMail)
{
utente.Email = nuovaMail;
await _managerService.UtenteService.Salva(utente, idClaim);
}
counter++;
StateHasChanged();
}
StateHasChanged();
ms.Close();
_dialogService.Close();
}
// Classe per mappare le colonne del CSV
private class CSVMail
{
public string Code { get; set; }
public string Mail { get; set; }
}
}

View File

@ -20,6 +20,9 @@
</div>
<div class="col-auto ms-auto">
<div class="btn-list">
<RadzenUpload class="btn-5 d-none d-sm-inline-block" Change=@onUpload ChooseText="Importa mail utenti da CSV" />
<a href="/management/Utenti/Modifica" class="btn btn-primary btn-5 d-none d-sm-inline-block">
Nuovo utente
</a>
@ -57,6 +60,7 @@
@code {
IQueryable<Utente> utenti;
RadzenDataGrid<Utente> userGrid;
private Ruolo? ruolo;
/// <summary>
/// Carica la lista degli utenti non eliminati, ordinandoli per cognome e nome.
@ -96,4 +100,25 @@
ordinamento: x => x.OrderBy(y => y.Cognome).ThenBy(z => z.Nome));
}
}
private async Task onUpload(UploadChangeEventArgs args)
{
var file = args.Files.FirstOrDefault();
if (file == null ||
!file.Name.EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
{
await _dialogService.Alert("Sono supportati solo file CSV .csv", "Errore");
return;
}
try
{
// await using var uploadStream = file.OpenReadStream(10_000_000);
await _dialogService.OpenAsync<Mail_Utenti_Import>("Importazione mail per utenti", new Dictionary<string, object>() { { "args", args } });
}
catch (Exception ex)
{
var er = ex.Message;
await _dialogService.Alert("Si è verificato un errore durante l'importazione del file.", "Errore");
}
}
}

View File

@ -13,18 +13,6 @@
<div class="container my-5 py-3">
<ClienteToggle ValueChanged="onClienteToggleChanged" />
@* @if (showCodiceCliente)
{
<div class="col-12">
<div class="row">
<div class="col-12 mb-3">
<RadzenFormField Text="Codice cliente o Partita IVA" Variant="Variant.Flat" Style="width: 100%;">
<RadzenTextBox Style="width: 100%" aria-label="Codice cliente o Partita IVA" Change="(args => onCodiceFornitoChanged(args))" />
</RadzenFormField>
</div>
</div>
</div>
} *@
@if (showForm)
{
@ -42,8 +30,6 @@
private Component_Registrazione registrazione { get; set; } = new();
private bool hasCliente { get; set; }
private string codiceFornito { get; set; }
private void onClienteToggleChanged(bool value)
{
if (!value)
@ -59,30 +45,6 @@
showCodiceCliente = value;
showForm = true;
}
private async Task onCodiceFornitoChanged(string text)
{
codiceFornito = text;
invito.CodiceFornito = codiceFornito;
var destinazione = (await _managerService.DestinazioneService.RicercaPer(
x => x.PartitaIva == codiceFornito || x.CodiceFiscale == codiceFornito || x.Rid == codiceFornito || x.Cliente.Rid == codiceFornito,
includi: x => x.Include(y => y.Cliente)));
if (destinazione == null || string.IsNullOrEmpty(text))
{
await _dialogService.Alert("Non è stato trovato il clente (Codice cliente/Partita IVA non presente)", "Errore", new ConfirmOptions() { OkButtonText = "Ok" });
}
else
{
invito.ClienteId = destinazione.ClienteId;
invito.DestinazioneId = destinazione.Id;
invito.CodiceFornito = codiceFornito;
invito.RagioneSociale = destinazione.Cliente?.RagioneSociale ?? string.Empty;
await registrazione.SetDatiCliente();
}
showForm = true;
StateHasChanged();
}
}

View File

@ -28,46 +28,6 @@
private Component_Registrazione registrazione { get; set; } = new();
private bool hasCliente { get; set; }
private string codiceFornito { get; set; }
private void onClienteToggleChanged(bool value)
{
if (!value)
{
hasCliente = false;
registrazione.NoCliente();
}
else
{
hasCliente = true;
registrazione.HasCliente();
}
showCodiceCliente = value;
showForm = true;
}
private async Task onCodiceFornitoChanged(string text)
{
codiceFornito = text;
invito.CodiceFornito = codiceFornito;
var destinazione = (await _managerService.DestinazioneService.RicercaPer(
x => x.PartitaIva == codiceFornito || x.CodiceFiscale == codiceFornito || x.Rid == codiceFornito || x.Cliente.Rid == codiceFornito,
includi: x => x.Include(y => y.Cliente)));
if (destinazione != null)
{
invito.ClienteId = destinazione.ClienteId;
invito.DestinazioneId = destinazione.Id;
invito.CodiceFornito = codiceFornito;
invito.RagioneSociale = destinazione.Cliente?.RagioneSociale ?? string.Empty;
await registrazione.SetDatiCliente();
}
showForm = true;
}
private async Task onSubmitCompleted(IscrizioneEvento model)
{
var queue = new MailQueue()