Bläddra i källkod

dbconfig新增同步更新数据SysUpdateData开关
修改同步数据,初始化数据支持实时插入和更新数据

zhontai 2 år sedan
förälder
incheckning
65beb13ebe

+ 3 - 1
src/hosts/ZhonTai.Host/Configs/dbconfig.json

@@ -26,8 +26,10 @@
 
   //同步结构
   "syncStructure": true,
-  //同步数据
+  //同步数据,只新增数据不修改数据,想要修改数据需开启sysUpdateData
   "syncData": true,
+  //同步更新数据
+  "sysUpdateData": false,
   //同步数据地址
   //"SyncDataPath": "InitData/Admin/Vue2",
 

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

@@ -48,6 +48,11 @@ public class DbConfig
     /// </summary>
     public bool SyncData { get; set; } = false;
 
+    /// <summary>
+    /// 同步更新数据
+    /// </summary>
+    public bool SysUpdateData { get; set; } = false;
+
     /// <summary>
     /// 同步数据地址
     /// </summary>

+ 13 - 29
src/platform/ZhonTai.Admin/Core/Db/Data/SyncData.cs

@@ -41,14 +41,14 @@ public abstract class SyncData
     /// <param name="db"></param>
     /// <param name="unitOfWork"></param>
     /// <param name="tran"></param>
-    /// <param name="data"></param>
+    /// <param name="dataList"></param>
     /// <param name="dbConfig"></param>
     /// <returns></returns>
     protected virtual async Task InitDataAsync<T>(
         IFreeSql db,
         IUnitOfWork unitOfWork,
         System.Data.Common.DbTransaction tran,
-        T[] data,
+        T[] dataList,
         DbConfig dbConfig = null
     ) where T : class, new()
     {
@@ -57,44 +57,28 @@ public abstract class SyncData
 
         try
         {
-            if (await db.Queryable<T>().DisableGlobalFilter(FilterNames.Tenant, FilterNames.Member).AnyAsync())
-            {
-                Console.WriteLine($" table: {tableName} record already exists");
-                return;
-            }
+            //if (await db.Queryable<T>().DisableGlobalFilter(FilterNames.Tenant, FilterNames.Member).AnyAsync())
+            //{
+            //    Console.WriteLine($" table: {tableName} record already exists");
+            //    return;
+            //}
 
-            if (!(data?.Length > 0))
+            if (!(dataList?.Length > 0))
             {
                 Console.WriteLine($" table: {tableName} import data []");
                 return;
             }
 
-            var repo = db.GetRepository<T>();
-            var insert = db.Insert<T>();
+            var insertOrUpdate = db.InsertOrUpdate<T>();
             if (unitOfWork != null)
             {
-                repo.UnitOfWork = unitOfWork;
-                insert = insert.WithTransaction(tran);
-            }
-
-            var isIdentity = CheckIdentity<T>();
-            if (isIdentity)
-            {
-                if (dbConfig.Type == DataType.SqlServer)
-                {
-                    var insrtSql = insert.AppendData(data).InsertIdentity().ToSql();
-                    await repo.Orm.Ado.ExecuteNonQueryAsync($"SET IDENTITY_INSERT {tableName} ON\n {insrtSql} \nSET IDENTITY_INSERT {tableName} OFF");
-                }
-                else
-                {
-                    await insert.AppendData(data).InsertIdentity().ExecuteAffrowsAsync();
-                }
+                insertOrUpdate = insertOrUpdate.WithTransaction(tran);
             }
-            else
+            if (!dbConfig.SysUpdateData)
             {
-                repo.DbContextOptions.EnableCascadeSave = true;
-                await repo.InsertAsync(data);
+                insertOrUpdate.IfExistsDoNothing();
             }
+            await insertOrUpdate.SetSource(dataList).ExecuteAffrowsAsync();
 
             Console.WriteLine($" table: {tableName} sync data succeed");
         }

+ 7 - 5
src/platform/ZhonTai.Admin/Repositories/CustomSyncData.cs

@@ -16,6 +16,8 @@ using ZhonTai.Admin.Domain.Org;
 using ZhonTai.Admin.Domain.UserStaff;
 using ZhonTai.Admin.Core.Db.Data;
 using ZhonTai.Admin.Domain.UserOrg;
+using System.Linq;
+using ZhonTai.Common.Extensions;
 
 namespace ZhonTai.Admin.Repositories;
 
@@ -39,20 +41,20 @@ public class CustomSyncData : SyncData, ISyncData
         var staffs = GetData<UserStaffEntity>(isTenant, dbConfig.SyncDataPath);
         await InitDataAsync(db, uow, tran, staffs, dbConfig);
 
-        var orgs = GetData<OrgEntity>(isTenant, dbConfig.SyncDataPath);
-        await InitDataAsync(db, uow, tran, orgs, dbConfig);
+        var orgTree = GetData<OrgEntity>(isTenant, dbConfig.SyncDataPath);
+        await InitDataAsync(db, uow, tran, orgTree.ToList().ToPlainList((a) => a.Childs).ToArray(), dbConfig);
 
         var roles = GetData<RoleEntity>(isTenant, dbConfig.SyncDataPath);
         await InitDataAsync(db, uow, tran, roles, dbConfig);
 
         var apiTree = GetData<ApiEntity>(path: dbConfig.SyncDataPath);
-        await InitDataAsync(db, uow, tran, apiTree, dbConfig);
+        await InitDataAsync(db, uow, tran, apiTree.ToList().ToPlainList((a) => a.Childs).ToArray(), dbConfig);
 
         var viewTree = GetData<ViewEntity>(path: dbConfig.SyncDataPath);
-        await InitDataAsync(db, uow, tran, viewTree, dbConfig);
+        await InitDataAsync(db, uow, tran, viewTree.ToList().ToPlainList((a) => a.Childs).ToArray(), dbConfig);
 
         var permissionTree = GetData<PermissionEntity>(path: dbConfig.SyncDataPath);
-        await InitDataAsync(db, uow, tran, permissionTree, dbConfig);
+        await InitDataAsync(db, uow, tran, permissionTree.ToList().ToPlainList((a) => a.Childs).ToArray(), dbConfig);
 
         var userRoles = GetData<UserRoleEntity>(path: dbConfig.SyncDataPath);
         await InitDataAsync(db, uow, tran, userRoles, dbConfig);

+ 5 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -571,6 +571,11 @@
             同步数据
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.DbConfig.SysUpdateData">
+            <summary>
+            同步更新数据
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Core.Configs.DbConfig.SyncDataPath">
             <summary>
             同步数据地址