47 lines
2.1 KiB
C#
Raw Normal View History

2023-05-31 07:46:59 +08:00

2023-07-23 16:28:56 +08:00
using System.Reflection;
2021-08-23 16:57:25 +08:00
namespace ZR.Admin.WebApi.Extensions
{
public static class EntityExtension
{
2022-03-06 14:26:05 +08:00
public static TSource ToCreate<TSource>(this TSource source, HttpContext? context = null)
2021-08-23 16:57:25 +08:00
{
2022-03-06 14:26:05 +08:00
var types = source?.GetType();
2022-08-15 20:22:19 +08:00
if (types == null) return source;
2023-07-23 16:28:56 +08:00
BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
types.GetProperty("CreateTime", flag)?.SetValue(source, DateTime.Now, null);
2023-08-09 15:16:45 +08:00
types.GetProperty("CreatedTime", flag)?.SetValue(source, DateTime.Now, null);
2023-07-23 16:28:56 +08:00
types.GetProperty("AddTime", flag)?.SetValue(source, DateTime.Now, null);
if (context != null)
{
types.GetProperty("CreateBy", flag)?.SetValue(source, context.GetName(), null);
types.GetProperty("Create_by", flag)?.SetValue(source, context.GetName(), null);
types.GetProperty("CreatedBy", flag)?.SetValue(source, context.GetName(), null);
types.GetProperty("UserId", flag)?.SetValue(source, context.GetUId(), null);
}
2021-08-23 16:57:25 +08:00
return source;
}
2022-08-15 20:22:19 +08:00
public static TSource ToUpdate<TSource>(this TSource source, HttpContext? context = null)
{
2022-08-15 20:22:19 +08:00
var types = source?.GetType();
if (types == null) return source;
2023-07-23 16:28:56 +08:00
BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
2023-07-23 16:28:56 +08:00
types.GetProperty("UpdateTime", flag)?.SetValue(source, DateTime.Now, null);
types.GetProperty("Update_time", flag)?.SetValue(source, DateTime.Now, null);
2023-08-09 15:16:45 +08:00
types.GetProperty("UpdatedTime", flag)?.SetValue(source, DateTime.Now, null);
if (context != null)
{
types.GetProperty("UpdateBy", flag)?.SetValue(source, context.GetName(), null);
types.GetProperty("Update_by", flag)?.SetValue(source, context.GetName(), null);
types.GetProperty("UpdatedBy", flag)?.SetValue(source, context.GetName(), null);
}
return source;
}
2021-08-23 16:57:25 +08:00
}
}