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
+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
}
}