using GPW.CORE.Data; using GPW.CORE.Data.DbModels; using GPW.CORE.Data.DTO; using GPW.CORE.Data.Services; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; namespace GPW.CORE.WRKLOG.Components.Pages { /// /// Gestione pagina principale del planner /// /// modale: vedere qui: https://gist.github.com/conficient/ba98d1662c659e170ec16650acea05c8 /// public partial class WeekPlan : IDisposable { #region Public Properties public RegAttivitaModel? clonedRA { get { return AppMServ.clonedRA; } set { AppMServ.clonedRA = value; } } #endregion Public Properties #region Public Methods public void Dispose() { GDataServ.mPipeTimb.EA_NewMessage -= MPipeTimb_EA_NewMessage; GDataServ.mPipeRich.EA_NewMessage -= MPipeRich_EA_NewMessage; UITimer?.Dispose(); } #endregion Public Methods #region Protected Fields protected DateTime lastRefresh = DateTime.Now; /// /// 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/ /// protected System.Timers.Timer UITimer = new System.Timers.Timer(); #endregion Protected Fields #region Protected Properties protected string actionCss { get => isLoading ? "disabled" : ""; } [Inject] protected MessageService AppMServ { get; set; } = null!; 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 GDataServ { get; set; } = null!; /// /// Dipendente corrente /// protected int IdxDipendente { get { return AppMServ.IdxDipendente; } } [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; protected bool modeEdit { get => (currRecord != null || ListTimbr != null || showTemp); } [Inject] protected NavigationManager NavManager { get; set; } = null!; protected DateTime TargetDate { get { return AppMServ.targetDate; } set { AppMServ.targetDate = value; } } #endregion Protected Properties #region Protected Methods protected async Task MoveWeek(int numWeek) { isLoading = true; TargetDate = TargetDate.AddDays(numWeek * 7); await Task.Delay(1); await InitData(false); await Task.Delay(1); await ReloadData(); } protected override async Task OnParametersSetAsync() { weekStatList = null; ListRecords = null; await InvokeAsync(StateHasChanged); await Task.Delay(1); await InitData(true); await TryReload(); // 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(); // aggiungo gestione notifica messaggi timbrature... GDataServ.mPipeTimb.EA_NewMessage += MPipeTimb_EA_NewMessage; GDataServ.mPipeRich.EA_NewMessage += MPipeRich_EA_NewMessage; } /// /// Indico item selezionato /// protected void PeriodoSelect(DailyDataDTO dayData) { // salvo periodo selezionato ListTimbr = dayData.ListTimbr; SelDayDTO = dayData; } protected async Task ReloadPeriodo() { selPeriod = false; showTemp = false; showDay = false; weekStatList = null; currRecord = null; ListTimbr = null; await Task.Delay(1); await InitData(false); await Task.Delay(1); await ReloadData(); } /// /// Indico item selezionato /// protected void ReportSelect(RegAttivitaModel selRecord) { selPeriod = false; showTemp = false; showDay = false; // recupero attivit� selezionata in curr record currRecord = selRecord; } protected void ResetClone() { clonedRA = null; } protected async Task ResetData() { currRecord = null; ListTimbr = null; SelDayDTO = null; await InitData(true); await ReloadData(); } protected async Task ResetDayAgenda() { selPeriod = false; showTemp = false; showDay = false; currRecord = null; ListTimbr = null; SelDayDTO = null; await InitData(true); await ReloadData(); } protected async Task ResetDayCheck() { selPeriod = false; showTemp = false; showDay = false; currRecord = null; ListTimbr = null; SelDayDTO = null; await InitData(true); await ReloadData(); } protected async Task ResetSelPeriodo() { selPeriod = false; showTemp = false; showDay = false; currRecord = null; ListTimbr = null; SelDayDTO = null; await InitData(true); await ReloadData(); } protected async Task ResetTimbData() { currRecord = null; ListTimbr = null; SelDayDTO = null; await ReloadData(); } protected async Task ResetWeek() { isLoading = true; TargetDate = DateTime.Today; await Task.Delay(1); await InitData(false); await Task.Delay(1); await ReloadData(); } protected async Task setWeekNumber(WeekData wData) { isLoading = true; // seleziono settimana! currYear = wData.anno; currWeekNum = wData.weekNumber; // resetto CurrRecord... currRecord = null; await ReloadData(); } /// /// Mostro in forma "verticale" l'elenco attività quotidiane /// protected void ShowDayAgenda(DailyDataDTO DayDTO) { // salvo data SelDayDTO = DayDTO; showDay = true; } protected void ShowSelPeriodo() { selPeriod = true; } /// /// Indico item selezionato /// protected void ShowTempRil(DateTime dataRif) { // salvo data DtSelected = dataRif; showTemp = true; } /// /// Gestione refresh condizionale /// /// 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 async Task UpdRegAttData() { isLoading = true; currRecord = null; ListTimbr = null; await ReloadPeriodo(); await Task.Delay(1); } protected async Task UpdTimbData() { isLoading = true; currRecord = null; await Task.Delay(1); await ReloadData(); } #endregion Protected Methods #region Private Fields private int _endHour = 18; private int _startHour = 8; private RegAttivitaModel? currRecord = null; private DateTime DtSelected = DateTime.Today; private List ListFasi = new List(); private List? ListRecords = null; private List? ListTimbr = null; private DailyDataDTO? SelDayDTO = null; private bool selPeriod = false; private bool showDay = false; private bool showTemp = false; private List? weekStatList = null; #endregion Private Fields #region Private Properties private int endHour { get => _endHour; set { // fix --> max 24... _endHour = value < 25 ? value : 24; } } private bool isLoading { get; set; } = false; private double periodoClonato { get { double answ = 8; // se ho record clonato --> 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 = value > 0 ? value : 0; } } #endregion Private Properties #region Private Methods private void checkHourRange() { int minHour = 12; int maxHour = 14; int currStart = 12; int currEnd = 12; int roundMin = 30; //int extraEnd = 30; if (ListRecords != null) { // per ogni giornata foreach (var giornata in ListRecords) { if (giornata.ListTimbr != null) { var minRecTimb = giornata.ListTimbr .Where(x => x.Entrata == true) .OrderBy(x => x.DataOra) .FirstOrDefault(); if (minRecTimb != null) { if (minHour > minRecTimb.DataOra.AddMinutes(roundMin / 3).Hour) { minHour = minRecTimb.DataOra.AddMinutes(roundMin / 3).Hour; } } var maxRecTimb = giornata.ListTimbr .Where(x => x.Entrata == false) .OrderByDescending(x => x.DataOra) .FirstOrDefault(); if (maxRecTimb != null) { if (maxHour < maxRecTimb.DataOra.AddMinutes(-roundMin / 3).Hour) { maxHour = maxRecTimb.DataOra.AddMinutes(-roundMin / 3).Hour + 1; } } } if (giornata.ListRA != null) { var minRecRa = giornata.ListRA .OrderBy(x => x.Inizio) .FirstOrDefault(); if (minRecRa != null) { // verifico se cambia giorno inizio/fine --> seleziono la mezzanotte currStart = minRecRa.Inizio.AddMinutes(-roundMin).Date.Equals(minRecRa.Fine.Date) ? minRecRa.Inizio.AddMinutes(-roundMin).Hour : 0; if (minHour > currStart) { minHour = currStart; } } var maxRecRa = giornata.ListRA .OrderByDescending(x => x.Fine) .FirstOrDefault(); if (maxRecRa != null) { // verifico se cambia giorno inizio/fine --> seleziono la mezzanotte currEnd = maxRecRa.Inizio.Date.Equals(maxRecRa.Fine.AddMinutes(roundMin).Date) ? maxRecRa.Fine.AddMinutes(roundMin).Hour + 1 : 24; if (maxHour < currEnd) { maxHour = currEnd; } } } } } startHour = minHour;// - 1; checkOrarioDip(); // minimo 6 h... x cui "allungo" verso fine if (maxHour - minHour < AppMServ.orarioDip) { minHour--; maxHour = minHour + AppMServ.orarioDip; } endHour = maxHour;// + 1; } private void checkOrarioDip() { // calcolo minimo da cod orario dipendente if (AppMServ.orarioDip == 0) { AnagOrariModel? schemaOrario = null; // recupero da DB schema settimanale... var pUpd = Task.Run(async () => { schemaOrario = await GDataServ.AnagOrarioByDip(AppMServ.IdxDipendente); }); pUpd.Wait(); if (schemaOrario != null) { AppMServ.orarioDip = (int)(Math.Ceiling(schemaOrario.oreOrdSett / 5)); } } } private async Task ForceReload() { await GDataServ.FlushRedisCache(); await InitData(false); await TryReload(); } /// /// Reinit dei dati /// /// /// private async Task InitData(bool dateReset) { isLoading = true; if (dateReset) { TargetDate = DateTime.Today; } weekStatList = await GDataServ.LastWeeks(IdxDipendente, TargetDate, 3); if (dateReset) { currWeekNum = weekStatList.Last().WeekNumber; } currYear = weekStatList.Last().Anno; isLoading = false; } private async void MPipeRich_EA_NewMessage(object? sender, EventArgs e) { await ForceReload(); } private async void MPipeTimb_EA_NewMessage(object? sender, EventArgs e) { await ForceReload(); } private async Task ReloadData() { isLoading = true; UITimer.Stop(); ListRecords = await GDataServ.DailyDetails(IdxDipendente, currWeekSel.inizio, currWeekSel.fine); await Task.Delay(1); checkHourRange(); await Task.Delay(1); lastRefresh = DateTime.Now; UITimer.Start(); isLoading = false; } #endregion Private Methods } }