diff --git a/DMS.Application/Interfaces/IDeviceAppService.cs b/DMS.Application/Interfaces/IDeviceAppService.cs
index 786817c..d60755e 100644
--- a/DMS.Application/Interfaces/IDeviceAppService.cs
+++ b/DMS.Application/Interfaces/IDeviceAppService.cs
@@ -35,6 +35,11 @@ public interface IDeviceAppService
/// 异步删除一个设备。
///
Task DeleteDeviceAsync(Device device);
+
+ ///
+ /// 异步删除一个设备。
+ ///
+ Task DeleteDeviceByIdAsync(int deviceId);
///
/// 异步切换设备的激活状态。
diff --git a/DMS.Application/Services/DeviceService.cs b/DMS.Application/Services/DeviceService.cs
index 31cdd8b..9347e4f 100644
--- a/DMS.Application/Services/DeviceService.cs
+++ b/DMS.Application/Services/DeviceService.cs
@@ -58,9 +58,9 @@ public class DeviceService : IDeviceAppService
{
throw new InvalidOperationException($"添加设备失败:{addDevice}");
}
-
+
MenuBean addDeviceMenu = null;
-
+
// 假设有设备菜单
if (dto.DeviceMenu != null)
{
@@ -68,20 +68,20 @@ public class DeviceService : IDeviceAppService
deviceMenu.ParentId = 2;
deviceMenu.MenuType = MenuType.DeviceMenu;
deviceMenu.TargetId = addDevice.Id;
- addDeviceMenu=await _repoManager.Menus.AddAsync(deviceMenu);
+ addDeviceMenu = await _repoManager.Menus.AddAsync(deviceMenu);
if (addDeviceMenu == null || addDeviceMenu.Id == 0)
{
throw new InvalidOperationException($"添加设备菜单失败:{addDeviceMenu}");
}
}
-
+
// 假设 CreateDeviceWithDetailsDto 包含了变量表和菜单信息
if (dto.VariableTable != null)
{
var variableTable = _mapper.Map(dto.VariableTable);
variableTable.DeviceId = device.Id; // 关联新设备ID
- variableTable.Protocol=device.Protocol;
+ variableTable.Protocol = device.Protocol;
var addVariableTable = await _repoManager.VariableTables.AddAsync(variableTable);
if (addVariableTable == null || addVariableTable.Id == 0)
{
@@ -95,14 +95,15 @@ public class DeviceService : IDeviceAppService
menu.ParentId = addDeviceMenu.Id;
menu.MenuType = MenuType.VariableTableMenu;
menu.TargetId = addVariableTable.Id;
- var addVariableTableMenu= await _repoManager.Menus.AddAsync(menu);
+ var addVariableTableMenu = await _repoManager.Menus.AddAsync(menu);
if (addVariableTableMenu == null || addVariableTableMenu.Id == 0)
{
- throw new InvalidOperationException($"添加设备变量表菜单失败,变量表:{variableTable.Name},变量表菜单:{menu.Header}");
+ throw new InvalidOperationException(
+ $"添加设备变量表菜单失败,变量表:{variableTable.Name},变量表菜单:{menu.Header}");
}
}
}
-
+
await _repoManager.CommitAsync();
return addDevice.Id;
@@ -136,8 +137,34 @@ public class DeviceService : IDeviceAppService
///
public async Task DeleteDeviceAsync(Device device)
{
- await _repoManager.Devices.DeleteAsync(device);
- await _repoManager.CommitAsync();
+ await DeleteDeviceByIdAsync(device.Id);
+ }
+
+ public async Task DeleteDeviceByIdAsync(int deviceId)
+ {
+ try
+ {
+ await _repoManager.BeginTranAsync();
+ var delRes = await _repoManager.Devices.DeleteByIdAsync(deviceId);
+ if (delRes == 0)
+ {
+ throw new InvalidOperationException($"删除设备失败:设备ID:{deviceId},请检查设备Id是否存在");
+ }
+
+ await _repoManager.VariableTables.DeleteByDeviceIdAsync(deviceId);
+
+ await _repoManager.Menus.DeleteMenuTreeByTargetIdAsync(MenuType.DeviceMenu,deviceId);
+
+ await _repoManager.CommitAsync();
+ return true;
+ }
+ catch (Exception ex)
+ {
+ await _repoManager.RollbackAsync();
+ // 可以在此记录日志
+ throw new ApplicationException($"删除设备时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
+ }
+
}
///
diff --git a/DMS.Application/Services/MenuService.cs b/DMS.Application/Services/MenuService.cs
index e2a1127..9ed7a73 100644
--- a/DMS.Application/Services/MenuService.cs
+++ b/DMS.Application/Services/MenuService.cs
@@ -75,7 +75,7 @@ public class MenuService : IMenuService
try
{
_repoManager.BeginTranAsync();
- await _repoManager.Menus.DeleteAsync(id);
+ await _repoManager.Menus.DeleteByIdAsync(id);
await _repoManager.CommitAsync();
}
catch (Exception ex)
diff --git a/DMS.Application/Services/MqttAliasAppService.cs b/DMS.Application/Services/MqttAliasAppService.cs
index cb44425..e0f072f 100644
--- a/DMS.Application/Services/MqttAliasAppService.cs
+++ b/DMS.Application/Services/MqttAliasAppService.cs
@@ -75,7 +75,7 @@ public class MqttAliasAppService : IMqttAliasAppService
try
{
_repoManager.BeginTranAsync();
- await _repoManager.VariableMqttAliases.DeleteAsync(id);
+ await _repoManager.VariableMqttAliases.DeleteByIdAsync(id);
await _repoManager.CommitAsync();
}
catch (Exception ex)
diff --git a/DMS.Application/Services/MqttAppService.cs b/DMS.Application/Services/MqttAppService.cs
index abfeca8..ba89aec 100644
--- a/DMS.Application/Services/MqttAppService.cs
+++ b/DMS.Application/Services/MqttAppService.cs
@@ -75,7 +75,7 @@ public class MqttAppService : IMqttAppService
try
{
_repoManager.BeginTranAsync();
- await _repoManager.MqttServers.DeleteAsync(id);
+ await _repoManager.MqttServers.DeleteByIdAsync(id);
await _repoManager.CommitAsync();
}
catch (Exception ex)
diff --git a/DMS.Application/Services/VariableAppService.cs b/DMS.Application/Services/VariableAppService.cs
index 36335ae..dc9be4e 100644
--- a/DMS.Application/Services/VariableAppService.cs
+++ b/DMS.Application/Services/VariableAppService.cs
@@ -75,7 +75,7 @@ public class VariableAppService : IVariableAppService
try
{
_repoManager.BeginTranAsync();
- await _repoManager.Variables.DeleteAsync(id);
+ await _repoManager.Variables.DeleteByIdAsync(id);
await _repoManager.CommitAsync();
}
catch (Exception ex)
diff --git a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs
index 520e47a..655da79 100644
--- a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs
+++ b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs
@@ -41,7 +41,7 @@ public interface IBaseRepository where T : class
/// 异步根据ID删除一个实体。
///
/// 要删除的实体的主键ID。
- Task DeleteAsync(int id);
+ Task DeleteByIdAsync(int id);
///
/// 从数据库获取数据。
diff --git a/DMS.Core/Interfaces/Repositories/IMenuRepository.cs b/DMS.Core/Interfaces/Repositories/IMenuRepository.cs
index d02a657..98ce90f 100644
--- a/DMS.Core/Interfaces/Repositories/IMenuRepository.cs
+++ b/DMS.Core/Interfaces/Repositories/IMenuRepository.cs
@@ -1,10 +1,12 @@
+using DMS.Core.Enums;
using DMS.Core.Models;
namespace DMS.Core.Interfaces.Repositories
{
public interface IMenuRepository:IBaseRepository
{
-
+ Task DeleteMenuTreeByIdAsync(int id);
+ Task DeleteMenuTreeByTargetIdAsync(MenuType menuType, int targetId);
}
}
\ No newline at end of file
diff --git a/DMS.Core/Interfaces/Repositories/IVariableTableRepository.cs b/DMS.Core/Interfaces/Repositories/IVariableTableRepository.cs
index aa97445..7317589 100644
--- a/DMS.Core/Interfaces/Repositories/IVariableTableRepository.cs
+++ b/DMS.Core/Interfaces/Repositories/IVariableTableRepository.cs
@@ -5,7 +5,6 @@ namespace DMS.Core.Interfaces.Repositories
{
public interface IVariableTableRepository:IBaseRepository
{
-
-
+ Task DeleteByDeviceIdAsync(int deviceId);
}
}
\ No newline at end of file
diff --git a/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs
index aa85a10..58a4990 100644
--- a/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs
+++ b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs
@@ -38,4 +38,14 @@ public class DeviceServiceTest : BaseServiceTest // 继承基类
// Assert
Assert.NotEqual(0, addedDeviceId);
}
+
+ [Fact]
+ public async Task DeleteDeviceAsyncTest()
+ {
+ // Act
+ var isSuccess = await _deviceService.DeleteDeviceByIdAsync(4);
+
+ // Assert
+ Assert.Equal(isSuccess,true);
+ }
}
\ No newline at end of file
diff --git a/DMS.Infrastructure/Repositories/DeviceRepository.cs b/DMS.Infrastructure/Repositories/DeviceRepository.cs
index b9eb0f7..a71881f 100644
--- a/DMS.Infrastructure/Repositories/DeviceRepository.cs
+++ b/DMS.Infrastructure/Repositories/DeviceRepository.cs
@@ -41,7 +41,7 @@ public class DeviceRepository : BaseRepository, IDeviceRepository
public async Task DeleteAsync(Core.Models.Device model) => await base.DeleteAsync(_mapper.Map(model));
- public async Task DeleteAsync(int id)
+ public async Task DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
diff --git a/DMS.Infrastructure/Repositories/MenuRepository.cs b/DMS.Infrastructure/Repositories/MenuRepository.cs
index ee4084f..c44da85 100644
--- a/DMS.Infrastructure/Repositories/MenuRepository.cs
+++ b/DMS.Infrastructure/Repositories/MenuRepository.cs
@@ -1,5 +1,6 @@
using System.Diagnostics;
using AutoMapper;
+using DMS.Core.Enums;
using DMS.Core.Helper;
using DMS.Core.Interfaces.Repositories;
using DMS.Core.Models;
@@ -54,7 +55,7 @@ public class MenuRepository : BaseRepository, IMenuRepository
public async Task DeleteAsync(MenuBean entity) => await base.DeleteAsync(_mapper.Map(entity));
- public async Task DeleteAsync(int id)
+ public async Task DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
@@ -64,11 +65,45 @@ public class MenuRepository : BaseRepository, IMenuRepository
NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
-
+
+ public async Task DeleteMenuTreeByIdAsync(int id)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ int delConut = 0;
+ var childList = await Db.Queryable()
+ .ToChildListAsync(c => c.ParentId, id);
+ delConut = await Db.Deleteable(childList)
+ .ExecuteCommandAsync();
+ delConut += await Db.Deleteable()
+ .Where(m => m.Id == id)
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return delConut;
+ }
+
+ public async Task DeleteMenuTreeByTargetIdAsync(MenuType menuType, int targetId)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var menu = await Db.Queryable().FirstAsync(m => m.MenuType == menuType && m.TargetId == targetId);
+ if (menu == null) return 0;
+ var childList = await Db.Queryable()
+ .ToChildListAsync(c => c.ParentId, menu.Id);
+ var delConut = await Db.Deleteable(childList)
+ .ExecuteCommandAsync();
+ delConut += await Db.Deleteable()
+ .Where(m => m.Id == menu.Id)
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete {typeof(DbMenu)},TargetId={targetId},耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return delConut;
+ }
+
public new async Task> TakeAsync(int number)
{
var dbList = await base.TakeAsync(number);
return _mapper.Map>(dbList);
-
}
-}
\ No newline at end of file
+}
diff --git a/DMS.Infrastructure/Repositories/MqttServerRepository.cs b/DMS.Infrastructure/Repositories/MqttServerRepository.cs
index 58283cc..4c57b35 100644
--- a/DMS.Infrastructure/Repositories/MqttServerRepository.cs
+++ b/DMS.Infrastructure/Repositories/MqttServerRepository.cs
@@ -43,7 +43,7 @@ public class MqttServerRepository : BaseRepository, IMqttServerRep
public async Task DeleteAsync(MqttServer entity) => await base.DeleteAsync(_mapper.Map(entity));
- public async Task DeleteAsync(int id)
+ public async Task DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
diff --git a/DMS.Infrastructure/Repositories/UserRepository.cs b/DMS.Infrastructure/Repositories/UserRepository.cs
index ab6e56d..8897a21 100644
--- a/DMS.Infrastructure/Repositories/UserRepository.cs
+++ b/DMS.Infrastructure/Repositories/UserRepository.cs
@@ -54,7 +54,7 @@ public class UserRepository : BaseRepository, IUserRepository
public async Task DeleteAsync(User entity) => await base.DeleteAsync(_mapper.Map(entity));
- public async Task DeleteAsync(int id)
+ public async Task DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
diff --git a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs
index d4d2870..519a128 100644
--- a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs
+++ b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs
@@ -46,7 +46,7 @@ public class VariableHistoryRepository : BaseRepository, IVar
public async Task DeleteAsync(VariableHistory entity) => await base.DeleteAsync(_mapper.Map(entity));
- public async Task DeleteAsync(int id)
+ public async Task DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
diff --git a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs
index 3d26a76..14deccf 100644
--- a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs
+++ b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs
@@ -51,7 +51,7 @@ public class VariableMqttAliasRepository : BaseRepository,
public async Task DeleteAsync(VariableMqttAlias entity) => await base.DeleteAsync(_mapper.Map(entity));
- public async Task DeleteAsync(int id)
+ public async Task DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
diff --git a/DMS.Infrastructure/Repositories/VariableRepository.cs b/DMS.Infrastructure/Repositories/VariableRepository.cs
index 6eacf73..c2a3d49 100644
--- a/DMS.Infrastructure/Repositories/VariableRepository.cs
+++ b/DMS.Infrastructure/Repositories/VariableRepository.cs
@@ -127,7 +127,7 @@ public class VariableRepository : BaseRepository, IVariableRepositor
public async Task DeleteAsync(Variable entity) => await base.DeleteAsync(_mapper.Map(entity));
- public async Task DeleteAsync(int id)
+ public async Task DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
diff --git a/DMS.Infrastructure/Repositories/VariableTableRepository.cs b/DMS.Infrastructure/Repositories/VariableTableRepository.cs
index c30a8bd..489b3b7 100644
--- a/DMS.Infrastructure/Repositories/VariableTableRepository.cs
+++ b/DMS.Infrastructure/Repositories/VariableTableRepository.cs
@@ -46,7 +46,7 @@ public class VariableTableRepository : BaseRepository, IVariabl
public async Task DeleteAsync(VariableTable entity) => await base.DeleteAsync(_mapper.Map(entity));
- public async Task DeleteAsync(int id)
+ public async Task DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
@@ -63,4 +63,16 @@ public class VariableTableRepository : BaseRepository, IVariabl
return _mapper.Map>(dbList);
}
+
+ public async Task DeleteByDeviceIdAsync(int deviceId)
+ {
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = await Db.Deleteable()
+ .Where(it => it.DeviceId == deviceId)
+ .ExecuteCommandAsync();
+ stopwatch.Stop();
+ NlogHelper.Info($"Delete VariableTable by DeviceId={deviceId}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
+ return result;
+ }
}
\ No newline at end of file