0
0

DbHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Serialization;
  7. using FreeSql;
  8. using FreeSql.DataAnnotations;
  9. using Admin.Core.Common.Configs;
  10. using Admin.Core.Common.Helpers;
  11. using Admin.Core.Model.Admin;
  12. using System.Collections.Generic;
  13. using System.Reflection;
  14. using Admin.Core.Common.BaseModel;
  15. using FreeSql.Aop;
  16. using Admin.Core.Common.Attributes;
  17. using Admin.Core.Common.Auth;
  18. using Yitter.IdGenerator;
  19. using Admin.Core.Common.Extensions;
  20. using Admin.Core.Repository.Admin.Output;
  21. using Admin.Core.Repository.Admin.View.Output;
  22. using Admin.Core.Repository.Admin.Permission.Output;
  23. namespace Admin.Core.Repository
  24. {
  25. public class DbHelper
  26. {
  27. /// <summary>
  28. /// 偏移时间
  29. /// </summary>
  30. public static TimeSpan TimeOffset;
  31. /// <summary>
  32. /// 创建数据库
  33. /// </summary>
  34. /// <param name="dbConfig"></param>
  35. /// <returns></returns>
  36. public async static Task CreateDatabaseAsync(DbConfig dbConfig)
  37. {
  38. if (!dbConfig.CreateDb || dbConfig.Type == DataType.Sqlite)
  39. {
  40. return;
  41. }
  42. var db = new FreeSqlBuilder()
  43. .UseConnectionString(dbConfig.Type, dbConfig.CreateDbConnectionString)
  44. .Build();
  45. try
  46. {
  47. Console.WriteLine("\r\n create database started");
  48. await db.Ado.ExecuteNonQueryAsync(dbConfig.CreateDbSql);
  49. Console.WriteLine(" create database succeed");
  50. }
  51. catch (Exception e)
  52. {
  53. Console.WriteLine($" create database failed.\n {e.Message}");
  54. }
  55. }
  56. /// <summary>
  57. /// 获得指定程序集表实体
  58. /// </summary>
  59. /// <returns></returns>
  60. public static Type[] GetEntityTypes()
  61. {
  62. List<string> assemblyNames = new List<string>()
  63. {
  64. "Admin.Core.Model"
  65. };
  66. List<Type> entityTypes = new List<Type>();
  67. foreach (var assemblyName in assemblyNames)
  68. {
  69. foreach (Type type in Assembly.Load(assemblyName).GetExportedTypes())
  70. {
  71. foreach (Attribute attribute in type.GetCustomAttributes())
  72. {
  73. if (attribute is TableAttribute tableAttribute)
  74. {
  75. if (tableAttribute.DisableSyncStructure == false)
  76. {
  77. entityTypes.Add(type);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. return entityTypes.ToArray();
  84. }
  85. /// <summary>
  86. /// 配置实体
  87. /// </summary>
  88. public static void ConfigEntity(IFreeSql db, AppConfig appConfig = null)
  89. {
  90. //租户生成和操作租户Id
  91. if (!appConfig.Tenant)
  92. {
  93. var iTenant = nameof(ITenant);
  94. var tenantId = nameof(ITenant.TenantId);
  95. //获得指定程序集表实体
  96. var entityTypes = GetEntityTypes();
  97. foreach (var entityType in entityTypes)
  98. {
  99. if (entityType.GetInterfaces().Any(a => a.Name == iTenant))
  100. {
  101. db.CodeFirst.Entity(entityType, a =>
  102. {
  103. a.Ignore(tenantId);
  104. });
  105. }
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// 审计数据
  111. /// </summary>
  112. /// <param name="e"></param>
  113. /// <param name="timeOffset"></param>
  114. /// <param name="user"></param>
  115. public static void AuditValue(AuditValueEventArgs e, TimeSpan timeOffset, IUser user)
  116. {
  117. if (e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
  118. && (e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
  119. && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
  120. {
  121. e.Value = DateTime.Now.Subtract(timeOffset);
  122. }
  123. if (e.Column.CsType == typeof(long)
  124. && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) != null
  125. && (e.Value == null || (long)e.Value == default || (long?)e.Value == default))
  126. {
  127. e.Value = YitIdHelper.NextId();
  128. }
  129. if (user == null || user.Id <= 0)
  130. {
  131. return;
  132. }
  133. if (e.AuditValueType == FreeSql.Aop.AuditValueType.Insert)
  134. {
  135. switch (e.Property.Name)
  136. {
  137. case "CreatedUserId":
  138. if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
  139. {
  140. e.Value = user.Id;
  141. }
  142. break;
  143. case "CreatedUserName":
  144. if (e.Value == null || ((string)e.Value).IsNull())
  145. {
  146. e.Value = user.Name;
  147. }
  148. break;
  149. case "TenantId":
  150. if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
  151. {
  152. e.Value = user.TenantId;
  153. }
  154. break;
  155. }
  156. }
  157. else if (e.AuditValueType == FreeSql.Aop.AuditValueType.Update)
  158. {
  159. switch (e.Property.Name)
  160. {
  161. case "ModifiedUserId":
  162. e.Value = user.Id;
  163. break;
  164. case "ModifiedUserName":
  165. e.Value = user.Name;
  166. break;
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// 同步结构
  172. /// </summary>
  173. public static void SyncStructure(IFreeSql db, string msg = null, DbConfig dbConfig = null, AppConfig appConfig = null)
  174. {
  175. //打印结构比对脚本
  176. //var dDL = db.CodeFirst.GetComparisonDDLStatements<PermissionEntity>();
  177. //Console.WriteLine("\r\n " + dDL);
  178. //打印结构同步脚本
  179. //db.Aop.SyncStructureAfter += (s, e) =>
  180. //{
  181. // if (e.Sql.NotNull())
  182. // {
  183. // Console.WriteLine(" sync structure sql:\n" + e.Sql);
  184. // }
  185. //};
  186. // 同步结构
  187. var dbType = dbConfig.Type.ToString();
  188. Console.WriteLine($"\r\n {(msg.NotNull() ? msg : $"sync {dbType} structure")} started");
  189. if(dbConfig.Type == DataType.Oracle)
  190. {
  191. db.CodeFirst.IsSyncStructureToUpper = true;
  192. }
  193. //获得指定程序集表实体
  194. var entityTypes = GetEntityTypes();
  195. db.CodeFirst.SyncStructure(entityTypes);
  196. Console.WriteLine($" {(msg.NotNull() ? msg : $"sync {dbType} structure")} succeed");
  197. }
  198. /// <summary>
  199. /// 检查实体属性是否为自增长
  200. /// </summary>
  201. /// <typeparam name="T"></typeparam>
  202. /// <returns></returns>
  203. private static bool CheckIdentity<T>() where T : class
  204. {
  205. var isIdentity = false;
  206. var properties = typeof(T).GetProperties();
  207. foreach (var property in properties)
  208. {
  209. if (property.GetCustomAttributes(typeof(ColumnAttribute), false).FirstOrDefault() is ColumnAttribute columnAttribute && columnAttribute.IsIdentity)
  210. {
  211. isIdentity = true;
  212. break;
  213. }
  214. }
  215. return isIdentity;
  216. }
  217. /// <summary>
  218. /// 初始化数据表数据
  219. /// </summary>
  220. /// <typeparam name="T"></typeparam>
  221. /// <param name="db"></param>
  222. /// <param name="unitOfWork"></param>
  223. /// <param name="tran"></param>
  224. /// <param name="data"></param>
  225. /// <param name="dbConfig"></param>
  226. /// <returns></returns>
  227. private static async Task InitDtDataAsync<T>(
  228. IFreeSql db,
  229. IUnitOfWork unitOfWork,
  230. System.Data.Common.DbTransaction tran,
  231. T[] data,
  232. DbConfig dbConfig = null
  233. ) where T : class
  234. {
  235. var table = typeof(T).GetCustomAttributes(typeof(TableAttribute),false).FirstOrDefault() as TableAttribute;
  236. var tableName = table.Name;
  237. try
  238. {
  239. if (await db.Queryable<T>().AnyAsync())
  240. {
  241. Console.WriteLine($" table: {tableName} record already exists");
  242. return;
  243. }
  244. if (!(data?.Length > 0))
  245. {
  246. Console.WriteLine($" table: {tableName} import data []");
  247. return;
  248. }
  249. var repo = db.GetRepository<T>();
  250. var insert = db.Insert<T>();
  251. if (unitOfWork != null)
  252. {
  253. repo.UnitOfWork = unitOfWork;
  254. insert = insert.WithTransaction(tran);
  255. }
  256. var isIdentity = CheckIdentity<T>();
  257. if (isIdentity)
  258. {
  259. if (dbConfig.Type == DataType.SqlServer)
  260. {
  261. var insrtSql = insert.AppendData(data).InsertIdentity().ToSql();
  262. await repo.Orm.Ado.ExecuteNonQueryAsync($"SET IDENTITY_INSERT {tableName} ON\n {insrtSql} \nSET IDENTITY_INSERT {tableName} OFF");
  263. }
  264. else
  265. {
  266. await insert.AppendData(data).InsertIdentity().ExecuteAffrowsAsync();
  267. }
  268. }
  269. else
  270. {
  271. repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
  272. await repo.InsertAsync(data);
  273. }
  274. Console.WriteLine($" table: {tableName} sync data succeed");
  275. }
  276. catch (Exception ex)
  277. {
  278. Console.WriteLine($" table: {tableName} sync data failed.\n{ex.Message}");
  279. }
  280. }
  281. /// <summary>
  282. /// 同步数据审计方法
  283. /// </summary>
  284. /// <param name="s"></param>
  285. /// <param name="e"></param>
  286. private static void SyncDataAuditValue(object s, AuditValueEventArgs e)
  287. {
  288. var user = new { Id = 161223411986501, Name = "admin", TenantId = 161223412138053 };
  289. if (e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
  290. && (e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
  291. && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
  292. {
  293. e.Value = DateTime.Now.Subtract(TimeOffset);
  294. }
  295. if (e.Column.CsType == typeof(long)
  296. && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) != null
  297. && (e.Value == null || (long)e.Value == default || (long?)e.Value == default))
  298. {
  299. e.Value = YitIdHelper.NextId();
  300. }
  301. if (user == null || user.Id <= 0)
  302. {
  303. return;
  304. }
  305. if (e.AuditValueType == AuditValueType.Insert)
  306. {
  307. switch (e.Property.Name)
  308. {
  309. case "CreatedUserId":
  310. if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
  311. {
  312. e.Value = user.Id;
  313. }
  314. break;
  315. case "CreatedUserName":
  316. if (e.Value == null || ((string)e.Value).IsNull())
  317. {
  318. e.Value = user.Name;
  319. }
  320. break;
  321. case "TenantId":
  322. if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
  323. {
  324. e.Value = user.TenantId;
  325. }
  326. break;
  327. }
  328. }
  329. else if (e.AuditValueType == AuditValueType.Update)
  330. {
  331. switch (e.Property.Name)
  332. {
  333. case "ModifiedUserId":
  334. e.Value = user.Id;
  335. break;
  336. case "ModifiedUserName":
  337. e.Value = user.Name;
  338. break;
  339. }
  340. }
  341. }
  342. /// <summary>
  343. /// 同步数据
  344. /// </summary>
  345. /// <returns></returns>
  346. public static async Task SyncDataAsync(IFreeSql db, DbConfig dbConfig = null, AppConfig appConfig = null)
  347. {
  348. try
  349. {
  350. //db.Aop.CurdBefore += (s, e) =>
  351. //{
  352. // Console.WriteLine($"{e.Sql}\r\n");
  353. //};
  354. Console.WriteLine("\r\n sync data started");
  355. db.Aop.AuditValue += SyncDataAuditValue;
  356. var fileName = appConfig.Tenant ? "data-share.json" : "data.json";
  357. var filePath = Path.Combine(AppContext.BaseDirectory, $"Db/Data/{fileName}").ToPath();
  358. var jsonData = FileHelper.ReadFile(filePath);
  359. var data = JsonConvert.DeserializeObject<Data>(jsonData);
  360. using (var uow = db.CreateUnitOfWork())
  361. using (var tran = uow.GetOrBeginTransaction())
  362. {
  363. var dualRepo = db.GetRepository<DualEntity>();
  364. dualRepo.UnitOfWork = uow;
  365. if (!await dualRepo.Select.AnyAsync())
  366. {
  367. await dualRepo.InsertAsync(new DualEntity { });
  368. }
  369. //await InitDtDataAsync(db, uow, tran, data.Dictionaries, dbConfig);
  370. await InitDtDataAsync(db, uow, tran, data.ApiTree, dbConfig);
  371. await InitDtDataAsync(db, uow, tran, data.ViewTree, dbConfig);
  372. await InitDtDataAsync(db, uow, tran, data.PermissionTree, dbConfig);
  373. await InitDtDataAsync(db, uow, tran, data.Users, dbConfig);
  374. await InitDtDataAsync(db, uow, tran, data.Roles, dbConfig);
  375. await InitDtDataAsync(db, uow, tran, data.UserRoles, dbConfig);
  376. await InitDtDataAsync(db, uow, tran, data.RolePermissions, dbConfig);
  377. await InitDtDataAsync(db, uow, tran, data.Tenants, dbConfig);
  378. uow.Commit();
  379. }
  380. db.Aop.AuditValue -= SyncDataAuditValue;
  381. Console.WriteLine(" sync data succeed\r\n");
  382. }
  383. catch (Exception ex)
  384. {
  385. throw new Exception($" sync data failed.\n{ex.Message}");
  386. }
  387. }
  388. /// <summary>
  389. /// 生成极简数据
  390. /// </summary>
  391. /// <param name="db"></param>
  392. /// <param name="appConfig"></param>
  393. /// <returns></returns>
  394. public static async Task GenerateSimpleJsonDataAsync(IFreeSql db, AppConfig appConfig = null)
  395. {
  396. try
  397. {
  398. Console.WriteLine("\r\n generate data started");
  399. #region 数据表
  400. #region 数据字典
  401. //var dictionaries = await db.Queryable<DictionaryEntity>().ToListAsync(a => new
  402. //{
  403. // a.TenantId,
  404. // a.Id,
  405. // a.ParentId,
  406. // a.Name,
  407. // a.Code,
  408. // a.Value,
  409. // a.Description,
  410. // a.Sort
  411. //});
  412. #endregion
  413. #region 接口
  414. var apis = await db.Queryable<ApiEntity>().ToListAsync<ApiDataOutput>();
  415. var apiTree = apis.ToTree((r, c) =>
  416. {
  417. return c.ParentId == 0;
  418. },
  419. (r, c) =>
  420. {
  421. return r.Id == c.ParentId;
  422. },
  423. (r, datalist) =>
  424. {
  425. r.Childs ??= new List<ApiDataOutput>();
  426. r.Childs.AddRange(datalist);
  427. });
  428. #endregion
  429. #region 视图
  430. var views = await db.Queryable<ViewEntity>().ToListAsync<ViewDataOutput>();
  431. var viewTree = views.ToTree((r, c) =>
  432. {
  433. return c.ParentId == 0;
  434. },
  435. (r, c) =>
  436. {
  437. return r.Id == c.ParentId;
  438. },
  439. (r, datalist) =>
  440. {
  441. r.Childs ??= new List<ViewDataOutput>();
  442. r.Childs.AddRange(datalist);
  443. });
  444. #endregion
  445. #region 权限
  446. var permissions = await db.Queryable<PermissionEntity>().ToListAsync<PermissionDataOutput>();
  447. var permissionTree = permissions.ToTree((r, c) =>
  448. {
  449. return c.ParentId == 0;
  450. },
  451. (r, c) =>
  452. {
  453. return r.Id == c.ParentId;
  454. },
  455. (r, datalist) =>
  456. {
  457. r.Childs ??= new List<PermissionDataOutput>();
  458. r.Childs.AddRange(datalist);
  459. });
  460. #endregion
  461. #region 用户
  462. var users = await db.Queryable<UserEntity>().ToListAsync(a => new
  463. {
  464. a.TenantId,
  465. a.Id,
  466. a.UserName,
  467. a.Password,
  468. a.NickName,
  469. a.Avatar,
  470. a.Status,
  471. a.Remark
  472. });
  473. #endregion
  474. #region 角色
  475. var roles = await db.Queryable<RoleEntity>().ToListAsync(a => new
  476. {
  477. a.TenantId,
  478. a.Id,
  479. a.Name,
  480. a.Code,
  481. a.Sort,
  482. a.Description
  483. });
  484. #endregion
  485. #region 用户角色
  486. var userRoles = await db.Queryable<UserRoleEntity>().ToListAsync(a => new
  487. {
  488. a.Id,
  489. a.UserId,
  490. a.RoleId
  491. });
  492. #endregion
  493. #region 角色权限
  494. var rolePermissions = await db.Queryable<RolePermissionEntity>().ToListAsync(a => new
  495. {
  496. a.Id,
  497. a.RoleId,
  498. a.PermissionId
  499. });
  500. #endregion
  501. #region 租户
  502. var tenants = await db.Queryable<TenantEntity>().ToListAsync(a => new
  503. {
  504. a.TenantId,
  505. a.Id,
  506. a.UserId,
  507. a.RoleId,
  508. a.Name,
  509. a.Code,
  510. a.RealName,
  511. a.Phone,
  512. a.Email,
  513. a.TenantType,
  514. a.DataIsolationType,
  515. a.DbType,
  516. a.ConnectionString,
  517. a.IdleTime,
  518. a.Description
  519. });
  520. #endregion
  521. #endregion
  522. if (!(users?.Count > 0))
  523. {
  524. return;
  525. }
  526. #region 生成数据
  527. var settings = new JsonSerializerSettings();
  528. settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
  529. settings.NullValueHandling = NullValueHandling.Ignore;
  530. settings.DefaultValueHandling = DefaultValueHandling.Ignore;
  531. var jsonData = JsonConvert.SerializeObject(new
  532. {
  533. //dictionaries,
  534. apis,
  535. apiTree,
  536. viewTree,
  537. permissionTree,
  538. users,
  539. roles,
  540. userRoles,
  541. rolePermissions,
  542. tenants
  543. },
  544. //Formatting.Indented,
  545. settings
  546. );
  547. var fileName = appConfig.Tenant ? "data-share.json" : "data.json";
  548. var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"Db/Data/{fileName}").ToPath();
  549. FileHelper.WriteFile(filePath, jsonData);
  550. #endregion
  551. Console.WriteLine(" generate data succeed\r\n");
  552. }
  553. catch (Exception ex)
  554. {
  555. throw new Exception($" generate data failed。\n{ex.Message}\r\n");
  556. }
  557. }
  558. }
  559. }