EnumToDictJob.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if (sysDictTypeList.Any(x => !x.Name.EndsWith("Enum"))) throw Oops.Bah("枚举类名称必须以Enum结尾");
  34. try
  35. {
  36. await db.BeginTranAsync();
  37. var storageable1 = await db.Storageable(sysDictTypeList)
  38. .WhereColumns(it => new { it.Code })
  39. .SplitInsert(it => !it.Any())
  40. .SplitUpdate(it => it.Any())
  41. .ToStorageAsync();
  42. await storageable1.BulkCopyAsync();
  43. await storageable1.BulkUpdateAsync();
  44. Log.Information($"系统枚举类转字典类型数据: 共{storageable1.TotalList.Count}条");
  45. var storageable2 = await db.Storageable(sysDictTypeList.SelectMany(x => x.Children).ToList())
  46. .WhereColumns(it => new { it.DictTypeId, it.Code })
  47. .SplitInsert(it => !it.Any())
  48. .SplitUpdate(it => it.Any())
  49. .ToStorageAsync();
  50. await storageable2.BulkCopyAsync();
  51. await storageable2.BulkUpdateAsync();
  52. Log.Information($"系统枚举项转字典值数据: 共{storageable2.TotalList.Count}条");
  53. await db.CommitTranAsync();
  54. }
  55. catch (Exception error)
  56. {
  57. await db.RollbackTranAsync();
  58. Log.Error($"系统枚举转换字典操作错误:{error.Message}\n堆栈跟踪:{error.StackTrace}", error);
  59. throw;
  60. }
  61. }
  62. /// <summary>
  63. /// 枚举信息转字典
  64. /// </summary>
  65. /// <param name="enumTypeList"></param>
  66. /// <returns></returns>
  67. private List<SysDictType> GetDictByEnumType(List<EnumTypeOutput> enumTypeList)
  68. {
  69. var orderNo = 1;
  70. var list = new List<SysDictType>();
  71. foreach (var type in enumTypeList)
  72. {
  73. var dictType = new SysDictType
  74. {
  75. Id = 900000000000 + CommonUtil.GetFixedHashCode(type.TypeName),
  76. Code = type.TypeName,
  77. Name = type.TypeDescribe,
  78. Remark = type.TypeRemark
  79. };
  80. dictType.Children = type.EnumEntities.Select(x => new SysDictData
  81. {
  82. Id = dictType.Id + orderNo++,
  83. DictTypeId = dictType.Id,
  84. Name = x.Name,
  85. Value = x.Describe,
  86. Code = x.Value.ToString(),
  87. OrderNo = x.Value + OrderOffset,
  88. TagType = x.Theme != "" ? x.Theme : DefaultTagType,
  89. }).ToList();
  90. list.Add(dictType);
  91. }
  92. return list;
  93. }
  94. }