From 59833ddbf47ed028462e1b089b46bf7c6f7518e3 Mon Sep 17 00:00:00 2001 From: sunpengfei <i@angelzzz.com> Date: 星期二, 05 八月 2025 18:31:38 +0800 Subject: [PATCH] feat:字典开发 --- FlexJobApi.Core/Utils/DbUtils/DbUtils.cs | 137 +++++++++++++++++++++++++++++++++------------ 1 files changed, 101 insertions(+), 36 deletions(-) diff --git a/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs b/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs index 1547067..ecaf344 100644 --- a/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs +++ b/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs @@ -1,55 +1,108 @@ 锘縰sing Furion; using Furion.DatabaseAccessor; using Furion.DistributedIDGenerator; +using Furion.FriendlyException; +using Mapster; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace FlexJobApi.Core { + /// <summary> + /// 鏁版嵁搴撳伐鍏� + /// </summary> public static class DbUtils { /// <summary> - /// 鐢熸垚瀹炰綋娉ㄩ噴 + /// 鍒犻櫎鏁版嵁 + /// </summary> + /// <typeparam name="TEntity"></typeparam> + /// <param name="request"></param> + /// <param name="query"></param> + /// <param name="cancellationToken"></param> + /// <returns></returns> + public static async Task<int> DeleteData<TEntity>(this DeleteDataCommand request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = null, CancellationToken cancellationToken = default) + where TEntity : CommonEntity, new() + { + var rep = Db.GetRepository<TEntity>(); + var q = rep.AsQueryable(); + if (query != null) q = query(q); + var entities = await q + .Where(it => request.Ids.Contains(it.Id)) + .ToListAsync(cancellationToken); + return entities.Any() + ? await rep.DeleteNowAsync(entities, cancellationToken) + : 0; + } + + /// <summary> + /// 淇濆瓨鏁版嵁 + /// </summary> + /// <typeparam name="TEntity"></typeparam> + /// <typeparam name="TRequest"></typeparam> + /// <param name="request"></param> + /// <param name="checkExist"></param> + /// <param name="cancellationToken"></param> + /// <returns></returns> + public static async Task<Guid> SaveData<TEntity, TRequest>(this TRequest request, Func<IQueryable<TEntity>, TEntity, TRequest, bool> checkExist = null, CancellationToken cancellationToken = default) + where TEntity : CommonEntity, new() + where TRequest : SaveDataCommand, new() + { + var xmlDoc = await XmlDocUtils.GetXmlDocAsync(); + var summary = await typeof(TEntity).GetSummary(xmlDoc); + var rep = Db.GetRepository<TEntity>(); + if (request.Id.HasValue) + { + var entity = await rep.AsQueryable().FirstOrDefaultAsync(it => it.Id == request.Id, cancellationToken); + if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, $"璇summary ?? "淇℃伅"}"); + if (checkExist != null && checkExist(rep.AsQueryable().AsNoTracking(), entity, request)) throw Oops.Oh(EnumErrorCodeType.s405, $"璇summary ?? "淇℃伅"}"); + request.Adapt(entity); + await rep.UpdateAsync(entity); + return entity.Id; + } + else + { + var entity = new TEntity(); + if (checkExist != null && checkExist(rep.AsQueryable().AsNoTracking(), entity, request)) throw Oops.Oh(EnumErrorCodeType.s405, $"璇summary ?? "淇℃伅"}"); + request.Adapt(entity); + await rep.InsertAsync(entity); + return entity.Id; + } + } + + /// <summary> + /// 鐢熸垚瀹炰綋 /// </summary> /// <param name="modelBuilder"></param> /// <param name="dbContextLocator"></param> /// <returns></returns> - public static async Task BuildEntityComment(ModelBuilder modelBuilder, Type dbContextLocator = null) + public static async Task BuildEntity(ModelBuilder modelBuilder, Type dbContextLocator = null) { var xmlDoc = await XmlDocUtils.GetXmlDocAsync(); - var entityTypes = App.Assemblies - .Where(it => it.FullName.Contains("FlexJob")) - .SelectMany(it => it.GetTypes()) - .Where(it => - it.IsClass - && !it.IsAbstract - && typeof(IPrivateEntity).IsAssignableFrom(it) - && (dbContextLocator == null - ? it.BaseType == typeof(CommonEntity) - : it.BaseType.GenericTypeArguments.Any(it => it == dbContextLocator))) - .ToList(); - foreach (var entityType in entityTypes) + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { Console.WriteLine($"姝e湪鐢熸垚琛細{entityType.Name}"); // 鑾峰彇瀹炰綋绫荤殑XML娉ㄩ噴锛屽苟璁剧疆涓鸿〃娉ㄩ噴 - var entityBuilder = modelBuilder.Entity(entityType); - string typeComment = (await entityType.GetXmlDocMemberAsync(xmlDoc))?.Summary; + var entityBuilder = modelBuilder.Entity(entityType.ClrType); + string typeComment = (await entityType.ClrType.GetXmlDocMemberAsync(xmlDoc))?.Summary; if (!string.IsNullOrEmpty(typeComment)) { Console.WriteLine($"姝e湪鐢熸垚琛ㄦ敞閲婏細{entityType.Name}-{typeComment}"); entityBuilder.ToTable(it => it.HasComment(typeComment)); } // 鑾峰彇瀹炰綋灞炴�х殑XML娉ㄩ噴锛屽苟璁剧疆涓哄垪娉ㄩ噴 - var properties = entityType.GetProperties() + var properties = entityType.ClrType.GetProperties() .Where(p => p.CanRead && p.CanWrite @@ -64,9 +117,29 @@ entityBuilder.Property(property.Name).HasComment(propComment); } } + + if (typeof(CommonEntity).IsAssignableFrom(entityType.ClrType)) + { + // 鏋勫缓绛涢�夋潯浠讹細IsDeleted == false + var parameter = Expression.Parameter(entityType.ClrType, "e"); + var property = Expression.Property(parameter, "IsDeleted"); + var constant = Expression.Constant(false); + var equal = Expression.Equal(property, constant); + var lambda = Expression.Lambda(equal, parameter); + + // 娣诲姞鍏ㄥ眬绛涢�夊櫒 + modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda); + } } + + Console.WriteLine("鏁版嵁搴撻摼鎺ュ湴鍧�锛�" + App.Configuration.GetConnectionString("FlexJobApi")); } + /// <summary> + /// 鏁版嵁鍙樻洿浜嬩欢 + /// </summary> + /// <param name="eventData"></param> + /// <param name="result"></param> public static void SavingChangesEvent(DbContextEventData eventData, InterceptionResult<int> result) { // 鑾峰彇褰撳墠浜嬩欢瀵瑰簲涓婁笅鏂� @@ -118,26 +191,33 @@ { prop.CurrentValue = logier?.UserInfoId; } + + // 璧嬪�艰窡韪狪d + prop = entity.Property(nameof(CommonEntity.TraceId)); + if (prop != null) + { + prop.CurrentValue = App.GetTraceId(); + } } else { // 璧嬪�间慨鏀规棩鏈� var prop = entity.Property(nameof(CommonEntity.UpdatedTime)); - if (prop != null && prop.CurrentValue?.ToDateTime() == null) + if (prop != null) { prop.CurrentValue = DateTimeOffset.Now; } // 璧嬪�肩敤鎴蜂俊鎭疘d prop = entity.Property(nameof(CommonEntity.UpdatedUserInfoId)); - if (prop != null && prop.CurrentValue == null) + if (prop != null) { prop.CurrentValue = logier?.UserInfoId; } // 璧嬪�艰窡韪狪d prop = entity.Property(nameof(CommonEntity.TraceId)); - if (prop != null && prop.CurrentValue == null) + if (prop != null) { prop.CurrentValue = App.GetTraceId(); } @@ -148,7 +228,7 @@ entity.State = EntityState.Modified; prop = entity.Property(nameof(CommonEntity.IsDeleted)); - if (prop != null && prop.CurrentValue == null) + if (prop != null) { prop.CurrentValue = true; } @@ -190,21 +270,6 @@ .ToDictionary(p => p.Metadata.Name, p => getOldValues ? p.OriginalValue : p.CurrentValue); return JsonConvert.SerializeObject(properties); - } - - public static void OnCreating(ModelBuilder modelBuilder, EntityTypeBuilder entityBuilder, DbContext dbContext, Type dbContextLocator) - { - var metadata = entityBuilder.Metadata; - - var parameter = Expression.Parameter(metadata.ClrType, "e"); - var property = Expression.Property(parameter, nameof(CommonEntity.IsDeleted)); - var falseConstant = Expression.Constant(false, typeof(bool)); - var fakeDeleteQueryFilterExpression = Expression.Lambda( - Expression.Equal(property, falseConstant), - parameter - ); - if (fakeDeleteQueryFilterExpression == null) return; - entityBuilder.HasQueryFilter(fakeDeleteQueryFilterExpression); } } } -- Gitblit v1.9.1