refactor:完成重构设备的添加,删除,更新。

This commit is contained in:
2025-10-22 14:06:16 +08:00
parent e995ec7207
commit 54d040b45f
76 changed files with 1028 additions and 1161 deletions

View File

@@ -16,6 +16,7 @@ namespace DMS.Application.Services.Database;
public class DeviceAppService : IDeviceAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IAppDataService _appDataService;
private readonly IMapper _mapper;
/// <summary>
@@ -23,9 +24,10 @@ public class DeviceAppService : IDeviceAppService
/// </summary>
/// <param name="repoManager">仓储管理器实例。</param>
/// <param name="mapper">AutoMapper 实例。</param>
public DeviceAppService(IRepositoryManager repoManager, IMapper mapper)
public DeviceAppService(IRepositoryManager repoManager, IAppDataService appStorageService, IMapper mapper)
{
_repoManager = repoManager;
_appDataService = appStorageService;
_mapper = mapper;
}
@@ -39,7 +41,6 @@ public class DeviceAppService : IDeviceAppService
var device = await _repoManager.Devices.GetByIdAsync(id);
return device;
}
/// <summary>
/// 异步获取所有设备数据传输对象列表。
/// </summary>
@@ -86,7 +87,6 @@ public class DeviceAppService : IDeviceAppService
$"添加设备变量表失败,设备:{dto.Device.Name},变量表:{dto?.VariableTable?.Name}");
}
dto.VariableTable.Device = dto.Device;
// 假设有设备菜单
if (dto.VariableTableMenu is not null && dto.VariableTableMenu is not null)
@@ -94,11 +94,6 @@ public class DeviceAppService : IDeviceAppService
dto.VariableTableMenu.ParentId = dto.DeviceMenu.Id; // 关联设备菜单作为父级
dto.VariableTableMenu.TargetId = dto.VariableTable.Id;
dto.VariableTableMenu = await _repoManager.Menus.AddAsync(dto.VariableTableMenu);
if (dto.VariableTableMenu == null || dto.VariableTableMenu.Id == 0)
{
throw new InvalidOperationException(
$"添加设备变量表菜单失败,变量表:{dto.VariableTable.Name},变量表菜单:{dto.VariableTableMenu.Header}");
}
}
}
@@ -123,16 +118,10 @@ public class DeviceAppService : IDeviceAppService
public async Task<int> UpdateDeviceAsync(Device device)
{
await _repoManager.BeginTranAsync();
var existingDevice = await _repoManager.Devices.GetByIdAsync(device.Id);
if (existingDevice == null)
{
throw new ApplicationException($"Device with ID {device.Id} not found.");
}
_mapper.Map(device, existingDevice);
int res = await _repoManager.Devices.UpdateAsync(existingDevice);
var menu = await _repoManager.Menus.GetMenuByTargetIdAsync(MenuType.DeviceMenu, device.Id);
if (menu != null)
int res = await _repoManager.Devices.UpdateAsync(device);
//获取到设备对应的菜单
var menu = _appDataService.Menus.Values.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == device.Id);
if (menu is not null && menu.Header!=device.Name)
{
menu.Header = device.Name;
await _repoManager.Menus.UpdateAsync(menu);
@@ -156,19 +145,17 @@ public class DeviceAppService : IDeviceAppService
{
await _repoManager.BeginTranAsync();
var delRes = await _repoManager.Devices.DeleteAsync(device);
if (delRes == 0)
{
throw new InvalidOperationException($"删除设备失败设备ID:{device.Id}请检查设备Id是否存在");
}
// 删除关联的变量
await _repoManager.Variables.DeleteByVariableTableIdAsync(device.Id);
// 删除关联的变量表
await _repoManager.VariableTables.DeleteAsync(device.VariableTables);
// 删除关联的菜单树
await _repoManager.Menus.DeleteMenuTreeByTargetIdAsync(MenuType.DeviceMenu, device.Id);
var menu= _appDataService.Menus.Values.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == device.Id);
if (menu is not null)
{
// 删除关联的菜单树
await _repoManager.Menus.DeleteAsync(menu);
}
await _repoManager.CommitAsync();
return true;