wip
This commit is contained in:
@@ -12,11 +12,11 @@ public interface IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步根据ID获取设备DTO。
|
||||
/// </summary>
|
||||
Task<Device> GetDeviceByIdAsync(int id);
|
||||
Task<Device> GetDeviceByIdAsync(int id);
|
||||
/// <summary>
|
||||
/// 异步获取所有设备DTO列表。
|
||||
/// </summary>
|
||||
Task<List<Device>> GetAllDevicesAsync();
|
||||
Task<List<Device>> GetAllDevicesAsync();
|
||||
/// <summary>
|
||||
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
|
||||
/// </summary>
|
||||
@@ -27,16 +27,12 @@ public interface IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的设备。
|
||||
/// </summary>
|
||||
Task<int> UpdateDeviceAsync(Device device);
|
||||
Task<int> UpdateDeviceAsync(Device device);
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
Task DeleteDeviceAsync(Device device);
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
Task<bool> DeleteDeviceByIdAsync(int deviceId);
|
||||
Task<bool> DeleteAsync(Device device);
|
||||
|
||||
/// <summary>
|
||||
/// 异步切换设备的激活状态。
|
||||
@@ -46,4 +42,5 @@ public interface IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步获取指定协议类型的设备列表。
|
||||
/// </summary>
|
||||
Task<List<Device>> GetDevicesByProtocolAsync(ProtocolType protocol);}
|
||||
Task<List<Device>> GetDevicesByProtocolAsync(ProtocolType protocol);
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public interface IDeviceManagementService
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
Task<bool> DeleteDeviceByIdAsync(int deviceId);
|
||||
Task<bool> DeleteAsync(Device device);
|
||||
|
||||
/// <summary>
|
||||
/// 异步切换设备的激活状态。
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
/// <param name="device">要删除的设备实体。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
public async Task DeleteDeviceAsync(Device device)
|
||||
{
|
||||
await DeleteDeviceByIdAsync(device.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID删除一个设备,包括其关联的变量表和菜单(事务性操作)。
|
||||
@@ -153,19 +146,19 @@ public class DeviceAppService : IDeviceAppService
|
||||
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
||||
/// <exception cref="InvalidOperationException">如果删除设备失败。</exception>
|
||||
/// <exception cref="ApplicationException">如果删除设备时发生其他错误。</exception>
|
||||
public async Task<bool> DeleteDeviceByIdAsync(int deviceId)
|
||||
public async Task<bool> 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);
|
||||
|
||||
|
||||
@@ -45,6 +45,9 @@ public class DeviceManagementService : IDeviceManagementService
|
||||
/// </summary>
|
||||
public async Task<CreateDeviceWithDetailsDto> 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
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
public async Task<bool> DeleteDeviceByIdAsync(int deviceId)
|
||||
public async Task<bool> 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))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user