梳理了所有的Db,DTO,ItemViewModel的属性

This commit is contained in:
2025-07-27 21:08:58 +08:00
parent e509b7de0b
commit 4a56405629
38 changed files with 417 additions and 479 deletions

View File

@@ -1,22 +0,0 @@
using DMS.Core.Enums;
namespace DMS.Application.DTOs;
/// <summary>
/// 用于创建新设备时传输数据的DTO。
/// </summary>
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; }
}

View File

@@ -5,7 +5,7 @@ namespace DMS.Application.DTOs;
/// </summary>
public class CreateDeviceWithDetailsDto
{
public CreateDeviceDto Device { get; set; }
public DeviceDto Device { get; set; }
public VariableTableDto VariableTable { get; set; }
public MenuBeanDto DeviceMenu { get; set; } // 如果需要包含菜单信息

View File

@@ -9,12 +9,18 @@ public class DeviceDto
{
public int Id { get; set; }
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; }
public bool IsRunning { get; set; }
public string Status { get; set; } // "在线", "离线", "连接中..."
public List<VariableTableDto> VariableTables { get; set; }
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
namespace DMS.Application.DTOs;
@@ -21,4 +22,5 @@ public class MqttServerDto
public DateTime? ConnectedAt { get; set; }
public long ConnectionDuration { get; set; }
public string MessageFormat { get; set; }
public List<VariableMqttAliasDto> VariableAliases { get; set; } = new();
}

View File

@@ -10,12 +10,12 @@ public class VariableDto
{
public int Id { get; set; }
public string Name { get; set; }
public string? S7Address { get; set; }
public string? DataValue { get; set; }
public string? DisplayValue { get; set; }
public string S7Address { get; set; }
public string DataValue { get; set; }
public string DisplayValue { get; set; }
public VariableTableDto? VariableTable { get; set; }
public List<VariableMqttAliasDto>? MqttAliases { get; set; }
public SignalType DataType { get; set; }
public SignalType SignalType { get; set; }
public PollLevelType PollLevel { get; set; }
public bool IsActive { get; set; }
public int VariableTableId { get; set; }
@@ -34,4 +34,5 @@ public class VariableDto
public string UpdatedBy { get; set; }
public bool IsModified { get; set; }
public string Description { get; set; }
public OpcUaUpdateType OpcUaUpdateType { get; set; }
}

View File

@@ -13,4 +13,5 @@ public class VariableTableDto
public bool IsActive { get; set; }
public int DeviceId { get; set; }
public ProtocolType Protocol { get; set; }
public List<VariableDto> Variables { get; set; } = new();
}

View File

@@ -24,7 +24,7 @@ public interface IDeviceAppService
/// </summary>
/// <param name="dto">包含设备、变量表和菜单信息的DTO。</param>
/// <returns>新创建设备的ID。</returns>
Task<int> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto);
Task<CreateDeviceWithDetailsDto> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto);
/// <summary>
/// 异步更新一个已存在的设备。

View File

@@ -11,17 +11,16 @@ public class MappingProfile : Profile
{
public MappingProfile()
{
// Device 映射
CreateMap<CreateDeviceDto, Device>()
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.VariableTables, opt => opt.Ignore());
// Device 映射
CreateMap<UpdateDeviceDto, Device>()
// 1. 首先忽略那些永远不应从DTO更新的属性
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.Description, opt => opt.Ignore())
.ForMember(dest => dest.VariableTables, opt => opt.Ignore())
.ForMember(dest => dest.CpuType, opt => opt.Ignore())
.ForMember(dest => dest.IsRunning, opt => opt.Ignore())
.ForMember(dest => dest.DeviceType, opt => opt.Ignore())
// 2. 然后,为每个可空属性单独设置条件
@@ -35,30 +34,21 @@ public class MappingProfile : Profile
.ForMember(dest => dest.IsActive, opt => opt.Condition(src => src.IsActive.HasValue));
CreateMap<Device, DeviceDto>()
.ForMember(dest => dest.Protocol, opt => opt.MapFrom(src => src.Protocol.ToString()))
.ForMember(dest => dest.Status, opt => opt.Ignore());
.ReverseMap();
// VariableTable 映射
CreateMap<VariableTable, VariableTableDto>().ReverseMap();
// 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.S7Address, opt => opt.MapFrom(src => src.S7Address))
.ForMember(dest => dest.DataValue, opt => opt.MapFrom(src => src.DataValue))
.ForMember(dest => dest.DisplayValue, opt => opt.MapFrom(src => src.DisplayValue))
.ForMember(dest => dest.VariableTable, opt => opt.MapFrom(src => src.VariableTable))
.ForMember(dest => dest.MqttAliases, opt => opt.MapFrom(src => src.MqttAliases))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));
.ReverseMap();
CreateMap<VariableDto, Variable>()
.ForMember(dest => dest.S7Address, opt => opt.MapFrom(src => src.S7Address))
.ForMember(dest => dest.VariableTable, opt => opt.Ignore())
.ForMember(dest => dest.MqttAliases, opt => opt.Ignore())
.ForMember(dest => dest.DataValue, opt => opt.Ignore())
.ForMember(dest => dest.DisplayValue, opt => opt.Ignore())
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));
.ReverseMap();
// MqttServer 映射
CreateMap<MqttServer, MqttServerDto>().ReverseMap();

View File

@@ -55,20 +55,22 @@ public class DeviceAppService : IDeviceAppService
/// <returns>新创建设备的ID。</returns>
/// <exception cref="InvalidOperationException">如果添加设备、设备菜单或变量表失败。</exception>
/// <exception cref="ApplicationException">如果创建设备时发生其他错误。</exception>
public async Task<int> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
public async Task<CreateDeviceWithDetailsDto> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
{
try
{
await _repoManager.BeginTranAsync();
var device = _mapper.Map<Device>(dto.Device);
device.IsActive = true; // 默认激活
if (device.Protocol == ProtocolType.OpcUA)
device.OpcUaServerUrl = $"opc.tcp://{device.IpAddress}:{device.Port}";
var addDevice = await _repoManager.Devices.AddAsync(device);
if (addDevice == null || addDevice.Id == 0)
{
throw new InvalidOperationException($"添加设备失败:{addDevice}");
}
_mapper.Map(addDevice,dto.Device);
MenuBean addDeviceMenu = null;
// 假设有设备菜单
@@ -83,6 +85,7 @@ public class DeviceAppService : IDeviceAppService
{
throw new InvalidOperationException($"添加设备菜单失败:{addDeviceMenu}");
}
_mapper.Map(addDeviceMenu,dto.DeviceMenu);
}
@@ -97,6 +100,7 @@ public class DeviceAppService : IDeviceAppService
{
throw new InvalidOperationException($"添加设备变量表失败,设备:{device.Name},变量表:{variableTable.Name}");
}
_mapper.Map(addVariableTable,dto.VariableTable);
// 假设有设备菜单
if (dto.VariableTableMenu != null)
@@ -111,12 +115,13 @@ public class DeviceAppService : IDeviceAppService
throw new InvalidOperationException(
$"添加设备变量表菜单失败,变量表:{variableTable.Name},变量表菜单:{menu.Header}");
}
_mapper.Map(menu,dto.VariableTableMenu);
}
}
await _repoManager.CommitAsync();
return addDevice.Id;
return dto;
}
catch (Exception ex)
{