完成编辑设备

This commit is contained in:
2025-07-29 20:02:09 +08:00
parent 61807bfc65
commit 3b5ecef895
8 changed files with 95 additions and 109 deletions

View File

@@ -1,19 +0,0 @@
using DMS.Core.Enums;
namespace DMS.Application.DTOs;
/// <summary>
/// 用于更新设备时传输数据的DTO。
/// </summary>
public class UpdateDeviceDto
{
public int Id { get; set; }
public string? Name { 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? OpcUaServerUrl { get; set; }
public bool? IsActive { get; set; }
}

View File

@@ -29,7 +29,7 @@ public interface IDeviceAppService
/// <summary>
/// 异步更新一个已存在的设备。
/// </summary>
Task<int> UpdateDeviceAsync(UpdateDeviceDto deviceDto);
Task<int> UpdateDeviceAsync(DeviceDto deviceDto);
/// <summary>
/// 异步删除一个设备。

View File

@@ -13,25 +13,25 @@ public class MappingProfile : Profile
{
// 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. 然后,为每个可空属性单独设置条件
.ForMember(dest => dest.Name, opt => opt.Condition(src => src.Name != null))
.ForMember(dest => dest.Protocol, opt => opt.Condition(src => src.Protocol.HasValue))
.ForMember(dest => dest.IpAddress, opt => opt.Condition(src => src.IpAddress != null))
.ForMember(dest => dest.Port, opt => opt.Condition(src => src.Port.HasValue))
.ForMember(dest => dest.Rack, opt => opt.Condition(src => src.Rack.HasValue))
.ForMember(dest => dest.Slot, opt => opt.Condition(src => src.Slot.HasValue))
.ForMember(dest => dest.OpcUaServerUrl, opt => opt.Condition(src => src.OpcUaServerUrl != null))
.ForMember(dest => dest.IsActive, opt => opt.Condition(src => src.IsActive.HasValue));
// // 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. 然后,为每个可空属性单独设置条件
// .ForMember(dest => dest.Name, opt => opt.Condition(src => src.Name != null))
// .ForMember(dest => dest.Protocol, opt => opt.Condition(src => src.Protocol.HasValue))
// .ForMember(dest => dest.IpAddress, opt => opt.Condition(src => src.IpAddress != null))
// .ForMember(dest => dest.Port, opt => opt.Condition(src => src.Port.HasValue))
// .ForMember(dest => dest.Rack, opt => opt.Condition(src => src.Rack.HasValue))
// .ForMember(dest => dest.Slot, opt => opt.Condition(src => src.Slot.HasValue))
// .ForMember(dest => dest.OpcUaServerUrl, opt => opt.Condition(src => src.OpcUaServerUrl != null))
// .ForMember(dest => dest.IsActive, opt => opt.Condition(src => src.IsActive.HasValue));
CreateMap<Device, DeviceDto>()
.ReverseMap();

View File

@@ -135,7 +135,7 @@ public class DeviceAppService : IDeviceAppService
/// <param name="deviceDto">要更新的设备数据传输对象。</param>
/// <returns>受影响的行数。</returns>
/// <exception cref="ApplicationException">如果找不到设备。</exception>
public async Task<int> UpdateDeviceAsync(UpdateDeviceDto deviceDto)
public async Task<int> UpdateDeviceAsync(DeviceDto deviceDto)
{
await _repoManager.BeginTranAsync();
var device = await _repoManager.Devices.GetByIdAsync(deviceDto.Id);
@@ -146,6 +146,12 @@ public class DeviceAppService : IDeviceAppService
_mapper.Map(deviceDto, device);
int res=await _repoManager.Devices.UpdateAsync(device);
var menu=await _repoManager.Menus.GetMenuByTargetIdAsync(MenuType.DeviceMenu, deviceDto.Id);
if (menu != null)
{
menu.Header = device.Name;
await _repoManager.Menus.UpdateAsync(menu);
}
await _repoManager.CommitAsync();
return res;
}