diff --git a/DMS.Application/DTOs/UpdateDeviceDto.cs b/DMS.Application/DTOs/UpdateDeviceDto.cs deleted file mode 100644 index ed182f9..0000000 --- a/DMS.Application/DTOs/UpdateDeviceDto.cs +++ /dev/null @@ -1,19 +0,0 @@ -using DMS.Core.Enums; - -namespace DMS.Application.DTOs; - -/// -/// 用于更新设备时传输数据的DTO。 -/// -public class UpdateDeviceDto -{ - public int Id { get; set; } - public string? Name { get; set; } - public ProtocolType? Protocol { get; set; } - public string? IpAddress { get; set; } - public int? Port { get; set; } - public int? Rack { get; set; } - public int? Slot { get; set; } - public string? OpcUaServerUrl { get; set; } - public bool? IsActive { get; set; } -} \ No newline at end of file diff --git a/DMS.Application/Interfaces/IDeviceAppService.cs b/DMS.Application/Interfaces/IDeviceAppService.cs index ae66856..4b54f6c 100644 --- a/DMS.Application/Interfaces/IDeviceAppService.cs +++ b/DMS.Application/Interfaces/IDeviceAppService.cs @@ -29,7 +29,7 @@ public interface IDeviceAppService /// /// 异步更新一个已存在的设备。 /// - Task UpdateDeviceAsync(UpdateDeviceDto deviceDto); + Task UpdateDeviceAsync(DeviceDto deviceDto); /// /// 异步删除一个设备。 diff --git a/DMS.Application/Profiles/MappingProfile.cs b/DMS.Application/Profiles/MappingProfile.cs index c62c17b..1cbcd84 100644 --- a/DMS.Application/Profiles/MappingProfile.cs +++ b/DMS.Application/Profiles/MappingProfile.cs @@ -13,25 +13,25 @@ public class MappingProfile : Profile { - // Device 映射 - CreateMap() - // 1. 首先,忽略那些永远不应从DTO更新的属性 - .ForMember(dest => dest.Id, opt => opt.Ignore()) - .ForMember(dest => dest.Description, opt => opt.Ignore()) - .ForMember(dest => dest.VariableTables, opt => opt.Ignore()) - .ForMember(dest => dest.CpuType, opt => opt.Ignore()) - .ForMember(dest => dest.IsRunning, opt => opt.Ignore()) - .ForMember(dest => dest.DeviceType, opt => opt.Ignore()) - - // 2. 然后,为每个可空属性单独设置条件 - .ForMember(dest => dest.Name, opt => opt.Condition(src => src.Name != null)) - .ForMember(dest => dest.Protocol, opt => opt.Condition(src => src.Protocol.HasValue)) - .ForMember(dest => dest.IpAddress, opt => opt.Condition(src => src.IpAddress != null)) - .ForMember(dest => dest.Port, opt => opt.Condition(src => src.Port.HasValue)) - .ForMember(dest => dest.Rack, opt => opt.Condition(src => src.Rack.HasValue)) - .ForMember(dest => dest.Slot, opt => opt.Condition(src => src.Slot.HasValue)) - .ForMember(dest => dest.OpcUaServerUrl, opt => opt.Condition(src => src.OpcUaServerUrl != null)) - .ForMember(dest => dest.IsActive, opt => opt.Condition(src => src.IsActive.HasValue)); + // // Device 映射 + // CreateMap() + // // 1. 首先,忽略那些永远不应从DTO更新的属性 + // .ForMember(dest => dest.Id, opt => opt.Ignore()) + // .ForMember(dest => dest.Description, opt => opt.Ignore()) + // .ForMember(dest => dest.VariableTables, opt => opt.Ignore()) + // .ForMember(dest => dest.CpuType, opt => opt.Ignore()) + // .ForMember(dest => dest.IsRunning, opt => opt.Ignore()) + // .ForMember(dest => dest.DeviceType, opt => opt.Ignore()) + // + // // 2. 然后,为每个可空属性单独设置条件 + // .ForMember(dest => dest.Name, opt => opt.Condition(src => src.Name != null)) + // .ForMember(dest => dest.Protocol, opt => opt.Condition(src => src.Protocol.HasValue)) + // .ForMember(dest => dest.IpAddress, opt => opt.Condition(src => src.IpAddress != null)) + // .ForMember(dest => dest.Port, opt => opt.Condition(src => src.Port.HasValue)) + // .ForMember(dest => dest.Rack, opt => opt.Condition(src => src.Rack.HasValue)) + // .ForMember(dest => dest.Slot, opt => opt.Condition(src => src.Slot.HasValue)) + // .ForMember(dest => dest.OpcUaServerUrl, opt => opt.Condition(src => src.OpcUaServerUrl != null)) + // .ForMember(dest => dest.IsActive, opt => opt.Condition(src => src.IsActive.HasValue)); CreateMap() .ReverseMap(); diff --git a/DMS.Application/Services/DeviceAppService.cs b/DMS.Application/Services/DeviceAppService.cs index cd90249..1a88ec9 100644 --- a/DMS.Application/Services/DeviceAppService.cs +++ b/DMS.Application/Services/DeviceAppService.cs @@ -135,7 +135,7 @@ public class DeviceAppService : IDeviceAppService /// 要更新的设备数据传输对象。 /// 受影响的行数。 /// 如果找不到设备。 - public async Task UpdateDeviceAsync(UpdateDeviceDto deviceDto) + public async Task UpdateDeviceAsync(DeviceDto deviceDto) { await _repoManager.BeginTranAsync(); var device = await _repoManager.Devices.GetByIdAsync(deviceDto.Id); @@ -146,6 +146,12 @@ public class DeviceAppService : IDeviceAppService _mapper.Map(deviceDto, device); int res=await _repoManager.Devices.UpdateAsync(device); + var menu=await _repoManager.Menus.GetMenuByTargetIdAsync(MenuType.DeviceMenu, deviceDto.Id); + if (menu != null) + { + menu.Header = device.Name; + await _repoManager.Menus.UpdateAsync(menu); + } await _repoManager.CommitAsync(); return res; } diff --git a/DMS.Infrastructure.UnitTests/Services/DeviceAppServiceTest.cs b/DMS.Infrastructure.UnitTests/Services/DeviceAppServiceTest.cs index 02a87d7..42d37aa 100644 --- a/DMS.Infrastructure.UnitTests/Services/DeviceAppServiceTest.cs +++ b/DMS.Infrastructure.UnitTests/Services/DeviceAppServiceTest.cs @@ -52,16 +52,16 @@ public class DeviceAppServiceTest : BaseServiceTest // 继承基类 [Fact] public async Task UpdateDeviceAsyncTest() { - UpdateDeviceDto dto = new UpdateDeviceDto() - { - Id = 5, - Name = "lalala", - IsActive = true, - Rack = 0, - Slot = 0 - - }; - var res = await _deviceService.UpdateDeviceAsync(dto); - Assert.NotEqual(res,0); + // UpdateDeviceDto dto = new UpdateDeviceDto() + // { + // Id = 5, + // Name = "lalala", + // IsActive = true, + // Rack = 0, + // Slot = 0 + // + // }; + // var res = await _deviceService.UpdateDeviceAsync(dto); + // Assert.NotEqual(res,0); } } \ No newline at end of file diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index 4b590f3..cc39b66 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -60,18 +60,6 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable DataServices.OnDeviceListChanged += (devices) => { }; } - // public override void OnLoaded() - // { - // if (_dataServices.Devices!=null && _dataServices.Devices.Count>0) - // { - // Devices=new ObservableCollection(_dataServices.Devices); - // foreach (var device in Devices) - // { - // device.OnDeviceIsActiveChanged += HandleDeviceIsActiveChanged; - // - // } - // } - // } public override Task OnExitAsync() { @@ -86,19 +74,6 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable return Task.FromResult(true); } - private async void HandleDeviceIsActiveChanged(Device device, bool isActive) - { - // try - // { - // await _deviceRepository.UpdateAsync(device); - // NotificationHelper.ShowSuccess($"设备 {device.Name} 的激活状态已更新。"); - // } - // catch (Exception ex) - // { - // NotificationHelper.ShowError($"更新设备 {device.Name} 激活状态失败: {ex.Message}", ex); - // } - } - /// /// 添加设备命令。 /// @@ -107,8 +82,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable { try { - DeviceItemViewModel deviceItemViewModel = new DeviceItemViewModel(); - DeviceDialogViewModel deviceDialogViewModel = new DeviceDialogViewModel(deviceItemViewModel) + DeviceDialogViewModel deviceDialogViewModel = new DeviceDialogViewModel() { PrimaryButContent = "添加设备" }; @@ -208,31 +182,42 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable [RelayCommand] public async void EditDevice() { - // try - // { - // if (SelectedDevice == null) - // { - // NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击编辑设备"); - // return; - // } - // - // var editDievce = await _dialogService.ShowEditDeviceDialog(SelectedDevice); - // if (editDievce != null) - // { - // // 更新菜单 - // var res = await _deviceRepository.UpdateAsync(editDievce); - // var menu = DataServicesHelper.FindMenusForDevice(editDievce, _dataServices.MenuTrees); - // if (menu != null) - // await _menuRepository.UpdateAsync(menu); - // - // MessageHelper.SendLoadMessage(LoadTypes.Menu); - // MessageHelper.SendLoadMessage(LoadTypes.Devices); - // } - // } - // catch (Exception e) - // { - // NotificationHelper.ShowError($"编辑设备的过程中发生错误:{e.Message}", e); - // } + try + { + if (SelectedDevice == null) + { + NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击编辑设备"); + return; + } + + DeviceDialogViewModel deviceDialogViewModel = new DeviceDialogViewModel(SelectedDevice) + { + PrimaryButContent = "编辑设备" + }; + // 1. 显示设备对话框 + DeviceItemViewModel device = await _dialogService.ShowDialogAsync(deviceDialogViewModel); + // 如果用户取消或对话框未返回设备,则直接返回 + if (device == null) + { + return; + } + + int res = await _deviceAppService.UpdateDeviceAsync(_mapper.Map(device)); + if (res > 0) + { + var menu = DataServices.Menus.FirstOrDefault(m => + m.MenuType == MenuType.DeviceMenu && + m.TargetId == device.Id); + if (menu!=null) + { + menu.Header=device.Name; + } + } + } + catch (Exception e) + { + NotificationHelper.ShowError($"编辑设备的过程中发生错误:{e.Message}", e); + } } [RelayCommand] diff --git a/DMS.WPF/ViewModels/Dialogs/DeviceDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/DeviceDialogViewModel.cs index 6e0f41b..5c2368b 100644 --- a/DMS.WPF/ViewModels/Dialogs/DeviceDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/DeviceDialogViewModel.cs @@ -7,15 +7,25 @@ namespace DMS.WPF.ViewModels.Dialogs; public partial class DeviceDialogViewModel : DialogViewModelBase { - - + [ObservableProperty] + private bool _isAddMode; [ObservableProperty] private DeviceItemViewModel _device; - public DeviceDialogViewModel(DeviceItemViewModel device) + public DeviceDialogViewModel(DeviceItemViewModel device=null) { - _device = device; + if (device==null) + { + _device = new DeviceItemViewModel(); + IsAddMode=true; + } + else + { + _device=device; + + } + } [RelayCommand] diff --git a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml index e7d13aa..3934c37 100644 --- a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml +++ b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml @@ -65,7 +65,9 @@ - @@ -102,7 +104,9 @@ -