完成新建设备的单元 测试包括,添加变量表,和添加菜单
This commit is contained in:
@@ -8,7 +8,15 @@ namespace DMS.Application.DTOs;
|
||||
public class CreateDeviceDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { 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 CpuType { get; set; }
|
||||
|
||||
public DeviceType DeviceType { get; set; }
|
||||
public string OpcUaServerUrl { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
@@ -7,5 +7,7 @@ public class CreateDeviceWithDetailsDto
|
||||
{
|
||||
public CreateDeviceDto Device { get; set; }
|
||||
public VariableTableDto VariableTable { get; set; }
|
||||
// public MenuBeanDto Menu { get; set; } // 如果需要包含菜单信息
|
||||
|
||||
public MenuBeanDto DeviceMenu { get; set; } // 如果需要包含菜单信息
|
||||
public MenuBeanDto VariableTableMenu { get; set; } // 如果需要包含菜单信息
|
||||
}
|
||||
@@ -7,7 +7,7 @@ public class DeviceDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Protocol { get; set; }
|
||||
public ProtocolType Protocol { get; set; }
|
||||
public string IpAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
public int Rack { get; set; }
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
@@ -9,7 +11,15 @@ public class MenuBeanDto
|
||||
public int? ParentId { get; set; }
|
||||
public string Header { get; set; }
|
||||
public string Icon { get; set; }
|
||||
public string TargetViewKey { get; set; }
|
||||
/// <summary>
|
||||
/// 菜单的类型,例如菜单关联的是设备,还是变量表,或者是MQTT
|
||||
/// </summary>
|
||||
public MenuType MenuType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 菜单关联的数据ID,例如设备Id,变量表Id
|
||||
/// </summary>
|
||||
public int TargetId { get; set; }
|
||||
public string NavigationParameter { get; set; }
|
||||
public int DisplayOrder { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.Profiles;
|
||||
|
||||
@@ -13,10 +12,19 @@ public class MappingProfile : Profile
|
||||
public MappingProfile()
|
||||
{
|
||||
// Device 映射
|
||||
CreateMap<CreateDeviceDto, Device>();
|
||||
CreateMap<UpdateDeviceDto, Device>();
|
||||
CreateMap<CreateDeviceDto, Device>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.VariableTables, opt => opt.Ignore());
|
||||
|
||||
CreateMap<UpdateDeviceDto, Device>()
|
||||
.ForMember(dest => dest.Description, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.VariableTables, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.CpuType, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.DeviceType, opt => opt.Ignore());
|
||||
|
||||
CreateMap<Device, DeviceDto>()
|
||||
.ForMember(dest => dest.Protocol, opt => opt.MapFrom(src => src.Protocol.ToString()));
|
||||
.ForMember(dest => dest.Protocol, opt => opt.MapFrom(src => src.Protocol.ToString()))
|
||||
.ForMember(dest => dest.Status, opt => opt.Ignore());
|
||||
|
||||
// VariableTable 映射
|
||||
CreateMap<VariableTable, VariableTableDto>().ReverseMap();
|
||||
@@ -24,16 +32,16 @@ public class MappingProfile : Profile
|
||||
// Variable 映射
|
||||
CreateMap<Variable, VariableDto>()
|
||||
.ForMember(dest => dest.DataType, opt => opt.MapFrom(src => src.DataType.ToString()))
|
||||
.ForMember(dest => dest.CSharpDataType, opt => opt.MapFrom(src => src.CSharpDataType));
|
||||
.ForMember(dest => dest.CSharpDataType, opt => opt.MapFrom(src => src.CSharpDataType))
|
||||
.ForMember(dest => dest.Address, opt => opt.Ignore());
|
||||
|
||||
// MqttServer 映射
|
||||
CreateMap<MqttServer, MqttServerDto>().ReverseMap();
|
||||
|
||||
// VariableMqttAlias 映射
|
||||
CreateMap<VariableMqttAlias, VariableMqttAliasDto>().ReverseMap();
|
||||
|
||||
// VariableTable 映射
|
||||
CreateMap<VariableTable, VariableTableDto>().ReverseMap();
|
||||
CreateMap<VariableMqttAlias, VariableMqttAliasDto>()
|
||||
.ForMember(dest => dest.MqttServerName, opt => opt.Ignore())
|
||||
.ReverseMap();
|
||||
|
||||
// VariableHistory 映射
|
||||
CreateMap<VariableHistory, VariableHistoryDto>().ReverseMap();
|
||||
@@ -44,4 +52,4 @@ public class MappingProfile : Profile
|
||||
// User 映射
|
||||
CreateMap<User, UserDto>().ReverseMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,37 +49,69 @@ public class DeviceService : IDeviceAppService
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
|
||||
var device = _mapper.Map<Device>(dto.Device);
|
||||
device.IsActive = true; // 默认激活
|
||||
await _repoManager.Devices.AddAsync(device);
|
||||
var addDevice = await _repoManager.Devices.AddAsync(device);
|
||||
if (addDevice == null || addDevice.Id == 0)
|
||||
{
|
||||
throw new InvalidOperationException($"添加设备失败:{addDevice}");
|
||||
}
|
||||
|
||||
MenuBean addDeviceMenu = null;
|
||||
|
||||
// 假设有设备菜单
|
||||
if (dto.DeviceMenu != null)
|
||||
{
|
||||
var deviceMenu = _mapper.Map<MenuBean>(dto.DeviceMenu);
|
||||
deviceMenu.ParentId = 2;
|
||||
deviceMenu.MenuType = MenuType.DeviceMenu;
|
||||
deviceMenu.TargetId = addDevice.Id;
|
||||
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<VariableTable>(dto.VariableTable);
|
||||
variableTable.DeviceId = device.Id; // 关联新设备ID
|
||||
await _repoManager.VariableTables.AddAsync(variableTable);
|
||||
variableTable.Protocol=device.Protocol;
|
||||
var addVariableTable = await _repoManager.VariableTables.AddAsync(variableTable);
|
||||
if (addVariableTable == null || addVariableTable.Id == 0)
|
||||
{
|
||||
throw new InvalidOperationException($"添加设备变量表失败,设备:{device.Name},变量表:{variableTable.Name}");
|
||||
}
|
||||
|
||||
// 假设有设备菜单
|
||||
if (dto.VariableTableMenu != null)
|
||||
{
|
||||
var menu = _mapper.Map<MenuBean>(dto.VariableTableMenu);
|
||||
menu.ParentId = addDeviceMenu.Id;
|
||||
menu.MenuType = MenuType.VariableTableMenu;
|
||||
menu.TargetId = addVariableTable.Id;
|
||||
var addVariableTableMenu= await _repoManager.Menus.AddAsync(menu);
|
||||
if (addVariableTableMenu == null || addVariableTableMenu.Id == 0)
|
||||
{
|
||||
throw new InvalidOperationException($"添加设备变量表菜单失败,变量表:{variableTable.Name},变量表菜单:{menu.Header}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 假设有菜单服务或仓储
|
||||
// if (dto.Menu != null)
|
||||
// {
|
||||
// var menu = _mapper.Map<Menu>(dto.Menu);
|
||||
// menu.TargetId = device.Id;
|
||||
// await _repoManager.Menus.AddAsync(menu);
|
||||
// }
|
||||
|
||||
|
||||
await _repoManager.CommitAsync();
|
||||
|
||||
return device.Id;
|
||||
return addDevice.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
// 可以在此记录日志
|
||||
throw new ApplicationException("创建设备时发生错误,操作已回滚。", ex);
|
||||
throw new ApplicationException($"创建设备时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +125,7 @@ public class DeviceService : IDeviceAppService
|
||||
{
|
||||
throw new ApplicationException($"Device with ID {deviceDto.Id} not found.");
|
||||
}
|
||||
|
||||
_mapper.Map(deviceDto, device);
|
||||
await _repoManager.Devices.UpdateAsync(device);
|
||||
await _repoManager.CommitAsync();
|
||||
@@ -117,6 +150,7 @@ public class DeviceService : IDeviceAppService
|
||||
{
|
||||
throw new ApplicationException($"Device with ID {id} not found.");
|
||||
}
|
||||
|
||||
device.IsActive = !device.IsActive;
|
||||
await _repoManager.Devices.UpdateAsync(device);
|
||||
await _repoManager.CommitAsync();
|
||||
|
||||
29
DMS.Application/Services/InitializeService.cs
Normal file
29
DMS.Application/Services/InitializeService.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using DMS.Core.Interfaces.Repositories;
|
||||
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
public class InitializeService
|
||||
{
|
||||
private readonly IInitializeRepository _repository;
|
||||
|
||||
public InitializeService(IInitializeRepository repository )
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public void InitializeTables()
|
||||
{
|
||||
_repository.InitializeTables();
|
||||
}
|
||||
|
||||
public void InitializeTableIndex()
|
||||
{
|
||||
_repository.InitializeTableIndex();
|
||||
}
|
||||
|
||||
public void InitializeMenus()
|
||||
{
|
||||
_repository.InitializeMenus();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user