EnumToDictJob.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using System.Security.Cryptography;
  7. namespace Admin.NET.Core;
  8. /// <summary>
  9. /// 枚举转字典
  10. /// </summary>
  11. [JobDetail("job_EnumToDictJob", Description = "枚举转字典", GroupName = "default", Concurrent = false)]
  12. [PeriodSeconds(1, TriggerId = "trigger_EnumToDictJob", Description = "枚举转字典", MaxNumberOfRuns = 1, RunOnStart = true)]
  13. public class EnumToDictJob : IJob
  14. {
  15. private readonly IServiceScopeFactory _scopeFactory;
  16. private const string DefaultTagType = null;
  17. private const int OrderOffset = 10;
  18. public EnumToDictJob(IServiceScopeFactory scopeFactory)
  19. {
  20. _scopeFactory = scopeFactory;
  21. }
  22. public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
  23. {
  24. var originColor = Console.ForegroundColor;
  25. Console.ForegroundColor = ConsoleColor.Green;
  26. Console.WriteLine($"【{DateTime.Now}】系统枚举转换字典");
  27. Console.ForegroundColor = originColor;
  28. using var serviceScope = _scopeFactory.CreateScope();
  29. var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
  30. var sysEnumService = serviceScope.ServiceProvider.GetRequiredService<SysEnumService>();
  31. var sysDictTypeList = GetDictByEnumType(sysEnumService.GetEnumTypeList());
  32. // 校验枚举类命名规范,字典相关功能中需要通过后缀判断是否为枚举类型
  33. foreach (var dictType in sysDictTypeList.Where(x => !x.Code.EndsWith("Enum"))) Log.Warning($"系统枚举转换字典的枚举类名称必须以Enum结尾: {dictType.Code} ({dictType.Name})");
  34. sysDictTypeList = sysDictTypeList.Where(x => x.Code.EndsWith("Enum")).ToList();
  35. try
  36. {
  37. await db.BeginTranAsync();
  38. var storageable1 = await db.Storageable(sysDictTypeList)
  39. .WhereColumns(it => new { it.Code })
  40. .SplitInsert(it => !it.Any())
  41. .SplitUpdate(it => it.Any())
  42. .ToStorageAsync();
  43. await storageable1.BulkCopyAsync();
  44. await storageable1.BulkUpdateAsync();
  45. Log.Information($"系统枚举类转字典类型数据: 共{storageable1.TotalList.Count}条");
  46. var storageable2 = await db.Storageable(sysDictTypeList.SelectMany(x => x.Children).ToList())
  47. .WhereColumns(it => new { it.DictTypeId, it.Code })
  48. .SplitInsert(it => !it.Any())
  49. .SplitUpdate(it => it.Any())
  50. .ToStorageAsync();
  51. await storageable2.BulkCopyAsync();
  52. await storageable2.BulkUpdateAsync();
  53. Log.Information($"系统枚举项转字典值数据: 共{storageable2.TotalList.Count}条");
  54. await db.CommitTranAsync();
  55. }
  56. catch (Exception error)
  57. {
  58. await db.RollbackTranAsync();
  59. Log.Error($"系统枚举转换字典操作错误:{error.Message}\n堆栈跟踪:{error.StackTrace}", error);
  60. throw;
  61. }
  62. }
  63. /// <summary>
  64. /// 枚举信息转字典
  65. /// </summary>
  66. /// <param name="enumTypeList"></param>
  67. /// <returns></returns>
  68. private List<SysDictType> GetDictByEnumType(List<EnumTypeOutput> enumTypeList)
  69. {
  70. var orderNo = 1;
  71. var list = new List<SysDictType>();
  72. foreach (var type in enumTypeList)
  73. {
  74. var dictType = new SysDictType
  75. {
  76. Id = 900000000000 + CommonUtil.GetFixedHashCode(type.TypeName),
  77. Code = type.TypeName,
  78. Name = type.TypeDescribe,
  79. Remark = type.TypeRemark
  80. };
  81. dictType.Children = type.EnumEntities.Select(x => new SysDictData
  82. {
  83. Id = dictType.Id + orderNo++,
  84. DictTypeId = dictType.Id,
  85. Name = x.Name,
  86. Value = x.Describe,
  87. Code = x.Value.ToString(),
  88. OrderNo = x.Value + OrderOffset,
  89. TagType = x.Theme != "" ? x.Theme : DefaultTagType,
  90. }).ToList();
  91. list.Add(dictType);
  92. }
  93. return list;
  94. }
  95. }