| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 | 
							- using System;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Threading.Tasks;
 
- using Newtonsoft.Json;
 
- using Newtonsoft.Json.Serialization;
 
- using FreeSql;
 
- using FreeSql.Aop;
 
- using FreeSql.DataAnnotations;
 
- using Admin.Core.Common.Configs;
 
- using Admin.Core.Common.Helpers;
 
- using Admin.Core.Model.Admin;
 
- using System.Collections.Generic;
 
- using System.Reflection;
 
- using Admin.Core.Common.BaseModel;
 
- namespace Admin.Core.Db
 
- {
 
-     public class DbHelper
 
-     {
 
-         /// <summary>
 
-         /// 创建数据库
 
-         /// </summary>
 
-         /// <param name="dbConfig"></param>
 
-         /// <returns></returns>
 
-         public async static Task CreateDatabaseAsync(DbConfig dbConfig)
 
-         {
 
-             if (!dbConfig.CreateDb || dbConfig.Type == DataType.Sqlite)
 
-             {
 
-                 return;
 
-             }
 
-             var db = new FreeSqlBuilder()
 
-                     .UseConnectionString(dbConfig.Type, dbConfig.CreateDbConnectionString)
 
-                     .Build();
 
-             try
 
-             {
 
-                 Console.WriteLine("\r\n create database started");
 
-                 await db.Ado.ExecuteNonQueryAsync(dbConfig.CreateDbSql);
 
-                 Console.WriteLine(" create database succeed");
 
-             }
 
-             catch (Exception e)
 
-             {
 
-                 Console.WriteLine($" create database failed.\n {e.Message}");
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 获得指定程序集表实体
 
-         /// </summary>
 
-         /// <returns></returns>
 
-         public static Type[] GetEntityTypes()
 
-         {
 
-             List<string> assemblyNames = new List<string>()
 
-             {
 
-                 "Admin.Core.Model"
 
-             };
 
-             List<Type> entityTypes = new List<Type>();
 
-             foreach (var assemblyName in assemblyNames)
 
-             {
 
-                 foreach (Type type in Assembly.Load(assemblyName).GetExportedTypes())
 
-                 {
 
-                     foreach (Attribute attribute in type.GetCustomAttributes())
 
-                     {
 
-                         if (attribute is TableAttribute tableAttribute)
 
-                         {
 
-                             if (tableAttribute.DisableSyncStructure == false)
 
-                             {
 
-                                 entityTypes.Add(type);
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-            
 
-             return entityTypes.ToArray();
 
-         }
 
-         /// <summary>
 
-         /// 同步结构
 
-         /// </summary>
 
-         public static void SyncStructure(IFreeSql db, string msg = null, DbConfig dbConfig = null, AppConfig appConfig = null)
 
-         {
 
-             //打印结构比对脚本
 
-             //var dDL = db.CodeFirst.GetComparisonDDLStatements<PermissionEntity>();
 
-             //Console.WriteLine("\r\n " + dDL);
 
-             //打印结构同步脚本
 
-             //db.Aop.SyncStructureAfter += (s, e) =>
 
-             //{
 
-             //    if (e.Sql.NotNull())
 
-             //    {
 
-             //        Console.WriteLine(" sync structure sql:\n" + e.Sql);
 
-             //    }
 
-             //};
 
-             // 同步结构
 
-             var dbType = dbConfig.Type.ToString();
 
-             Console.WriteLine($"\r\n {(msg.NotNull() ? msg : $"sync {dbType} structure")} started");
 
-             if(dbConfig.Type == DataType.Oracle)
 
-             {
 
-                 db.CodeFirst.IsSyncStructureToUpper = true;
 
-             }
 
-             //获得指定程序集表实体
 
-             var entityTypes = GetEntityTypes();
 
-             //非共享数据库实体配置,不生成租户Id
 
-             if(appConfig.TenantType != TenantType.Share)
 
-             {
 
-                 var iTenant = nameof(ITenant);
 
-                 var tenantId = nameof(ITenant.TenantId);
 
-                 foreach (var entityType in entityTypes)
 
-                 {
 
-                     if(entityType.GetInterfaces().Any(a=> a.Name == iTenant))
 
-                     {
 
-                         db.CodeFirst.Entity(entityType, a =>
 
-                         {
 
-                             a.Ignore(tenantId);
 
-                         });
 
-                     }
 
-                 }
 
-             }
 
-             db.CodeFirst.SyncStructure(entityTypes);
 
-             Console.WriteLine($" {(msg.NotNull() ? msg : $"sync {dbType} structure")} succeed");
 
-         }
 
-         /// <summary>
 
-         /// 检查实体属性是否为自增长
 
-         /// </summary>
 
-         /// <typeparam name="T"></typeparam>
 
-         /// <returns></returns>
 
-         private static bool CheckIdentity<T>() where T : class
 
-         {
 
-             var isIdentity = false;
 
-             var properties = typeof(T).GetProperties();
 
-             foreach (var property in properties)
 
-             {
 
-                 if (property.GetCustomAttributes(typeof(ColumnAttribute), false).FirstOrDefault() is ColumnAttribute columnAttribute && columnAttribute.IsIdentity)
 
-                 {
 
-                     isIdentity = true;
 
-                     break;
 
-                 }
 
-             }
 
-             return isIdentity;
 
-         }
 
-         /// <summary>
 
-         /// 初始化数据表数据
 
-         /// </summary>
 
-         /// <typeparam name="T"></typeparam>
 
-         /// <param name="db"></param>
 
-         /// <param name="data"></param>
 
-         /// <param name="tran"></param>
 
-         /// <param name="dbConfig"></param>
 
-         /// <returns></returns>
 
-         private static async Task InitDtDataAsync<T>(
 
-             IFreeSql db, 
 
-             T[] data, 
 
-             System.Data.Common.DbTransaction tran, 
 
-             DbConfig dbConfig = null
 
-         ) where T : class
 
-         {
 
-             var table = typeof(T).GetCustomAttributes(typeof(TableAttribute),false).FirstOrDefault() as TableAttribute;
 
-             var tableName = table.Name;
 
-             try
 
-             {
 
-                 if (!await db.Queryable<T>().AnyAsync())
 
-                 {
 
-                     if (data?.Length > 0)
 
-                     {
 
-                         var insert = db.Insert<T>();
 
-                         if(tran != null)
 
-                         {
 
-                             insert = insert.WithTransaction(tran);
 
-                         }
 
-                         var isIdentity = CheckIdentity<T>();
 
-                         if (isIdentity)
 
-                         {
 
-                             if (dbConfig.Type == DataType.SqlServer)
 
-                             {
 
-                                 var insrtSql = insert.AppendData(data).InsertIdentity().ToSql();
 
-                                 await db.Ado.ExecuteNonQueryAsync($"SET IDENTITY_INSERT {tableName} ON\n {insrtSql} \nSET IDENTITY_INSERT {tableName} OFF");
 
-                             }
 
-                             else
 
-                             {
 
-                                 await insert.AppendData(data).InsertIdentity().ExecuteAffrowsAsync();
 
-                             }
 
-                         }
 
-                         else
 
-                         {
 
-                             await insert.AppendData(data).ExecuteAffrowsAsync();
 
-                         }
 
-                         Console.WriteLine($" table: {tableName} sync data succeed");
 
-                     }
 
-                     else
 
-                     {
 
-                         Console.WriteLine($" table: {tableName} import data []");
 
-                     }
 
-                 }
 
-                 else
 
-                 {
 
-                     Console.WriteLine($" table: {tableName} record already exists");
 
-                 }
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 Console.WriteLine($" table: {tableName} sync data failed.\n{ex.Message}");
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 同步数据审计方法
 
-         /// </summary>
 
-         /// <param name="s"></param>
 
-         /// <param name="e"></param>
 
-         private static void SyncDataAuditValue(object s, AuditValueEventArgs e)
 
-         {
 
-             if (e.AuditValueType == AuditValueType.Insert)
 
-             {
 
-                 switch (e.Property.Name)
 
-                 {
 
-                     case "CreatedUserId":
 
-                         e.Value = 2;
 
-                         break;
 
-                     case "CreatedUserName":
 
-                         e.Value = "admin";
 
-                         break;
 
-                 }
 
-             }
 
-             else if (e.AuditValueType == AuditValueType.Update)
 
-             {
 
-                 switch (e.Property.Name)
 
-                 {
 
-                     case "ModifiedUserId":
 
-                         e.Value = 2;
 
-                         break;
 
-                     case "ModifiedUserName":
 
-                         e.Value = "admin";
 
-                         break;
 
-                 }
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 同步数据
 
-         /// </summary>
 
-         /// <returns></returns>
 
-         public static async Task SyncDataAsync(IFreeSql db, DbConfig dbConfig = null)
 
-         {
 
-             try
 
-             {
 
-                 //db.Aop.CurdBefore += (s, e) =>
 
-                 //{
 
-                 //    Console.WriteLine($"{e.Sql}\r\n");
 
-                 //};
 
-                 Console.WriteLine("\r\n sync data started");
 
-                 db.Aop.AuditValue += SyncDataAuditValue;
 
-                
 
-                 var filePath = Path.Combine(AppContext.BaseDirectory, "Db/Data/data.json").ToPath();
 
-                 var jsonData = FileHelper.ReadFile(filePath);
 
-                 var data = JsonConvert.DeserializeObject<Data>(jsonData);
 
-                 using (var uow = db.CreateUnitOfWork())
 
-                 using (var tran = uow.GetOrBeginTransaction())
 
-                 {
 
-                     await InitDtDataAsync(db, data.Dictionaries, tran, dbConfig);
 
-                     await InitDtDataAsync(db, data.Apis, tran, dbConfig);
 
-                     await InitDtDataAsync(db, data.Views, tran, dbConfig);
 
-                     await InitDtDataAsync(db, data.Permissions, tran, dbConfig);
 
-                     await InitDtDataAsync(db, data.Users, tran, dbConfig);
 
-                     await InitDtDataAsync(db, data.Roles, tran, dbConfig);
 
-                     await InitDtDataAsync(db, data.UserRoles, tran, dbConfig);
 
-                     await InitDtDataAsync(db, data.RolePermissions, tran, dbConfig);
 
-                     await InitDtDataAsync(db, data.Tenants, tran, dbConfig);
 
-                     uow.Commit();
 
-                 }
 
-                 db.Aop.AuditValue -= SyncDataAuditValue;
 
-                 Console.WriteLine(" sync data succeed");
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 throw new Exception($" sync data failed.\n{ex.Message}");
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 生成极简数据
 
-         /// </summary>
 
-         /// <param name="db"></param>
 
-         /// <returns></returns>
 
-         public static async Task GenerateSimpleJsonDataAsync(IFreeSql db)
 
-         {
 
-             try
 
-             {
 
-                 Console.WriteLine("\r\n generate data started");
 
-                 #region 数据表
 
-                 #region 数据字典
 
-                 var dictionaries = await db.Queryable<DictionaryEntity>().ToListAsync(a => new
 
-                 {
 
-                     a.Id,
 
-                     a.ParentId,
 
-                     a.Name,
 
-                     a.Code,
 
-                     a.Value,
 
-                     a.Description,
 
-                     a.Sort
 
-                 });
 
-                 #endregion
 
-                 #region 接口
 
-                 var apis = await db.Queryable<ApiEntity>().ToListAsync(a => new
 
-                 {
 
-                     a.Id,
 
-                     a.ParentId,
 
-                     a.Name,
 
-                     a.Label,
 
-                     a.Path,
 
-                     a.HttpMethods,
 
-                     a.Description,
 
-                     a.Sort
 
-                 });
 
-                 #endregion
 
-                 #region 视图
 
-                 var views = await db.Queryable<ViewEntity>().ToListAsync(a => new
 
-                 {
 
-                     a.Id,
 
-                     a.ParentId,
 
-                     a.Name,
 
-                     a.Label,
 
-                     a.Path,
 
-                     a.Description,
 
-                     a.Sort
 
-                 });
 
-                 #endregion
 
-                 #region 权限
 
-                 var permissions = await db.Queryable<PermissionEntity>().ToListAsync(a => new
 
-                 {
 
-                     a.Id,
 
-                     a.ParentId,
 
-                     a.Label,
 
-                     a.Code,
 
-                     a.Type,
 
-                     a.ViewId,
 
-                     a.ApiId,
 
-                     a.Path,
 
-                     a.Icon,
 
-                     a.Closable,
 
-                     a.Opened,
 
-                     a.NewWindow,
 
-                     a.External,
 
-                     a.Sort,
 
-                     a.Description
 
-                 });
 
-                 #endregion
 
-                 #region 用户
 
-                 var users = await db.Queryable<UserEntity>().ToListAsync(a => new
 
-                 {
 
-                     a.Id,
 
-                     a.UserName,
 
-                     a.Password,
 
-                     a.NickName,
 
-                     a.Avatar,
 
-                     a.Status,
 
-                     a.Remark
 
-                 });
 
-                 #endregion
 
-                 #region 角色
 
-                 var roles = await db.Queryable<RoleEntity>().ToListAsync(a => new
 
-                 {
 
-                     a.Id,
 
-                     a.Name,
 
-                     a.Sort,
 
-                     a.Description
 
-                 });
 
-                 #endregion
 
-                 #region 用户角色
 
-                 var userRoles = await db.Queryable<UserRoleEntity>().ToListAsync(a => new
 
-                 {
 
-                     a.Id,
 
-                     a.UserId,
 
-                     a.RoleId
 
-                 });
 
-                 #endregion
 
-                 #region 角色权限
 
-                 var rolePermissions = await db.Queryable<RolePermissionEntity>().ToListAsync(a => new
 
-                 {
 
-                     a.Id,
 
-                     a.RoleId,
 
-                     a.PermissionId
 
-                 });
 
-                 #endregion
 
-                 #region 租户
 
-                 var tenants = await db.Queryable<TenantEntity>().ToListAsync(a => new
 
-                 {
 
-                     a.Id,
 
-                     a.Name,
 
-                     a.Code,
 
-                     a.DbType,
 
-                     a.ConnectionString,
 
-                     a.IdleTime,
 
-                     a.Description
 
-                 });
 
-                 #endregion
 
-                 #endregion
 
-                 if (!(users?.Count > 0))
 
-                 {
 
-                     return;
 
-                 }
 
-                 #region 生成数据
 
-                 var settings = new JsonSerializerSettings();
 
-                 settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
 
-                 settings.NullValueHandling = NullValueHandling.Ignore;
 
-                 settings.DefaultValueHandling = DefaultValueHandling.Ignore;
 
-                 var jsonData = JsonConvert.SerializeObject(new
 
-                 {
 
-                     dictionaries,
 
-                     apis,
 
-                     views,
 
-                     permissions,
 
-                     users,
 
-                     roles,
 
-                     userRoles,
 
-                     rolePermissions,
 
-                     tenants
 
-                 },
 
-                 //Formatting.Indented, 
 
-                 settings
 
-                 );
 
-                 var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Db/Data/data.json").ToPath();
 
-                 FileHelper.WriteFile(filePath, jsonData);
 
-                 #endregion
 
-                 Console.WriteLine(" generate data succeed\r\n");
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 throw new Exception($" generate data failed。\n{ex.Message}\r\n");
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |