Files
mapo-core/MP-TAB-SERV/Components/NotesEditor.razor.cs
T
2023-12-04 15:21:05 +01:00

261 lines
6.8 KiB
C#

using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
using System.Net.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using MP_TAB_SERV;
using MP_TAB_SERV.Shared;
using MP_TAB_SERV.Components;
using MP.Data;
using MP.Data.DatabaseModels;
using MP.Data.DTO;
using MP.Data.Services;
using Newtonsoft.Json;
using NLog;
namespace MP_TAB_SERV.Components
{
public partial class NotesEditor
{
#region Public Properties
[Parameter]
public bool CanSave { get; set; } = false;
[Parameter]
public EventCallback<bool> E_relData { get; set; }
[Parameter]
public CommentiModel? CurrComm
{
set
{
if (value != null)
{
DateSel = value.InizioStato;
UserComment = value.Value;
ShowBtn = false;
}
}
}
[Parameter]
public EventCallback<string> E_CommRec { get; set; }
[Parameter]
public EventCallback<DateTime> E_DateSel { get; set; }
[Parameter]
public EventCallback<bool> E_Updated { get; set; }
[Parameter]
public MappaStatoExpl? RecMSE { get; set; } = null;
[Parameter]
public string Title { get; set; } = "NA";
[Parameter]
public EventListModel? CurrRecord
{
set
{
if (value != null)
{
DateSel = value.InizioStato ?? DateTime.Now;
UserComment = value.Value;
ShowBtn = false;
}
}
}
#endregion Public Properties
#region Protected Properties
protected DateTime DateSel
{
get => dateSel.Round(TimeSpan.FromSeconds(1));
set
{
if (dateSel != value)
{
dateSel = value;
E_DateSel.InvokeAsync(value).ConfigureAwait(false);
}
}
}
[Inject]
protected MessageService MServ { get; set; } = null!;
[Inject]
protected SharedMemService SMServ { get; set; } = null!;
[Inject]
protected TabDataService TabServ { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected async Task doCancel()
{
await E_relData.InvokeAsync(true);
DoReset();
}
/// <summary>
/// Forza salvataggio impostando data-ora
/// </summary>
/// <param name="dtRif"></param>
/// <returns></returns>
public async Task ForceSave(DateTime dtRif)
{
DateSel = dtRif;
// ora salvo
await doSave();
}
protected async Task doSave()
{
// registro evento
EventListModel newRec = new EventListModel()
{
IdxMacchina = IdxMacc,
InizioStato = DateSel,
IdxTipo = idxTipoCommento,
CodArticolo = CodArt,
Value = UserComment,
MatrOpr = MatrOpr,
pallet = "-"
};
// elimino vecchio se c'è...
await TabServ.EvListDelete(IdxMacc, DateSel, idxTipoCommento);
// inserisco
await TabServ.EvListInsert(newRec, MP.Data.Objects.Enums.tipoInputEvento.commento);
// reset
await E_relData.InvokeAsync(true);
DoReset();
//StateHasChanged();
//ToggleCtrl();
}
protected override void OnInitialized()
{
idxTipoCommento = SMServ.GetConfInt("idxTipoCommento");
dltMinRealtime = SMServ.GetConfInt("dltMinRealtime");
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// verifico SE ci sia una data da usare...
bool hasDtRif = await MServ.SessHasVal(MessageService.KeyCommDtRif);
if (hasDtRif)
{
await Task.Delay(1);
ShowBtn = false;
UserComment = await MServ.CommentoValGet(true);
DateSel = await MServ.CommentoDtRifGet(true);
await InvokeAsync(StateHasChanged);
}
}
protected int dltMinRealtime = 1;
/// <summary>
/// Determina se insert sia Realtime o batch con DataOra (in base a diff tra DataOra
/// selezionata e realtime, se superiore ad X minuti NON realtime)
/// </summary>
protected bool insRealtime
{
get
{
bool answ = true;
try
{
if (Math.Abs(DateSel.Subtract(DateTime.Now).TotalMinutes) > dltMinRealtime)
{
answ = false;
}
}
catch
{ }
return answ;
}
}
protected void ToggleCtrl()
{
ShowBtn = !ShowBtn;
if (ShowBtn)
{
DateSel = DateTime.Now;
}
}
#endregion Protected Methods
#region Private Fields
private int idxTipoCommento = 0;
#endregion Private Fields
#region Private Properties
private string CodArt
{
get => RecMSE != null ? RecMSE.CodArticolo : "";
}
private DateTime dateSel { get; set; } = DateTime.Now;
private string IdxMacc
{
get => RecMSE != null ? RecMSE.IdxMacchina : "";
}
private int MatrOpr
{
get => MServ.MatrOpr;
}
private bool ShowBtn { get; set; } = true;
private string userComment { get; set; } = "";
private string UserComment
{
get => userComment.Trim();
set
{
if (userComment != value)
{
userComment = value;
E_CommRec.InvokeAsync(value).ConfigureAwait(false);
}
}
}
#endregion Private Properties
#region Private Methods
private void DoReset()
{
UserComment = "";
DateSel = DateTime.Now;
//CurrComm = null;
ShowBtn = true;
//StateHasChanged();
}
#endregion Private Methods
}
}