Files
magman/MagMan.UI/Components/CustomerMan.razor.cs
T
2024-02-02 11:31:24 +01:00

305 lines
9.7 KiB
C#

// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this
// file to you under the MIT license.
using EgwCoreLib.Razor;
using k8s.Models;
using MagMan.Data.Admin.DbModels;
using MagMan.Data.Admin.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
namespace MagMan.UI.Components
{
public partial class CustomerMan
{
#region Protected Properties
[Inject]
protected IConfiguration Configuration { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected MTAdminService MTService { get; set; } = null!;
protected int totalCount { get; set; } = 0;
#endregion Protected Properties
#region Protected Methods
protected string CheckSel(CustomerModel curItem)
{
string answ = "";
if (CurrItem != null)
{
answ = curItem.CustomerID == CurrItem.CustomerID ? "table-info" : "";
}
return answ;
}
protected async Task CreateDb(CustomerModel currRec)
{
await Task.Delay(1);
bool dbOk = Data.Admin.DbConfig.CheckCustDb(currRec.MainKey);
if (!dbOk)
{
dbOk = Data.Admin.DbConfig.CheckCustDb(currRec.MainKey);
}
// se ok --> migration...
if (dbOk)
{
string dbServerAddr = Configuration["DbConfig:Server"];
//Data.Tenant.DbConfig.InitDb(dbServerAddr, currRec.MainKey);
string connStr = Data.Tenant.DbConfig.CustomerConnString(dbServerAddr, currRec.MainKey);
// verifico se serve applicazione migrazioni
Data.Tenant.DbConfig.ExecMigrationMain(connStr);
}
// aggiorno comunque status DB...
currRec.HasDb = dbOk;
await MTService.CustomerUpdate(currRec);
await ReloadData();
}
protected async Task CreateNew()
{
CurrItemEdit = new CustomerModel()
{
Name = "Nuovo Cliente",
Note = "...",
DtActivation = DateTime.Today,
IsActive = true
};
await InvokeAsync(StateHasChanged);
}
/// <summary>
/// Elimina record cliente
/// </summary>
/// <param name="selItem"></param>
/// <returns></returns>
protected async Task DeleteRecord(CustomerModel selItem)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler eliminare il record?"))
return;
await MTService.CustomerDelete(selItem);
await ReloadData();
}
protected async Task DoEdit(CustomerModel? selItem)
{
CurrItem = selItem;
CurrItemEdit = selItem;
int custId = selItem != null ? selItem.CustomerID : 0;
await Task.Delay(1);
}
protected async Task DoSelect(CustomerModel? selItem)
{
CurrItem = selItem;
int custId = selItem != null ? selItem.CustomerID : 0;
await Task.Delay(1);
}
protected async Task ForceReload(bool force)
{
CurrItem = null;
await ReloadData();
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
}
/// <summary>
/// Rigenera Token Rest del cliente
/// </summary>
/// <param name="selItem"></param>
/// <returns></returns>
protected async Task RegenToken(CustomerModel selItem)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler rigenerare il token di comunicazione? gli applicativi già configurati smetteranno di comunicare fino ad aggiornamento token"))
return;
selItem.RestToken = Guid.NewGuid().ToString();
await MTService.CustomerUpdate(selItem);
await ReloadData();
}
protected async Task ResetSel()
{
CurrItem = null;
CurrItemEdit = null;
await Task.Delay(1);
}
protected void SetNumRec(int newNum)
{
numRecord = newNum;
currPage = 1;
}
protected void SetPage(int newNum)
{
currPage = newNum;
}
protected async Task SortRequested(Sorter.SortCallBack e)
{
sortField = e.ParamName;
sortAsc = e.IsAscending;
await ReloadData();
}
#endregion Protected Methods
#region Private Fields
private CustomerModel? CurrItem = null;
private CustomerModel? CurrItemEdit = null;
private List<CustomerModel>? ListRecords = null;
private List<CustomerModel>? SearchRecords = null;
private bool sortAsc = true;
private string sortField = "";
#endregion Private Fields
#region Private Properties
private int currPage { get; set; } = 1;
private bool isLoading { get; set; } = false;
private int numRecord { get; set; } = 10;
#endregion Private Properties
#region Private Methods
private async Task ReloadData()
{
isLoading = true;
ListRecords = null;
SearchRecords = await MTService.CustomerGetAll();
totalCount = SearchRecords.Count;
SortTable();
isLoading = false;
}
private void SortTable()
{
if (SearchRecords != null)
{
// se ho ordinamento riordino...
if (!string.IsNullOrEmpty(sortField))
{
switch (sortField)
{
case "CustomerID":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.CustomerID).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.CustomerID).ToList();
}
break;
case "Note":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.Note).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.Note).ToList();
}
break;
case "DtActivation":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.DtActivation).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.DtActivation).ToList();
}
break;
case "Name":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.Name).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.Name).ToList();
}
break;
case "RestToken":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.RestToken).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.RestToken).ToList();
}
break;
case "MainKey":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.MainKey).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.MainKey).ToList();
}
break;
case "HasDb":
if (sortAsc)
{
SearchRecords = SearchRecords.OrderBy(x => x.HasDb).ToList();
}
else
{
SearchRecords = SearchRecords.OrderByDescending(x => x.HasDb).ToList();
}
break;
default:
break;
}
}
// filtro x display
ListRecords = SearchRecords
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
}
else
{
ListRecords = new List<CustomerModel>();
}
}
private string textCss(bool isActive)
{
return isActive ? "text-dark" : "text-secondary text-decoration-line-through";
}
#endregion Private Methods
}
}