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 InsertOrUpdate(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 InsertOrUpdate(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(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().Name; return entity; } public static TEntity SetUpdateDefaultValue(this TEntity entity) where TEntity : DEntityBase { entity.UpdatedTime = DateTime.Now; entity.UpdatedUserId = GetUserId(); entity.UpdatedUserName = App.GetService().Name; return entity; } private static string GetUserId() { var userid = App.GetService().UserId; //if (string.IsNullOrWhiteSpace(userid)) // userid = App.GetService().WorkUSERID; return userid; } } }