Files
StandManager/StandManager/Components/Pages/Management/Clienti_Import.razor

154 lines
6.2 KiB
Plaintext

@using ClosedXML.Excel
@using Microsoft.EntityFrameworkCore
@using StandManager.Model
@attribute [Authorize]
@rendermode InteractiveServer
@inject AuthenticationStateProvider auth
<RadzenStack Gap="1rem" class="rz-m-12">
<RadzenProgressBar Value="@counter" Max="@clientiTotali" Unit="@counterLabel" AriaLabel="" />
</RadzenStack>
@code {
[CascadingParameter] private Dialog _dialog { get; set; }
[Parameter] public UploadChangeEventArgs args { get; set; }
private int clientiTotali { 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 workbook = new XLWorkbook(ms);
var ws = workbook.Worksheet(1);
var usedRange = ws.RangeUsed();
var rows = new List<ClienteExcelViewModel>(usedRange.RowCount() - 1);
foreach (var row in usedRange.RowsUsed().Skip(1))
rows.Add(new ClienteExcelViewModel(row));
var codiciAgente = rows.Select(r => new { CodiceAgente = r.Agente, Nome = r.DescrizioneAgente, Capoarea = r.CapoArea }).Distinct().ToList();
var codiciCapoarea = rows.Select(r => new { CodiceCapoarea = r.CapoArea, Nome = r.DescrizioneCapoArea }).Distinct().ToList();
foreach (var fileCapoarea in codiciCapoarea)
{
var capoarea = await _managerService.UtenteService.RicercaPer(x => x.CodiceAgente == fileCapoarea.CodiceCapoarea, solaLettura: false);
capoarea = await mapCapoArea(capoarea ?? new Utente(), fileCapoarea.CodiceCapoarea, fileCapoarea.Nome);
await _managerService.UtenteService.Salva(capoarea, idClaim);
}
var capoareaList = await _managerService.UtenteService.ListaCapoarea();
foreach (var fileAgente in codiciAgente)
{
var agente = await _managerService.UtenteService.RicercaPer(x => x.CodiceAgente == fileAgente.CodiceAgente, solaLettura: false);
// if ((agente?.Id ?? new Guid()) != Guid.Empty)
// continue;
var capoarea = capoareaList.FirstOrDefault(x => x.CodiceAgente == fileAgente.Capoarea);
agente = await mapAgente(agente ?? new Utente(), fileAgente.CodiceAgente, fileAgente.Nome, capoarea?.Id);
await _managerService.UtenteService.Salva(agente, idClaim);
}
var ragioniSociali = rows.Select(r => new { Rid = r.CodCli, RagioneSociale = r.RagSocCliente }).Distinct().ToList();
clientiTotali = ragioniSociali.Count;
counterLabel = " di " + clientiTotali;
foreach (var cliente in ragioniSociali)
{
var righeCliente = rows.Where(r => r.CodCli == cliente.Rid).ToList();
var clienteDb = await _managerService.ClienteService.RicercaPer(
x => x.Rid == cliente.Rid && x.Eliminato == false,
includi: x => x.Include(i => i.Destinazioni).Include(y => y.Agente).Include(z => z.Capoarea),
solaLettura: false) ?? new Cliente();
clienteDb = await mapCliente(clienteDb, righeCliente, capoareaList, idClaim);
var codiceCapoarea = righeCliente.FirstOrDefault().CapoArea;
var codiceAgente = righeCliente.FirstOrDefault().Agente;
clienteDb.CapoareaId = (await _managerService.UtenteService.RicercaPer(x => x.CodiceAgente == codiceCapoarea))?.Id;
clienteDb.AgenteId = (await _managerService.UtenteService.RicercaPer(x => x.CodiceAgente == codiceAgente))?.Id;
await _managerService.ClienteService.Salva(clienteDb, idClaim);
counter += 1;
StateHasChanged();
}
ms.Close();
_dialogService.Close();
}
private async Task<Cliente> mapCliente(Cliente model, List<ClienteExcelViewModel> rows, List<Utente> capoareaList, Guid idClaim)
{
var firstRow = rows.First();
model.Rid = firstRow.CodCli;
model.RagioneSociale = firstRow.RagSocCliente;
model.PartitaIva = firstRow.PartitaIva;
model.TipologiaGestionale = firstRow.Tipologia;
model.Destinazioni ??= new();
foreach (var destinazioneRiga in rows)
{
var destinazione = model.Destinazioni.FirstOrDefault(x => x.Rid == destinazioneRiga.CodDes) ?? new Destinazione();
destinazione = await mapDestinazione(destinazione, model.Id, destinazioneRiga, model.AgenteId);
if (destinazione.Id == Guid.Empty)
model.Destinazioni.Add(destinazione);
}
return model;
}
private async Task<Destinazione> mapDestinazione(Destinazione model, Guid clienteId, ClienteExcelViewModel row, Guid? agenteId)
{
model.Rid = row.CodDes;
model.RagioneSociale = row.RagSocDestinazione;
model.PartitaIva = row.PartitaIva;
model.ClienteId = clienteId;
model.Indirizzo = $"{row.Indirizzo} {row.NumeroCivico}";
model.Cap = row.Cap;
model.Citta = row.Comune;
model.Provincia = row.Provincia;
model.Email = row.MailFatturazione;
model.EmailInvito = row.MailFatturazione;
model.NumeroTelefono = row.Telefono;
model.AgenteId = agenteId;
return model;
}
private async Task<Utente> mapCapoArea(Utente model, string codice, string nome)
{
model.Username = nome;
model.CodiceAgente = codice;
model.Nome = nome;
model.Cognome = string.Empty;
model.Email = string.Empty;
model.Password = string.Empty;
model.IsCapoarea = true;
return model;
}
private async Task<Utente> mapAgente(Utente model, string codice, string nome, Guid? capoareaId)
{
model.Username = nome;
model.CodiceAgente = codice;
model.Nome = nome;
model.Cognome = string.Empty;
model.Email = string.Empty;
model.Password = string.Empty;
model.CapoareaId = capoareaId;
return model;
}
}