diff --git a/DMS.Application/Interfaces/Database/IDeviceAppService.cs b/DMS.Application/Interfaces/Database/IDeviceAppService.cs
index 1766220..d35b9ba 100644
--- a/DMS.Application/Interfaces/Database/IDeviceAppService.cs
+++ b/DMS.Application/Interfaces/Database/IDeviceAppService.cs
@@ -12,11 +12,11 @@ public interface IDeviceAppService
///
/// 异步根据ID获取设备DTO。
///
- Task GetDeviceByIdAsync(int id);
+ Task GetDeviceByIdAsync(int id);
///
/// 异步获取所有设备DTO列表。
///
- Task> GetAllDevicesAsync();
+ Task> GetAllDevicesAsync();
///
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
///
@@ -27,16 +27,12 @@ public interface IDeviceAppService
///
/// 异步更新一个已存在的设备。
///
- Task UpdateDeviceAsync(Device device);
+ Task UpdateDeviceAsync(Device device);
+
///
/// 异步删除一个设备。
///
- Task DeleteDeviceAsync(Device device);
-
- ///
- /// 异步删除一个设备。
- ///
- Task DeleteDeviceByIdAsync(int deviceId);
+ Task DeleteAsync(Device device);
///
/// 异步切换设备的激活状态。
@@ -46,4 +42,5 @@ public interface IDeviceAppService
///
/// 异步获取指定协议类型的设备列表。
///
- Task> GetDevicesByProtocolAsync(ProtocolType protocol);}
\ No newline at end of file
+ Task> GetDevicesByProtocolAsync(ProtocolType protocol);
+}
\ No newline at end of file
diff --git a/DMS.Application/Interfaces/Management/IDeviceManagementService.cs b/DMS.Application/Interfaces/Management/IDeviceManagementService.cs
index a906f8c..13687e5 100644
--- a/DMS.Application/Interfaces/Management/IDeviceManagementService.cs
+++ b/DMS.Application/Interfaces/Management/IDeviceManagementService.cs
@@ -28,7 +28,7 @@ public interface IDeviceManagementService
///
/// 异步删除一个设备。
///
- Task DeleteDeviceByIdAsync(int deviceId);
+ Task DeleteAsync(Device device);
///
/// 异步切换设备的激活状态。
diff --git a/DMS.Application/Services/Database/DeviceAppService.cs b/DMS.Application/Services/Database/DeviceAppService.cs
index 5c4fc45..93864e7 100644
--- a/DMS.Application/Services/Database/DeviceAppService.cs
+++ b/DMS.Application/Services/Database/DeviceAppService.cs
@@ -68,6 +68,7 @@ public class DeviceAppService : IDeviceAppService
// 假设有设备菜单
if (dto.DeviceMenu is not null)
{
+ dto.DeviceMenu.TargetId = dto.Device.Id;
dto.DeviceMenu = await _repoManager.Menus.AddAsync(dto.DeviceMenu);
}
@@ -82,6 +83,7 @@ public class DeviceAppService : IDeviceAppService
{
throw new InvalidOperationException($"添加设备变量表失败,设备:{dto.Device.Name},变量表:{dto?.VariableTable?.Name}");
}
+ dto.VariableTable.Device = dto.Device;
// 假设有设备菜单
if (dto.VariableTableMenu is not null && dto.VariableTableMenu is not null)
@@ -136,15 +138,6 @@ public class DeviceAppService : IDeviceAppService
return res;
}
- ///
- /// 异步删除一个设备。
- ///
- /// 要删除的设备实体。
- /// 表示异步操作的任务。
- public async Task DeleteDeviceAsync(Device device)
- {
- await DeleteDeviceByIdAsync(device.Id);
- }
///
/// 异步根据ID删除一个设备,包括其关联的变量表和菜单(事务性操作)。
@@ -153,19 +146,19 @@ public class DeviceAppService : IDeviceAppService
/// 如果删除成功则为 true,否则为 false。
/// 如果删除设备失败。
/// 如果删除设备时发生其他错误。
- public async Task DeleteDeviceByIdAsync(int deviceId)
+ public async Task DeleteAsync(Device device)
{
try
{
await _repoManager.BeginTranAsync();
- var delRes = await _repoManager.Devices.DeleteByIdAsync(deviceId);
+ var delRes = await _repoManager.Devices.DeleteAsync(device);
if (delRes == 0)
{
- throw new InvalidOperationException($"删除设备失败:设备ID:{deviceId},请检查设备Id是否存在");
+ throw new InvalidOperationException($"删除设备失败:设备ID:{device.Id},请检查设备Id是否存在");
}
// 删除关联的变量表
- await _repoManager.VariableTables.DeleteByDeviceIdAsync(deviceId);
+ await _repoManager.VariableTables.DeleteAsync(device.VariableTables);
// 删除关联的变量
await _repoManager.Variables.DeleteByVariableTableIdAsync(deviceId);
diff --git a/DMS.Application/Services/Management/DeviceManagementService.cs b/DMS.Application/Services/Management/DeviceManagementService.cs
index cdc8145..b0f2c2b 100644
--- a/DMS.Application/Services/Management/DeviceManagementService.cs
+++ b/DMS.Application/Services/Management/DeviceManagementService.cs
@@ -45,6 +45,9 @@ public class DeviceManagementService : IDeviceManagementService
///
public async Task CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
{
+
+
+
var result = await _deviceAppService.CreateDeviceWithDetailsAsync(dto);
// 创建成功后,将设备添加到内存中
@@ -54,10 +57,25 @@ public class DeviceManagementService : IDeviceManagementService
{
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Added, result.Device));
}
- if (_appStorageService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable))
+ if (result.VariableTable is not null)
{
- _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs(DataChangeType.Added, result.VariableTable));
+ if (_appStorageService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable))
+ {
+ _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs(DataChangeType.Added, result.VariableTable));
+ }
}
+
+ if (result.DeviceMenu is not null)
+ {
+ _appStorageService.Menus.TryAdd(result.DeviceMenu.Id, result.DeviceMenu);
+ }
+
+ if (result.VariableTableMenu is not null)
+ {
+ _appStorageService.Menus.TryAdd(result.VariableTableMenu.Id, result.VariableTableMenu);
+ }
+
+
}
return result;
@@ -83,13 +101,12 @@ public class DeviceManagementService : IDeviceManagementService
///
/// 异步删除一个设备。
///
- public async Task DeleteDeviceByIdAsync(int deviceId)
+ public async Task DeleteAsync(Device device)
{
- var device = await _deviceAppService.GetDeviceByIdAsync(deviceId); // 获取设备信息用于内存删除
- var result = await _deviceAppService.DeleteDeviceByIdAsync(deviceId);
+ var result = await _deviceAppService.DeleteDeviceByIdAsync(device);
// 删除成功后,从内存中移除设备
- if (result && device != null)
+ if (result )
{
if (_appStorageService.Devices.TryGetValue(deviceId, out var deviceInStorage))
{
diff --git a/DMS.Infrastructure/Repositories/MenuRepository.cs b/DMS.Infrastructure/Repositories/MenuRepository.cs
index 572fd64..3c5f214 100644
--- a/DMS.Infrastructure/Repositories/MenuRepository.cs
+++ b/DMS.Infrastructure/Repositories/MenuRepository.cs
@@ -144,9 +144,6 @@ public class MenuRepository : BaseRepository, IMenuRepository
.ToChildListAsync(c => c.ParentId, menu.Id);
var delConut = await _dbContext.GetInstance().Deleteable(childList)
.ExecuteCommandAsync();
- delConut += await _dbContext.GetInstance().Deleteable()
- .Where(m => m.Id == menu.Id)
- .ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"Delete {typeof(DbMenu)},TargetId={targetId},耗时:{stopwatch.ElapsedMilliseconds}ms");
return delConut;
diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs
index fa6146b..7596cd7 100644
--- a/DMS.WPF/App.xaml.cs
+++ b/DMS.WPF/App.xaml.cs
@@ -302,10 +302,10 @@ public partial class App : System.Windows.Application
services.AddSingleton();
// 注册新的数据服务
- services.AddSingleton();
+ services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
- services.AddSingleton();
+ services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
diff --git a/DMS.WPF/DMS.WPF.csproj b/DMS.WPF/DMS.WPF.csproj
index 9cabeb9..514f9f2 100644
--- a/DMS.WPF/DMS.WPF.csproj
+++ b/DMS.WPF/DMS.WPF.csproj
@@ -187,4 +187,10 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/DMS.WPF/Interfaces/IDeviceDataService.cs b/DMS.WPF/Interfaces/IDeviceDataService.cs
index 3f209fe..f102be5 100644
--- a/DMS.WPF/Interfaces/IDeviceDataService.cs
+++ b/DMS.WPF/Interfaces/IDeviceDataService.cs
@@ -22,7 +22,7 @@ public interface IDeviceDataService
///
/// 添加设备。
///
- Task AddDevice(CreateDeviceWithDetailsDto dto);
+ Task AddDeviceAsync(CreateDeviceWithDetailsDto dto);
///
/// 删除设备。
@@ -33,5 +33,4 @@ public interface IDeviceDataService
/// 更新设备。
///
Task UpdateDevice(DeviceItem device);
- Task AddDevice(CreateDeviceWithDetailsDto dto);
}
\ No newline at end of file
diff --git a/DMS.WPF/Interfaces/IMenuWpfService.cs b/DMS.WPF/Interfaces/IMenuViewService.cs
similarity index 93%
rename from DMS.WPF/Interfaces/IMenuWpfService.cs
rename to DMS.WPF/Interfaces/IMenuViewService.cs
index 08de4c3..07e9a76 100644
--- a/DMS.WPF/Interfaces/IMenuWpfService.cs
+++ b/DMS.WPF/Interfaces/IMenuViewService.cs
@@ -6,7 +6,7 @@ namespace DMS.WPF.Interfaces;
///
/// 菜单数据服务接口。
///
-public interface IMenuWpfService
+public interface IMenuViewService
{
diff --git a/DMS.WPF/Interfaces/IWPFDataService.cs b/DMS.WPF/Interfaces/IWPFDataService.cs
index 3cc61f2..a0b3efa 100644
--- a/DMS.WPF/Interfaces/IWPFDataService.cs
+++ b/DMS.WPF/Interfaces/IWPFDataService.cs
@@ -24,7 +24,7 @@ public interface IWPFDataService
///
/// 菜单数据服务。
///
- IMenuWpfService MenuDataService { get; }
+ IMenuViewService MenuDataService { get; }
///
/// MQTT数据服务。
diff --git a/DMS.WPF/Services/DeviceWpfService.cs b/DMS.WPF/Services/DeviceViewService.cs
similarity index 74%
rename from DMS.WPF/Services/DeviceWpfService.cs
rename to DMS.WPF/Services/DeviceViewService.cs
index 032ad9a..dfd0e1f 100644
--- a/DMS.WPF/Services/DeviceWpfService.cs
+++ b/DMS.WPF/Services/DeviceViewService.cs
@@ -9,13 +9,14 @@ using DMS.Core.Enums;
using DMS.Core.Events;
using DMS.WPF.Interfaces;
using DMS.WPF.ItemViewModel;
+using DMS.WPF.ViewModels;
namespace DMS.WPF.Services;
///
/// 设备数据服务类,负责管理设备相关的数据和操作。
///
-public class DeviceWpfService : IDeviceDataService
+public class DeviceViewService : IDeviceDataService
{
private readonly IMapper _mapper;
private readonly IAppCenterService _appCenterService;
@@ -24,7 +25,7 @@ public class DeviceWpfService : IDeviceDataService
private readonly IVariableTableDataService _variableTableDataService;
private readonly IEventService _eventService;
private readonly INotificationService _notificationService;
- private readonly IMenuWpfService _menuDataService;
+ private readonly IMenuViewService _menuDataService;
private readonly IVariableDataService _variableDataService;
private readonly Dispatcher _uiDispatcher;
@@ -33,12 +34,12 @@ public class DeviceWpfService : IDeviceDataService
///
/// AutoMapper 实例。
/// 数据服务中心实例。
- public DeviceWpfService(IMapper mapper, IAppCenterService appCenterService,
- IAppStorageService appStorageService, IWpfDataService dataStorageService, IVariableTableDataService variableTableDataService,
+ public DeviceViewService(IMapper mapper, IAppCenterService appCenterService,
+ IAppStorageService appStorageService, IWpfDataService dataStorageService,
+ IVariableTableDataService variableTableDataService,
IEventService eventService, INotificationService notificationService,
- IMenuWpfService menuDataService, IVariableDataService variableDataService)
+ IMenuViewService menuDataService, IVariableDataService variableDataService)
{
-
_mapper = mapper;
_appCenterService = appCenterService;
_appStorageService = appStorageService;
@@ -60,10 +61,8 @@ public class DeviceWpfService : IDeviceDataService
{
_uiDispatcher.Invoke(() =>
{
-
if (_dataStorageService.Devices.TryGetValue(e.DeviceId, out DeviceItem device))
{
-
device.IsRunning = e.StateValue;
if (device.IsRunning)
{
@@ -92,10 +91,27 @@ public class DeviceWpfService : IDeviceDataService
///
/// 添加设备。
///
- public async Task AddDevice(CreateDeviceWithDetailsDto dto)
+ public async Task AddDeviceAsync(CreateDeviceWithDetailsDto dto)
{
// 添加null检查
if (dto is null) return null;
+
+ if (dto.VariableTable is not null)
+ {
+ dto.VariableTable.Protocol = dto.Device.Protocol;
+ dto.VariableTableMenu.MenuType = MenuType.VariableTableMenu;
+ }
+
+
+ //查找到设备的根菜单
+ var parentMenu
+ = _appStorageService.Menus.Values.FirstOrDefault(m => m.TargetViewKey == nameof(DevicesViewModel) &&
+ m.TargetId == 0);
+ if (parentMenu is not null)
+ {
+ dto.DeviceMenu.MenuType = MenuType.DeviceMenu;
+ dto.DeviceMenu.ParentId = parentMenu.Id;
+ }
var addDto = await _appCenterService.DeviceManagementService.CreateDeviceWithDetailsAsync(dto);
@@ -109,10 +125,8 @@ public class DeviceWpfService : IDeviceDataService
if (addDto.DeviceMenu != null)
{
_menuDataService.AddMenuToView(_mapper.Map
/// AutoMapper 实例。
/// 数据服务中心实例。
- public MenuWpfService(IMapper mapper, IWpfDataService dataStorageService, IAppStorageService appStorageService, IMenuManagementService menuManagementService)
+ public MenuViewService(IMapper mapper, IWpfDataService dataStorageService, IAppStorageService appStorageService, IMenuManagementService menuManagementService)
{
_mapper = mapper;
_wpfDataService = dataStorageService;
@@ -46,7 +46,16 @@ public class MenuWpfService : IMenuWpfService
///
public void BuildMenuTrees()
{
- _wpfDataService.MenuTrees.Clear();
+ // 创建一个新的临时列表来存储根菜单
+ var newRootMenus = new List();
+
+ // 首先,确保所有菜单项的Children集合是干净的
+ foreach (var menu in _wpfDataService.Menus)
+ {
+ // 避免循环引用,清空Children集合
+ menu.Children.Clear();
+ }
+
// 遍历所有菜单项,构建树形结构
foreach (var menu in _wpfDataService.Menus)
{
@@ -63,9 +72,16 @@ public class MenuWpfService : IMenuWpfService
else
{
// 如果没有父ID,则这是一个根菜单
- _wpfDataService.MenuTrees.Add(menu);
+ newRootMenus.Add(menu);
}
}
+
+ // 在UI线程安全地更新MenuTrees集合
+ _wpfDataService.MenuTrees.Clear();
+ foreach (var rootMenu in newRootMenus)
+ {
+ _wpfDataService.MenuTrees.Add(rootMenu);
+ }
}
///
@@ -75,14 +91,17 @@ public class MenuWpfService : IMenuWpfService
{
if (MenuItem is null) return;
- var deviceMenu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == MenuItem.ParentId);
- if (deviceMenu is not null)
+ var parentMenu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == MenuItem.ParentId);
+ if (parentMenu is not null)
{
-
- deviceMenu.Children.Add(MenuItem);
+ // 首先添加到菜单列表
_wpfDataService.Menus.Add(MenuItem);
- BuildMenuTrees();
-
+
+ // 然后将当前菜单添加到父菜单的Children列表中
+ if (!parentMenu.Children.Contains(MenuItem))
+ {
+ parentMenu.Children.Add(MenuItem);
+ }
}
}
@@ -119,7 +138,7 @@ public class MenuWpfService : IMenuWpfService
// 从扁平菜单列表中移除
_wpfDataService.Menus.Remove(MenuItem);
- //// 从树形结构中移除
+ // 从树形结构中移除
if (MenuItem.ParentId.HasValue && MenuItem.ParentId.Value != 0)
{
// 如果有父菜单,从父菜单的Children中移除
@@ -132,6 +151,7 @@ public class MenuWpfService : IMenuWpfService
_wpfDataService.MenuTrees.Remove(MenuItem);
}
- //BuildMenuTrees();
+ // 重新构建整个菜单树以确保一致性
+ BuildMenuTrees();
}
}
\ No newline at end of file
diff --git a/DMS.WPF/Services/MqttDataService.cs b/DMS.WPF/Services/MqttDataService.cs
index 1fdf49f..ffe1a07 100644
--- a/DMS.WPF/Services/MqttDataService.cs
+++ b/DMS.WPF/Services/MqttDataService.cs
@@ -19,7 +19,7 @@ public class MqttDataService : IMqttDataService
private readonly IMapper _mapper;
private readonly IAppStorageService _appStorageService;
private readonly IMqttManagementService _mqttManagementService;
- private readonly IMenuWpfService _menuDataService;
+ private readonly IMenuViewService _menuDataService;
private readonly IMenuManagementService _menuManagementServiceImpl;
private readonly IWpfDataService _dataStorageService;
@@ -29,7 +29,7 @@ public class MqttDataService : IMqttDataService
///
/// AutoMapper 实例。
/// MQTT应用服务实例。
- public MqttDataService(IMapper mapper, IAppStorageService appStorageService, IMqttManagementService mqttManagementService, IMenuWpfService menuDataService, IMenuManagementService menuManagementServiceImpl, IWpfDataService dataStorageService)
+ public MqttDataService(IMapper mapper, IAppStorageService appStorageService, IMqttManagementService mqttManagementService, IMenuViewService menuDataService, IMenuManagementService menuManagementServiceImpl, IWpfDataService dataStorageService)
{
_mapper = mapper;
_appStorageService = appStorageService;
@@ -87,7 +87,7 @@ public class MqttDataService : IMqttDataService
MenuType = MenuType.MqttServerMenu,
TargetViewKey = nameof(MqttServerDetailViewModel),
};
- await _menuDataService.AddMenuToView(_mapper.Map(mqttServerMenu));
+ _menuDataService.AddMenuToView(_mapper.Map(mqttServerMenu));
}
return mqttServerItem;
diff --git a/DMS.WPF/Services/TriggerDataService.cs b/DMS.WPF/Services/TriggerDataService.cs
index d9d66ee..a93b57f 100644
--- a/DMS.WPF/Services/TriggerDataService.cs
+++ b/DMS.WPF/Services/TriggerDataService.cs
@@ -24,7 +24,7 @@ public class TriggerDataService : ITriggerDataService
{
private readonly IMapper _mapper;
private readonly IAppCenterService _appCenterService;
- private readonly IMenuWpfService _menuDataService;
+ private readonly IMenuViewService _menuDataService;
private readonly IAppStorageService _appStorageService;
private readonly IWpfDataService _dataStorageService;
private readonly IEventService _eventService;
@@ -41,7 +41,7 @@ public class TriggerDataService : ITriggerDataService
/// 事件服务实例。
/// 通知服务实例。
public TriggerDataService(IMapper mapper, IAppCenterService appCenterService,
- IMenuWpfService menuDataService,
+ IMenuViewService menuDataService,
IAppStorageService appStorageService, IWpfDataService dataStorageService,
IEventService eventService, INotificationService notificationService)
{
@@ -111,7 +111,7 @@ public class TriggerDataService : ITriggerDataService
Icon = "\uE945", // 使用触发器图标
TargetViewKey = nameof(TriggerDetailViewModel),
};
- await _menuDataService.AddMenuToView(menuItem);
+ _menuDataService.AddMenuToView(menuItem);
}
diff --git a/DMS.WPF/Services/VariableTableDataService.cs b/DMS.WPF/Services/VariableTableDataService.cs
index 942f610..efaf7aa 100644
--- a/DMS.WPF/Services/VariableTableDataService.cs
+++ b/DMS.WPF/Services/VariableTableDataService.cs
@@ -14,12 +14,12 @@ public class VariableTableDataService : IVariableTableDataService
private readonly IMapper _mapper;
private readonly IWpfDataService _dataStorageService;
private readonly IAppCenterService _appCenterService;
- private readonly IMenuWpfService _menuDataService;
+ private readonly IMenuViewService _menuDataService;
public VariableTableDataService(IMapper mapper, IWpfDataService dataStorageService, IAppCenterService appCenterService,
- IMenuWpfService menuDataService)
+ IMenuViewService menuDataService)
{
_mapper = mapper;
_dataStorageService = dataStorageService;
@@ -53,7 +53,7 @@ public class VariableTableDataService : IVariableTableDataService
createDto.Menu = menuDto;
var resDto = await _appCenterService.VariableTableManagementService.CreateVariableTableAsync(createDto);
- await _menuDataService.AddMenuToView(_mapper.Map(resDto.Menu));
+ _menuDataService.AddMenuToView(_mapper.Map(resDto.Menu));
return resDto.VariableTable.Id;
}
diff --git a/DMS.WPF/Services/WPFDataService.cs b/DMS.WPF/Services/WPFDataService.cs
index c09a20d..9e0966d 100644
--- a/DMS.WPF/Services/WPFDataService.cs
+++ b/DMS.WPF/Services/WPFDataService.cs
@@ -32,7 +32,7 @@ public class WPFDataService : IWPFDataService
///
/// 菜单数据服务。
///
- public IMenuWpfService MenuDataService { get; }
+ public IMenuViewService MenuDataService { get; }
///
/// MQTT数据服务。
@@ -62,7 +62,7 @@ public class WPFDataService : IWPFDataService
IAppCenterService appCenterService,
IDeviceDataService deviceDataService,
IVariableDataService variableDataService,
- IMenuWpfService menuDataService,
+ IMenuViewService menuDataService,
IMqttDataService mqttDataService,
ILogDataService logDataService,
IVariableTableDataService variableTableDataService,
diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs
index 04fdf8a..c4ea3a1 100644
--- a/DMS.WPF/ViewModels/DevicesViewModel.cs
+++ b/DMS.WPF/ViewModels/DevicesViewModel.cs
@@ -132,7 +132,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
// 添加null检查
if (_wpfDataService != null && _wpfDataService.DeviceDataService != null)
{
- var addDto = await _wpfDataService.DeviceDataService.AddDevice(dto);
+ var addDto = await _wpfDataService.DeviceDataService.AddDeviceAsync(dto);
// 添加null检查
if (addDto != null && addDto.Device != null && _notificationService != null)