ow
This commit is contained in:
1 parent
dc1c073ac6
commit
20abc22653
38 files changed
+143
-40
No files matched your search
@@ -27,6 +27,7 @@ namespace CloudBuilder.Core.DatabaseAccessor
|
||||
private readonly IConfiguration configuration;
|
||||
private readonly IHttpContextAccessor httpContextAccessor;
|
||||
private FileLog fileLog;
|
||||
public bool SkipAudit { get; set; } = false;
|
||||
|
||||
public MasterDbContext(DbContextOptions<MasterDbContext> options,
|
||||
IEnumerable<IMasterRegistEntityType<ModelBuilder>> entityManagers,
|
||||
@@ -67,6 +68,8 @@ namespace CloudBuilder.Core.DatabaseAccessor
|
||||
|
||||
public void SavingChangesEvent()
|
||||
{
|
||||
if (SkipAudit) return;
|
||||
|
||||
var entities = this.ChangeTracker.Entries()
|
||||
.Where(u => (u.State == EntityState.Modified || u.State == EntityState.Deleted || u.State == EntityState.Added)).ToList();
|
||||
if (entities == null || entities.Count < 1) return;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using CloudBuilder.Core.DependencyInjection.EFCore;
|
||||
using CloudBuilder.Core.Policy;
|
||||
using CloudBuilder.Core.Service;
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
@@ -20,6 +21,8 @@ namespace CloudBuilder.Core.DatabaseAccessor
|
||||
public IEnumerable<IMasterRegistEntityType<ModelBuilder>> _entityManagers;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IHttpContextAccessor httpContextAccessor;
|
||||
private FileLog fileLog;
|
||||
public bool SkipAudit { get; set; } = false;
|
||||
|
||||
public SecondaryDbContext(DbContextOptions<SecondaryDbContext> options,
|
||||
IEnumerable<IMasterRegistEntityType<ModelBuilder>> entityManagers,
|
||||
@@ -50,10 +53,14 @@ namespace CloudBuilder.Core.DatabaseAccessor
|
||||
|
||||
public void SavingChangesEvent()
|
||||
{
|
||||
if (SkipAudit) return;
|
||||
|
||||
var entities = this.ChangeTracker.Entries()
|
||||
.Where(u => (u.State == EntityState.Modified || u.State == EntityState.Deleted || u.State == EntityState.Added)).ToList();
|
||||
if (entities == null || entities.Count < 1) return;
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
string uid = this.httpContextAccessor.HttpContext?.User?.Identity?.Name;
|
||||
|
||||
if (string.IsNullOrEmpty(uid))
|
||||
@@ -115,6 +122,60 @@ namespace CloudBuilder.Core.DatabaseAccessor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
var entityName = entity.Metadata.GetTableName() ?? entity.Entity.GetType().Name;
|
||||
var state = entity.State;
|
||||
|
||||
// ======================================
|
||||
// 1. 获取所有主键(支持复合主键)
|
||||
// ======================================
|
||||
var pkProps = entity.Properties
|
||||
.Where(p => p.Metadata.IsPrimaryKey())
|
||||
.ToList();
|
||||
|
||||
// 拼接 WHERE 条件:Id=1 AND TenantId=2
|
||||
var whereClauses = pkProps
|
||||
.Select(p => $"{p.Metadata.GetColumnName() ?? p.Metadata.Name} = '{p.CurrentValue?.ToString()?.Replace("'", "''")}'");
|
||||
string whereSql = string.Join(" AND ", whereClauses);
|
||||
|
||||
string sqlLog = "";
|
||||
|
||||
// ======================================
|
||||
// 修改 UPDATE
|
||||
// ======================================
|
||||
if (state == EntityState.Modified)
|
||||
{
|
||||
var setClauses = entity.Properties
|
||||
.Where(p => p.IsModified) // 只记录真正修改的字段
|
||||
.Select(p => $"{p.Metadata.GetColumnName() ?? p.Metadata.Name} = '{p.CurrentValue?.ToString()?.Replace("'", "''")}'");
|
||||
|
||||
string setSql = string.Join(", ", setClauses);
|
||||
sqlLog = $"[{uid}][{now.ToString("yyyy-MM-dd HH:mm:ss")}]UPDATE {entityName} SET {setSql} WHERE {whereSql}";
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// 新增 INSERT
|
||||
// ======================================
|
||||
else if (state == EntityState.Added)
|
||||
{
|
||||
var data = entity.Properties.ToDictionary(p => p.Metadata.GetColumnName() ?? p.Metadata.Name, p => p.CurrentValue);
|
||||
var columns = string.Join(", ", data.Keys);
|
||||
var values = string.Join(", ", data.Values.Select(v => $"'{v?.ToString()?.Replace("'", "''")}'"));
|
||||
sqlLog = $"[{uid}][{now.ToString("yyyy-MM-dd HH:mm:ss")}]INSERT INTO {entityName} ({columns}) VALUES ({values})";
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// 删除 DELETE
|
||||
// ======================================
|
||||
else if (state == EntityState.Deleted)
|
||||
{
|
||||
sqlLog = $"[{uid}][{now.ToString("yyyy-MM-dd HH:mm:ss")}]DELETE FROM {entityName} WHERE {whereSql}";
|
||||
}
|
||||
|
||||
fileLog.WriteToFile(sqlLog);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
|
||||
Reference in new issue
Block a user