完成保存修改后的变量,和离开视图时提示有变量没有保存
This commit is contained in:
@@ -87,6 +87,26 @@ public class VarDataRepository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
/// <summary>
|
||||||
/// 根据ID删除VariableData
|
/// 根据ID删除VariableData
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ public partial class VariableData : ObservableObject
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 数据转换规则或表达式。
|
/// 数据转换规则或表达式。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Converstion { get; set; } = String.Empty;
|
[ObservableProperty]
|
||||||
|
private string converstion = String.Empty;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 数据类型,例如Int、Float、String等。
|
/// 数据类型,例如Int、Float、String等。
|
||||||
@@ -32,12 +33,14 @@ public partial class VariableData : ObservableObject
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量当前原始数据值。
|
/// 变量当前原始数据值。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string DataValue { get; set; } = String.Empty;
|
[ObservableProperty]
|
||||||
|
private string dataValue = String.Empty;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量描述。
|
/// 变量描述。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Description { get; set; } = String.Empty;
|
[ObservableProperty]
|
||||||
|
private string description = String.Empty;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量唯一标识符。
|
/// 变量唯一标识符。
|
||||||
@@ -47,7 +50,8 @@ public partial class VariableData : ObservableObject
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量经过转换或格式化后的显示值。
|
/// 变量经过转换或格式化后的显示值。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string DisplayValue { get; set; } = String.Empty;
|
[ObservableProperty]
|
||||||
|
private string displayValue = String.Empty;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 指示是否需要对变量进行报警监测。
|
/// 指示是否需要对变量进行报警监测。
|
||||||
@@ -83,7 +87,8 @@ public partial class VariableData : ObservableObject
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量名称。
|
/// 变量名称。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Name { get; set; }
|
[ObservableProperty]
|
||||||
|
private string name;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 节点ID,用于标识变量在设备或系统中的唯一路径。
|
/// 节点ID,用于标识变量在设备或系统中的唯一路径。
|
||||||
@@ -108,7 +113,8 @@ public partial class VariableData : ObservableObject
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量数据最后更新时间。
|
/// 变量数据最后更新时间。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime UpdateTime { get; set; } = DateTime.Now;
|
[ObservableProperty]
|
||||||
|
private DateTime updateTime = DateTime.Now;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 最后更新变量数据的用户。
|
/// 最后更新变量数据的用户。
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ public partial class NavgatorServices : ObservableRecipient, IRecipient<Navgator
|
|||||||
{
|
{
|
||||||
private readonly ILogger<NavgatorServices> _logger;
|
private readonly ILogger<NavgatorServices> _logger;
|
||||||
|
|
||||||
[ObservableProperty] private ViewModelBase currentViewModel;
|
// [ObservableProperty]
|
||||||
|
private ViewModelBase currentViewModel;
|
||||||
|
|
||||||
public NavgatorServices(ILogger<NavgatorServices> logger)
|
public NavgatorServices(ILogger<NavgatorServices> logger)
|
||||||
{
|
{
|
||||||
@@ -21,35 +22,44 @@ public partial class NavgatorServices : ObservableRecipient, IRecipient<Navgator
|
|||||||
IsActive = true;
|
IsActive = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
partial void OnCurrentViewModelChanging(ViewModelBase viewModel)
|
// partial void OnCurrentViewModelChanging(ViewModelBase viewModel)
|
||||||
{
|
|
||||||
viewModel?.OnLoading();
|
|
||||||
}
|
|
||||||
|
|
||||||
partial void OnCurrentViewModelChanged(ViewModelBase viewModel)
|
|
||||||
{
|
|
||||||
OnViewModelChanged?.Invoke();
|
|
||||||
viewModel?.OnLoaded();
|
|
||||||
}
|
|
||||||
|
|
||||||
// public ViewModelBase CurrentViewModel
|
|
||||||
// {
|
// {
|
||||||
// get => currentViewModel;
|
// viewModel?.OnLoading();
|
||||||
// set
|
// }
|
||||||
// {
|
//
|
||||||
// value?.OnLoading();
|
// partial void OnCurrentViewModelChanged(ViewModelBase viewModel)
|
||||||
// currentViewModel = value;
|
// {
|
||||||
// OnViewModelChanged?.Invoke();
|
// OnViewModelChanged?.Invoke();
|
||||||
// currentViewModel?.OnLoaded();
|
// viewModel?.OnLoaded();
|
||||||
// }
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
public ViewModelBase CurrentViewModel
|
||||||
|
{
|
||||||
|
get => currentViewModel;
|
||||||
|
set { currentViewModel = value; }
|
||||||
|
}
|
||||||
|
|
||||||
public void Receive(NavgatorMessage message)
|
|
||||||
|
public async void Receive(NavgatorMessage message)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
ViewModelBase nextViewModel = message.Value;
|
||||||
|
//如果OnExit返回False终止跳转
|
||||||
|
|
||||||
|
if (currentViewModel != null)
|
||||||
|
{
|
||||||
|
var isExit = await currentViewModel.OnExitAsync();
|
||||||
|
if (!isExit)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nextViewModel?.OnLoading();
|
||||||
CurrentViewModel = message.Value;
|
CurrentViewModel = message.Value;
|
||||||
|
OnViewModelChanged?.Invoke();
|
||||||
|
currentViewModel?.OnLoaded();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -63,6 +63,52 @@ partial class VariableTableViewModel : ViewModelBase
|
|||||||
|
|
||||||
IsLoadCompletion = true;
|
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]
|
[RelayCommand]
|
||||||
public async void EditVarData(VariableTable variableTable)
|
public async void EditVarData(VariableTable variableTable)
|
||||||
@@ -74,7 +120,6 @@ partial class VariableTableViewModel : ViewModelBase
|
|||||||
// // 如果用户取消或对话框未返回设备,则直接返回
|
// // 如果用户取消或对话框未返回设备,则直接返回
|
||||||
if (varData == null)
|
if (varData == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
varData.VariableTableId = variableTable.Id;
|
varData.VariableTableId = variableTable.Id;
|
||||||
// 更新数据库
|
// 更新数据库
|
||||||
await _varDataRepository.UpdateAsync(varData);
|
await _varDataRepository.UpdateAsync(varData);
|
||||||
@@ -106,12 +151,14 @@ partial class VariableTableViewModel : ViewModelBase
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
varData.VariableTableId = variableTable.Id;
|
varData.VariableTableId = variableTable.Id;
|
||||||
var addVarData = await _varDataRepository.AddAsync(varData);
|
// 更新数据库
|
||||||
DataVariables?.Add(addVarData);
|
await _varDataRepository.UpdateAsync(varData);
|
||||||
variableTable.DataVariables?.Add(addVarData);
|
// 更新当前页面的
|
||||||
var msg = addVarData.Id > 0 ? $"添加变量成功:{varData?.Name}" : $"添加变量成功:{varData.Name}";
|
var index = variableTable.DataVariables.IndexOf(SelectedVariableData);
|
||||||
var type = addVarData.Id > 0 ? NotificationType.Success : NotificationType.Error;
|
// 更新变量表中的
|
||||||
NotificationHelper.ShowMessage(msg, type);
|
if (index >= 0 && index < variableTable.DataVariables.Count)
|
||||||
|
variableTable.DataVariables[index] = varData;
|
||||||
|
NotificationHelper.ShowMessage($"添加变量成功:{varData?.Name}", NotificationType.Success);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,6 +14,11 @@ public abstract class ViewModelBase : ObservableObject
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task<bool> OnExitAsync()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -20,10 +20,11 @@
|
|||||||
|
|
||||||
</ui:ContentDialog.Resources>
|
</ui:ContentDialog.Resources>
|
||||||
|
|
||||||
<Grid Width="480"
|
<Grid Width="360"
|
||||||
Margin="10">
|
Margin="10">
|
||||||
<TextBlock Margin="20"
|
<TextBlock Margin="20"
|
||||||
FontSize="18"
|
FontSize="14"
|
||||||
|
TextWrapping="Wrap"
|
||||||
FontWeight="Bold"
|
FontWeight="Bold"
|
||||||
Text="{Binding Message}">
|
Text="{Binding Message}">
|
||||||
</TextBlock>
|
</TextBlock>
|
||||||
|
|||||||
@@ -61,6 +61,7 @@
|
|||||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
|
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
|
||||||
</ui:AppBarButton.Icon>
|
</ui:AppBarButton.Icon>
|
||||||
</ui:AppBarButton>
|
</ui:AppBarButton>
|
||||||
|
|
||||||
<ui:AppBarButton Command="{Binding EditVarDataCommand}"
|
<ui:AppBarButton Command="{Binding EditVarDataCommand}"
|
||||||
CommandParameter="{Binding VariableTable}"
|
CommandParameter="{Binding VariableTable}"
|
||||||
Label="编辑变量">
|
Label="编辑变量">
|
||||||
@@ -68,6 +69,13 @@
|
|||||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
|
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
|
||||||
</ui:AppBarButton.Icon>
|
</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>
|
||||||
<ui:AppBarButton x:Name="ShareButton"
|
<ui:AppBarButton x:Name="ShareButton"
|
||||||
Label="Share">
|
Label="Share">
|
||||||
|
|||||||
Reference in New Issue
Block a user