using Castle.Components.DictionaryAdapter.Xml; 
 | 
using Microsoft.AspNetCore.Mvc.Rendering; 
 | 
using Newtonsoft.Json; 
 | 
using SqlSugar; 
 | 
using System; 
 | 
using System.Collections.Generic; 
 | 
using System.Dynamic; 
 | 
using System.Linq; 
 | 
using System.Net.Sockets; 
 | 
using System.Reflection; 
 | 
using System.Text; 
 | 
using System.Threading.Tasks; 
 | 
using WIDESEA_Core.Const; 
 | 
using WIDESEA_Core.DB; 
 | 
using WIDESEA_Core.Helper; 
 | 
using WIDESEA_Core.Tenants; 
 | 
  
 | 
namespace WIDESEA_Core.Seed 
 | 
{ 
 | 
    public class DBSeed 
 | 
    { 
 | 
        private static string SeedDataFolder = "WIDESEAWCS_DB.DBSeed.Json/{0}.tsv"; 
 | 
  
 | 
        /// <summary> 
 | 
        /// 异步添加种子数据 
 | 
        /// </summary> 
 | 
        /// <param name="myContext"></param> 
 | 
        /// <param name="WebRootPath"></param> 
 | 
        /// <returns></returns> 
 | 
        public static async Task SeedAsync(DBContext dbContext, string WebRootPath) 
 | 
        { 
 | 
            try 
 | 
            { 
 | 
                if (string.IsNullOrEmpty(WebRootPath)) 
 | 
                { 
 | 
                    throw new Exception("获取wwwroot路径时,异常!"); 
 | 
                } 
 | 
  
 | 
                SeedDataFolder = Path.Combine(WebRootPath, SeedDataFolder); 
 | 
  
 | 
                Console.WriteLine("************ WIDESEA DataBase Set *****************"); 
 | 
  
 | 
                //Console.WriteLine($"Master DB ConId: {DBContext.ConnId}"); 
 | 
                Console.WriteLine($"Master DB Type: {DBContext.DbType}"); 
 | 
                //Console.WriteLine($"Master DB ConnectString: {DBContext.ConnectionString}"); 
 | 
  
 | 
                Console.WriteLine(); 
 | 
  
 | 
                // 创建数据库 
 | 
                Console.WriteLine($"Create Database(The Db Id:{DBContext.ConnId})..."); 
 | 
  
 | 
                if (DBContext.DbType != SqlSugar.DbType.Oracle) 
 | 
                { 
 | 
                    dbContext.Db.DbMaintenance.CreateDatabase(); 
 | 
                    ConsoleHelper.WriteSuccessLine($"Database Created Successfully!"); 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    //Oracle 数据库不支持该操作 
 | 
                    ConsoleHelper.WriteSuccessLine($"Oracle 数据库不支持该操作,可手动创建Oracle数据库!"); 
 | 
                } 
 | 
  
 | 
                // 创建数据库表,遍历指定命名空间下的class, 
 | 
                // 注意不要把其他命名空间下的也添加进来。 
 | 
                Console.WriteLine("Create Tables..."); 
 | 
  
 | 
                var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory; 
 | 
                var referencedAssemblies = System.IO.Directory.GetFiles(path, MainDb.AssemblyName).Select(Assembly.LoadFrom).ToArray(); 
 | 
  
 | 
                var modelTypes = referencedAssemblies 
 | 
                    .SelectMany(a => a.DefinedTypes) 
 | 
                    .Select(type => type.AsType()) 
 | 
                    .Where(x => x.IsClass && x.Namespace is MainDb.EntityNameSpace && x.GetCustomAttribute<SugarTable>() != null) 
 | 
                    .ToList(); 
 | 
  
 | 
                modelTypes.ForEach(t => 
 | 
                { 
 | 
                    //var diffString = dbContext.Db.CodeFirst.GetDifferenceTables(t).ToDiffString(); 
 | 
                    // 这里只支持添加表,不支持删除 
 | 
                    // 如果想要删除,数据库直接右键删除,或者联系SqlSugar作者; 
 | 
                    IDbMaintenance dbMaintenance = dbContext.Db.DbMaintenance; 
 | 
                    if (!dbMaintenance.IsAnyTable(t.Name, false)) 
 | 
                    { 
 | 
                        ConsoleHelper.WriteSuccessLine($"Table [{t.Name}] Created Successfully"); 
 | 
                        dbContext.Db.CodeFirst.InitTables(t); 
 | 
  
 | 
                        string seedData = FileHelper.ReadFile(string.Format(SeedDataFolder, t.Name), Encoding.UTF8); 
 | 
  
 | 
                        #region AddSeedData 
 | 
                        if (seedData != "不存在相应的目录") 
 | 
                        { 
 | 
                            List<Dictionary<string, object>> dicFile = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(seedData); 
 | 
  
 | 
                            if (dicFile.Count > 0) 
 | 
                            { 
 | 
                                List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>(); 
 | 
  
 | 
                                List<string> columnNames = dbContext.Db.DbMaintenance.GetColumnInfosByTableName(t.Name, false).Select(x => x.DbColumnName).ToList(); 
 | 
                                var a = t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name)); 
 | 
  
 | 
                                List<PropertyInfo> propertyInfos = t.GetProperties().Where(x => columnNames.Contains(x.Name)).ToList(); 
 | 
                                for (int j = 0; j < dicFile.Count; j++) 
 | 
                                { 
 | 
                                    Dictionary<string, object> keyValuePairs = new Dictionary<string, object>(); 
 | 
                                    for (int i = 0; i < propertyInfos.Count; i++) 
 | 
                                    { 
 | 
                                        PropertyInfo propertyInfo = propertyInfos[i]; 
 | 
                                        SugarColumn sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); 
 | 
                                        if (sugarColumn != null) 
 | 
                                        { 
 | 
                                            if (!sugarColumn.IsIgnore) 
 | 
                                            { 
 | 
                                                keyValuePairs.Add(propertyInfo.Name, dicFile[j][propertyInfo.Name]); 
 | 
                                            } 
 | 
                                        } 
 | 
                                    } 
 | 
                                    dic.Add(keyValuePairs); 
 | 
                                } 
 | 
  
 | 
                                if (dic.Count > 0) 
 | 
                                { 
 | 
                                    for (int i = 0; i < dic.Count; i++) 
 | 
                                    { 
 | 
                                        if (dic[i].ContainsKey("CreateDate")) 
 | 
                                            dic[i]["CreateDate"] = DateTime.Now; 
 | 
                                        else 
 | 
                                            dic[i].Add("CreateDate", DateTime.Now); 
 | 
                                    } 
 | 
                                    string str = $"SET IDENTITY_INSERT {t.Name} ON;"; 
 | 
  
 | 
                                    str += dbContext.Db.Insertable(dic).AS(t.Name).ToSqlString(); 
 | 
  
 | 
                                    str += ($"SET IDENTITY_INSERT {t.Name} OFF;"); 
 | 
  
 | 
                                    dbContext.Db.Ado.ExecuteCommand(str); 
 | 
  
 | 
                                    ConsoleHelper.WriteSuccessLine($"Table [{t.Name}] SeedData Added Successfully"); 
 | 
                                } 
 | 
                            } 
 | 
                        } 
 | 
                        #endregion 
 | 
                    } 
 | 
                    else 
 | 
                    { 
 | 
                        List<string> columnNames = dbContext.Db.DbMaintenance.GetColumnInfosByTableName(t.Name, false).Select(x => x.DbColumnName).ToList(); 
 | 
                        if (t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name)) != null) 
 | 
                        { 
 | 
                            bool isChange = true; 
 | 
                            List<PropertyInfo> propertyInfos = t.GetProperties().Where(x => !columnNames.Contains(x.Name)).ToList(); 
 | 
                            for (int i = 0; i < propertyInfos.Count; i++) 
 | 
                            { 
 | 
                                PropertyInfo propertyInfo = propertyInfos[i]; 
 | 
                                SugarColumn sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>(); 
 | 
                                if (sugarColumn != null) 
 | 
                                { 
 | 
                                    if (!sugarColumn.IsIgnore) 
 | 
                                    { 
 | 
                                        if (!sugarColumn.IsNullable) 
 | 
                                        { 
 | 
                                            isChange = false; 
 | 
                                            break; 
 | 
                                        } 
 | 
                                    } 
 | 
                                } 
 | 
                            } 
 | 
                            if (isChange) 
 | 
                                dbContext.Db.CodeFirst.InitTables(t); 
 | 
                        } 
 | 
                    } 
 | 
                }); 
 | 
                ConsoleHelper.WriteSuccessLine($"Tables Created Successfully!"); 
 | 
                Console.WriteLine(); 
 | 
  
 | 
  
 | 
            } 
 | 
            catch (Exception ex) 
 | 
            { 
 | 
                // 1、若是Mysql,查看常见问题:https://github.com/anjoy8/Blog.Core/issues/148#issue-776281770 
 | 
                //2、若是Oracle,查看常见问题:https://github.com/anjoy8/Blog.Core/issues/148#issuecomment-752340231 
 | 
                throw new Exception("错误:" + ex.Message); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        /// <summary> 
 | 
        /// 初始化 多租户 
 | 
        /// </summary> 
 | 
        /// <param name="dbContext"></param> 
 | 
        /// <returns></returns> 
 | 
        public static async Task TenantSeedAsync(DBContext dbContext) 
 | 
        { 
 | 
  
 | 
            if (BaseDBConfig.MutiConnectionString.Where(x => x.ConnId != MainDb.CurrentDbConnId).Any()) 
 | 
            { 
 | 
                Console.WriteLine($@"Init Multi Tenant Db"); 
 | 
                foreach (MutiDBOperate tenant in BaseDBConfig.MutiConnectionString.Where(x => x.ConnId != MainDb.CurrentDbConnId)) 
 | 
                { 
 | 
  
 | 
                    Console.WriteLine($@"Init Multi Tenant Db : {tenant.ConnId}"); 
 | 
                    ConnectionConfig connectionConfig = new ConnectionConfig() 
 | 
                    { 
 | 
                        ConfigId = tenant.ConnId, 
 | 
                        ConnectionString = tenant.Connection, 
 | 
                        IsAutoCloseConnection = true, 
 | 
                        MoreSettings = new ConnMoreSettings() 
 | 
                        { 
 | 
                            IsAutoRemoveDataCache = true 
 | 
                        }, 
 | 
                        DbType = (DbType)tenant.DbType, 
 | 
                    }; 
 | 
                    await InitTenantSeedAsync(dbContext.Db.AsTenant(), connectionConfig); 
 | 
                } 
 | 
  
 | 
                Console.WriteLine(DateTime.Now + $@"Init Multi Tenant Db Finish"); 
 | 
            } 
 | 
  
 | 
            //tenants = await myContext.Db.Queryable<SysTenant>().Where(s => s.TenantType == TenantTypeEnum.Tables).ToListAsync(); 
 | 
            //if (tenants.Any()) 
 | 
            //{ 
 | 
            //    await InitTenantSeedAsync(myContext, tenants); 
 | 
            //} 
 | 
        } 
 | 
  
 | 
        #region 多租户 多库 初始化 
 | 
  
 | 
        /// <summary> 
 | 
        /// 初始化多库 
 | 
        /// </summary> 
 | 
        /// <param name="itenant"></param> 
 | 
        /// <param name="config"></param> 
 | 
        /// <returns></returns> 
 | 
        public static async Task InitTenantSeedAsync(ITenant itenant, ConnectionConfig config) 
 | 
        { 
 | 
            Console.WriteLine(DateTime.Now + $@"Init Multi Tenant Db"); 
 | 
            //itenant.RemoveConnection(config.ConfigId); 
 | 
            itenant.AddConnection(config); 
 | 
  
 | 
            var db = itenant.GetConnectionScope(config.ConfigId); 
 | 
  
 | 
            db.DbMaintenance.CreateDatabase(); 
 | 
            ConsoleHelper.WriteSuccessLine($"Init Multi Tenant Db : {config.ConfigId} Database created successfully!"); 
 | 
  
 | 
            Console.WriteLine($@"Init Multi Tenant Db : {config.ConfigId}  Create Tables"); 
 | 
  
 | 
            // 获取所有实体表-初始化租户业务表 
 | 
            var entityTypes = TenantUtil.GetTenantEntityTypes(TenantTypeEnum.Db); 
 | 
            if (!entityTypes.Any()) return; 
 | 
            foreach (var entityType in entityTypes) 
 | 
            { 
 | 
                //var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>(); 
 | 
                //if (splitTable == null) 
 | 
                db.CodeFirst.InitTables(entityType); 
 | 
                //else 
 | 
                //    db.CodeFirst.SplitTables().InitTables(entityType); 
 | 
  
 | 
                Console.WriteLine(entityType.Name); 
 | 
            } 
 | 
            Console.WriteLine(DateTime.Now + $@"Init Multi Tenant Db Finish"); 
 | 
            //多租户初始化种子数据 
 | 
            //await TenantSeedDataAsync(db, TenantTypeEnum.Db); 
 | 
        } 
 | 
  
 | 
        #endregion 
 | 
  
 | 
        //private static async Task TenantSeedDataAsync(ISqlSugarClient db, TenantTypeEnum tenantType) 
 | 
        //{ 
 | 
        //    // 获取所有种子配置-初始化数据 
 | 
        //    var seedDataTypes = AssemblysExtensions.GetAllAssemblies().SelectMany(s => s.DefinedTypes) 
 | 
        //        .Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass) 
 | 
        //        .Where(u => 
 | 
        //        { 
 | 
        //            var esd = u.GetInterfaces().FirstOrDefault(i => i.HasImplementedRawGeneric(typeof(IEntitySeedData<>))); 
 | 
        //            if (esd is null) 
 | 
        //            { 
 | 
        //                return false; 
 | 
        //            } 
 | 
  
 | 
        //            var eType = esd.GenericTypeArguments[0]; 
 | 
        //            return eType.IsTenantEntity(tenantType); 
 | 
        //        }); 
 | 
        //    if (!seedDataTypes.Any()) return; 
 | 
        //    foreach (var seedType in seedDataTypes) 
 | 
        //    { 
 | 
        //        dynamic instance = Activator.CreateInstance(seedType); 
 | 
        //        //初始化数据 
 | 
        //        { 
 | 
        //            var seedData = instance.InitSeedData(); 
 | 
        //            if (seedData != null && Enumerable.Any(seedData)) 
 | 
        //            { 
 | 
        //                var entityType = seedType.GetInterfaces().First().GetGenericArguments().First(); 
 | 
        //                var entity = db.EntityMaintenance.GetEntityInfo(entityType); 
 | 
  
 | 
        //                if (!await db.Queryable(entity.DbTableName, "").AnyAsync()) 
 | 
        //                { 
 | 
        //                    await db.Insertable(Enumerable.ToList(seedData)).ExecuteCommandAsync(); 
 | 
        //                    Console.WriteLine($"Table:{entity.DbTableName} init success!"); 
 | 
        //                } 
 | 
        //            } 
 | 
        //        } 
 | 
  
 | 
        //        //种子数据 
 | 
        //        { 
 | 
        //            var seedData = instance.SeedData(); 
 | 
        //            if (seedData != null && Enumerable.Any(seedData)) 
 | 
        //            { 
 | 
        //                var entityType = seedType.GetInterfaces().First().GetGenericArguments().First(); 
 | 
        //                var entity = db.EntityMaintenance.GetEntityInfo(entityType); 
 | 
  
 | 
        //                await db.Storageable(Enumerable.ToList(seedData)).ExecuteCommandAsync(); 
 | 
        //                Console.WriteLine($"Table:{entity.DbTableName} seedData success!"); 
 | 
        //            } 
 | 
        //        } 
 | 
  
 | 
        //        //自定义处理 
 | 
        //        { 
 | 
        //            await instance.CustomizeSeedData(db); 
 | 
        //        } 
 | 
        //    } 
 | 
        //} 
 | 
    } 
 | 
} 
 |