392 lines
15 KiB
C#
392 lines
15 KiB
C#
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.ToString(),
|
||
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<EquipmentEntity> → List<EquipmentDto>
|
||
/// </summary>
|
||
public static List<EquipmentDto> ToDtoList(this List<EquipmentEntity> entities)
|
||
{
|
||
return entities?.Select(e => e.ToDto()).ToList() ?? new List<EquipmentDto>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// EquipmentDto → EquipmentEntity(入参映射,用于新增/修改)
|
||
/// 安全约定:IsDel/CreateTime/UpdateTime 等服务端管控字段刻意不映射,防止过度绑定(over-binding);
|
||
/// Id 解析失败时为 0(新增时由雪花算法自动生成)
|
||
/// </summary>
|
||
public static EquipmentEntity ToEntity(this EquipmentDto dto)
|
||
{
|
||
if (dto == null) return null;
|
||
return new EquipmentEntity
|
||
{
|
||
Id = long.TryParse(dto.Id, out var id) ? id : 0,
|
||
Code = dto.Code,
|
||
Name = dto.Name,
|
||
CategoryId = long.TryParse(dto.CategoryId, out var categoryId) ? categoryId : 0,
|
||
Type = dto.Type,
|
||
Brand = dto.Brand,
|
||
Model = dto.Model,
|
||
Specifications = dto.Specifications,
|
||
Manufacturer = dto.Manufacturer,
|
||
Supplier = dto.Supplier,
|
||
ImageUrl = dto.ImageUrl,
|
||
QrCode = dto.QrCode,
|
||
Description = dto.Description,
|
||
Department = dto.Department,
|
||
Location = dto.Location,
|
||
ResponsiblePerson = dto.ResponsiblePerson,
|
||
ContactPhone = dto.ContactPhone,
|
||
Custodian = dto.Custodian,
|
||
PurchaseDate = dto.PurchaseDate,
|
||
PurchasePrice = dto.PurchasePrice,
|
||
WarrantyExpireDate = dto.WarrantyExpireDate,
|
||
AcceptanceDate = dto.AcceptanceDate,
|
||
EnableDate = dto.EnableDate,
|
||
ServiceLifeYears = dto.ServiceLifeYears,
|
||
ScrapDate = dto.ScrapDate,
|
||
Status = dto.Status,
|
||
CalibrationStatus = dto.CalibrationStatus,
|
||
LastCalibrationDate = dto.LastCalibrationDate,
|
||
NextCalibrationDate = dto.NextCalibrationDate,
|
||
InspectionDate = dto.InspectionDate,
|
||
NextInspectionDate = dto.NextInspectionDate,
|
||
MaintenanceStatus = dto.MaintenanceStatus,
|
||
LastMaintenanceDate = dto.LastMaintenanceDate,
|
||
NextMaintenanceDate = dto.NextMaintenanceDate,
|
||
Remark = dto.Remark
|
||
};
|
||
}
|
||
#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.ToString(),
|
||
FileName = entity.FileName,
|
||
FileType = entity.FileType,
|
||
FileExt = entity.FileExt,
|
||
FileUrl = entity.FileUrl,
|
||
FileSize = entity.FileSize,
|
||
Uploader = entity.Uploader,
|
||
Remark = entity.Remark
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// List<EquipmentAttachmentEntity> → List<EquipmentAttachmentDto>
|
||
/// </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.ToString(),
|
||
Name = entity.Name,
|
||
Code = entity.Code,
|
||
Level = entity.Level,
|
||
Sort = entity.Sort,
|
||
Remark = entity.Remark
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// List<EquipmentCategoryEntity> → List<EquipmentCategoryDto>
|
||
/// </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.ToString(),
|
||
FromStatus = entity.FromStatus,
|
||
ToStatus = entity.ToStatus,
|
||
ChangeTime = entity.ChangeTime,
|
||
Operator = entity.Operator,
|
||
Remark = entity.Remark
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// List<EquipmentStatusRecordEntity> → List<EquipmentStatusRecordDto>
|
||
/// </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<TemperatureBoxDataEntity> → List<TemperatureBoxDataDto>
|
||
/// </summary>
|
||
public static List<TemperatureBoxDataDto> ToDtoList(this List<TemperatureBoxDataEntity> entities)
|
||
{
|
||
return entities?.Select(e => e.ToDto()).ToList() ?? new List<TemperatureBoxDataDto>();
|
||
}
|
||
#endregion
|
||
|
||
#region 告警规则
|
||
/// <summary>
|
||
/// AlertRuleEntity → AlertRuleDto
|
||
/// </summary>
|
||
public static AlertRuleDto ToDto(this AlertRuleEntity entity)
|
||
{
|
||
if (entity == null) return null;
|
||
return new AlertRuleDto
|
||
{
|
||
Id = entity.Id.ToString(),
|
||
IsDel = entity.IsDel,
|
||
CreateTime = entity.CreateTime,
|
||
RuleName = entity.RuleName,
|
||
DeviceType = entity.DeviceType,
|
||
Metric = entity.Metric,
|
||
Operator = entity.Operator,
|
||
Threshold = entity.Threshold,
|
||
AlarmLevel = entity.AlarmLevel,
|
||
Enabled = entity.Enabled,
|
||
TriggerCount = entity.TriggerCount,
|
||
LastAlarmTime = entity.LastAlarmTime,
|
||
Remark = entity.Remark
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// List<AlertRuleEntity> → List<AlertRuleDto>
|
||
/// </summary>
|
||
public static List<AlertRuleDto> ToDtoList(this List<AlertRuleEntity> entities)
|
||
{
|
||
return entities?.Select(e => e.ToDto()).ToList() ?? new List<AlertRuleDto>();
|
||
}
|
||
#endregion
|
||
|
||
#region 数据转发规则
|
||
/// <summary>
|
||
/// DataForwardRuleEntity → DataForwardRuleDto
|
||
/// </summary>
|
||
public static DataForwardRuleDto ToDto(this DataForwardRuleEntity entity)
|
||
{
|
||
if (entity == null) return null;
|
||
return new DataForwardRuleDto
|
||
{
|
||
Id = entity.Id.ToString(),
|
||
IsDel = entity.IsDel,
|
||
CreateTime = entity.CreateTime,
|
||
ForwardName = entity.ForwardName,
|
||
SourceDeviceType = entity.SourceDeviceType,
|
||
ForwardInterval = entity.ForwardInterval,
|
||
NotifyType = entity.NotifyType,
|
||
TargetAddress = entity.TargetAddress,
|
||
Enabled = entity.Enabled,
|
||
LastForwardTime = entity.LastForwardTime,
|
||
Remark = entity.Remark
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// List<DataForwardRuleEntity> → List<DataForwardRuleDto>
|
||
/// </summary>
|
||
public static List<DataForwardRuleDto> ToDtoList(this List<DataForwardRuleEntity> entities)
|
||
{
|
||
return entities?.Select(e => e.ToDto()).ToList() ?? new List<DataForwardRuleDto>();
|
||
}
|
||
#endregion
|
||
|
||
#region 反向转换 DTO → Entity(前端入参转实体入库用,Id 由 string 转回 long)
|
||
/// <summary>
|
||
/// string Id 安全转 long(空/非法返回 0,新增时由雪花算法自动赋值)
|
||
/// </summary>
|
||
private static long ParseId(string id)
|
||
{
|
||
return long.TryParse(id, out var value) ? value : 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// EquipmentDto → EquipmentEntity
|
||
/// </summary>
|
||
/// <summary>
|
||
/// TemperatureBoxDataDto → TemperatureBoxDataEntity
|
||
/// </summary>
|
||
public static TemperatureBoxDataEntity ToEntity(this TemperatureBoxDataDto dto)
|
||
{
|
||
if (dto == null) return null;
|
||
return new TemperatureBoxDataEntity
|
||
{
|
||
Id = ParseId(dto.Id),
|
||
IsDel = dto.IsDel,
|
||
CreateTime = dto.CreateTime,
|
||
DeviceCode = dto.DeviceCode,
|
||
DeviceType = dto.DeviceType,
|
||
DeviceTemperature = dto.DeviceTemperature,
|
||
DeviceHumidity = dto.DeviceHumidity,
|
||
Status = dto.Status,
|
||
AlarmInfo = dto.AlarmInfo
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// AlertRuleDto → AlertRuleEntity
|
||
/// </summary>
|
||
public static AlertRuleEntity ToEntity(this AlertRuleDto dto)
|
||
{
|
||
if (dto == null) return null;
|
||
return new AlertRuleEntity
|
||
{
|
||
Id = ParseId(dto.Id),
|
||
IsDel = dto.IsDel,
|
||
CreateTime = dto.CreateTime,
|
||
RuleName = dto.RuleName,
|
||
DeviceType = dto.DeviceType,
|
||
Metric = dto.Metric,
|
||
Operator = dto.Operator,
|
||
Threshold = dto.Threshold,
|
||
AlarmLevel = dto.AlarmLevel,
|
||
Enabled = dto.Enabled,
|
||
TriggerCount = dto.TriggerCount,
|
||
LastAlarmTime = dto.LastAlarmTime,
|
||
Remark = dto.Remark
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataForwardRuleDto → DataForwardRuleEntity
|
||
/// </summary>
|
||
public static DataForwardRuleEntity ToEntity(this DataForwardRuleDto dto)
|
||
{
|
||
if (dto == null) return null;
|
||
return new DataForwardRuleEntity
|
||
{
|
||
Id = ParseId(dto.Id),
|
||
IsDel = dto.IsDel,
|
||
CreateTime = dto.CreateTime,
|
||
ForwardName = dto.ForwardName,
|
||
SourceDeviceType = dto.SourceDeviceType,
|
||
ForwardInterval = dto.ForwardInterval,
|
||
NotifyType = dto.NotifyType,
|
||
TargetAddress = dto.TargetAddress,
|
||
Enabled = dto.Enabled,
|
||
LastForwardTime = dto.LastForwardTime,
|
||
Remark = dto.Remark
|
||
};
|
||
}
|
||
#endregion
|
||
}
|
||
}
|