diff --git a/Data/Repositories/VarDataRepository.cs b/Data/Repositories/VarDataRepository.cs index 17b6074..75ad395 100644 --- a/Data/Repositories/VarDataRepository.cs +++ b/Data/Repositories/VarDataRepository.cs @@ -86,6 +86,26 @@ public class VarDataRepository return result; } } + + /// + /// 更新VariableData + /// + /// VariableData实体 + /// + public async Task UpdateAsync(List variableDatas) + { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + using (var _db = DbContext.GetInstance()) + { + var dbVarDatas = variableDatas.Select(vd=>vd.CopyTo()); + var result = await _db.Updateable(dbVarDatas.ToList()).ExecuteCommandAsync(); + + stopwatch.Stop(); + Logger.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; + } + } /// /// 根据ID删除VariableData diff --git a/Models/VariableData.cs b/Models/VariableData.cs index e4b6fe6..ca981c0 100644 --- a/Models/VariableData.cs +++ b/Models/VariableData.cs @@ -22,7 +22,8 @@ public partial class VariableData : ObservableObject /// /// 数据转换规则或表达式。 /// - public string Converstion { get; set; } = String.Empty; + [ObservableProperty] + private string converstion = String.Empty; /// /// 数据类型,例如Int、Float、String等。 @@ -32,12 +33,14 @@ public partial class VariableData : ObservableObject /// /// 变量当前原始数据值。 /// - public string DataValue { get; set; } = String.Empty; + [ObservableProperty] + private string dataValue = String.Empty; /// /// 变量描述。 /// - public string Description { get; set; } = String.Empty; + [ObservableProperty] + private string description = String.Empty; /// /// 变量唯一标识符。 @@ -47,7 +50,8 @@ public partial class VariableData : ObservableObject /// /// 变量经过转换或格式化后的显示值。 /// - public string DisplayValue { get; set; } = String.Empty; + [ObservableProperty] + private string displayValue = String.Empty; /// /// 指示是否需要对变量进行报警监测。 @@ -83,7 +87,8 @@ public partial class VariableData : ObservableObject /// /// 变量名称。 /// - public string Name { get; set; } + [ObservableProperty] + private string name; /// /// 节点ID,用于标识变量在设备或系统中的唯一路径。 @@ -108,7 +113,8 @@ public partial class VariableData : ObservableObject /// /// 变量数据最后更新时间。 /// - public DateTime UpdateTime { get; set; } = DateTime.Now; + [ObservableProperty] + private DateTime updateTime = DateTime.Now; /// /// 最后更新变量数据的用户。 diff --git a/Services/NavgatorServices.cs b/Services/NavgatorServices.cs index b53bedf..b43072f 100644 --- a/Services/NavgatorServices.cs +++ b/Services/NavgatorServices.cs @@ -13,7 +13,8 @@ public partial class NavgatorServices : ObservableRecipient, IRecipient _logger; - [ObservableProperty] private ViewModelBase currentViewModel; + // [ObservableProperty] + private ViewModelBase currentViewModel; public NavgatorServices(ILogger logger) { @@ -21,35 +22,44 @@ public partial class NavgatorServices : ObservableRecipient, IRecipient currentViewModel; - // set - // { - // value?.OnLoading(); - // currentViewModel = value; - // OnViewModelChanged?.Invoke(); - // currentViewModel?.OnLoaded(); - // } + // viewModel?.OnLoading(); + // } + // + // partial void OnCurrentViewModelChanged(ViewModelBase viewModel) + // { + // OnViewModelChanged?.Invoke(); + // viewModel?.OnLoaded(); // } + public ViewModelBase CurrentViewModel + { + get => currentViewModel; + set { currentViewModel = value; } + } - public void Receive(NavgatorMessage message) + + public async void Receive(NavgatorMessage message) { try { + ViewModelBase nextViewModel = message.Value; + //如果OnExit返回False终止跳转 + + if (currentViewModel != null) + { + var isExit = await currentViewModel.OnExitAsync(); + if (!isExit) + { + return; + } + } + + nextViewModel?.OnLoading(); CurrentViewModel = message.Value; + OnViewModelChanged?.Invoke(); + currentViewModel?.OnLoaded(); } catch (Exception e) { diff --git a/ViewModels/VariableTableViewModel.cs b/ViewModels/VariableTableViewModel.cs index 896cc34..0456caf 100644 --- a/ViewModels/VariableTableViewModel.cs +++ b/ViewModels/VariableTableViewModel.cs @@ -63,6 +63,52 @@ partial class VariableTableViewModel : ViewModelBase IsLoadCompletion = true; } + /// + /// 退出当前实体时调用 + /// + /// + public override async Task OnExitAsync() + { + var modifiedDatas = DataVariables.Where(d => d.IsModified == true) + .ToList(); + if (modifiedDatas.Count == 0) + return true; + var isExit = await _dialogService.ShowConfrimeDialog( + "数据未保存", $"你有{modifiedDatas.Count}个修改的变量没有保存,离开后这些数据就可能丢失了确认要离开吗?", "离开"); + if (!isExit) + { + // 不保存数据,还原原来的数据 + foreach (var modifiedData in modifiedDatas) + { + var oldData = _originalDataVariables.First(od=>od.Id ==modifiedData.Id); + oldData.CopyTo( modifiedData); + modifiedData.IsModified = false; + } + + return false; + + } + + return true; + } + + /// + /// 保存修改过的变量数据 + /// + [RelayCommand] + private async void SaveModifiedVarData() + { + var modifiedDatas = DataVariables.Where(d => d.IsModified == true) + .ToList(); + ///更新数据库 + await _varDataRepository.UpdateAsync(modifiedDatas); + // 还原修改状态 + foreach (var modifiedData in modifiedDatas) + { + modifiedData.IsModified = false; + } + NotificationHelper.ShowMessage($"修改的{modifiedDatas.Count}变量保存成功.", NotificationType.Success); + } [RelayCommand] public async void EditVarData(VariableTable variableTable) @@ -74,7 +120,6 @@ partial class VariableTableViewModel : ViewModelBase // // 如果用户取消或对话框未返回设备,则直接返回 if (varData == null) return; - varData.VariableTableId = variableTable.Id; // 更新数据库 await _varDataRepository.UpdateAsync(varData); @@ -106,12 +151,14 @@ partial class VariableTableViewModel : ViewModelBase return; varData.VariableTableId = variableTable.Id; - var addVarData = await _varDataRepository.AddAsync(varData); - DataVariables?.Add(addVarData); - variableTable.DataVariables?.Add(addVarData); - var msg = addVarData.Id > 0 ? $"添加变量成功:{varData?.Name}" : $"添加变量成功:{varData.Name}"; - var type = addVarData.Id > 0 ? NotificationType.Success : NotificationType.Error; - NotificationHelper.ShowMessage(msg, type); + // 更新数据库 + await _varDataRepository.UpdateAsync(varData); + // 更新当前页面的 + var index = variableTable.DataVariables.IndexOf(SelectedVariableData); + // 更新变量表中的 + if (index >= 0 && index < variableTable.DataVariables.Count) + variableTable.DataVariables[index] = varData; + NotificationHelper.ShowMessage($"添加变量成功:{varData?.Name}", NotificationType.Success); } catch (Exception e) { diff --git a/ViewModels/ViewModelBase.cs b/ViewModels/ViewModelBase.cs index e265141..e98e48a 100644 --- a/ViewModels/ViewModelBase.cs +++ b/ViewModels/ViewModelBase.cs @@ -13,6 +13,11 @@ public abstract class ViewModelBase : ObservableObject { } + + public virtual async Task OnExitAsync() + { + return true; + } diff --git a/Views/Dialogs/ConfirmDialog.xaml b/Views/Dialogs/ConfirmDialog.xaml index ec6a0ea..c10ecf0 100644 --- a/Views/Dialogs/ConfirmDialog.xaml +++ b/Views/Dialogs/ConfirmDialog.xaml @@ -20,10 +20,11 @@ - diff --git a/Views/VariableTableView.xaml b/Views/VariableTableView.xaml index ac5e0af..268dc6d 100644 --- a/Views/VariableTableView.xaml +++ b/Views/VariableTableView.xaml @@ -61,6 +61,7 @@ + @@ -68,6 +69,13 @@ + + + + + +