namespace MoviconWebApi.API.ClearDataQrApi
{
///
/// 数据映射器
///
public static class Mapper
{
///
/// 将数据库实体映射到响应模型
///
/// 数据库实体
/// 响应模型
public static ClearDataQrResponse ToResponse(dynamic entity)
{
if (entity == null) return null;
var response = new ClearDataQrResponse
{
DeviceCode = entity.DeviceCode?.ToString(),
DeviceName = entity.DeviceName?.ToString(),
program_process = entity.program_process?.ToString(),
vehicle_model = entity.vehicle_model?.ToString(),
locomotive_number = entity.locomotive_number?.ToString(),
repair_process = entity.repair_process?.ToString(),
component_name = entity.component_name?.ToString(),
part_position = entity.part_position?.ToString(),
part_num = entity.part_num?.ToString(),
part_qrid = entity.part_qrid?.ToString(),
Test_FrameworkPerModelCleaningDuration = entity.Test_FrameworkPerModelCleaningDuration?.ToString(),
Test_FrameworkPerModelCleaningAgentUsage = entity.Test_FrameworkPerModelCleaningAgentUsage?.ToString(),
Test_FrameworkPerModelWaterUsage = entity.Test_FrameworkPerModelWaterUsage?.ToString(),
WaterTank_Temp = entity.WaterTank_Temp?.ToString(),
AgentTank_Temp = entity.AgentTank_Temp?.ToString(),
SoakingTank1_Temp = entity.SoakingTank1_Temp?.ToString(),
SoakingTank2_Temp = entity.SoakingTank2_Temp?.ToString()
};
// 处理时间字段
if (entity.CreateTime != null)
{
if (entity.CreateTime is DateTime dt)
{
response.CreateTime = dt.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
response.CreateTime = entity.CreateTime.ToString();
}
}
return response;
}
///
/// 格式化日期时间
///
/// 日期时间字符串
/// 格式化后的字符串
public static string FormatDateTime(string dateTimeString)
{
if (string.IsNullOrWhiteSpace(dateTimeString))
return dateTimeString;
if (DateTime.TryParse(dateTimeString, out DateTime dateTime))
{
return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
}
return dateTimeString;
}
///
/// 解析数值类型
///
/// 原始值
/// 数值或null
public static double? ParseDouble(object value)
{
if (value == null) return null;
if (value is double d)
return d;
if (value is float f)
return f;
if (value is decimal dec)
return (double)dec;
if (value is int i)
return i;
if (double.TryParse(value.ToString(), out double result))
return result;
return null;
}
}
}