RecordIdManager.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Timers;
  6. using System.Xml;
  7. using Abp.UI;
  8. using IwbZero.ToolCommon.LogHelpers;
  9. using IwbZero.ToolCommon.StringModel;
  10. using WeApp.BaseInfo;
  11. using WeApp.Configuration;
  12. namespace WeApp.CommonManager.AppGuids
  13. {
  14. public static class RecordIdManager
  15. {
  16. private static readonly decimal Interval = ConfigurationManager.AppSettings["WeApp.RecordId.TimerInterval"]?.ValD() ?? (decimal)1;
  17. private static bool HasChanged = true;
  18. /// <summary>
  19. /// 定时器
  20. /// </summary>
  21. private static Timer Timer;
  22. private static string FileName => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, IwbConsts.ResourceBasePath, "Guids.iwbx");
  23. //分钟
  24. private static ConcurrentDictionary<AppGuidType, RecordId> RecordIddDictionary { get; set; }
  25. /// <summary>
  26. /// 获取下一记录ID
  27. /// </summary>
  28. /// <param name="type"></param>
  29. /// <returns></returns>
  30. public static int GetNextRecordId(AppGuidType type)
  31. {
  32. HasChanged = true;
  33. if (RecordIddDictionary.TryGetValue(type, out var record))
  34. {
  35. return record.GetNextId();
  36. }
  37. throw new UserFriendlyException("获取下一记录ID失败!");
  38. }
  39. /// <summary>
  40. /// 加载文件数据
  41. /// </summary>
  42. public static void LoadDictionary()
  43. {
  44. if (!File.Exists(FileName))
  45. {
  46. throw new Exception("未查询到Guid文件,请检查配置!");
  47. }
  48. RecordIddDictionary = new ConcurrentDictionary<AppGuidType, RecordId>();
  49. try
  50. {
  51. var xDoc = new XmlDocument();
  52. xDoc.Load(FileName);
  53. XmlNode xmlNode = xDoc.LastChild;
  54. if (xmlNode != null && xmlNode.HasChildNodes)
  55. {
  56. foreach (XmlNode node in xmlNode.ChildNodes)
  57. {
  58. var record = new RecordId
  59. {
  60. IdType = (node.Attributes?["type"] != null ? node.Attributes["type"].Value : "")
  61. .GetEnumByName<AppGuidType>(),
  62. Step = (node.Attributes?["step"] != null ? node.Attributes["step"].Value : "").ValI(),
  63. LastId = (node.Attributes?["last-id"] != null ? node.Attributes["last-id"].Value : "")
  64. .ValI(),
  65. LastDate = (node.Attributes?["last-date"] != null
  66. ? node.Attributes["last-date"].Value
  67. : "").StrToDt()
  68. };
  69. if (!RecordIddDictionary.ContainsKey(record.IdType))
  70. {
  71. RecordIddDictionary.TryAdd(record.IdType, record);
  72. }
  73. }
  74. }
  75. //触发定时器执行
  76. ExecuteTimer();
  77. }
  78. catch (Exception e)
  79. {
  80. typeof(RecordIdManager).LogError(e);
  81. throw new Exception($"Guid文件解析失败,请检查配置!({e.Message})");
  82. }
  83. }
  84. /// <summary>
  85. /// 执行定时器
  86. /// </summary>
  87. private static void ExecuteTimer()
  88. {
  89. if (Timer != null)
  90. {
  91. Timer.Stop();
  92. Timer.Close();
  93. Timer = null;
  94. }
  95. //分钟转换成毫秒
  96. var interval = Convert.ToDouble(Interval * 60 * 1000);
  97. Timer = new Timer(interval);
  98. Timer.Elapsed += Save;
  99. Timer.Enabled = true;
  100. Timer.AutoReset = true;
  101. Timer.Start();
  102. typeof(RecordIdManager).LogDebug("GUID保存定时器已执行");
  103. }
  104. /// <summary>
  105. /// 定时保存数据
  106. /// </summary>
  107. /// <param name="source"></param>
  108. /// <param name="ev"></param>
  109. private static void Save(object source, ElapsedEventArgs ev)
  110. {
  111. if (!HasChanged)
  112. return;
  113. if (RecordIddDictionary == null)
  114. return;
  115. var str = "";
  116. foreach (var dic in RecordIddDictionary)
  117. {
  118. var record = dic.Value;
  119. str += $"<Guid type=\"{record.IdType}\" step=\"{record.Step}\" last-id=\"{record.LastId}\" last-date=\"{record.LastDate}\"></Guid>";
  120. }
  121. str = $"<Guids>{str}</Guids>";
  122. try
  123. {
  124. typeof(RecordIdManager).LogDebug($"保存GUID文件:{FileName}");
  125. var xDoc = new XmlDocument { InnerXml = str };
  126. xDoc.Save(FileName);
  127. }
  128. catch (Exception e)
  129. {
  130. typeof(RecordIdManager).LogDebug($"保存失败!");
  131. typeof(RecordIdManager).LogError(e);
  132. }
  133. HasChanged = false;
  134. }
  135. }
  136. }