EnumToDictJob.cs 4.2 KB

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