SqlSugarSetup.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using Microsoft.Data.Sqlite;
  7. using SqlSugar;
  8. using System.Data.Common;
  9. using DbType = SqlSugar.DbType;
  10. namespace Admin.NET.Core;
  11. public static class SqlSugarSetup
  12. {
  13. // 多租户实例
  14. public static ITenant ITenant { get; set; }
  15. // 是否正在处理种子数据
  16. private static bool _isHandlingSeedData = false;
  17. /// <summary>
  18. /// SqlSugar 上下文初始化
  19. /// </summary>
  20. /// <param name="services"></param>
  21. public static void AddSqlSugar(this IServiceCollection services)
  22. {
  23. // 注册雪花Id
  24. var snowIdOpt = App.GetConfig<SnowIdOptions>("SnowId", true);
  25. YitIdHelper.SetIdGenerator(snowIdOpt);
  26. // 自定义 SqlSugar 雪花ID算法
  27. SnowFlakeSingle.WorkId = snowIdOpt.WorkerId;
  28. StaticConfig.CustomSnowFlakeFunc = () =>
  29. {
  30. return YitIdHelper.NextId();
  31. };
  32. // 动态表达式 SqlFunc 支持,https://www.donet5.com/Home/Doc?typeId=2569
  33. StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser);
  34. StaticConfig.DynamicExpressionParsingConfig = new ParsingConfig
  35. {
  36. CustomTypeProvider = new SqlSugarTypeProvider()
  37. };
  38. var dbOptions = App.GetConfig<DbConnectionOptions>("DbConnection", true);
  39. dbOptions.ConnectionConfigs.ForEach(SetDbConfig);
  40. SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs.Adapt<List<ConnectionConfig>>(), db =>
  41. {
  42. dbOptions.ConnectionConfigs.ForEach(config =>
  43. {
  44. var dbProvider = db.GetConnectionScope(config.ConfigId);
  45. SetDbAop(dbProvider, dbOptions.EnableConsoleSql, dbOptions.SuperAdminIgnoreIDeletedFilter);
  46. SetDbDiffLog(dbProvider, config);
  47. });
  48. });
  49. ITenant = sqlSugar;
  50. services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
  51. services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册
  52. services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 事务与工作单元注册
  53. // 初始化数据库表结构及种子数据
  54. dbOptions.ConnectionConfigs.ForEach(config =>
  55. {
  56. InitDatabase(sqlSugar, config);
  57. });
  58. }
  59. /// <summary>
  60. /// 配置连接属性
  61. /// </summary>
  62. /// <param name="config"></param>
  63. public static void SetDbConfig(DbConnectionConfig config)
  64. {
  65. if (config.DbSettings.EnableConnStringEncrypt)
  66. config.ConnectionString = CryptogramUtil.Decrypt(config.ConnectionString);
  67. var configureExternalServices = new ConfigureExternalServices
  68. {
  69. EntityNameService = (type, entity) => // 处理表
  70. {
  71. entity.IsDisabledDelete = true; // 禁止删除非 sqlsugar 创建的列
  72. // 只处理贴了特性[SugarTable]表
  73. if (!type.GetCustomAttributes<SugarTable>().Any())
  74. return;
  75. if (config.DbSettings.EnableUnderLine && !entity.DbTableName.Contains('_'))
  76. entity.DbTableName = UtilMethods.ToUnderLine(entity.DbTableName); // 驼峰转下划线
  77. },
  78. EntityService = (type, column) => // 处理列
  79. {
  80. // 只处理贴了特性[SugarColumn]列
  81. if (!type.GetCustomAttributes<SugarColumn>().Any())
  82. return;
  83. if (new NullabilityInfoContext().Create(type).WriteState is NullabilityState.Nullable)
  84. column.IsNullable = true;
  85. if (config.DbSettings.EnableUnderLine && !column.IsIgnore && !column.DbColumnName.Contains('_'))
  86. column.DbColumnName = UtilMethods.ToUnderLine(column.DbColumnName); // 驼峰转下划线
  87. },
  88. DataInfoCacheService = new SqlSugarCache(),
  89. };
  90. config.ConfigureExternalServices = configureExternalServices;
  91. config.InitKeyType = InitKeyType.Attribute;
  92. config.IsAutoCloseConnection = true;
  93. config.MoreSettings = new ConnMoreSettings
  94. {
  95. IsAutoRemoveDataCache = true, // 启用自动删除缓存,所有增删改会自动调用.RemoveDataCache()
  96. IsAutoDeleteQueryFilter = true, // 启用删除查询过滤器
  97. IsAutoUpdateQueryFilter = true, // 启用更新查询过滤器
  98. SqlServerCodeFirstNvarchar = true // 采用Nvarchar
  99. };
  100. // 若库类型是人大金仓则默认设置PG模式
  101. if (config.DbType == DbType.Kdbndp)
  102. config.MoreSettings.DatabaseModel = DbType.PostgreSQL; // 配置PG模式主要是兼容系统表差异
  103. // 若库类型是Oracle则默认主键名字和参数名字最大长度
  104. if (config.DbType == DbType.Oracle)
  105. config.MoreSettings.MaxParameterNameLength = 30;
  106. }
  107. /// <summary>
  108. /// 配置Aop
  109. /// </summary>
  110. /// <param name="db"></param>
  111. /// <param name="enableConsoleSql"></param>
  112. /// <param name="superAdminIgnoreIDeletedFilter"></param>
  113. public static void SetDbAop(SqlSugarScopeProvider db, bool enableConsoleSql, bool superAdminIgnoreIDeletedFilter)
  114. {
  115. // 设置超时时间
  116. db.Ado.CommandTimeOut = 30;
  117. // 打印SQL语句
  118. if (enableConsoleSql)
  119. {
  120. db.Aop.OnLogExecuting = (sql, pars) =>
  121. {
  122. //// 若参数值超过100个字符则进行截取
  123. //foreach (var par in pars)
  124. //{
  125. // if (par.DbType != System.Data.DbType.String || par.Value == null) continue;
  126. // if (par.Value.ToString().Length > 100)
  127. // par.Value = string.Concat(par.Value.ToString()[..100], "......");
  128. //}
  129. var log = $"【{DateTime.Now}——执行SQL】\r\n{UtilMethods.GetNativeSql(sql, pars)}\r\n";
  130. var originColor = Console.ForegroundColor;
  131. if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
  132. Console.ForegroundColor = ConsoleColor.Green;
  133. if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
  134. Console.ForegroundColor = ConsoleColor.Yellow;
  135. if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase))
  136. Console.ForegroundColor = ConsoleColor.Red;
  137. Console.WriteLine(log);
  138. Console.ForegroundColor = originColor;
  139. };
  140. }
  141. db.Aop.OnError = ex =>
  142. {
  143. if (ex.Parametres == null) return;
  144. var log = $"【{DateTime.Now}——错误SQL】\r\n{UtilMethods.GetNativeSql(ex.Sql, (SugarParameter[])ex.Parametres)}\r\n";
  145. Log.Error(log, ex);
  146. };
  147. db.Aop.OnLogExecuted = (sql, pars) =>
  148. {
  149. //// 若参数值超过100个字符则进行截取
  150. //foreach (var par in pars)
  151. //{
  152. // if (par.DbType != System.Data.DbType.String || par.Value == null) continue;
  153. // if (par.Value.ToString().Length > 100)
  154. // par.Value = string.Concat(par.Value.ToString()[..100], "......");
  155. //}
  156. // 执行时间超过5秒时
  157. if (!(db.Ado.SqlExecutionTime.TotalSeconds > 5)) return;
  158. var fileName = db.Ado.SqlStackTrace.FirstFileName; // 文件名
  159. var fileLine = db.Ado.SqlStackTrace.FirstLine; // 行号
  160. var firstMethodName = db.Ado.SqlStackTrace.FirstMethodName; // 方法名
  161. var log = $"【{DateTime.Now}——超时SQL】\r\n【所在文件名】:{fileName}\r\n【代码行数】:{fileLine}\r\n【方法名】:{firstMethodName}\r\n" + $"【SQL语句】:{UtilMethods.GetNativeSql(sql, pars)}";
  162. Log.Warning(log);
  163. };
  164. // 数据审计
  165. db.Aop.DataExecuting = (_, entityInfo) =>
  166. {
  167. // 若正在处理种子数据则直接返回
  168. if (_isHandlingSeedData) return;
  169. // 新增/插入
  170. if (entityInfo.OperationType == DataFilterType.InsertByObject)
  171. {
  172. // 若主键是长整型且空则赋值雪花Id
  173. if (entityInfo.EntityColumnInfo.IsPrimarykey && !entityInfo.EntityColumnInfo.IsIdentity && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
  174. {
  175. var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
  176. if (id == null || (long)id == 0)
  177. entityInfo.SetValue(YitIdHelper.NextId());
  178. }
  179. // 若创建时间为空则赋值当前时间
  180. else if (entityInfo.PropertyName == nameof(EntityBase.CreateTime))
  181. {
  182. var createTime = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue)!;
  183. if (createTime == null || createTime.Equals(DateTime.MinValue))
  184. entityInfo.SetValue(DateTime.Now);
  185. }
  186. // 若当前用户为空(非web线程时)
  187. if (App.User == null) return;
  188. dynamic entityValue = entityInfo.EntityValue;
  189. if (entityInfo.PropertyName == nameof(EntityBaseTenantId.TenantId))
  190. {
  191. var tenantId = entityValue.TenantId;
  192. if (tenantId == null || tenantId == 0)
  193. entityInfo.SetValue(App.User.FindFirst(ClaimConst.TenantId)?.Value);
  194. }
  195. else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserId))
  196. {
  197. var createUserId = entityValue.CreateUserId;
  198. if (createUserId == 0 || createUserId == null)
  199. entityInfo.SetValue(App.User.FindFirst(ClaimConst.UserId)?.Value);
  200. }
  201. else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserName))
  202. {
  203. var createUserName = entityValue.CreateUserName;
  204. if (string.IsNullOrEmpty(createUserName))
  205. entityInfo.SetValue(App.User.FindFirst(ClaimConst.RealName)?.Value);
  206. }
  207. else if (entityInfo.PropertyName == "CreateOrgId")
  208. {
  209. var createOrgId = entityValue.CreateOrgId;
  210. if (createOrgId == 0 || createOrgId == null)
  211. entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgId)?.Value);
  212. }
  213. else if (entityInfo.PropertyName == "CreateOrgName")
  214. {
  215. var createOrgName = entityValue.CreateOrgName;
  216. if (string.IsNullOrEmpty(createOrgName))
  217. entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgName)?.Value);
  218. }
  219. }
  220. // 编辑/更新
  221. else if (entityInfo.OperationType == DataFilterType.UpdateByObject)
  222. {
  223. if (entityInfo.PropertyName == nameof(EntityBase.UpdateTime))
  224. entityInfo.SetValue(DateTime.Now);
  225. else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserId))
  226. entityInfo.SetValue(App.User?.FindFirst(ClaimConst.UserId)?.Value);
  227. else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserName))
  228. entityInfo.SetValue(App.User?.FindFirst(ClaimConst.RealName)?.Value);
  229. }
  230. };
  231. // 是否为超级管理员
  232. var isSuperAdmin = App.User?.FindFirst(ClaimConst.AccountType)?.Value == ((int)AccountTypeEnum.SuperAdmin).ToString();
  233. // 配置假删除过滤器,如果当前用户是超级管理员并且允许忽略软删除过滤器则不会应用
  234. if (!isSuperAdmin || !superAdminIgnoreIDeletedFilter)
  235. db.QueryFilter.AddTableFilter<IDeletedFilter>(u => u.IsDelete == false);
  236. // 超管排除其他过滤器
  237. if (isSuperAdmin) return;
  238. // 配置租户过滤器
  239. var tenantId = App.User?.FindFirst(ClaimConst.TenantId)?.Value;
  240. if (!string.IsNullOrWhiteSpace(tenantId))
  241. db.QueryFilter.AddTableFilter<ITenantIdFilter>(u => u.TenantId == long.Parse(tenantId));
  242. // 配置用户机构(数据范围)过滤器
  243. SqlSugarFilter.SetOrgEntityFilter(db);
  244. // 配置自定义过滤器
  245. SqlSugarFilter.SetCustomEntityFilter(db);
  246. }
  247. /// <summary>
  248. /// 开启库表差异化日志
  249. /// </summary>
  250. /// <param name="db"></param>
  251. /// <param name="config"></param>
  252. private static void SetDbDiffLog(SqlSugarScopeProvider db, DbConnectionConfig config)
  253. {
  254. if (!config.DbSettings.EnableDiffLog) return;
  255. async void AopOnDiffLogEvent(DiffLogModel u)
  256. {
  257. // 记录差异数据
  258. var diffData = new List<dynamic>();
  259. for (int i = 0; i < u.AfterData.Count; i++)
  260. {
  261. var diffColumns = new List<dynamic>();
  262. var afterColumns = u.AfterData[i].Columns;
  263. var beforeColumns = u.BeforeData[i].Columns;
  264. for (int j = 0; j < afterColumns.Count; j++)
  265. {
  266. if (afterColumns[j].Value.Equals(beforeColumns[j].Value)) continue;
  267. diffColumns.Add(new
  268. {
  269. afterColumns[j].IsPrimaryKey,
  270. afterColumns[j].ColumnName,
  271. afterColumns[j].ColumnDescription,
  272. BeforeValue = beforeColumns[j].Value,
  273. AfterValue = afterColumns[j].Value,
  274. });
  275. }
  276. diffData.Add(new { u.AfterData[i].TableName, u.AfterData[i].TableDescription, Columns = diffColumns });
  277. }
  278. var logDiff = new SysLogDiff
  279. {
  280. // 差异数据(字段描述、列名、值、表名、表描述)
  281. DiffData = JSON.Serialize(diffData),
  282. // 传进来的对象(如果对象为空,则使用首个数据的表名作为业务对象)
  283. BusinessData = u.BusinessData == null ? u.AfterData.FirstOrDefault()?.TableName : JSON.Serialize(u.BusinessData),
  284. // 枚举(insert、update、delete)
  285. DiffType = u.DiffType.ToString(),
  286. Sql = u.Sql,
  287. Parameters = JSON.Serialize(u.Parameters.Select(e => new { e.ParameterName, e.Value, TypeName = e.DbType.ToString() })),
  288. Elapsed = u.Time == null ? 0 : (long)u.Time.Value.TotalMilliseconds
  289. };
  290. var logDb = ITenant.IsAnyConnection(SqlSugarConst.LogConfigId) ? ITenant.GetConnectionScope(SqlSugarConst.LogConfigId) : ITenant.GetConnectionScope(SqlSugarConst.MainConfigId);
  291. await logDb.CopyNew().Insertable(logDiff).ExecuteCommandAsync();
  292. Console.ForegroundColor = ConsoleColor.Red;
  293. Console.WriteLine(DateTime.Now + $"\r\n*****开始差异日志*****\r\n{Environment.NewLine}{JSON.Serialize(logDiff)}{Environment.NewLine}*****结束差异日志*****\r\n");
  294. }
  295. db.Aop.OnDiffLogEvent = AopOnDiffLogEvent;
  296. }
  297. /// <summary>
  298. /// 初始化视图
  299. /// </summary>
  300. /// <param name="dbProvider"></param>
  301. private static void InitView(SqlSugarScopeProvider dbProvider)
  302. {
  303. var totalWatch = Stopwatch.StartNew(); // 开始总计时
  304. Log.Information($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId}");
  305. var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
  306. int taskIndex = 0, size = viewTypeList.Count;
  307. var taskList = viewTypeList.Select(viewType => Task.Run(() =>
  308. {
  309. // 开始计时
  310. var stopWatch = Stopwatch.StartNew();
  311. // 获取视图实体和配置信息
  312. var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
  313. // 如果视图存在,则删除视图
  314. if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
  315. dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
  316. // 获取初始化视图查询SQL
  317. var sql = viewType.GetMethod(nameof(ISqlSugarView.GetQueryableSqlString))?.Invoke(Activator.CreateInstance(viewType), [dbProvider]) as string;
  318. if (string.IsNullOrWhiteSpace(sql)) throw new Exception("视图初始化Sql语句不能为空");
  319. // 创建视图
  320. dbProvider.Ado.ExecuteCommand($"CREATE VIEW {entityInfo.DbTableName} AS " + Environment.NewLine + " " + sql);
  321. // 停止计时
  322. stopWatch.Stop();
  323. Console.ForegroundColor = ConsoleColor.Green;
  324. Console.WriteLine($"初始化视图 {viewType.FullName,-58} ({dbProvider.CurrentConnectionConfig.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
  325. }));
  326. Task.WaitAll(taskList.ToArray());
  327. totalWatch.Stop(); // 停止总计时
  328. Console.ForegroundColor = ConsoleColor.Green;
  329. Console.WriteLine($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds:N0} ms");
  330. }
  331. /// <summary>
  332. /// 初始化数据库
  333. /// </summary>
  334. /// <param name="db">SqlSugarScope 实例</param>
  335. /// <param name="config">数据库连接配置</param>
  336. private static void InitDatabase(SqlSugarScope db, DbConnectionConfig config)
  337. {
  338. var dbProvider = db.GetConnectionScope(config.ConfigId);
  339. // 初始化数据库
  340. if (config.DbSettings.EnableInitDb)
  341. {
  342. Log.Information($"初始化数据库 {config.DbType} - {config.ConfigId} - {config.ConnectionString}");
  343. if (config.DbType != DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase();
  344. }
  345. // 初始化表结构
  346. if (config.TableSettings.EnableInitTable)
  347. {
  348. Log.Information($"初始化表结构 {config.DbType} - {config.ConfigId}");
  349. var entityTypes = GetEntityTypesForInit(config);
  350. InitializeTables(dbProvider, entityTypes, config);
  351. }
  352. // 初始化视图
  353. if (config.DbSettings.EnableInitView) InitView(dbProvider);
  354. // 初始化种子数据
  355. if (config.SeedSettings.EnableInitSeed) InitSeedData(db, config);
  356. }
  357. /// <summary>
  358. /// 获取需要初始化的实体类型
  359. /// </summary>
  360. /// <param name="config">数据库连接配置</param>
  361. /// <returns>实体类型列表</returns>
  362. private static List<Type> GetEntityTypesForInit(DbConnectionConfig config)
  363. {
  364. return App.EffectiveTypes
  365. .Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))
  366. .Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
  367. .WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false))
  368. .Where(u => IsEntityForConfig(u, config))
  369. .ToList();
  370. }
  371. /// <summary>
  372. /// 判断实体是否属于当前配置
  373. /// </summary>
  374. /// <param name="entityType">实体类型</param>
  375. /// <param name="config">数据库连接配置</param>
  376. /// <returns>是否属于当前配置</returns>
  377. private static bool IsEntityForConfig(Type entityType, DbConnectionConfig config)
  378. {
  379. switch (config.ConfigId.ToString())
  380. {
  381. case SqlSugarConst.MainConfigId:
  382. return entityType.GetCustomAttributes<SysTableAttribute>().Any() ||
  383. (!entityType.GetCustomAttributes<LogTableAttribute>().Any() &&
  384. !entityType.GetCustomAttributes<TenantAttribute>().Any());
  385. case SqlSugarConst.LogConfigId:
  386. return entityType.GetCustomAttributes<LogTableAttribute>().Any();
  387. default:
  388. {
  389. var tenantAttribute = entityType.GetCustomAttribute<TenantAttribute>();
  390. return tenantAttribute != null && tenantAttribute.configId.ToString() == config.ConfigId.ToString();
  391. }
  392. }
  393. }
  394. /// <summary>
  395. /// 初始化表结构
  396. /// </summary>
  397. /// <param name="dbProvider">SqlSugarScopeProvider 实例</param>
  398. /// <param name="entityTypes">实体类型列表</param>
  399. /// <param name="config">数据库连接配置</param>
  400. private static void InitializeTables(SqlSugarScopeProvider dbProvider, List<Type> entityTypes, DbConnectionConfig config)
  401. {
  402. // 删除视图再初始化表结构,防止因为视图导致无法同步表结构
  403. var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
  404. foreach (var viewType in viewTypeList)
  405. {
  406. var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
  407. if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
  408. dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
  409. }
  410. int count = 0, sum = entityTypes.Count;
  411. var tasks = entityTypes.Select(entityType => Task.Run(() =>
  412. {
  413. Console.WriteLine($"初始化表结构 {entityType.FullName,-64} ({config.ConfigId} - {Interlocked.Increment(ref count):D003}/{sum:D003})");
  414. UpdateNullableColumns(dbProvider, entityType);
  415. InitializeTable(dbProvider, entityType);
  416. }));
  417. Task.WhenAll(tasks).GetAwaiter().GetResult();
  418. }
  419. /// <summary>
  420. /// 更新表中不存在于实体的字段为可空
  421. /// </summary>
  422. /// <param name="dbProvider">SqlSugarScopeProvider 实例</param>
  423. /// <param name="entityType">实体类型</param>
  424. private static void UpdateNullableColumns(SqlSugarScopeProvider dbProvider, Type entityType)
  425. {
  426. var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
  427. var dbColumns = dbProvider.DbMaintenance.GetColumnInfosByTableName(entityInfo.DbTableName) ?? new List<DbColumnInfo>();
  428. foreach (var dbColumn in dbColumns.Where(c => !c.IsPrimarykey && entityInfo.Columns.All(u => u.DbColumnName != c.DbColumnName)))
  429. {
  430. dbColumn.IsNullable = true;
  431. Retry(() =>
  432. {
  433. dbProvider.DbMaintenance.UpdateColumn(entityInfo.DbTableName, dbColumn);
  434. }, maxRetry: 3, retryIntervalMs: 1000);
  435. }
  436. }
  437. /// <summary>
  438. /// 初始化表
  439. /// </summary>
  440. /// <param name="dbProvider">SqlSugarScopeProvider 实例</param>
  441. /// <param name="entityType">实体类型</param>
  442. private static void InitializeTable(SqlSugarScopeProvider dbProvider, Type entityType)
  443. {
  444. Retry(() =>
  445. {
  446. if (entityType.GetCustomAttribute<SplitTableAttribute>() == null)
  447. {
  448. dbProvider.CodeFirst.InitTables(entityType);
  449. }
  450. else
  451. {
  452. dbProvider.CodeFirst.SplitTables().InitTables(entityType);
  453. }
  454. }, maxRetry: 3, retryIntervalMs: 1000);
  455. }
  456. /// <summary>
  457. /// 初始化种子数据
  458. /// </summary>
  459. /// <param name="db">SqlSugarScope 实例</param>
  460. /// <param name="config">数据库连接配置</param>
  461. private static void InitSeedData(SqlSugarScope db, DbConnectionConfig config)
  462. {
  463. var dbProvider = db.GetConnectionScope(config.ConfigId);
  464. _isHandlingSeedData = true;
  465. Log.Information($"初始化种子数据 {config.DbType} - {config.ConfigId}");
  466. var seedDataTypes = GetSeedDataTypes(config);
  467. int count = 0, sum = seedDataTypes.Count;
  468. var tasks = seedDataTypes.Select(seedType => Task.Run(() =>
  469. {
  470. var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
  471. if (!IsEntityForConfig(entityType, config)) return;
  472. var seedData = GetSeedData(seedType);
  473. if (seedData == null) return;
  474. AdjustSeedDataIds(seedData, config);
  475. InsertOrUpdateSeedData(dbProvider, seedType, entityType, seedData, config, ref count, sum);
  476. }));
  477. Task.WhenAll(tasks).GetAwaiter().GetResult();
  478. _isHandlingSeedData = false;
  479. }
  480. /// <summary>
  481. /// 获取种子数据类型
  482. /// </summary>
  483. /// <param name="config">数据库连接配置</param>
  484. /// <returns>种子数据类型列表</returns>
  485. private static List<Type> GetSeedDataTypes(DbConnectionConfig config)
  486. {
  487. return App.EffectiveTypes
  488. .Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
  489. .WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false))
  490. .OrderBy(u => u.GetCustomAttributes(typeof(SeedDataAttribute), false).Length > 0 ? ((SeedDataAttribute)u.GetCustomAttributes(typeof(SeedDataAttribute), false)[0]).Order : 0)
  491. .ToList();
  492. }
  493. /// <summary>
  494. /// 获取种子数据
  495. /// </summary>
  496. /// <param name="seedType">种子数据类型</param>
  497. /// <returns>种子数据列表</returns>
  498. private static IEnumerable<object> GetSeedData(Type seedType)
  499. {
  500. var instance = Activator.CreateInstance(seedType);
  501. var hasDataMethod = seedType.GetMethod("HasData");
  502. return ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
  503. }
  504. /// <summary>
  505. /// 调整种子数据的 ID
  506. /// </summary>
  507. /// <param name="seedData">种子数据列表</param>
  508. /// <param name="config">数据库连接配置</param>
  509. private static void AdjustSeedDataIds(IEnumerable<object> seedData, DbConnectionConfig config)
  510. {
  511. var seedId = config.ConfigId.ToLong();
  512. foreach (var data in seedData)
  513. {
  514. var idProperty = data.GetType().GetProperty(nameof(EntityBaseId.Id));
  515. if (idProperty == null) continue;
  516. var idValue = idProperty.GetValue(data);
  517. if (idValue == null || idValue.ToString() == "0" || string.IsNullOrWhiteSpace(idValue.ToString()))
  518. {
  519. idProperty.SetValue(data, ++seedId);
  520. }
  521. }
  522. }
  523. /// <summary>
  524. /// 插入或更新种子数据
  525. /// </summary>
  526. /// <param name="dbProvider">SqlSugarScopeProvider 实例</param>
  527. /// <param name="seedType">种子数据类型</param>
  528. /// <param name="entityType">实体类型</param>
  529. /// <param name="seedData">种子数据列表</param>
  530. /// <param name="config">数据库连接配置</param>
  531. /// <param name="count">当前处理的数量</param>
  532. /// <param name="sum">总数量</param>
  533. private static void InsertOrUpdateSeedData(SqlSugarScopeProvider dbProvider, Type seedType, Type entityType, IEnumerable<object> seedData, DbConnectionConfig config, ref int count, int sum)
  534. {
  535. var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
  536. var dataList = seedData.ToList();
  537. if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null)
  538. {
  539. var initMethod = seedType.GetMethod("Init");
  540. initMethod?.Invoke(Activator.CreateInstance(seedType), new object[] { dbProvider });
  541. }
  542. else
  543. {
  544. int updateCount = 0, insertCount = 0;
  545. if (entityInfo.Columns.Any(u => u.IsPrimarykey))
  546. {
  547. var storage = dbProvider.StorageableByObject(dataList).ToStorage();
  548. if (seedType.GetCustomAttribute<IgnoreUpdateSeedAttribute>() == null)
  549. {
  550. updateCount = storage.AsUpdateable
  551. .IgnoreColumns(entityInfo.Columns
  552. .Where(u => u.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null)
  553. .Select(u => u.PropertyName).ToArray())
  554. .ExecuteCommand();
  555. }
  556. insertCount = storage.AsInsertable.ExecuteCommand();
  557. }
  558. else
  559. {
  560. if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
  561. {
  562. insertCount = dataList.Count;
  563. dbProvider.InsertableByObject(dataList).ExecuteCommand();
  564. }
  565. }
  566. Console.WriteLine($"添加数据 {entityInfo.DbTableName,-32} ({config.ConfigId} - {Interlocked.Increment(ref count):D003}/{sum:D003},数据量:{dataList.Count:D003},插入 {insertCount:D003} 条记录,修改 {updateCount:D003} 条记录)");
  567. }
  568. }
  569. /// <summary>
  570. /// 初始化租户业务数据库
  571. /// </summary>
  572. /// <param name="iTenant"></param>
  573. /// <param name="config"></param>
  574. public static void InitTenantDatabase(ITenant iTenant, DbConnectionConfig config)
  575. {
  576. SetDbConfig(config);
  577. if (!iTenant.IsAnyConnection(config.ConfigId.ToString()))
  578. iTenant.AddConnection(config);
  579. var db = iTenant.GetConnectionScope(config.ConfigId.ToString());
  580. db.DbMaintenance.CreateDatabase();
  581. // 获取所有业务表-初始化租户库表结构(排除系统表、日志表、特定库表)
  582. var entityTypes = App.EffectiveTypes
  583. .Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
  584. .Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false) &&
  585. !u.IsDefined(typeof(SysTableAttribute), false) && !u.IsDefined(typeof(LogTableAttribute), false) && !u.IsDefined(typeof(TenantAttribute), false)).ToList();
  586. if (entityTypes.Count == 0) return;
  587. foreach (var entityType in entityTypes)
  588. {
  589. var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>();
  590. if (splitTable == null)
  591. db.CodeFirst.InitTables(entityType);
  592. else
  593. db.CodeFirst.SplitTables().InitTables(entityType);
  594. }
  595. }
  596. /// <summary>
  597. /// 简单的重试机制
  598. /// </summary>
  599. /// <param name="action"></param>
  600. /// <param name="maxRetry"></param>
  601. /// <param name="retryIntervalMs"></param>
  602. private static void Retry(Action action, int maxRetry, int retryIntervalMs)
  603. {
  604. int attempt = 0;
  605. while (true)
  606. {
  607. try
  608. {
  609. action();
  610. return;
  611. }
  612. catch (SqliteException ex) when (ex.SqliteErrorCode == 5) // SQLITE_BUSY
  613. {
  614. if (++attempt >= maxRetry)
  615. {
  616. Log.Error($"简单的重试机制:{ex.Message}"); throw;
  617. }
  618. Log.Information($"数据库忙,正在重试... (尝试 {attempt}/{maxRetry})");
  619. Thread.Sleep(retryIntervalMs);
  620. }
  621. }
  622. }
  623. }