Merge branch 'main' of https://git.oaservice.it/g.vitari/StandManager
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
@using ClosedXML.Excel
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using StandManager.Model
|
||||
@using System.Threading.Tasks
|
||||
|
||||
@rendermode InteractiveServer
|
||||
|
||||
@ -69,7 +70,10 @@
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
|
||||
clienti = (await _managerService.ClienteService.RicercaQueryable(x => x.Eliminato == false, includi: x => x.Include(y => y.Agente)))
|
||||
clienti = (await _managerService.ClienteService.RicercaQueryable(
|
||||
x => x.Eliminato == false,
|
||||
includi: x => x.Include(y => y.Agente),
|
||||
ordinamento: x => x.OrderBy(y => y.RagioneSociale)))
|
||||
.Select(x => (ClienteViewModel)x).ToList();
|
||||
}
|
||||
|
||||
@ -100,37 +104,16 @@
|
||||
private async Task onUpload(UploadChangeEventArgs args)
|
||||
{
|
||||
var file = args.Files.FirstOrDefault();
|
||||
if (file == null && (!file.Name.EndsWith(".xlsx") || !file.Name.EndsWith(".xls")))
|
||||
if (file == null ||
|
||||
!file.Name.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await _dialogService.Alert("Il file selezionato non è nel formato corretto.", "Errore");
|
||||
await _dialogService.Alert("Sono supportati solo file Excel .xlsx", "Errore");
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
await using var uploadStream = file.OpenReadStream(10_000_000);
|
||||
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 ragioniSociali = ws.RangeUsed().RowsUsed().Skip(1).Select(r => new { Rid = r.Cell(1).GetString(), RagioneSociale = r.Cell(4).GetString() }).Distinct().ToList();
|
||||
|
||||
foreach (var cliente in ragioniSociali)
|
||||
{
|
||||
var righeCliente = usedRange.RowsUsed().Where(r => r.Cell(1).GetString() == cliente.Rid).ToList();
|
||||
|
||||
var clienteDb = await _managerService.ClienteService.RicercaPer(
|
||||
x => x.Rid == cliente.Rid && x.Eliminato == false,
|
||||
includi: x => x.Include(i => i.Destinazioni),
|
||||
solaLettura: false) ?? new Cliente();
|
||||
|
||||
clienteDb = mapCliente(clienteDb, righeCliente);
|
||||
}
|
||||
|
||||
ms.Close();
|
||||
// await using var uploadStream = file.OpenReadStream(10_000_000);
|
||||
await _dialogService.OpenAsync<Clienti_Import>("Importazione clienti e destinazioni", new Dictionary<string, object>() { { "args", args } });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -138,13 +121,4 @@
|
||||
await _dialogService.Alert("Si è verificato un errore durante l'importazione del file.", "Errore");
|
||||
}
|
||||
}
|
||||
|
||||
private Cliente mapCliente(Cliente model, List<IXLRangeRow> rows)
|
||||
{
|
||||
var firstRow = rows.First();
|
||||
model.Rid = firstRow.Cell(1).GetString();
|
||||
model.RagioneSociale = firstRow.Cell(4).GetString();
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
@ -184,7 +184,7 @@
|
||||
{
|
||||
Resizable = false,
|
||||
Draggable = false,
|
||||
Width = "700px"
|
||||
Width = "900px"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -0,0 +1,91 @@
|
||||
@using ClosedXML.Excel
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@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 ragioniSociali = ws.RangeUsed().RowsUsed().Skip(1).Select(r => new { Rid = r.Cell(1).GetString(), RagioneSociale = r.Cell(4).GetString() }).Distinct().ToList();
|
||||
clientiTotali = ragioniSociali.Count;
|
||||
counterLabel = " di " + clientiTotali;
|
||||
|
||||
foreach (var cliente in ragioniSociali)
|
||||
{
|
||||
var righeCliente = usedRange.RowsUsed().Where(r => r.Cell(1).GetString() == cliente.Rid).ToList();
|
||||
|
||||
var clienteDb = await _managerService.ClienteService.RicercaPer(
|
||||
x => x.Rid == cliente.Rid && x.Eliminato == false,
|
||||
includi: x => x.Include(i => i.Destinazioni),
|
||||
solaLettura: false) ?? new Cliente();
|
||||
|
||||
clienteDb = await mapCliente(clienteDb, righeCliente);
|
||||
await _managerService.ClienteService.Salva(clienteDb, idClaim);
|
||||
counter += 1;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
ms.Close();
|
||||
_dialogService.Close();
|
||||
}
|
||||
|
||||
private async Task<Cliente> mapCliente(Cliente model, List<IXLRangeRow> rows)
|
||||
{
|
||||
var idClaim = await MembershipUtils.GetUserId(auth);
|
||||
var firstRow = rows.First();
|
||||
model.Rid = firstRow.Cell(1).GetString();
|
||||
model.RagioneSociale = firstRow.Cell(4).GetString();
|
||||
model.PartitaIva = firstRow.Cell(5).GetString();
|
||||
model.Destinazioni ??= new();
|
||||
foreach (var destinazioneRiga in rows)
|
||||
{
|
||||
var destinazione = model.Destinazioni.FirstOrDefault(x => x.Rid == destinazioneRiga.Cell(2).GetString()) ?? new Destinazione();
|
||||
destinazione = mapDestinazione(destinazione, model.Id, destinazioneRiga);
|
||||
if (destinazione.Id == Guid.Empty)
|
||||
model.Destinazioni.Add(destinazione);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
private Destinazione mapDestinazione(Destinazione model, Guid clienteId, IXLRangeRow? row)
|
||||
{
|
||||
model.Rid = row.Cell(2).GetString();
|
||||
model.RagioneSociale = row.Cell(7).GetString();
|
||||
model.PartitaIva = row.Cell(5).GetString();
|
||||
model.ClienteId = clienteId;
|
||||
model.Indirizzo = $"{row.Cell(8).GetString()} {row.Cell(9).GetString()}";
|
||||
model.Cap = row.Cell(15).GetString();
|
||||
model.Citta = row.Cell(11).GetString();
|
||||
model.Email = row.Cell(21).GetString();
|
||||
model.EmailInvito = row.Cell(21).GetString();
|
||||
model.NumeroTelefono = row.Cell(16).GetString();
|
||||
return model;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user