hutongqing
2024-11-20 0845150c79d1ebd664753931933e786ed8bd06c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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 = "WIDESEA_DB.DBSeed.Json/{0}.tsv";
 
        /// <summary>
        /// 异步添加种子数据
        /// </summary>
        /// <param name="myContext"></param>
        /// <param name="WebRootPath"></param>
        /// <returns></returns>
        public static void 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 Type: {DBContext.DbType}");
 
                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 = 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 != null && 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);
        //        }
        //    }
        //}
    }
}