Browse Source

新增dbconfig.json.syncDataUser同步数据操作用户配置

zhontai 2 years ago
parent
commit
7a5a4c9c9a

+ 6 - 0
src/hosts/ZhonTai.Host/Configs/dbconfig.json

@@ -43,6 +43,12 @@
   //同步排除表["ad_user"]
   //同步数据排除表,指定表不同步
   "syncDataExcludeTables": [],
+  //同步数据操作用户
+  "syncDataUser": {
+    "id": 161223411986501,
+    "userName": "admin",
+    "tenantId": 161223412138053
+  },
 
   //项目初始化不开启生成数据,发布生产环境前,如果开发环境有配置数据需要更新数据包,可以开启生成数据包,使用完记得关闭
   //开启生成数据前先关闭createDb syncStructure syncData sysUpdateData

+ 32 - 32
src/hosts/ZhonTai.Host/Program.cs

@@ -18,21 +18,21 @@ new HostApp(new HostAppOptions
     //配置FreeSql
     ConfigureFreeSql = (freeSql, dbConfig) =>
     {
-        //if(dbConfig.Key == DbKeys.AppDb)
-        //{
-        //    freeSql.SyncSchedulerStructure(dbConfig, (fsql) =>
-        //    {
-        //        fsql.CodeFirst
-        //        .ConfigEntity<TaskInfo>(a =>
-        //        {
-        //            a.Name("app_task");
-        //        })
-        //        .ConfigEntity<TaskLog>(a =>
-        //        {
-        //            a.Name("app_task_log");
-        //        });
-        //    });
-        //}
+        if (dbConfig.Key == DbKeys.AppDb)
+        {
+            freeSql.SyncSchedulerStructure(dbConfig, (fsql) =>
+            {
+                fsql.CodeFirst
+                .ConfigEntity<TaskInfo>(a =>
+                {
+                    a.Name("app_task");
+                })
+                .ConfigEntity<TaskLog>(a =>
+                {
+                    a.Name("app_task_log");
+                });
+            });
+        }
     },
 
 	//配置后置服务
@@ -66,24 +66,24 @@ new HostApp(new HostAppOptions
         }).AddSubscriberAssembly(assemblies);
 
         //添加任务调度
-        //context.Services.AddTaskScheduler(DbKeys.AppDb, options =>
-        //{
-        //    options.ConfigureFreeSql = freeSql =>
-        //    {
-        //        freeSql.CodeFirst
-        //        .ConfigEntity<TaskInfo>(a =>
-        //        {
-        //            a.Name("app_task");
-        //        })
-        //        .ConfigEntity<TaskLog>(a =>
-        //        {
-        //            a.Name("app_task_log");
-        //        });
-        //    };
+        context.Services.AddTaskScheduler(DbKeys.AppDb, options =>
+        {
+            options.ConfigureFreeSql = freeSql =>
+            {
+                freeSql.CodeFirst
+                .ConfigEntity<TaskInfo>(a =>
+                {
+                    a.Name("app_task");
+                })
+                .ConfigEntity<TaskLog>(a =>
+                {
+                    a.Name("app_task_log");
+                });
+            };
 
-        //    //模块任务处理器
-        //    options.TaskHandler = new CloudTaskHandler(options.FreeSqlCloud, DbKeys.AppDb);
-        //});
+            //模块任务处理器
+            options.TaskHandler = new CloudTaskHandler(options.FreeSqlCloud, DbKeys.AppDb);
+        });
     },
 
     //配置Autofac容器

+ 26 - 0
src/platform/ZhonTai.Admin/Core/Configs/DbConfig.cs

@@ -78,6 +78,11 @@ public class DbConfig
     /// </summary>
     public string[] SyncDataExcludeTables { get; set; }
 
+    /// <summary>
+    /// 同步数据操作用户
+    /// </summary>
+    public SyncDataUser SyncDataUser { get; set; } = new SyncDataUser { Id = 161223411986501, UserName = "admin", TenantId = 161223412138053 };
+
     /// <summary>
     /// 建库
     /// </summary>
@@ -133,4 +138,25 @@ public class SlaveDb
     /// 数据库连接字符串
     /// </summary>
     public string ConnectionString { get; set; }
+}
+
+/// <summary>
+/// 同步数据操作用户
+/// </summary>
+public class SyncDataUser
+{
+    /// <summary>
+    /// 用户Id
+    /// </summary>
+    public long Id { get; set; }
+
+    /// <summary>
+    /// 账号
+    /// </summary>
+    public string UserName { get; set; }
+
+    /// <summary>
+    /// 租户Id
+    /// </summary>
+    public long TenantId { get; set; }
 }

+ 72 - 76
src/platform/ZhonTai.Admin/Core/Db/DbHelper.cs

@@ -74,11 +74,11 @@ public class DbHelper
     /// <returns></returns>
     public static Type[] GetEntityTypes(string[] assemblyNames)
     {
-        if(!(assemblyNames?.Length > 0))
+        if (!(assemblyNames?.Length > 0))
         {
             return null;
         }
- 
+
         var entityTypes = new List<Type>();
 
         foreach (var assemblyName in assemblyNames)
@@ -203,7 +203,7 @@ public class DbHelper
 
             }
         }
-        
+
         if (e.AuditValueType is AuditValueType.Update or AuditValueType.InsertOrUpdate)
         {
             switch (e.Property.Name)
@@ -237,7 +237,7 @@ public class DbHelper
         //Console.WriteLine($"{Environment.NewLine}" + dDL);
 
         //打印结构同步脚本
-        if(dbConfig.SyncStructureSql)
+        if (dbConfig.SyncStructureSql)
         {
             db.Aop.SyncStructureAfter += SyncStructureAfter;
         }
@@ -271,76 +271,6 @@ public class DbHelper
         }
     }
 
-    /// <summary>
-    /// 同步数据审计方法
-    /// </summary>
-    /// <param name="s"></param>
-    /// <param name="e"></param>
-    private static void SyncDataAuditValue(object s, AuditValueEventArgs e)
-    {
-        var user = new { Id = 161223411986501, Name = "admin", TenantId = 161223412138053 };
-
-        if (e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
-               && (e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
-               && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
-        {
-            e.Value = DateTime.Now.Subtract(TimeOffset);
-        }
-
-        if (e.Column.CsType == typeof(long)
-        && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) != null
-        && (e.Value == null || (long)e.Value == default || (long?)e.Value == default))
-        {
-            e.Value = YitIdHelper.NextId();
-        }
-
-        if (user == null || user.Id <= 0)
-        {
-            return;
-        }
-
-        if (e.AuditValueType is AuditValueType.Insert or AuditValueType.InsertOrUpdate)
-        {
-            switch (e.Property.Name)
-            {
-                case "CreatedUserId":
-                    if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
-                    {
-                        e.Value = user.Id;
-                    }
-                    break;
-
-                case "CreatedUserName":
-                    if (e.Value == null || ((string)e.Value).IsNull())
-                    {
-                        e.Value = user.Name;
-                    }
-                    break;
-
-                case "TenantId":
-                    if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
-                    {
-                        e.Value = user.TenantId;
-                    }
-                    break;
-            }
-        }
-
-        if (e.AuditValueType is AuditValueType.Update or AuditValueType.InsertOrUpdate)
-        {
-            switch (e.Property.Name)
-            {
-                case "ModifiedUserId":
-                    e.Value = user.Id;
-                    break;
-
-                case "ModifiedUserName":
-                    e.Value = user.Name;
-                    break;
-            }
-        }
-    }
-
     /// <summary>
     /// 同步数据
     /// </summary>
@@ -350,8 +280,8 @@ public class DbHelper
     /// <returns></returns>
     /// <exception cref="Exception"></exception>
     public static async Task SyncDataAsync(
-        IFreeSql db, 
-        DbConfig dbConfig = null, 
+        IFreeSql db,
+        DbConfig dbConfig = null,
         AppConfig appConfig = null
     )
     {
@@ -361,6 +291,72 @@ public class DbHelper
 
             if (dbConfig.AssemblyNames?.Length > 0)
             {
+                var user = dbConfig.SyncDataUser;
+
+                // 同步数据审计方法
+                void SyncDataAuditValue(object s, AuditValueEventArgs e)
+                {
+                    if (e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
+                           && (e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
+                           && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
+                    {
+                        e.Value = DateTime.Now.Subtract(TimeOffset);
+                    }
+
+                    if (e.Column.CsType == typeof(long)
+                    && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) != null
+                    && (e.Value == null || (long)e.Value == default || (long?)e.Value == default))
+                    {
+                        e.Value = YitIdHelper.NextId();
+                    }
+
+                    if (user == null || user.Id <= 0)
+                    {
+                        return;
+                    }
+
+                    if (e.AuditValueType is AuditValueType.Insert or AuditValueType.InsertOrUpdate)
+                    {
+                        switch (e.Property.Name)
+                        {
+                            case "CreatedUserId":
+                                if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
+                                {
+                                    e.Value = user.Id;
+                                }
+                                break;
+
+                            case "CreatedUserName":
+                                if (e.Value == null || ((string)e.Value).IsNull())
+                                {
+                                    e.Value = user.UserName;
+                                }
+                                break;
+
+                            case "TenantId":
+                                if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
+                                {
+                                    e.Value = user.TenantId;
+                                }
+                                break;
+                        }
+                    }
+
+                    if (e.AuditValueType is AuditValueType.Update or AuditValueType.InsertOrUpdate)
+                    {
+                        switch (e.Property.Name)
+                        {
+                            case "ModifiedUserId":
+                                e.Value = user.Id;
+                                break;
+
+                            case "ModifiedUserName":
+                                e.Value = user.UserName;
+                                break;
+                        }
+                    }
+                }
+
                 db.Aop.AuditValue += SyncDataAuditValue;
 
                 if (dbConfig.SyncDataCurd)

+ 10 - 11
src/platform/ZhonTai.Admin/Repositories/CustomSyncData.cs

@@ -20,7 +20,6 @@ using System.Linq;
 using ZhonTai.Common.Extensions;
 using System;
 using FreeSql;
-using ZhonTai.Admin.Core.Auth;
 using Mapster;
 using System.Collections.Generic;
 
@@ -70,7 +69,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);
@@ -128,7 +127,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);
@@ -187,7 +186,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);
@@ -246,7 +245,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);
@@ -306,7 +305,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);
@@ -365,7 +364,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);
@@ -424,7 +423,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);
@@ -483,7 +482,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);
@@ -542,7 +541,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);
@@ -820,7 +819,7 @@ public class CustomSyncData : SyncData, ISyncData
             }
 
             //修改
-            if (!dbConfig.SysUpdateData && recordList?.Count > 0)
+            if (dbConfig.SysUpdateData && recordList?.Count > 0)
             {
                 var updateDataList = dataList.Where(a => recordIds.Contains(a.Id));
                 await rep.UpdateAsync(updateDataList);

+ 1 - 1
src/platform/ZhonTai.Admin/ZhonTai.Admin.csproj

@@ -33,7 +33,7 @@
 		<PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
 		<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
 		<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
-		<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.28.1" />
+		<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.29.0" />
 		<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
 		<PackageReference Include="UAParser" Version="3.1.47" />
 		<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />

+ 25 - 7
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -680,6 +680,11 @@
             同步数据排除表列表
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.DbConfig.SyncDataUser">
+            <summary>
+            同步数据操作用户
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Core.Configs.DbConfig.CreateDb">
             <summary>
             建库
@@ -735,6 +740,26 @@
             数据库连接字符串
             </summary>
         </member>
+        <member name="T:ZhonTai.Admin.Core.Configs.SyncDataUser">
+            <summary>
+            同步数据操作用户
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.SyncDataUser.Id">
+            <summary>
+            用户Id
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.SyncDataUser.UserName">
+            <summary>
+            账号
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.SyncDataUser.TenantId">
+            <summary>
+            租户Id
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Configs.JwtConfig">
             <summary>
             Jwt配置
@@ -1120,13 +1145,6 @@
             同步结构
             </summary>
         </member>
-        <member name="M:ZhonTai.Admin.Core.Db.DbHelper.SyncDataAuditValue(System.Object,FreeSql.Aop.AuditValueEventArgs)">
-            <summary>
-            同步数据审计方法
-            </summary>
-            <param name="s"></param>
-            <param name="e"></param>
-        </member>
         <member name="M:ZhonTai.Admin.Core.Db.DbHelper.SyncDataAsync(IFreeSql,ZhonTai.Admin.Core.Configs.DbConfig,ZhonTai.Admin.Core.Configs.AppConfig)">
             <summary>
             同步数据