Files
gpw_next/GPW.CORE.UI/Pages/Planner.razor.cs
T
2022-01-26 14:38:18 +01:00

498 lines
13 KiB
C#

using GPW.CORE.Data;
using GPW.CORE.Data.DbModels;
using GPW.CORE.Data.DTO;
using GPW.CORE.UI.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace GPW.CORE.UI.Pages
{
/// <summary>
/// Gestione pagina principale del planner
///
/// modale: vedere qui: https://gist.github.com/conficient/ba98d1662c659e170ec16650acea05c8
/// </summary>
//[Authorize(Roles = "SuperAdmin, Admin")]
public partial class Planner : IDisposable
{
#region Private Fields
private int _endHour = 19;
private int _startHour = 8;
private RegAttivitaModel currRecord = null;
private List<AnagFasiModel> ListFasi = new List<AnagFasiModel>();
private List<DailyDataDTO> ListRecords = new List<DailyDataDTO>();
private List<TimbratureModel> ListTimbr = null;
private bool selPeriod = false;
private List<WeekStatDTO> weekStatList = new List<WeekStatDTO>();
private bool showTemp = false;
private DateTime DtRifTempRil = DateTime.Today;
#endregion Private Fields
#region Private Properties
private int _currPage { get; set; } = 1;
private int _numRecord { get; set; } = 10;
private int _selPlantId { get; set; } = 0;
private int _selSuppId { get; set; } = 0;
private int _selTraspId { get; set; } = 0;
private int currPage
{
get => _currPage;
set
{
if (_currPage != value)
{
_currPage = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private int endHour
{
get => _endHour;
set
{
// fix --> max 24...
_endHour = _endHour > 24 ? 24 : _endHour;
if (_endHour != value)
{
_endHour = value;
}
}
}
private bool isLoading { get; set; } = false;
private int numRecord
{
get => _numRecord;
set
{
if (_numRecord != value)
{
_numRecord = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private double periodoClonato
{
get
{
double answ = 8;
// se ho record clkonato --> uso quello
if (clonedRA != null)
{
answ = clonedRA.Durata.TotalHours;
}
else
{
answ = endHour - startHour;
}
return answ;
}
}
private int startHour
{
get => _startHour;
set
{
// fix --> MIN 0...
_startHour = _startHour < 0 ? 0 : _startHour;
if (_startHour != value)
{
_startHour = value;
}
}
}
#endregion Private Properties
#region Protected Properties
protected string actionCss
{
get => isLoading ? "disabled" : "";
}
[Inject]
protected MessageService AppMServ { get; set; }
protected int currWeekNum
{
get
{
return AppMServ.selWeekNum;
}
set
{
AppMServ.selWeekNum = value;
}
}
protected WeekData currWeekSel
{
get
{
return new WeekData(currYear, currWeekNum);
}
}
protected int currYear
{
get
{
return AppMServ.selYear;
}
set
{
AppMServ.selYear = value;
}
}
[Inject]
protected GpwDataService DataService { get; set; }
/// <summary>
/// Dipendente corrente
/// </summary>
protected int IdxDipendente
{
get
{
return AppMServ.IdxDipendente;
}
}
[Inject]
protected IJSRuntime JSRuntime { get; set; }
[Inject]
protected NavigationManager NavManager { get; set; }
protected DateTime TargetDate
{
get
{
return AppMServ.targetDate;
}
set
{
AppMServ.targetDate = value;
}
}
#endregion Protected Properties
#region Public Properties
public RegAttivitaModel clonedRA
{
get
{
return AppMServ.clonedRA;
}
set
{
AppMServ.clonedRA = value;
}
}
#endregion Public Properties
#region Private Methods
private void checkHourRange()
{
int minHour = 9;
int maxHour = 18;
if (ListRecords != null)
{
int roundMin = 15;
// per ogni giornata
foreach (var giornata in ListRecords)
{
var minRecTimb = giornata.ListTimbr
.Where(x => x.Entrata == true)
.OrderBy(x => x.DataOra)
.FirstOrDefault();
if (minRecTimb != null)
{
if (minHour > minRecTimb.DataOra.AddMinutes(-roundMin).Hour)
{
minHour = minRecTimb.DataOra.AddMinutes(-roundMin).Hour;
}
}
var minRecRa = giornata.ListRA
.OrderBy(x => x.Inizio)
.FirstOrDefault();
if (minRecRa != null)
{
if (minHour > minRecRa.Inizio.AddMinutes(-roundMin).Hour)
{
minHour = minRecRa.Inizio.AddMinutes(-roundMin).Hour;
}
}
var maxRecTimb = giornata.ListTimbr
.Where(x => x.Entrata == false)
.OrderByDescending(x => x.DataOra)
.FirstOrDefault();
if (maxRecTimb != null)
{
if (maxHour < maxRecTimb.DataOra.AddMinutes(roundMin).Hour + 1)
{
maxHour = maxRecTimb.DataOra.AddMinutes(roundMin).Hour + 1;
}
}
var maxRecRa = giornata.ListRA
.OrderByDescending(x => x.Fine)
.FirstOrDefault();
if (maxRecRa != null)
{
if (maxHour < maxRecRa.Fine.AddMinutes(roundMin).Hour + 1)
{
maxHour = maxRecRa.Fine.AddMinutes(roundMin).Hour + 1;
}
}
}
}
startHour = minHour;
endHour = maxHour;
}
/// <summary>
/// Reinit dei dati
/// </summary>
/// <param name="dateReset"></param>
/// <returns></returns>
private async Task InitData(bool dateReset)
{
isLoading = true;
if (dateReset)
{
TargetDate = DateTime.Today;
}
weekStatList = await DataService.LastWeeks(IdxDipendente, TargetDate, 4);
currWeekNum = weekStatList.Last().WeekNumber;
isLoading = false;
}
private async Task ReloadData()
{
isLoading = true;
ListRecords = await DataService.DailyDetails(IdxDipendente, currWeekSel.inizio, currWeekSel.fine);
await Task.Delay(100);
checkHourRange();
await Task.Delay(100);
lastRefresh = DateTime.Now;
isLoading = false;
}
#endregion Private Methods
#region Protected Methods
protected async Task MoveWeek(int numWeek)
{
isLoading = true;
TargetDate = TargetDate.AddDays(numWeek * 7);
await Task.Delay(100);
await InitData(false);
await Task.Delay(100);
await ReloadData();
}
protected override async Task OnInitializedAsync()
{
await InitData(true);
await ReloadData();
}
/// <summary>
/// Indico item selezionato
/// </summary>
protected async void PeriodoSelect(List<TimbratureModel> newList)
{
// salvo periodo selezionato
ListTimbr = newList;
}
/// <summary>
/// Indico item selezionato
/// </summary>
protected async void ShowTempRil(DateTime dataRif)
{
// salvo data
DtRifTempRil = dataRif;
showTemp = true;
}
/// <summary>
/// Timer per refresh dati
///
/// https://stackoverflow.com/questions/63060065/blazor-timer-call-async-api-task-to-update-ui
/// https://www.janduniec.blog/index.php/100/timers-in-blazor/
/// </summary>
protected System.Timers.Timer UITimer;
protected DateTime lastRefresh = DateTime.Now;
protected override void OnInitialized()
{
// avvio un timer infinito x refresh pagina COMUNQUE ogni 60 sec anche non succedesse nulla...
UITimer = new System.Timers.Timer
{
Interval = 1000 * 60,
AutoReset = true,
Enabled = true
};
UITimer.Elapsed += async (s, ea) => await TryReload();
}
public void Dispose()
{
UITimer?.Dispose();
}
/// <summary>
/// Gestione refresh condizionale
/// </summary>
/// <returns></returns>
protected async Task TryReload()
{
await Task.Delay(1);
// effettuo reload SOLO SE no ho editing in corso...
if (!modeEdit)
{
await ReloadData();
await InvokeAsync(StateHasChanged);
}
}
protected bool modeEdit
{
get => (currRecord != null || ListTimbr != null || showTemp);
//get => (selPeriod || showTemp || currRecord != null || weekStatList != null || ListTimbr != null);
}
protected async Task ReloadPeriodo()
{
selPeriod = false;
showTemp = false;
weekStatList = null;
currRecord = null;
ListTimbr = null;
await Task.Delay(50);
await InitData(false);
await Task.Delay(50);
await ReloadData();
}
/// <summary>
/// Indico item selezionato
/// </summary>
protected async void ReportSelect(RegAttivitaModel selRecord)
{
// recupero attività selezionata in curr record
currRecord = selRecord;
}
protected void ResetClone()
{
clonedRA = null;
}
protected async Task ResetData()
{
currRecord = null;
ListTimbr = null;
await InitData(true);
await ReloadData();
}
protected async Task ResetSelPeriodo()
{
selPeriod = false;
showTemp = false;
currRecord = null;
ListTimbr = null;
await InitData(true);
await ReloadData();
}
protected async Task ResetDayCheck()
{
selPeriod = false;
showTemp = false;
currRecord = null;
ListTimbr = null;
await InitData(true);
await ReloadData();
}
protected async Task ResetTimbData()
{
currRecord = null;
ListTimbr = null;
await ReloadData();
}
protected async Task ResetWeek()
{
isLoading = true;
TargetDate = DateTime.Today;
await Task.Delay(100);
await InitData(false);
await Task.Delay(100);
await ReloadData();
}
protected async Task setWeekNumber(WeekData wData)
{
isLoading = true;
// seleziono settimana!
currYear = wData.anno;
currWeekNum = wData.weekNumber;
// resetto currRecord...
currRecord = null;
await ReloadData();
}
protected async Task ShowSelPeriodo()
{
selPeriod = true;
}
protected async Task UpdRegAttData()
{
isLoading = true;
currRecord = null;
ListTimbr = null;
await Task.Delay(100);
await ReloadData();
}
protected async Task UpdTimbData()
{
isLoading = true;
currRecord = null;
await Task.Delay(100);
await ReloadData();
}
#endregion Protected Methods
}
}