automapper自动映射(传入前端数据ID必须转string)

This commit is contained in:
“hsc”
2026-08-28 13:15:44 +08:00
parent 9e78aa7769
commit 01f51fa3b4
13 changed files with 493 additions and 10 deletions
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Service.Interface; using Service.Interface;
using Model.Entity.Inspection;
namespace WebAPI.Controllers namespace WebAPI.Controllers
{ {
+1
View File
@@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="16.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup> </ItemGroup>
+1 -1
View File
@@ -1,4 +1,4 @@
using Model.Entity; using Model.Entity.Inspection;
using ORM; using ORM;
namespace WebAPI.Services namespace WebAPI.Services
+43
View File
@@ -0,0 +1,43 @@
using Model.Entity.Asset;
namespace Model.Dto.Asset
{
/// <summary>
/// 设备附件 DTOId 由 long 改为 string,避免前端精度丢失)
/// </summary>
public class EquipmentAttachmentDto
{
/// <summary>主键 Idlong → string</summary>
public string Id { get; set; }
/// <summary>删除状态(0、未删除;1、已删除)</summary>
public byte IsDel { get; set; }
/// <summary>创建时间</summary>
public DateTime? CreateTime { get; set; }
/// <summary>设备Id(关联 EquipmentEntity.Id</summary>
public long EquipmentId { get; set; }
/// <summary>文件名称</summary>
public string? FileName { get; set; }
/// <summary>文件类型(说明书/合格证/校准证书等)</summary>
public AttachmentTypeEnum FileType { get; set; }
/// <summary>文件扩展名(如 .pdf、.jpg</summary>
public string? FileExt { get; set; }
/// <summary>文件存储地址</summary>
public string? FileUrl { get; set; }
/// <summary>文件大小(byte</summary>
public long FileSize { get; set; }
/// <summary>上传人</summary>
public string? Uploader { get; set; }
/// <summary>备注</summary>
public string? Remark { get; set; }
}
}
+35
View File
@@ -0,0 +1,35 @@
namespace Model.Dto.Asset
{
/// <summary>
/// 设备分类 DTOId 由 long 改为 string,避免前端精度丢失)
/// </summary>
public class EquipmentCategoryDto
{
/// <summary>主键 Idlong → string</summary>
public string Id { get; set; }
/// <summary>删除状态(0、未删除;1、已删除)</summary>
public byte IsDel { get; set; }
/// <summary>创建时间</summary>
public DateTime? CreateTime { get; set; }
/// <summary>父级分类Id0表示顶级)</summary>
public long ParentId { get; set; }
/// <summary>分类名称</summary>
public string? Name { get; set; }
/// <summary>分类编码</summary>
public string? Code { get; set; }
/// <summary>层级(1级、2级……)</summary>
public int Level { get; set; }
/// <summary>排序号</summary>
public int Sort { get; set; }
/// <summary>备注</summary>
public string? Remark { get; set; }
}
}
+130
View File
@@ -0,0 +1,130 @@
using Model.Entity.Asset;
namespace Model.Dto.Asset
{
/// <summary>
/// 设备台账 DTOId 由 long 改为 string,避免前端精度丢失)
/// </summary>
public class EquipmentDto
{
/// <summary>主键 Idlong → string</summary>
public string Id { get; set; }
/// <summary>删除状态(0、未删除;1、已删除)</summary>
public byte IsDel { get; set; }
/// <summary>创建时间</summary>
public DateTime? CreateTime { get; set; }
/// <summary>设备编号(唯一)</summary>
public string? Code { get; set; }
/// <summary>设备名称</summary>
public string? Name { get; set; }
/// <summary>设备分类Id(关联 EquipmentCategoryEntity.Id</summary>
public long CategoryId { get; set; }
/// <summary>设备类型</summary>
public string? Type { get; set; }
/// <summary>设备品牌</summary>
public string? Brand { get; set; }
/// <summary>设备型号</summary>
public string? Model { get; set; }
/// <summary>规格参数</summary>
public string? Specifications { get; set; }
/// <summary>制造商</summary>
public string? Manufacturer { get; set; }
/// <summary>供应商</summary>
public string? Supplier { get; set; }
/// <summary>设备图片地址</summary>
public string? ImageUrl { get; set; }
/// <summary>二维码编号(一物一码)</summary>
public string? QrCode { get; set; }
/// <summary>设备描述</summary>
public string? Description { get; set; }
/// <summary>使用部门名称</summary>
public string? Department { get; set; }
/// <summary>存放位置</summary>
public string? Location { get; set; }
/// <summary>责任人</summary>
public string? ResponsiblePerson { get; set; }
/// <summary>责任人联系电话</summary>
public string? ContactPhone { get; set; }
/// <summary>保管人</summary>
public string? Custodian { get; set; }
/// <summary>购置日期</summary>
public DateTime? PurchaseDate { get; set; }
/// <summary>购置价格</summary>
public decimal? PurchasePrice { get; set; }
/// <summary>保修到期日期</summary>
public DateTime? WarrantyExpireDate { get; set; }
/// <summary>验收日期</summary>
public DateTime? AcceptanceDate { get; set; }
/// <summary>启用日期</summary>
public DateTime? EnableDate { get; set; }
/// <summary>使用年限(年)</summary>
public int? ServiceLifeYears { get; set; }
/// <summary>报废日期</summary>
public DateTime? ScrapDate { get; set; }
/// <summary>设备当前状态</summary>
public EquipmentStatusEnum Status { get; set; }
/// <summary>校准状态(正常/超期)</summary>
public ComplianceStatusEnum CalibrationStatus { get; set; }
/// <summary>上次校准日期</summary>
public DateTime? LastCalibrationDate { get; set; }
/// <summary>下次校准到期日期</summary>
public DateTime? NextCalibrationDate { get; set; }
/// <summary>检定日期</summary>
public DateTime? InspectionDate { get; set; }
/// <summary>下次检定到期日期</summary>
public DateTime? NextInspectionDate { get; set; }
/// <summary>保养状态(正常/超期)</summary>
public ComplianceStatusEnum MaintenanceStatus { get; set; }
/// <summary>上次保养日期</summary>
public DateTime? LastMaintenanceDate { get; set; }
/// <summary>下次保养到期日期</summary>
public DateTime? NextMaintenanceDate { get; set; }
/// <summary>备注</summary>
public string? Remark { get; set; }
/// <summary>创建人</summary>
public string? CreateBy { get; set; }
/// <summary>更新人</summary>
public string? UpdateBy { get; set; }
/// <summary>更新时间</summary>
public DateTime? UpdateTime { get; set; }
}
}
@@ -0,0 +1,37 @@
using Model.Entity.Asset;
namespace Model.Dto.Asset
{
/// <summary>
/// 设备状态记录 DTOId 由 long 改为 string,避免前端精度丢失)
/// </summary>
public class EquipmentStatusRecordDto
{
/// <summary>主键 Idlong → string</summary>
public string Id { get; set; }
/// <summary>删除状态(0、未删除;1、已删除)</summary>
public byte IsDel { get; set; }
/// <summary>创建时间</summary>
public DateTime? CreateTime { get; set; }
/// <summary>设备Id(关联 EquipmentEntity.Id</summary>
public long EquipmentId { get; set; }
/// <summary>变更前状态</summary>
public EquipmentStatusEnum FromStatus { get; set; }
/// <summary>变更后状态</summary>
public EquipmentStatusEnum ToStatus { get; set; }
/// <summary>状态变更时间</summary>
public DateTime ChangeTime { get; set; }
/// <summary>操作人</summary>
public string? Operator { get; set; }
/// <summary>变更原因/备注</summary>
public string? Remark { get; set; }
}
}
@@ -0,0 +1,35 @@
namespace Model.Dto.Inspection
{
/// <summary>
/// 温度箱看板数据 DTOId 由 long 改为 string,避免前端精度丢失)
/// </summary>
public class TemperatureBoxDataDto
{
/// <summary>主键 Idlong → string</summary>
public string Id { get; set; }
/// <summary>删除状态(0、未删除;1、已删除)</summary>
public byte IsDel { get; set; }
/// <summary>采集时间</summary>
public DateTime? CreateTime { get; set; }
/// <summary>设备标识</summary>
public string DeviceCode { get; set; }
/// <summary>设备类型</summary>
public string DeviceType { get; set; }
/// <summary>设备温度(℃)</summary>
public double? DeviceTemperature { get; set; }
/// <summary>设备湿度(%</summary>
public double? DeviceHumidity { get; set; }
/// <summary>设备状态</summary>
public string Status { get; set; }
/// <summary>设备告警信息</summary>
public string AlarmInfo { get; set; }
}
}
@@ -1,7 +1,7 @@
using SqlSugar; using SqlSugar;
using System; using System;
namespace Model.Entity namespace Model.Entity.Inspection
{ {
/// <summary> /// <summary>
/// 温度箱看板数据实体 /// 温度箱看板数据实体
+198
View File
@@ -0,0 +1,198 @@
using Model.Dto.Asset;
using Model.Dto.Inspection;
using Model.Entity.Asset;
using Model.Entity.Inspection;
namespace Model.Mapper
{
/// <summary>
/// Entity → DTO 映射器(统一在此处做实体到传输对象的转换)
/// 核心转换:雪花 Id 由 long 转 string,避免前端 JavaScript 大数精度丢失
/// 使用方法 给查询的数据后面加上.ToDtoList()或者.ToDto()
/// </summary>
public static class EntityMapper
{
#region
/// <summary>
/// EquipmentEntity → EquipmentDto
/// </summary>
public static EquipmentDto ToDto(this EquipmentEntity entity)
{
if (entity == null) return null;
return new EquipmentDto
{
Id = entity.Id.ToString(),
IsDel = entity.IsDel,
CreateTime = entity.CreateTime,
Code = entity.Code,
Name = entity.Name,
CategoryId = entity.CategoryId,
Type = entity.Type,
Brand = entity.Brand,
Model = entity.Model,
Specifications = entity.Specifications,
Manufacturer = entity.Manufacturer,
Supplier = entity.Supplier,
ImageUrl = entity.ImageUrl,
QrCode = entity.QrCode,
Description = entity.Description,
Department = entity.Department,
Location = entity.Location,
ResponsiblePerson = entity.ResponsiblePerson,
ContactPhone = entity.ContactPhone,
Custodian = entity.Custodian,
PurchaseDate = entity.PurchaseDate,
PurchasePrice = entity.PurchasePrice,
WarrantyExpireDate = entity.WarrantyExpireDate,
AcceptanceDate = entity.AcceptanceDate,
EnableDate = entity.EnableDate,
ServiceLifeYears = entity.ServiceLifeYears,
ScrapDate = entity.ScrapDate,
Status = entity.Status,
CalibrationStatus = entity.CalibrationStatus,
LastCalibrationDate = entity.LastCalibrationDate,
NextCalibrationDate = entity.NextCalibrationDate,
InspectionDate = entity.InspectionDate,
NextInspectionDate = entity.NextInspectionDate,
MaintenanceStatus = entity.MaintenanceStatus,
LastMaintenanceDate = entity.LastMaintenanceDate,
NextMaintenanceDate = entity.NextMaintenanceDate,
Remark = entity.Remark,
CreateBy = entity.CreateBy,
UpdateBy = entity.UpdateBy,
UpdateTime = entity.UpdateTime
};
}
/// <summary>
/// List&lt;EquipmentEntity&gt; → List&lt;EquipmentDto&gt;
/// </summary>
public static List<EquipmentDto> ToDtoList(this List<EquipmentEntity> entities)
{
return entities?.Select(e => e.ToDto()).ToList() ?? new List<EquipmentDto>();
}
#endregion
#region
/// <summary>
/// EquipmentAttachmentEntity → EquipmentAttachmentDto
/// </summary>
public static EquipmentAttachmentDto ToDto(this EquipmentAttachmentEntity entity)
{
if (entity == null) return null;
return new EquipmentAttachmentDto
{
Id = entity.Id.ToString(),
IsDel = entity.IsDel,
CreateTime = entity.CreateTime,
EquipmentId = entity.EquipmentId,
FileName = entity.FileName,
FileType = entity.FileType,
FileExt = entity.FileExt,
FileUrl = entity.FileUrl,
FileSize = entity.FileSize,
Uploader = entity.Uploader,
Remark = entity.Remark
};
}
/// <summary>
/// List&lt;EquipmentAttachmentEntity&gt; → List&lt;EquipmentAttachmentDto&gt;
/// </summary>
public static List<EquipmentAttachmentDto> ToDtoList(this List<EquipmentAttachmentEntity> entities)
{
return entities?.Select(e => e.ToDto()).ToList() ?? new List<EquipmentAttachmentDto>();
}
#endregion
#region
/// <summary>
/// EquipmentCategoryEntity → EquipmentCategoryDto
/// </summary>
public static EquipmentCategoryDto ToDto(this EquipmentCategoryEntity entity)
{
if (entity == null) return null;
return new EquipmentCategoryDto
{
Id = entity.Id.ToString(),
IsDel = entity.IsDel,
CreateTime = entity.CreateTime,
ParentId = entity.ParentId,
Name = entity.Name,
Code = entity.Code,
Level = entity.Level,
Sort = entity.Sort,
Remark = entity.Remark
};
}
/// <summary>
/// List&lt;EquipmentCategoryEntity&gt; → List&lt;EquipmentCategoryDto&gt;
/// </summary>
public static List<EquipmentCategoryDto> ToDtoList(this List<EquipmentCategoryEntity> entities)
{
return entities?.Select(e => e.ToDto()).ToList() ?? new List<EquipmentCategoryDto>();
}
#endregion
#region
/// <summary>
/// EquipmentStatusRecordEntity → EquipmentStatusRecordDto
/// </summary>
public static EquipmentStatusRecordDto ToDto(this EquipmentStatusRecordEntity entity)
{
if (entity == null) return null;
return new EquipmentStatusRecordDto
{
Id = entity.Id.ToString(),
IsDel = entity.IsDel,
CreateTime = entity.CreateTime,
EquipmentId = entity.EquipmentId,
FromStatus = entity.FromStatus,
ToStatus = entity.ToStatus,
ChangeTime = entity.ChangeTime,
Operator = entity.Operator,
Remark = entity.Remark
};
}
/// <summary>
/// List&lt;EquipmentStatusRecordEntity&gt; → List&lt;EquipmentStatusRecordDto&gt;
/// </summary>
public static List<EquipmentStatusRecordDto> ToDtoList(this List<EquipmentStatusRecordEntity> entities)
{
return entities?.Select(e => e.ToDto()).ToList() ?? new List<EquipmentStatusRecordDto>();
}
#endregion
#region
/// <summary>
/// TemperatureBoxDataEntity → TemperatureBoxDataDto
/// </summary>
public static TemperatureBoxDataDto ToDto(this TemperatureBoxDataEntity entity)
{
if (entity == null) return null;
return new TemperatureBoxDataDto
{
Id = entity.Id.ToString(),
IsDel = entity.IsDel,
CreateTime = entity.CreateTime,
DeviceCode = entity.DeviceCode,
DeviceType = entity.DeviceType,
DeviceTemperature = entity.DeviceTemperature,
DeviceHumidity = entity.DeviceHumidity,
Status = entity.Status,
AlarmInfo = entity.AlarmInfo
};
}
/// <summary>
/// List&lt;TemperatureBoxDataEntity&gt; → List&lt;TemperatureBoxDataDto&gt;
/// </summary>
public static List<TemperatureBoxDataDto> ToDtoList(this List<TemperatureBoxDataEntity> entities)
{
return entities?.Select(e => e.ToDto()).ToList() ?? new List<TemperatureBoxDataDto>();
}
#endregion
}
}
+9 -6
View File
@@ -6,14 +6,17 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="DTOS\**" />
<EmbeddedResource Remove="DTOS\**" />
<None Remove="DTOS\**" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="SqlSugarCore" Version="5.1.4.210" /> <PackageReference Include="SqlSugarCore" Version="5.1.4.210" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Dto\Asset\" />
<Folder Include="Dto\Config\" />
<Folder Include="Dto\Inspection\" />
<Folder Include="Dto\System\" />
<Folder Include="Entity\System\" />
<Folder Include="Entity\Config\" />
</ItemGroup>
</Project> </Project>
@@ -1,5 +1,5 @@
using Model; using Model;
using Model.Entity; using Model.Entity.Inspection;
using ORM; using ORM;
using Service.Interface; using Service.Interface;
using SqlSugar; using SqlSugar;
@@ -1,5 +1,5 @@
using Model; using Model;
using Model.Entity; using Model.Entity.Inspection;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;