完成保存修改后的变量,和离开视图时提示有变量没有保存
This commit is contained in:
@@ -86,6 +86,26 @@ public class VarDataRepository
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新VariableData
|
||||
/// </summary>
|
||||
/// <param name="variableData">VariableData实体</param>
|
||||
/// <returns></returns>
|
||||
public async Task<int> UpdateAsync(List<VariableData> variableDatas)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
using (var _db = DbContext.GetInstance())
|
||||
{
|
||||
var dbVarDatas = variableDatas.Select(vd=>vd.CopyTo<DbVariableData>());
|
||||
var result = await _db.Updateable(dbVarDatas.ToList()).ExecuteCommandAsync();
|
||||
|
||||
stopwatch.Stop();
|
||||
Logger.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID删除VariableData
|
||||
|
||||
@@ -22,7 +22,8 @@ public partial class VariableData : ObservableObject
|
||||
/// <summary>
|
||||
/// 数据转换规则或表达式。
|
||||
/// </summary>
|
||||
public string Converstion { get; set; } = String.Empty;
|
||||
[ObservableProperty]
|
||||
private string converstion = String.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 数据类型,例如Int、Float、String等。
|
||||
@@ -32,12 +33,14 @@ public partial class VariableData : ObservableObject
|
||||
/// <summary>
|
||||
/// 变量当前原始数据值。
|
||||
/// </summary>
|
||||
public string DataValue { get; set; } = String.Empty;
|
||||
[ObservableProperty]
|
||||
private string dataValue = String.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 变量描述。
|
||||
/// </summary>
|
||||
public string Description { get; set; } = String.Empty;
|
||||
[ObservableProperty]
|
||||
private string description = String.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 变量唯一标识符。
|
||||
@@ -47,7 +50,8 @@ public partial class VariableData : ObservableObject
|
||||
/// <summary>
|
||||
/// 变量经过转换或格式化后的显示值。
|
||||
/// </summary>
|
||||
public string DisplayValue { get; set; } = String.Empty;
|
||||
[ObservableProperty]
|
||||
private string displayValue = String.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 指示是否需要对变量进行报警监测。
|
||||
@@ -83,7 +87,8 @@ public partial class VariableData : ObservableObject
|
||||
/// <summary>
|
||||
/// 变量名称。
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
[ObservableProperty]
|
||||
private string name;
|
||||
|
||||
/// <summary>
|
||||
/// 节点ID,用于标识变量在设备或系统中的唯一路径。
|
||||
@@ -108,7 +113,8 @@ public partial class VariableData : ObservableObject
|
||||
/// <summary>
|
||||
/// 变量数据最后更新时间。
|
||||
/// </summary>
|
||||
public DateTime UpdateTime { get; set; } = DateTime.Now;
|
||||
[ObservableProperty]
|
||||
private DateTime updateTime = DateTime.Now;
|
||||
|
||||
/// <summary>
|
||||
/// 最后更新变量数据的用户。
|
||||
|
||||
@@ -13,7 +13,8 @@ public partial class NavgatorServices : ObservableRecipient, IRecipient<Navgator
|
||||
{
|
||||
private readonly ILogger<NavgatorServices> _logger;
|
||||
|
||||
[ObservableProperty] private ViewModelBase currentViewModel;
|
||||
// [ObservableProperty]
|
||||
private ViewModelBase currentViewModel;
|
||||
|
||||
public NavgatorServices(ILogger<NavgatorServices> logger)
|
||||
{
|
||||
@@ -21,35 +22,44 @@ public partial class NavgatorServices : ObservableRecipient, IRecipient<Navgator
|
||||
IsActive = true;
|
||||
}
|
||||
|
||||
partial void OnCurrentViewModelChanging(ViewModelBase viewModel)
|
||||
{
|
||||
viewModel?.OnLoading();
|
||||
}
|
||||
|
||||
partial void OnCurrentViewModelChanged(ViewModelBase viewModel)
|
||||
{
|
||||
OnViewModelChanged?.Invoke();
|
||||
viewModel?.OnLoaded();
|
||||
}
|
||||
|
||||
// public ViewModelBase CurrentViewModel
|
||||
// partial void OnCurrentViewModelChanging(ViewModelBase viewModel)
|
||||
// {
|
||||
// get => 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)
|
||||
{
|
||||
|
||||
@@ -63,6 +63,52 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
|
||||
IsLoadCompletion = true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 退出当前实体时调用
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override async Task<bool> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存修改过的变量数据
|
||||
/// </summary>
|
||||
[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)
|
||||
{
|
||||
|
||||
@@ -13,6 +13,11 @@ public abstract class ViewModelBase : ObservableObject
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public virtual async Task<bool> OnExitAsync()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -20,10 +20,11 @@
|
||||
|
||||
</ui:ContentDialog.Resources>
|
||||
|
||||
<Grid Width="480"
|
||||
<Grid Width="360"
|
||||
Margin="10">
|
||||
<TextBlock Margin="20"
|
||||
FontSize="18"
|
||||
FontSize="14"
|
||||
TextWrapping="Wrap"
|
||||
FontWeight="Bold"
|
||||
Text="{Binding Message}">
|
||||
</TextBlock>
|
||||
|
||||
@@ -61,6 +61,7 @@
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
|
||||
</ui:AppBarButton.Icon>
|
||||
</ui:AppBarButton>
|
||||
|
||||
<ui:AppBarButton Command="{Binding EditVarDataCommand}"
|
||||
CommandParameter="{Binding VariableTable}"
|
||||
Label="编辑变量">
|
||||
@@ -68,6 +69,13 @@
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
|
||||
</ui:AppBarButton.Icon>
|
||||
|
||||
</ui:AppBarButton>
|
||||
<ui:AppBarButton Command="{Binding SaveModifiedVarDataCommand}"
|
||||
Label="保存变量">
|
||||
<ui:AppBarButton.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Save}" />
|
||||
</ui:AppBarButton.Icon>
|
||||
|
||||
</ui:AppBarButton>
|
||||
<ui:AppBarButton x:Name="ShareButton"
|
||||
Label="Share">
|
||||
|
||||
Reference in New Issue
Block a user