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);
|
|
|
|
|
|
types.GetProperty("AddTime", flag)?.SetValue(source, DateTime.Now, null);
|
|
|
|
|
|
types.GetProperty("CreateBy", flag)?.SetValue(source, context.GetName(), null);
|
|
|
|
|
|
types.GetProperty("Create_by", 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)
|
2021-09-18 16:10:34 +08:00
|
|
|
|
{
|
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;
|
2021-09-18 16:10:34 +08:00
|
|
|
|
|
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);
|
|
|
|
|
|
types.GetProperty("UpdateBy", flag)?.SetValue(source, context.GetName(), null);
|
|
|
|
|
|
types.GetProperty("Update_by", flag)?.SetValue(source, context.GetName(), null);
|
2023-03-18 07:58:39 +08:00
|
|
|
|
|
2021-09-18 16:10:34 +08:00
|
|
|
|
return source;
|
|
|
|
|
|
}
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|