添加了变量表的停用和启用功能,并优化了添加变量表和添加设备的逻辑

This commit is contained in:
2025-07-02 18:33:08 +08:00
parent 39a6366a5e
commit 31f6cf64f4
19 changed files with 415 additions and 181 deletions

View File

@@ -36,54 +36,135 @@ public partial class DevicesViewModel : ViewModelBase
_dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection<Device>(devices); };
}
// /// <summary>
// /// 添加设备
// /// </summary>
// [RelayCommand]
// public async void AddDevice()
// {
// Device device = null;
// try
// {
// device = await _dialogService.ShowAddDeviceDialog();
// if (device != null)
// {
// device = await _deviceRepository.Add(device);
// if (device != null)
// {
// var msg = $"添加设备成功:{device.Name}";
// _logger.LogInformation(msg);
//
// bool addMenuRes = await _menuRepository.AddDeviceMenu(device);
// if (addMenuRes)
// {
// // 通知更新菜单
// MessageHelper.SendLoadMessage(LoadTypes.Menu);
// MessageHelper.SendLoadMessage(LoadTypes.Devices);
// NotificationHelper.ShowMessage(msg, NotificationType.Success);
// }
// else
// {
// var msgerr = $"给设备添加菜单失败:{device.Name}";
// _logger.LogInformation(msgerr);
// NotificationHelper.ShowMessage(msgerr, NotificationType.Error);
// }
// }
// else
// {
// var msg = $"添加设备失败:{device.Name}";
// _logger.LogInformation(msg);
// NotificationHelper.ShowMessage(msg, NotificationType.Error);
// }
// }
// }
// catch (Exception e)
// {
// var msg = $"添加设备失败:{e.Message}";
// _logger.LogError(msg);
// NotificationHelper.ShowMessage(msg, NotificationType.Error);
// }
// }
/// <summary>
/// 添加设备
/// </summary>
[RelayCommand]
public async void AddDevice()
{
Device device = null;
try
{
device = await _dialogService.ShowAddDeviceDialog();
if (device != null)
{
device = await _deviceRepository.Add(device);
if (device != null)
{
var msg = $"添加设备成功:{device.Name}";
_logger.LogInformation(msg);
/// 辅助方法:处理成功通知和日志
/// </summary>
private void HandleOperationSuccess(string entityType, string operation, string entityName)
{
var message = $"{entityType}{operation}成功:{entityName}";
_logger.LogInformation(message);
NotificationHelper.ShowMessage(message, NotificationType.Success);
}
bool addMenuRes = await _menuRepository.AddDeviceMenu(device);
if (addMenuRes)
{
// 通知更新菜单
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);
NotificationHelper.ShowMessage(msg, NotificationType.Success);
}
else
{
var msgerr = $"给设备添加菜单失败:{device.Name}";
_logger.LogInformation(msgerr);
NotificationHelper.ShowMessage(msgerr, NotificationType.Error);
}
}
else
{
var msg = $"添加设备失败:{device.Name}";
_logger.LogInformation(msg);
NotificationHelper.ShowMessage(msg, NotificationType.Error);
}
}
}
catch (Exception e)
/// <summary>
/// 辅助方法:处理失败通知和日志
/// </summary>
private void HandleOperationFailure(string entityType, string operation, string entityName, Exception ex = null)
{
var message = $"{entityType}{operation}失败:{entityName}";
if (ex != null)
{
_logger.LogError(ex, message);
}
else
{
_logger.LogError(message);
}
NotificationHelper.ShowMessage(message, NotificationType.Error);
}
/// <summary>
/// 添加设备
/// </summary>
[RelayCommand]
public async void AddDevice()
{
try
{
// 1. 显示添加设备对话框
var device = await _dialogService.ShowAddDeviceDialog();
// 如果用户取消或对话框未返回设备,则直接返回
if (device == null)
{
var msg = $"添加设备失败:{e.Message}";
_logger.LogError(msg);
NotificationHelper.ShowMessage(msg, NotificationType.Error);
_logger.LogInformation("用户取消了添加设备操作。");
return;
}
// 2. 将设备添加到数据库
var addedDevice = await _deviceRepository.Add(device);
// 如果数据库添加失败
if (addedDevice == null)
{
HandleOperationFailure("设备", "添加", device.Name);
return; // 提前返回
}
// 3. 设备成功添加到数据库,进行菜单添加
// 这里立即发出成功的通知和日志
HandleOperationSuccess("设备", "添加", addedDevice.Name);
// 4. 为新设备添加菜单
bool menuAddedSuccessfully = await _menuRepository.AddDeviceMenu(addedDevice);
if (menuAddedSuccessfully)
{
// 菜单也添加成功,通知 UI 更新
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);
}
else
{
// 菜单添加失败,通知用户并记录日志
HandleOperationFailure("设备", "添加菜单", addedDevice.Name);
// 考虑:如果菜单添加失败,是否需要回滚设备添加?
// 例如await _deviceRepository.Delete(addedDevice.Id);
}
}
catch (Exception e)
{
// 捕获并记录所有未预期的异常
_logger.LogError(e, "在添加设备过程中发生未预期错误。");
NotificationHelper.ShowMessage($"添加设备失败:{e.Message}", NotificationType.Error);
}
}
/// <summary>
@@ -153,14 +234,8 @@ public partial class DevicesViewModel : ViewModelBase
_logger.LogError($"删除设备的过程中发生错误:{e}");
}
}
[RelayCommand]
public void NavigateVt()
{
}
public override async void OnLoaded()
{
}
}