using System;
|
using System.Collections.Generic;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Reflection;
|
using System.Threading.Tasks;
|
|
namespace CMS.Plugin.HIAWms.Domain.Shared.Util
|
{
|
/// <summary>
|
/// 导出帮助类.
|
/// </summary>
|
public static class ExportHelper
|
{
|
// 通用转换方法(兼容 Func<T, Dictionary<string, object>>)
|
public static Dictionary<string, object> ConvertToExportData<T>(T entity)
|
{
|
var dict = new Dictionary<string, object>();
|
if (entity == null) return dict;
|
|
foreach (var prop in typeof(T).GetProperties())
|
{
|
var value = prop.GetValue(entity);
|
dict[prop.Name] = FormatValue(value);
|
}
|
return dict;
|
}
|
|
// 批量转换方法
|
public static List<Dictionary<string, object>> ConvertListToExportData<T>(IEnumerable<T> list)
|
{
|
return list.Select(ConvertToExportData).ToList();
|
}
|
|
private static object FormatValue(object value)
|
{
|
if (value == null) return string.Empty;
|
|
// 处理枚举
|
if (value is Enum enumValue)
|
return GetEnumDescriptionUtil.GetEnumDescription(enumValue);
|
|
// 处理布尔
|
if (value is bool boolValue)
|
return boolValue ? "是" : "否";
|
|
return value;
|
}
|
|
private static string GetEnumDescription(Enum value)
|
{
|
var fieldInfo = value.GetType().GetField(value.ToString());
|
var description = fieldInfo?
|
.GetCustomAttributes(typeof(DescriptionAttribute), false)
|
.FirstOrDefault() as DescriptionAttribute;
|
return description?.Description ?? value.ToString();
|
}
|
}
|
}
|