0
0

DbHelper.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. using Microsoft.Extensions.DependencyModel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using FreeSql;
  8. using FreeSql.Aop;
  9. using FreeSql.DataAnnotations;
  10. using Yitter.IdGenerator;
  11. using ZhonTai.Admin.Core.Configs;
  12. using ZhonTai.Admin.Core.Entities;
  13. using ZhonTai.Admin.Core.Attributes;
  14. using ZhonTai.Admin.Core.Auth;
  15. using System.IO;
  16. using ZhonTai.Common.Helpers;
  17. using ZhonTai.Admin.Core.Db.Data;
  18. using StackExchange.Profiling;
  19. using ZhonTai.Admin.Core.Consts;
  20. using ZhonTai.Admin.Core.Startup;
  21. using ZhonTai.Admin.Domain.Role;
  22. using ZhonTai.Admin.Domain.User;
  23. namespace ZhonTai.Admin.Core.Db;
  24. public class DbHelper
  25. {
  26. /// <summary>
  27. /// 偏移时间
  28. /// </summary>
  29. public static TimeSpan TimeOffset;
  30. /// <summary>
  31. /// 创建数据库
  32. /// </summary>
  33. /// <param name="dbConfig"></param>
  34. /// <returns></returns>
  35. public async static Task CreateDatabaseAsync(DbConfig dbConfig)
  36. {
  37. if (!dbConfig.CreateDb || dbConfig.Type == DataType.Sqlite)
  38. {
  39. return;
  40. }
  41. var db = new FreeSqlBuilder()
  42. .UseConnectionString(dbConfig.Type, dbConfig.CreateDbConnectionString)
  43. .Build();
  44. try
  45. {
  46. Console.WriteLine($"{Environment.NewLine} create database started");
  47. var filePath = Path.Combine(AppContext.BaseDirectory, "Configs/createdbsql.txt").ToPath();
  48. if (File.Exists(filePath))
  49. {
  50. var createDbSql = FileHelper.ReadFile(filePath);
  51. if (createDbSql.NotNull())
  52. {
  53. dbConfig.CreateDbSql = createDbSql;
  54. }
  55. }
  56. await db.Ado.ExecuteNonQueryAsync(dbConfig.CreateDbSql);
  57. Console.WriteLine(" create database succeed");
  58. }
  59. catch (Exception e)
  60. {
  61. Console.WriteLine($" create database failed.\n {e.Message}");
  62. }
  63. }
  64. /// <summary>
  65. /// 获得指定程序集表实体
  66. /// </summary>
  67. /// <param name="assemblyNames"></param>
  68. /// <returns></returns>
  69. public static Type[] GetEntityTypes(string[] assemblyNames)
  70. {
  71. if(!(assemblyNames?.Length > 0))
  72. {
  73. return null;
  74. }
  75. Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
  76. .Where(a => assemblyNames.Contains(a.Name))
  77. .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
  78. var entityTypes = new List<Type>();
  79. foreach (var assembly in assemblies)
  80. {
  81. foreach (Type type in assembly.GetExportedTypes())
  82. {
  83. foreach (Attribute attribute in type.GetCustomAttributes())
  84. {
  85. if (attribute is TableAttribute tableAttribute)
  86. {
  87. if (tableAttribute.DisableSyncStructure == false)
  88. {
  89. entityTypes.Add(type);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return entityTypes.ToArray();
  96. }
  97. /// <summary>
  98. /// 配置实体
  99. /// </summary>
  100. /// <param name="db"></param>
  101. /// <param name="appConfig"></param>
  102. /// <param name="dbConfig"></param>
  103. public static void ConfigEntity(IFreeSql db, AppConfig appConfig = null, DbConfig dbConfig = null)
  104. {
  105. //租户生成和操作租户Id
  106. if (!appConfig.Tenant)
  107. {
  108. var iTenant = nameof(ITenant);
  109. var tenantId = nameof(ITenant.TenantId);
  110. //获得指定程序集表实体
  111. var entityTypes = GetEntityTypes(dbConfig.AssemblyNames);
  112. foreach (var entityType in entityTypes)
  113. {
  114. if (entityType.GetInterfaces().Any(a => a.Name == iTenant))
  115. {
  116. db.CodeFirst.Entity(entityType, a =>
  117. {
  118. a.Ignore(tenantId);
  119. });
  120. }
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// 审计数据
  126. /// </summary>
  127. /// <param name="e"></param>
  128. /// <param name="timeOffset"></param>
  129. /// <param name="user"></param>
  130. public static void AuditValue(AuditValueEventArgs e, TimeSpan timeOffset, IUser user)
  131. {
  132. if (e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
  133. && (e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
  134. && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
  135. {
  136. e.Value = DateTime.Now.Subtract(timeOffset);
  137. }
  138. if (e.Column.CsType == typeof(long)
  139. && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) is SnowflakeAttribute snowflakeAttribute
  140. && snowflakeAttribute.Enable && (e.Value == null || (long)e.Value == default || (long?)e.Value == default))
  141. {
  142. e.Value = YitIdHelper.NextId();
  143. }
  144. if (user == null || user.Id <= 0)
  145. {
  146. return;
  147. }
  148. if (e.AuditValueType == AuditValueType.Insert)
  149. {
  150. switch (e.Property.Name)
  151. {
  152. case "CreatedUserId":
  153. case "OwnerId":
  154. case "MemberId":
  155. if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
  156. {
  157. e.Value = user.Id;
  158. }
  159. break;
  160. case "CreatedUserName":
  161. if (e.Value == null || ((string)e.Value).IsNull())
  162. {
  163. e.Value = user.UserName;
  164. }
  165. break;
  166. case "OwnerOrgId":
  167. if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
  168. {
  169. e.Value = user.DataPermission?.OrgId;
  170. }
  171. break;
  172. case "TenantId":
  173. if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
  174. {
  175. e.Value = user.TenantId;
  176. }
  177. break;
  178. }
  179. }
  180. else if (e.AuditValueType == AuditValueType.Update)
  181. {
  182. switch (e.Property.Name)
  183. {
  184. case "ModifiedUserId":
  185. e.Value = user.Id;
  186. break;
  187. case "ModifiedUserName":
  188. e.Value = user.UserName;
  189. break;
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// 同步结构
  195. /// </summary>
  196. public static void SyncStructure(IFreeSql db, string msg = null, DbConfig dbConfig = null, AppConfig appConfig = null)
  197. {
  198. //打印结构比对脚本
  199. //var dDL = db.CodeFirst.GetComparisonDDLStatements<PermissionEntity>();
  200. //Console.WriteLine($"{Environment.NewLine} " + dDL);
  201. //打印结构同步脚本
  202. //db.Aop.SyncStructureAfter += (s, e) =>
  203. //{
  204. // if (e.Sql.NotNull())
  205. // {
  206. // Console.WriteLine(" sync structure sql:\n" + e.Sql);
  207. // }
  208. //};
  209. // 同步结构
  210. var dbType = dbConfig.Type.ToString();
  211. Console.WriteLine($"{Environment.NewLine} {(msg.NotNull() ? msg : $"sync {dbType} structure")} started");
  212. if (dbConfig.Type == DataType.Oracle)
  213. {
  214. db.CodeFirst.IsSyncStructureToUpper = true;
  215. }
  216. //获得指定程序集表实体
  217. var entityTypes = GetEntityTypes(dbConfig.AssemblyNames);
  218. db.CodeFirst.SyncStructure(entityTypes);
  219. Console.WriteLine($" {(msg.NotNull() ? msg : $"sync {dbType} structure")} succeed");
  220. }
  221. /// <summary>
  222. /// 同步数据审计方法
  223. /// </summary>
  224. /// <param name="s"></param>
  225. /// <param name="e"></param>
  226. private static void SyncDataAuditValue(object s, AuditValueEventArgs e)
  227. {
  228. var user = new { Id = 161223411986501, Name = "admin", TenantId = 161223412138053 };
  229. if (e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
  230. && (e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
  231. && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
  232. {
  233. e.Value = DateTime.Now.Subtract(TimeOffset);
  234. }
  235. if (e.Column.CsType == typeof(long)
  236. && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) != null
  237. && (e.Value == null || (long)e.Value == default || (long?)e.Value == default))
  238. {
  239. e.Value = YitIdHelper.NextId();
  240. }
  241. if (user == null || user.Id <= 0)
  242. {
  243. return;
  244. }
  245. if (e.AuditValueType == AuditValueType.Insert)
  246. {
  247. switch (e.Property.Name)
  248. {
  249. case "CreatedUserId":
  250. if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
  251. {
  252. e.Value = user.Id;
  253. }
  254. break;
  255. case "CreatedUserName":
  256. if (e.Value == null || ((string)e.Value).IsNull())
  257. {
  258. e.Value = user.Name;
  259. }
  260. break;
  261. case "TenantId":
  262. if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
  263. {
  264. e.Value = user.TenantId;
  265. }
  266. break;
  267. }
  268. }
  269. else if (e.AuditValueType == AuditValueType.Update)
  270. {
  271. switch (e.Property.Name)
  272. {
  273. case "ModifiedUserId":
  274. e.Value = user.Id;
  275. break;
  276. case "ModifiedUserName":
  277. e.Value = user.Name;
  278. break;
  279. }
  280. }
  281. }
  282. /// <summary>
  283. /// 同步数据
  284. /// </summary>
  285. /// <param name="db"></param>
  286. /// <param name="dbConfig"></param>
  287. /// <param name="appConfig"></param>
  288. /// <returns></returns>
  289. /// <exception cref="Exception"></exception>
  290. public static async Task SyncDataAsync(
  291. IFreeSql db,
  292. DbConfig dbConfig = null,
  293. AppConfig appConfig = null
  294. )
  295. {
  296. try
  297. {
  298. Console.WriteLine($"{Environment.NewLine} sync data started");
  299. if (dbConfig.AssemblyNames?.Length > 0)
  300. {
  301. db.Aop.AuditValue += SyncDataAuditValue;
  302. Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
  303. .Where(a => dbConfig.AssemblyNames.Contains(a.Name))
  304. .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
  305. List<ISyncData> syncDatas = assemblies.Select(assembly => assembly.GetTypes()
  306. .Where(x => typeof(ISyncData).GetTypeInfo().IsAssignableFrom(x.GetTypeInfo()) && x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract))
  307. .SelectMany(registerTypes => registerTypes.Select(registerType => (ISyncData)Activator.CreateInstance(registerType))).ToList();
  308. foreach (ISyncData syncData in syncDatas)
  309. {
  310. await syncData.SyncDataAsync(db, dbConfig, appConfig);
  311. }
  312. db.Aop.AuditValue -= SyncDataAuditValue;
  313. }
  314. Console.WriteLine($" sync data succeed{Environment.NewLine}");
  315. }
  316. catch (Exception ex)
  317. {
  318. throw new Exception($" sync data failed.\n{ex.Message}");
  319. }
  320. }
  321. /// <summary>
  322. /// 生成数据
  323. /// </summary>
  324. /// <param name="db"></param>
  325. /// <param name="appConfig"></param>
  326. /// <param name="dbConfig"></param>
  327. /// <returns></returns>
  328. /// <exception cref="Exception"></exception>
  329. public static async Task GenerateDataAsync(IFreeSql db, AppConfig appConfig = null, DbConfig dbConfig = null)
  330. {
  331. try
  332. {
  333. Console.WriteLine($"{Environment.NewLine} generate data started");
  334. if (dbConfig.AssemblyNames?.Length > 0)
  335. {
  336. Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
  337. .Where(a => dbConfig.AssemblyNames.Contains(a.Name))
  338. .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
  339. List<IGenerateData> generateDatas = assemblies.Select(assembly => assembly.GetTypes()
  340. .Where(x => typeof(IGenerateData).GetTypeInfo().IsAssignableFrom(x.GetTypeInfo()) && x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract))
  341. .SelectMany(registerTypes => registerTypes.Select(registerType => (IGenerateData)Activator.CreateInstance(registerType))).ToList();
  342. foreach (IGenerateData generateData in generateDatas)
  343. {
  344. await generateData.GenerateDataAsync(db, appConfig);
  345. }
  346. }
  347. Console.WriteLine($" generate data succeed{Environment.NewLine}");
  348. }
  349. catch (Exception ex)
  350. {
  351. throw new Exception($" generate data failed。\n{ex.Message}{Environment.NewLine}");
  352. }
  353. }
  354. /// <summary>
  355. /// 注册数据库
  356. /// </summary>
  357. /// <param name="freeSqlCloud"></param>
  358. /// <param name="user"></param>
  359. /// <param name="dbConfig"></param>
  360. /// <param name="appConfig"></param>
  361. /// <param name="hostAppOptions"></param>
  362. public static void RegisterDb(
  363. FreeSqlCloud freeSqlCloud,
  364. IUser user,
  365. DbConfig dbConfig,
  366. AppConfig appConfig,
  367. HostAppOptions hostAppOptions
  368. )
  369. {
  370. //注册数据库
  371. freeSqlCloud.Register(dbConfig.Key, () =>
  372. {
  373. //创建数据库
  374. if (dbConfig.CreateDb)
  375. {
  376. CreateDatabaseAsync(dbConfig).Wait();
  377. }
  378. var providerType = dbConfig.ProviderType.NotNull() ? Type.GetType(dbConfig.ProviderType) : null;
  379. var freeSqlBuilder = new FreeSqlBuilder()
  380. .UseConnectionString(dbConfig.Type, dbConfig.ConnectionString, providerType)
  381. .UseAutoSyncStructure(false)
  382. .UseLazyLoading(false)
  383. .UseNoneCommandParameter(true);
  384. if (dbConfig.SlaveList?.Length > 0)
  385. {
  386. var slaveList = dbConfig.SlaveList.Select(a => a.ConnectionString).ToArray();
  387. var slaveWeightList = dbConfig.SlaveList.Select(a => a.Weight).ToArray();
  388. freeSqlBuilder.UseSlave(slaveList).UseSlaveWeight(slaveWeightList);
  389. }
  390. hostAppOptions?.ConfigureFreeSqlBuilder?.Invoke(freeSqlBuilder);
  391. #region 监听所有命令
  392. if (dbConfig.MonitorCommand)
  393. {
  394. freeSqlBuilder.UseMonitorCommand(cmd => { }, (cmd, traceLog) =>
  395. {
  396. //Console.WriteLine($"{cmd.CommandText}\n{traceLog}{Environment.NewLine}");
  397. Console.WriteLine($"{cmd.CommandText}{Environment.NewLine}");
  398. });
  399. }
  400. #endregion 监听所有命令
  401. var fsql = freeSqlBuilder.Build();
  402. //生成数据
  403. if (dbConfig.GenerateData && !dbConfig.CreateDb && !dbConfig.SyncData)
  404. {
  405. GenerateDataAsync(fsql, appConfig, dbConfig).Wait();
  406. }
  407. //软删除过滤器
  408. fsql.GlobalFilter.ApplyOnly<IDelete>(FilterNames.Delete, a => a.IsDeleted == false);
  409. //租户过滤器
  410. if (appConfig.Tenant)
  411. {
  412. fsql.GlobalFilter.ApplyOnly<ITenant>(FilterNames.Tenant, a => a.TenantId == user.TenantId);
  413. }
  414. //会员过滤器
  415. fsql.GlobalFilter.ApplyOnly<IMember>(FilterNames.Member, a => a.MemberId == user.Id);
  416. //数据权限过滤器
  417. fsql.GlobalFilter.ApplyOnlyIf<IData>(FilterNames.Self,
  418. () =>
  419. {
  420. if (!(user?.Id > 0))
  421. return false;
  422. var dataPermission = user.DataPermission;
  423. if (user.Type == UserType.DefaultUser && dataPermission != null)
  424. return dataPermission.DataScope != DataScope.All && dataPermission.OrgIds.Count == 0;
  425. return false;
  426. },
  427. a => a.OwnerId == user.Id
  428. );
  429. fsql.GlobalFilter.ApplyOnlyIf<IData>(FilterNames.Data,
  430. () =>
  431. {
  432. if (!(user?.Id > 0))
  433. return false;
  434. var dataPermission = user.DataPermission;
  435. if (user.Type == UserType.DefaultUser && dataPermission != null)
  436. return dataPermission.DataScope != DataScope.All && dataPermission.OrgIds.Count > 0;
  437. return false;
  438. },
  439. a => a.OwnerId == user.Id || user.DataPermission.OrgIds.Contains(a.OwnerOrgId.Value)
  440. );
  441. //配置实体
  442. ConfigEntity(fsql, appConfig, dbConfig);
  443. hostAppOptions?.ConfigureFreeSql?.Invoke(fsql);
  444. #region 初始化数据库
  445. //同步结构
  446. if (dbConfig.SyncStructure)
  447. {
  448. SyncStructure(fsql, dbConfig: dbConfig, appConfig: appConfig);
  449. }
  450. #region 审计数据
  451. //计算服务器时间
  452. var serverTime = fsql.Ado.QuerySingle(() => DateTime.UtcNow);
  453. var timeOffset = DateTime.UtcNow.Subtract(serverTime);
  454. TimeOffset = timeOffset;
  455. fsql.Aop.AuditValue += (s, e) =>
  456. {
  457. AuditValue(e, timeOffset, user);
  458. };
  459. #endregion 审计数据
  460. //同步数据
  461. if (dbConfig.SyncData)
  462. {
  463. SyncDataAsync(fsql, dbConfig, appConfig).Wait();
  464. }
  465. #endregion 初始化数据库
  466. #region 监听Curd操作
  467. if (dbConfig.Curd)
  468. {
  469. fsql.Aop.CurdBefore += (s, e) =>
  470. {
  471. if (appConfig.MiniProfiler)
  472. {
  473. MiniProfiler.Current.CustomTiming("CurdBefore", e.Sql);
  474. }
  475. Console.WriteLine($"{e.Sql}{Environment.NewLine}");
  476. };
  477. fsql.Aop.CurdAfter += (s, e) =>
  478. {
  479. if (appConfig.MiniProfiler)
  480. {
  481. MiniProfiler.Current.CustomTiming("CurdAfter", $"{e.ElapsedMilliseconds}");
  482. }
  483. };
  484. }
  485. #endregion 监听Curd操作
  486. return fsql;
  487. });
  488. //执行注册数据库
  489. var fsql = freeSqlCloud.Use(dbConfig.Key);
  490. if (dbConfig.SyncStructure)
  491. {
  492. var _ = fsql.CodeFirst;
  493. }
  494. }
  495. }