From 470c9084539575b85a9873fc1508a50ecb7860f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= <454313500@qq.com> Date: Thu, 28 Dec 2023 15:23:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E5=AE=A1=E8=AE=A1?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=A8=A1=E5=9D=97=E6=90=AD=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/SqlSugarObjectRepository.cs | 154 +++++ .../Entities/AuditLogEntity.cs | 14 + .../SqlSugarCoreAuditLogRepository.cs | 556 ++++++++++-------- .../Yi.AuditLogging.SqlSugarCore.csproj | 1 + .../YiAuditLoggingDbContext.cs | 152 +++-- .../YiAuditLoggingSqlSugarCoreModule.cs | 30 +- 6 files changed, 578 insertions(+), 329 deletions(-) create mode 100644 Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarObjectRepository.cs create mode 100644 Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/Entities/AuditLogEntity.cs diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarObjectRepository.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarObjectRepository.cs new file mode 100644 index 00000000..2bc8ed41 --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarObjectRepository.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; +using Volo.Abp.Domain.Entities; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.Linq; +using Yi.Framework.SqlSugarCore.Abstractions; + +namespace Yi.Framework.SqlSugarCore.Repositories +{ + public class SqlSugarObjectRepository : IRepository where TEntity : class, IEntity + { + public ISqlSugarClient _Db => GetDbContextAsync().Result; + private ISugarDbContextProvider _sugarDbContextProvider; + /// + /// 获取DB + /// + /// + public virtual async Task GetDbContextAsync() + { + + var db = (await _sugarDbContextProvider.GetDbContextAsync()).SqlSugarClient; + //await Console.Out.WriteLineAsync("获取的id:" + db.ContextID); + return db; + } + + public IAsyncQueryableExecuter AsyncExecuter => throw new NotImplementedException(); + + public bool? IsChangeTrackingEnabled => throw new NotImplementedException(); + + public Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteDirectAsync(Expression> predicate, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task FindAsync(Expression> predicate, bool includeDetails = true, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task GetAsync(Expression> predicate, bool includeDetails = true, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task GetCountAsync(CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> GetListAsync(Expression> predicate, bool includeDetails = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> GetQueryableAsync() + { + throw new NotImplementedException(); + } + + public async Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) + { + await (await GetDbContextAsync()).InsertableByObject(entity).ExecuteCommandAsync(); + return entity; + } + + public Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public IQueryable WithDetails() + { + throw new NotImplementedException(); + } + + public IQueryable WithDetails(params Expression>[] propertySelectors) + { + throw new NotImplementedException(); + } + + public Task> WithDetailsAsync() + { + throw new NotImplementedException(); + } + + public Task> WithDetailsAsync(params Expression>[] propertySelectors) + { + throw new NotImplementedException(); + } + } + + public class SqlSugarObjectRepository : SqlSugarObjectRepository, IRepository where TEntity : class, IEntity + { + public Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteManyAsync(IEnumerable ids, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task FindAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task GetAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + } +} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/Entities/AuditLogEntity.cs b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/Entities/AuditLogEntity.cs new file mode 100644 index 00000000..3d8e8c8c --- /dev/null +++ b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/Entities/AuditLogEntity.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp.AuditLogging; + +namespace Yi.AuditLogging.SqlSugarCore.Entities +{ + public class AuditLogEntity : AuditLog + { + public AuditLogEntity() { } + } +} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/SqlSugarCoreAuditLogRepository.cs b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/SqlSugarCoreAuditLogRepository.cs index cf8d7646..920e037f 100644 --- a/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/SqlSugarCoreAuditLogRepository.cs +++ b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/SqlSugarCoreAuditLogRepository.cs @@ -1,263 +1,347 @@ -//using System.Linq.Dynamic.Core; -//using System.Net; -//using Volo.Abp.Auditing; -//using Volo.Abp.Domain.Entities; -//using Yi.Framework.SqlSugarCore.Abstractions; -//using Yi.Framework.SqlSugarCore.Repositories; +using System.Linq.Dynamic.Core; +using System.Linq.Expressions; +using System.Net; +using Mapster; +using SqlSugar; +using Volo.Abp.Auditing; +using Volo.Abp.Domain.Entities; +using Volo.Abp.Domain.Repositories; +using Yi.AuditLogging.SqlSugarCore.Entities; +using Yi.Framework.SqlSugarCore.Repositories; -//namespace Volo.Abp.AuditLogging.EntityFrameworkCore; +namespace Volo.Abp.AuditLogging.EntityFrameworkCore; -//public class SqlSugarCoreAuditLogRepository : SqlSugarNoConstraintsRepository, IAuditLogRepository -//{ +public class SqlSugarCoreAuditLogRepository : SqlSugarObjectRepository,IAuditLogRepository +{ + public virtual async Task> GetListAsync( + string sorting = null, + int maxResultCount = 50, + int skipCount = 0, + DateTime? startTime = null, + DateTime? endTime = null, + string httpMethod = null, + string url = null, + Guid? userId = null, + string userName = null, + string applicationName = null, + string clientIpAddress = null, + string correlationId = null, + int? maxExecutionDuration = null, + int? minExecutionDuration = null, + bool? hasException = null, + HttpStatusCode? httpStatusCode = null, + bool includeDetails = false, + CancellationToken cancellationToken = default) + { + var query = await GetListQueryAsync( + startTime, + endTime, + httpMethod, + url, + userId, + userName, + applicationName, + clientIpAddress, + correlationId, + maxExecutionDuration, + minExecutionDuration, + hasException, + httpStatusCode, + includeDetails + ); -// public virtual async Task> GetListAsync( -// string sorting = null, -// int maxResultCount = 50, -// int skipCount = 0, -// DateTime? startTime = null, -// DateTime? endTime = null, -// string httpMethod = null, -// string url = null, -// Guid? userId = null, -// string userName = null, -// string applicationName = null, -// string clientIpAddress = null, -// string correlationId = null, -// int? maxExecutionDuration = null, -// int? minExecutionDuration = null, -// bool? hasException = null, -// HttpStatusCode? httpStatusCode = null, -// bool includeDetails = false, -// CancellationToken cancellationToken = default) -// { -// var query = await GetListQueryAsync( -// startTime, -// endTime, -// httpMethod, -// url, -// userId, -// userName, -// applicationName, -// clientIpAddress, -// correlationId, -// maxExecutionDuration, -// minExecutionDuration, -// hasException, -// httpStatusCode, -// includeDetails -// ); + var auditLogs = await query + .OrderBy(sorting.IsNullOrWhiteSpace() ? (nameof(AuditLog.ExecutionTime) + " DESC") : sorting) + .ToPageListAsync(skipCount, maxResultCount, cancellationToken); -// var auditLogs = await query -// .OrderBy(sorting.IsNullOrWhiteSpace() ? (nameof(AuditLog.ExecutionTime) + " DESC") : sorting) -// .PageBy(skipCount, maxResultCount) -// .ToListAsync(GetCancellationToken(cancellationToken)); + return auditLogs.Adapt>(); + } -// return auditLogs; -// } + public virtual async Task GetCountAsync( + DateTime? startTime = null, + DateTime? endTime = null, + string httpMethod = null, + string url = null, + Guid? userId = null, + string userName = null, + string applicationName = null, + string clientIpAddress = null, + string correlationId = null, + int? maxExecutionDuration = null, + int? minExecutionDuration = null, + bool? hasException = null, + HttpStatusCode? httpStatusCode = null, + CancellationToken cancellationToken = default) + { + var query = await GetListQueryAsync( + startTime, + endTime, + httpMethod, + url, + userId, + userName, + applicationName, + clientIpAddress, + correlationId, + maxExecutionDuration, + minExecutionDuration, + hasException, + httpStatusCode + ); -// public virtual async Task GetCountAsync( -// DateTime? startTime = null, -// DateTime? endTime = null, -// string httpMethod = null, -// string url = null, -// Guid? userId = null, -// string userName = null, -// string applicationName = null, -// string clientIpAddress = null, -// string correlationId = null, -// int? maxExecutionDuration = null, -// int? minExecutionDuration = null, -// bool? hasException = null, -// HttpStatusCode? httpStatusCode = null, -// CancellationToken cancellationToken = default) -// { -// var query = await GetListQueryAsync( -// startTime, -// endTime, -// httpMethod, -// url, -// userId, -// userName, -// applicationName, -// clientIpAddress, -// correlationId, -// maxExecutionDuration, -// minExecutionDuration, -// hasException, -// httpStatusCode -// ); + var totalCount = await query.CountAsync(cancellationToken); -// var totalCount = await query.LongCountAsync(GetCancellationToken(cancellationToken)); + return totalCount; + } -// return totalCount; -// } + protected virtual async Task> GetListQueryAsync( + DateTime? startTime = null, + DateTime? endTime = null, + string httpMethod = null, + string url = null, + Guid? userId = null, + string userName = null, + string applicationName = null, + string clientIpAddress = null, + string correlationId = null, + int? maxExecutionDuration = null, + int? minExecutionDuration = null, + bool? hasException = null, + HttpStatusCode? httpStatusCode = null, + bool includeDetails = false) + { + var nHttpStatusCode = (int?)httpStatusCode; + return (await GetDbContextAsync()).Queryable() + .WhereIF(startTime.HasValue, auditLog => auditLog.ExecutionTime >= startTime) + .WhereIF(endTime.HasValue, auditLog => auditLog.ExecutionTime <= endTime) + .WhereIF(hasException.HasValue && hasException.Value, auditLog => auditLog.Exceptions != null && auditLog.Exceptions != "") + .WhereIF(hasException.HasValue && !hasException.Value, auditLog => auditLog.Exceptions == null || auditLog.Exceptions == "") + .WhereIF(httpMethod != null, auditLog => auditLog.HttpMethod == httpMethod) + .WhereIF(url != null, auditLog => auditLog.Url != null && auditLog.Url.Contains(url)) + .WhereIF(userId != null, auditLog => auditLog.UserId == userId) + .WhereIF(userName != null, auditLog => auditLog.UserName == userName) + .WhereIF(applicationName != null, auditLog => auditLog.ApplicationName == applicationName) + .WhereIF(clientIpAddress != null, auditLog => auditLog.ClientIpAddress != null && auditLog.ClientIpAddress == clientIpAddress) + .WhereIF(correlationId != null, auditLog => auditLog.CorrelationId == correlationId) + .WhereIF(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == nHttpStatusCode) + .WhereIF(maxExecutionDuration != null && maxExecutionDuration.Value > 0, auditLog => auditLog.ExecutionDuration <= maxExecutionDuration) + .WhereIF(minExecutionDuration != null && minExecutionDuration.Value > 0, auditLog => auditLog.ExecutionDuration >= minExecutionDuration); + } -// protected virtual async Task> GetListQueryAsync( -// DateTime? startTime = null, -// DateTime? endTime = null, -// string httpMethod = null, -// string url = null, -// Guid? userId = null, -// string userName = null, -// string applicationName = null, -// string clientIpAddress = null, -// string correlationId = null, -// int? maxExecutionDuration = null, -// int? minExecutionDuration = null, -// bool? hasException = null, -// HttpStatusCode? httpStatusCode = null, -// bool includeDetails = false) -// { -// var nHttpStatusCode = (int?)httpStatusCode; -// return (await GetDbSetAsync()).AsNoTracking() -// .IncludeDetails(includeDetails) -// .WhereIf(startTime.HasValue, auditLog => auditLog.ExecutionTime >= startTime) -// .WhereIf(endTime.HasValue, auditLog => auditLog.ExecutionTime <= endTime) -// .WhereIf(hasException.HasValue && hasException.Value, auditLog => auditLog.Exceptions != null && auditLog.Exceptions != "") -// .WhereIf(hasException.HasValue && !hasException.Value, auditLog => auditLog.Exceptions == null || auditLog.Exceptions == "") -// .WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod == httpMethod) -// .WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.Contains(url)) -// .WhereIf(userId != null, auditLog => auditLog.UserId == userId) -// .WhereIf(userName != null, auditLog => auditLog.UserName == userName) -// .WhereIf(applicationName != null, auditLog => auditLog.ApplicationName == applicationName) -// .WhereIf(clientIpAddress != null, auditLog => auditLog.ClientIpAddress != null && auditLog.ClientIpAddress == clientIpAddress) -// .WhereIf(correlationId != null, auditLog => auditLog.CorrelationId == correlationId) -// .WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == nHttpStatusCode) -// .WhereIf(maxExecutionDuration != null && maxExecutionDuration.Value > 0, auditLog => auditLog.ExecutionDuration <= maxExecutionDuration) -// .WhereIf(minExecutionDuration != null && minExecutionDuration.Value > 0, auditLog => auditLog.ExecutionDuration >= minExecutionDuration); -// } + public virtual async Task> GetAverageExecutionDurationPerDayAsync( + DateTime startDate, + DateTime endDate, + CancellationToken cancellationToken = default) + { + var result = await (await GetDbContextAsync()).Queryable() + .Where(a => a.ExecutionTime < endDate.AddDays(1) && a.ExecutionTime > startDate) + .OrderBy(t => t.ExecutionTime) + .GroupBy(t => new { t.ExecutionTime.Date }) + .Select(g => new { Day =SqlFunc.AggregateMin(g.ExecutionTime), avgExecutionTime = SqlFunc.AggregateAvg( g.ExecutionDuration) }) + .ToListAsync(cancellationToken); -// public virtual async Task> GetAverageExecutionDurationPerDayAsync( -// DateTime startDate, -// DateTime endDate, -// CancellationToken cancellationToken = default) -// { -// var result = await (await GetDbSetAsync()).AsNoTracking() -// .Where(a => a.ExecutionTime < endDate.AddDays(1) && a.ExecutionTime > startDate) -// .OrderBy(t => t.ExecutionTime) -// .GroupBy(t => new { t.ExecutionTime.Date }) -// .Select(g => new { Day = g.Min(t => t.ExecutionTime), avgExecutionTime = g.Average(t => t.ExecutionDuration) }) -// .ToListAsync(GetCancellationToken(cancellationToken)); + return result.ToDictionary(element => element.Day.ClearTime(), element => (double)element.avgExecutionTime); + } -// return result.ToDictionary(element => element.Day.ClearTime(), element => element.avgExecutionTime); -// } + public virtual async Task GetEntityChange( + Guid entityChangeId, + CancellationToken cancellationToken = default) + { + var entityChange = await (await GetDbContextAsync()).Queryable() + .Where(x => x.Id == entityChangeId) + .OrderBy(x => x.Id) + .FirstAsync(cancellationToken); -// [Obsolete("Use WithDetailsAsync method.")] -// public override IQueryable WithDetails() -// { -// return GetQueryable().IncludeDetails(); -// } + if (entityChange == null) + { + throw new EntityNotFoundException(typeof(EntityChange)); + } -// public override async Task> WithDetailsAsync() -// { -// return (await GetQueryableAsync()).IncludeDetails(); -// } + return entityChange; + } -// public virtual async Task GetEntityChange( -// Guid entityChangeId, -// CancellationToken cancellationToken = default) -// { -// var entityChange = await (await GetDbContextAsync()).Set() -// .AsNoTracking() -// .IncludeDetails() -// .Where(x => x.Id == entityChangeId) -// .OrderBy(x => x.Id) -// .FirstOrDefaultAsync(GetCancellationToken(cancellationToken)); + public virtual async Task> GetEntityChangeListAsync( + string sorting = null, + int maxResultCount = 50, + int skipCount = 0, + Guid? auditLogId = null, + DateTime? startTime = null, + DateTime? endTime = null, + EntityChangeType? changeType = null, + string entityId = null, + string entityTypeFullName = null, + bool includeDetails = false, + CancellationToken cancellationToken = default) + { + var query = await GetEntityChangeListQueryAsync(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName, includeDetails); -// if (entityChange == null) -// { -// throw new EntityNotFoundException(typeof(EntityChange)); -// } + return await query.OrderBy(sorting.IsNullOrWhiteSpace() ? (nameof(EntityChange.ChangeTime) + " DESC") : sorting) + .ToPageListAsync(skipCount, maxResultCount, cancellationToken); + } -// return entityChange; -// } + public virtual async Task GetEntityChangeCountAsync( + Guid? auditLogId = null, + DateTime? startTime = null, + DateTime? endTime = null, + EntityChangeType? changeType = null, + string entityId = null, + string entityTypeFullName = null, + CancellationToken cancellationToken = default) + { + var query = await GetEntityChangeListQueryAsync(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); -// public virtual async Task> GetEntityChangeListAsync( -// string sorting = null, -// int maxResultCount = 50, -// int skipCount = 0, -// Guid? auditLogId = null, -// DateTime? startTime = null, -// DateTime? endTime = null, -// EntityChangeType? changeType = null, -// string entityId = null, -// string entityTypeFullName = null, -// bool includeDetails = false, -// CancellationToken cancellationToken = default) -// { -// var query = await GetEntityChangeListQueryAsync(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName, includeDetails); + var totalCount = await query.CountAsync(cancellationToken); -// return await query.OrderBy(sorting.IsNullOrWhiteSpace() ? (nameof(EntityChange.ChangeTime) + " DESC") : sorting) -// .PageBy(skipCount, maxResultCount) -// .ToListAsync(GetCancellationToken(cancellationToken)); -// } + return totalCount; + } -// public virtual async Task GetEntityChangeCountAsync( -// Guid? auditLogId = null, -// DateTime? startTime = null, -// DateTime? endTime = null, -// EntityChangeType? changeType = null, -// string entityId = null, -// string entityTypeFullName = null, -// CancellationToken cancellationToken = default) -// { -// var query = await GetEntityChangeListQueryAsync(auditLogId, startTime, endTime, changeType, entityId, entityTypeFullName); + public virtual async Task GetEntityChangeWithUsernameAsync( + Guid entityChangeId, + CancellationToken cancellationToken = default) + { + var auditLog = await (await GetDbContextAsync()).Queryable() + .Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId)).FirstAsync(cancellationToken); -// var totalCount = await query.LongCountAsync(GetCancellationToken(cancellationToken)); + return new EntityChangeWithUsername() + { + EntityChange = auditLog.EntityChanges.First(x => x.Id == entityChangeId), + UserName = auditLog.UserName + }; + } -// return totalCount; -// } + public virtual async Task> GetEntityChangesWithUsernameAsync( + string entityId, + string entityTypeFullName, + CancellationToken cancellationToken = default) + { + var dbContext = await GetDbContextAsync(); -// public virtual async Task GetEntityChangeWithUsernameAsync( -// Guid entityChangeId, -// CancellationToken cancellationToken = default) -// { -// var auditLog = await (await GetDbSetAsync()).AsNoTracking().IncludeDetails() -// .Where(x => x.EntityChanges.Any(y => y.Id == entityChangeId)).FirstAsync(GetCancellationToken(cancellationToken)); + var query = dbContext.Queryable() + .Where(x => x.EntityId == entityId && x.EntityTypeFullName == entityTypeFullName); + return await query.LeftJoin((change, audit) => change.AuditLogId == audit.Id) + .Select((change, audit) => new EntityChangeWithUsername { EntityChange = change, UserName = audit.UserName }, true) + .OrderByDescending(x => x.EntityChange.ChangeTime).ToListAsync(cancellationToken); -// return new EntityChangeWithUsername() -// { -// EntityChange = auditLog.EntityChanges.First(x => x.Id == entityChangeId), -// UserName = auditLog.UserName -// }; -// } + } -// public virtual async Task> GetEntityChangesWithUsernameAsync( -// string entityId, -// string entityTypeFullName, -// CancellationToken cancellationToken = default) -// { -// var dbContext = await GetDbContextAsync(); + protected virtual async Task> GetEntityChangeListQueryAsync( + Guid? auditLogId = null, + DateTime? startTime = null, + DateTime? endTime = null, + EntityChangeType? changeType = null, + string entityId = null, + string entityTypeFullName = null, + bool includeDetails = false) + { + return (await GetDbContextAsync()) + .Queryable() + .WhereIF(auditLogId.HasValue, e => e.AuditLogId == auditLogId) + .WhereIF(startTime.HasValue, e => e.ChangeTime >= startTime) + .WhereIF(endTime.HasValue, e => e.ChangeTime <= endTime) + .WhereIF(changeType.HasValue, e => e.ChangeType == changeType) + .WhereIF(!string.IsNullOrWhiteSpace(entityId), e => e.EntityId == entityId) + .WhereIF(!string.IsNullOrWhiteSpace(entityTypeFullName), e => e.EntityTypeFullName.Contains(entityTypeFullName)); + } -// var query = dbContext.Set() -// .AsNoTracking() -// .IncludeDetails() -// .Where(x => x.EntityId == entityId && x.EntityTypeFullName == entityTypeFullName); + Task> IAuditLogRepository.GetListAsync(string sorting, int maxResultCount, int skipCount, DateTime? startTime, DateTime? endTime, string httpMethod, string url, Guid? userId, string userName, string applicationName, string clientIpAddress, string correlationId, int? maxExecutionDuration, int? minExecutionDuration, bool? hasException, HttpStatusCode? httpStatusCode, bool includeDetails, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } -// return await (from e in query -// join auditLog in dbContext.AuditLogs on e.AuditLogId equals auditLog.Id -// select new EntityChangeWithUsername { EntityChange = e, UserName = auditLog.UserName }) -// .OrderByDescending(x => x.EntityChange.ChangeTime).ToListAsync(GetCancellationToken(cancellationToken)); -// } -// protected virtual async Task> GetEntityChangeListQueryAsync( -// Guid? auditLogId = null, -// DateTime? startTime = null, -// DateTime? endTime = null, -// EntityChangeType? changeType = null, -// string entityId = null, -// string entityTypeFullName = null, -// bool includeDetails = false) -// { -// return (await GetDbContextAsync()) -// .Set() -// .AsNoTracking() -// .IncludeDetails(includeDetails) -// .WhereIf(auditLogId.HasValue, e => e.AuditLogId == auditLogId) -// .WhereIf(startTime.HasValue, e => e.ChangeTime >= startTime) -// .WhereIf(endTime.HasValue, e => e.ChangeTime <= endTime) -// .WhereIf(changeType.HasValue, e => e.ChangeType == changeType) -// .WhereIf(!string.IsNullOrWhiteSpace(entityId), e => e.EntityId == entityId) -// .WhereIf(!string.IsNullOrWhiteSpace(entityTypeFullName), e => e.EntityTypeFullName.Contains(entityTypeFullName)); -// } -//} + + public Task GetAsync(Expression> predicate, bool includeDetails = true, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteDirectAsync(Expression> predicate, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + IQueryable IReadOnlyRepository.WithDetails() + { + throw new NotImplementedException(); + } + + public IQueryable WithDetails(params Expression>[] propertySelectors) + { + throw new NotImplementedException(); + } + + Task> IReadOnlyRepository.WithDetailsAsync() + { + throw new NotImplementedException(); + } + + public Task> WithDetailsAsync(params Expression>[] propertySelectors) + { + throw new NotImplementedException(); + } + + Task> IReadOnlyRepository.GetQueryableAsync() + { + throw new NotImplementedException(); + } + + public Task> GetListAsync(Expression> predicate, bool includeDetails = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task InsertAsync(AuditLog entity, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(AuditLog entity, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(AuditLog entity, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + Task IReadOnlyBasicRepository.GetAsync(Guid id, bool includeDetails, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + Task IReadOnlyBasicRepository.FindAsync(Guid id, bool includeDetails, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + Task> IReadOnlyBasicRepository.GetListAsync(bool includeDetails, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + Task> IReadOnlyBasicRepository.GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } +} diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/Yi.AuditLogging.SqlSugarCore.csproj b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/Yi.AuditLogging.SqlSugarCore.csproj index f16bc4cd..b27ef778 100644 --- a/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/Yi.AuditLogging.SqlSugarCore.csproj +++ b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/Yi.AuditLogging.SqlSugarCore.csproj @@ -11,6 +11,7 @@ + diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/YiAuditLoggingDbContext.cs b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/YiAuditLoggingDbContext.cs index 1726fe9c..5922f30b 100644 --- a/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/YiAuditLoggingDbContext.cs +++ b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/YiAuditLoggingDbContext.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; +using System.Reflection; using SqlSugar; using Volo.Abp.AuditLogging; using Volo.Abp.DependencyInjection; @@ -23,11 +18,11 @@ namespace Yi.AuditLogging.SqlSugarCore column.DbTableName = AbpAuditLoggingDbProperties.DbTablePrefix + "AuditLogs"; column.IfTable() - + .UpdateProperty(x => x.Id, x => { - x.IsPrimarykey = true; + x.IsPrimarykey = true; }) .UpdateProperty(x => x.ApplicationName, @@ -38,97 +33,98 @@ namespace Yi.AuditLogging.SqlSugarCore }); - //builder.Entity(b => - //{ - // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "AuditLogs", AbpAuditLoggingDbProperties.DbSchema); + // builder.Entity(b => + // { + // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "AuditLogs", AbpAuditLoggingDbProperties.DbSchema); - // b.ConfigureByConvention(); + // b.ConfigureByConvention(); - // b.Property(x => x.ApplicationName).HasMaxLength(AuditLogConsts.MaxApplicationNameLength).HasColumnName(nameof(AuditLog.ApplicationName)); - // b.Property(x => x.ClientIpAddress).HasMaxLength(AuditLogConsts.MaxClientIpAddressLength).HasColumnName(nameof(AuditLog.ClientIpAddress)); - // b.Property(x => x.ClientName).HasMaxLength(AuditLogConsts.MaxClientNameLength).HasColumnName(nameof(AuditLog.ClientName)); - // b.Property(x => x.ClientId).HasMaxLength(AuditLogConsts.MaxClientIdLength).HasColumnName(nameof(AuditLog.ClientId)); - // b.Property(x => x.CorrelationId).HasMaxLength(AuditLogConsts.MaxCorrelationIdLength).HasColumnName(nameof(AuditLog.CorrelationId)); - // b.Property(x => x.BrowserInfo).HasMaxLength(AuditLogConsts.MaxBrowserInfoLength).HasColumnName(nameof(AuditLog.BrowserInfo)); - // b.Property(x => x.HttpMethod).HasMaxLength(AuditLogConsts.MaxHttpMethodLength).HasColumnName(nameof(AuditLog.HttpMethod)); - // b.Property(x => x.Url).HasMaxLength(AuditLogConsts.MaxUrlLength).HasColumnName(nameof(AuditLog.Url)); - // b.Property(x => x.HttpStatusCode).HasColumnName(nameof(AuditLog.HttpStatusCode)); + // b.Property(x => x.ApplicationName).HasMaxLength(AuditLogConsts.MaxApplicationNameLength).HasColumnName(nameof(AuditLog.ApplicationName)); + // b.Property(x => x.ClientIpAddress).HasMaxLength(AuditLogConsts.MaxClientIpAddressLength).HasColumnName(nameof(AuditLog.ClientIpAddress)); + // b.Property(x => x.ClientName).HasMaxLength(AuditLogConsts.MaxClientNameLength).HasColumnName(nameof(AuditLog.ClientName)); + // b.Property(x => x.ClientId).HasMaxLength(AuditLogConsts.MaxClientIdLength).HasColumnName(nameof(AuditLog.ClientId)); + // b.Property(x => x.CorrelationId).HasMaxLength(AuditLogConsts.MaxCorrelationIdLength).HasColumnName(nameof(AuditLog.CorrelationId)); + // b.Property(x => x.BrowserInfo).HasMaxLength(AuditLogConsts.MaxBrowserInfoLength).HasColumnName(nameof(AuditLog.BrowserInfo)); + // b.Property(x => x.HttpMethod).HasMaxLength(AuditLogConsts.MaxHttpMethodLength).HasColumnName(nameof(AuditLog.HttpMethod)); + // b.Property(x => x.Url).HasMaxLength(AuditLogConsts.MaxUrlLength).HasColumnName(nameof(AuditLog.Url)); + // b.Property(x => x.HttpStatusCode).HasColumnName(nameof(AuditLog.HttpStatusCode)); - // b.Property(x => x.Comments).HasMaxLength(AuditLogConsts.MaxCommentsLength).HasColumnName(nameof(AuditLog.Comments)); - // b.Property(x => x.ExecutionDuration).HasColumnName(nameof(AuditLog.ExecutionDuration)); - // b.Property(x => x.ImpersonatorTenantId).HasColumnName(nameof(AuditLog.ImpersonatorTenantId)); - // b.Property(x => x.ImpersonatorUserId).HasColumnName(nameof(AuditLog.ImpersonatorUserId)); - // b.Property(x => x.ImpersonatorTenantName).HasMaxLength(AuditLogConsts.MaxTenantNameLength).HasColumnName(nameof(AuditLog.ImpersonatorTenantName)); - // b.Property(x => x.ImpersonatorUserName).HasMaxLength(AuditLogConsts.MaxUserNameLength).HasColumnName(nameof(AuditLog.ImpersonatorUserName)); - // b.Property(x => x.UserId).HasColumnName(nameof(AuditLog.UserId)); - // b.Property(x => x.UserName).HasMaxLength(AuditLogConsts.MaxUserNameLength).HasColumnName(nameof(AuditLog.UserName)); - // b.Property(x => x.TenantId).HasColumnName(nameof(AuditLog.TenantId)); - // b.Property(x => x.TenantName).HasMaxLength(AuditLogConsts.MaxTenantNameLength).HasColumnName(nameof(AuditLog.TenantName)); + // b.Property(x => x.Comments).HasMaxLength(AuditLogConsts.MaxCommentsLength).HasColumnName(nameof(AuditLog.Comments)); + // b.Property(x => x.ExecutionDuration).HasColumnName(nameof(AuditLog.ExecutionDuration)); + // b.Property(x => x.ImpersonatorTenantId).HasColumnName(nameof(AuditLog.ImpersonatorTenantId)); + // b.Property(x => x.ImpersonatorUserId).HasColumnName(nameof(AuditLog.ImpersonatorUserId)); + // b.Property(x => x.ImpersonatorTenantName).HasMaxLength(AuditLogConsts.MaxTenantNameLength).HasColumnName(nameof(AuditLog.ImpersonatorTenantName)); + // b.Property(x => x.ImpersonatorUserName).HasMaxLength(AuditLogConsts.MaxUserNameLength).HasColumnName(nameof(AuditLog.ImpersonatorUserName)); + // b.Property(x => x.UserId).HasColumnName(nameof(AuditLog.UserId)); + // b.Property(x => x.UserName).HasMaxLength(AuditLogConsts.MaxUserNameLength).HasColumnName(nameof(AuditLog.UserName)); + // b.Property(x => x.TenantId).HasColumnName(nameof(AuditLog.TenantId)); + // b.Property(x => x.TenantName).HasMaxLength(AuditLogConsts.MaxTenantNameLength).HasColumnName(nameof(AuditLog.TenantName)); - // b.HasMany(a => a.Actions).WithOne().HasForeignKey(x => x.AuditLogId).IsRequired(); - // b.HasMany(a => a.EntityChanges).WithOne().HasForeignKey(x => x.AuditLogId).IsRequired(); + // b.HasMany(a => a.Actions).WithOne().HasForeignKey(x => x.AuditLogId).IsRequired(); + // b.HasMany(a => a.EntityChanges).WithOne().HasForeignKey(x => x.AuditLogId).IsRequired(); - // b.HasIndex(x => new { x.TenantId, x.ExecutionTime }); - // b.HasIndex(x => new { x.TenantId, x.UserId, x.ExecutionTime }); + // b.HasIndex(x => new { x.TenantId, x.ExecutionTime }); + // b.HasIndex(x => new { x.TenantId, x.UserId, x.ExecutionTime }); - // b.ApplyObjectExtensionMappings(); - //}); + // b.ApplyObjectExtensionMappings(); + // }); - //builder.Entity(b => - //{ - // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "AuditLogActions", AbpAuditLoggingDbProperties.DbSchema); + // builder.Entity(b => + // { + // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "AuditLogActions", AbpAuditLoggingDbProperties.DbSchema); - // b.ConfigureByConvention(); + // b.ConfigureByConvention(); - // b.Property(x => x.AuditLogId).HasColumnName(nameof(AuditLogAction.AuditLogId)); - // b.Property(x => x.ServiceName).HasMaxLength(AuditLogActionConsts.MaxServiceNameLength).HasColumnName(nameof(AuditLogAction.ServiceName)); - // b.Property(x => x.MethodName).HasMaxLength(AuditLogActionConsts.MaxMethodNameLength).HasColumnName(nameof(AuditLogAction.MethodName)); - // b.Property(x => x.Parameters).HasMaxLength(AuditLogActionConsts.MaxParametersLength).HasColumnName(nameof(AuditLogAction.Parameters)); - // b.Property(x => x.ExecutionTime).HasColumnName(nameof(AuditLogAction.ExecutionTime)); - // b.Property(x => x.ExecutionDuration).HasColumnName(nameof(AuditLogAction.ExecutionDuration)); + // b.Property(x => x.AuditLogId).HasColumnName(nameof(AuditLogAction.AuditLogId)); + // b.Property(x => x.ServiceName).HasMaxLength(AuditLogActionConsts.MaxServiceNameLength).HasColumnName(nameof(AuditLogAction.ServiceName)); + // b.Property(x => x.MethodName).HasMaxLength(AuditLogActionConsts.MaxMethodNameLength).HasColumnName(nameof(AuditLogAction.MethodName)); + // b.Property(x => x.Parameters).HasMaxLength(AuditLogActionConsts.MaxParametersLength).HasColumnName(nameof(AuditLogAction.Parameters)); + // b.Property(x => x.ExecutionTime).HasColumnName(nameof(AuditLogAction.ExecutionTime)); + // b.Property(x => x.ExecutionDuration).HasColumnName(nameof(AuditLogAction.ExecutionDuration)); - // b.HasIndex(x => new { x.AuditLogId }); - // b.HasIndex(x => new { x.TenantId, x.ServiceName, x.MethodName, x.ExecutionTime }); + // b.HasIndex(x => new { x.AuditLogId }); + // b.HasIndex(x => new { x.TenantId, x.ServiceName, x.MethodName, x.ExecutionTime }); - // b.ApplyObjectExtensionMappings(); - //}); + // b.ApplyObjectExtensionMappings(); + // }); - //builder.Entity(b => - //{ - // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "EntityChanges", AbpAuditLoggingDbProperties.DbSchema); + // builder.Entity(b => + // { + // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "EntityChanges", AbpAuditLoggingDbProperties.DbSchema); - // b.ConfigureByConvention(); + // b.ConfigureByConvention(); - // b.Property(x => x.EntityTypeFullName).HasMaxLength(EntityChangeConsts.MaxEntityTypeFullNameLength).IsRequired().HasColumnName(nameof(EntityChange.EntityTypeFullName)); - // b.Property(x => x.EntityId).HasMaxLength(EntityChangeConsts.MaxEntityIdLength).IsRequired().HasColumnName(nameof(EntityChange.EntityId)); - // b.Property(x => x.AuditLogId).IsRequired().HasColumnName(nameof(EntityChange.AuditLogId)); - // b.Property(x => x.ChangeTime).IsRequired().HasColumnName(nameof(EntityChange.ChangeTime)); - // b.Property(x => x.ChangeType).IsRequired().HasColumnName(nameof(EntityChange.ChangeType)); - // b.Property(x => x.TenantId).HasColumnName(nameof(EntityChange.TenantId)); + // b.Property(x => x.EntityTypeFullName).HasMaxLength(EntityChangeConsts.MaxEntityTypeFullNameLength).IsRequired().HasColumnName(nameof(EntityChange.EntityTypeFullName)); + // b.Property(x => x.EntityId).HasMaxLength(EntityChangeConsts.MaxEntityIdLength).IsRequired().HasColumnName(nameof(EntityChange.EntityId)); + // b.Property(x => x.AuditLogId).IsRequired().HasColumnName(nameof(EntityChange.AuditLogId)); + // b.Property(x => x.ChangeTime).IsRequired().HasColumnName(nameof(EntityChange.ChangeTime)); + // b.Property(x => x.ChangeType).IsRequired().HasColumnName(nameof(EntityChange.ChangeType)); + // b.Property(x => x.TenantId).HasColumnName(nameof(EntityChange.TenantId)); - // b.HasMany(a => a.PropertyChanges).WithOne().HasForeignKey(x => x.EntityChangeId); + // b.HasMany(a => a.PropertyChanges).WithOne().HasForeignKey(x => x.EntityChangeId); - // b.HasIndex(x => new { x.AuditLogId }); - // b.HasIndex(x => new { x.TenantId, x.EntityTypeFullName, x.EntityId }); + // b.HasIndex(x => new { x.AuditLogId }); + // b.HasIndex(x => new { x.TenantId, x.EntityTypeFullName, x.EntityId }); - // b.ApplyObjectExtensionMappings(); - //}); + // b.ApplyObjectExtensionMappings(); + // }); - //builder.Entity(b => - //{ - // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "EntityPropertyChanges", AbpAuditLoggingDbProperties.DbSchema); + // builder.Entity(b => + // { + // b.ToTable(AbpAuditLoggingDbProperties.DbTablePrefix + "EntityPropertyChanges", AbpAuditLoggingDbProperties.DbSchema); - // b.ConfigureByConvention(); + // b.ConfigureByConvention(); - // b.Property(x => x.NewValue).HasMaxLength(EntityPropertyChangeConsts.MaxNewValueLength).HasColumnName(nameof(EntityPropertyChange.NewValue)); - // b.Property(x => x.PropertyName).HasMaxLength(EntityPropertyChangeConsts.MaxPropertyNameLength).IsRequired().HasColumnName(nameof(EntityPropertyChange.PropertyName)); - // b.Property(x => x.PropertyTypeFullName).HasMaxLength(EntityPropertyChangeConsts.MaxPropertyTypeFullNameLength).IsRequired().HasColumnName(nameof(EntityPropertyChange.PropertyTypeFullName)); - // b.Property(x => x.OriginalValue).HasMaxLength(EntityPropertyChangeConsts.MaxOriginalValueLength).HasColumnName(nameof(EntityPropertyChange.OriginalValue)); + // b.Property(x => x.NewValue).HasMaxLength(EntityPropertyChangeConsts.MaxNewValueLength).HasColumnName(nameof(EntityPropertyChange.NewValue)); + // b.Property(x => x.PropertyName).HasMaxLength(EntityPropertyChangeConsts.MaxPropertyNameLength).IsRequired().HasColumnName(nameof(EntityPropertyChange.PropertyName)); + // b.Property(x => x.PropertyTypeFullName).HasMaxLength(EntityPropertyChangeConsts.MaxPropertyTypeFullNameLength).IsRequired().HasColumnName(nameof(EntityPropertyChange.PropertyTypeFullName)); + // b.Property(x => x.OriginalValue).HasMaxLength(EntityPropertyChangeConsts.MaxOriginalValueLength).HasColumnName(nameof(EntityPropertyChange.OriginalValue)); - // b.HasIndex(x => new { x.EntityChangeId }); + // b.HasIndex(x => new { x.EntityChangeId }); - // b.ApplyObjectExtensionMappings(); - //}); + // b.ApplyObjectExtensionMappings(); + // }); + //} } } -} +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/YiAuditLoggingSqlSugarCoreModule.cs b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/YiAuditLoggingSqlSugarCoreModule.cs index 71e497d3..234c3096 100644 --- a/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/YiAuditLoggingSqlSugarCoreModule.cs +++ b/Yi.Abp.Net8/module/audit-logging/Yi.AuditLogging.SqlSugarCore/YiAuditLoggingSqlSugarCoreModule.cs @@ -1,16 +1,16 @@ -//using Volo.Abp.AuditLogging; -//using Volo.Abp.Modularity; -//using Yi.Framework.SqlSugarCore; +using Volo.Abp.AuditLogging; +using Volo.Abp.Modularity; +using Yi.Framework.SqlSugarCore; -//namespace Yi.AuditLogging.SqlSugarCore -//{ -// [DependsOn(typeof(AbpAuditLoggingDomainModule))] -// [DependsOn(typeof(YiFrameworkSqlSugarCoreModule))] -// public class YiAuditLoggingSqlSugarCoreModule:AbpModule -// { -// public override void ConfigureServices(ServiceConfigurationContext context) -// { -// context.Services.AddYiDbContext(); -// } -// } -//} +namespace Yi.AuditLogging.SqlSugarCore +{ + [DependsOn(typeof(AbpAuditLoggingDomainModule))] + [DependsOn(typeof(YiFrameworkSqlSugarCoreModule))] + public class YiAuditLoggingSqlSugarCoreModule : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddYiDbContext(); + } + } +}