100 lines
3.5 KiB
C#
100 lines
3.5 KiB
C#
namespace MoviconWebApi.API.ClearDataQrApi
|
|
{
|
|
/// <summary>
|
|
/// 数据映射器
|
|
/// </summary>
|
|
public static class Mapper
|
|
{
|
|
/// <summary>
|
|
/// 将数据库实体映射到响应模型
|
|
/// </summary>
|
|
/// <param name="entity">数据库实体</param>
|
|
/// <returns>响应模型</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 格式化日期时间
|
|
/// </summary>
|
|
/// <param name="dateTimeString">日期时间字符串</param>
|
|
/// <returns>格式化后的字符串</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解析数值类型
|
|
/// </summary>
|
|
/// <param name="value">原始值</param>
|
|
/// <returns>数值或null</returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|