59 lines
2.3 KiB
C#
59 lines
2.3 KiB
C#
using Furion;
|
|
using Furion.DatabaseAccessor;
|
|
using Furion.DatabaseAccessor.Extensions;
|
|
using Furion.LinqBuilder;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Ewide.Core.Util
|
|
{
|
|
public static class DEntityExtensions
|
|
{
|
|
public async static Task<TEntity> InsertOrUpdate<TEntity>(this TEntity entity) where TEntity : DEntityBase, IPrivateEntity, new()
|
|
{
|
|
if (entity.Id.IsNullOrEmpty())
|
|
await entity.SetInsertDefaultValue().InsertAsync();
|
|
else
|
|
await entity.SetUpdateDefaultValue().UpdateExcludeAsync(new[] { nameof(entity.CreatedTime), nameof(entity.CreatedUserId), nameof(entity.CreatedUserName) });
|
|
return entity;
|
|
}
|
|
public async static Task<TEntity> InsertOrUpdate<TEntity>(this TEntity entity, bool isInsert) where TEntity : DEntityBase, IPrivateEntity, new()
|
|
{
|
|
if (isInsert)
|
|
await entity.SetInsertDefaultValue().InsertAsync();
|
|
else
|
|
await entity.SetUpdateDefaultValue().UpdateAsync();
|
|
return entity;
|
|
}
|
|
public static TEntity SetInsertDefaultValue<TEntity>(this TEntity entity) where TEntity : DEntityBase
|
|
{
|
|
if (string.IsNullOrEmpty(entity.Id))
|
|
entity.Id = Guid.NewGuid().ToString();
|
|
if (!entity.CreatedTime.HasValue)
|
|
entity.CreatedTime = DateTime.Now;
|
|
//if (string.IsNullOrEmpty(entity.CreatedUserId))
|
|
entity.CreatedUserId = GetUserId();
|
|
if (string.IsNullOrEmpty(entity.CreatedUserName))
|
|
entity.CreatedUserName = App.GetService<UserManager>().Name;
|
|
return entity;
|
|
}
|
|
public static TEntity SetUpdateDefaultValue<TEntity>(this TEntity entity) where TEntity : DEntityBase
|
|
{
|
|
entity.UpdatedTime = DateTime.Now;
|
|
entity.UpdatedUserId = GetUserId();
|
|
entity.UpdatedUserName = App.GetService<UserManager>().Name;
|
|
return entity;
|
|
}
|
|
private static string GetUserId()
|
|
{
|
|
var userid = App.GetService<UserManager>().UserId;
|
|
//if (string.IsNullOrWhiteSpace(userid))
|
|
// userid = App.GetService<UserManager>().WorkUSERID;
|
|
return userid;
|
|
}
|
|
}
|
|
}
|