| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using System.Collections.Concurrent;
- using System.Timers;
- using System.Xml;
- using Abp.Dependency;
- using Abp.Logging;
- using Abp.UI;
- using VberZero.Configuration;
- using VberZero.Tools.StringModel;
- using Timer = System.Timers.Timer;
- namespace VberZero.DomainService.AppGuids;
- public class RecordIdManager : ISingletonDependency
- {
- public RecordIdManager(IWebHostEnvironment env)
- {
- var cfg = env.GetAppConfiguration();
- Interval = cfg["RecordId:TimerInterval"]?.ValD() ?? 10;
- HasChanged = true;
- FileName = Path.Combine(env.ContentRootPath, cfg["RecordId:Path"] ?? VzConsts.ResourceBasePath,
- "Guids.Vzx");
- }
- //分钟
- private decimal Interval { get; }
- private bool HasChanged { get; set; }
- private string FileName { get; set; }
- /// <summary>
- /// 定时器
- /// </summary>
- private Timer _timer;
- private static ConcurrentDictionary<AppGuidType, RecordId> RecordIddDictionary { get; set; }
- /// <summary>
- /// 获取下一记录ID
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public int GetNextRecordId(AppGuidType type)
- {
- HasChanged = true;
- if (RecordIddDictionary.TryGetValue(type, out var record))
- {
- return record.GetNextId();
- }
- throw new UserFriendlyException("获取下一记录ID失败!");
- }
- /// <summary>
- /// 加载文件数据
- /// </summary>
- public void LoadDictionary(string fileName = null)
- {
- if (fileName.NotEmpty())
- {
- FileName = fileName;
- }
- if (!File.Exists(FileName))
- {
- //throw new Exception("未查询到Guid文件,请检查配置!");
- LogHelper.Logger.Error("【未查询到Guid文件,请检查配置!】");
- return;
- }
- RecordIddDictionary = new ConcurrentDictionary<AppGuidType, RecordId>();
- try
- {
- var xDoc = new XmlDocument();
- xDoc.Load(FileName ?? throw new InvalidOperationException());
- XmlNode xmlNode = xDoc.LastChild;
- if (xmlNode is { HasChildNodes: true })
- {
- foreach (XmlNode node in xmlNode.ChildNodes)
- {
- var record = new RecordId
- {
- IdType = (node.Attributes?["type"] != null ? node.Attributes["type"].Value : "")
- .GetEnumByName<AppGuidType>(),
- Step = (node.Attributes?["step"] != null ? node.Attributes["step"].Value : "").ValI(),
- LastId = (node.Attributes?["last-id"] != null ? node.Attributes["last-id"].Value : "")
- .ValI(),
- LastDate = (node.Attributes?["last-date"] != null
- ? node.Attributes["last-date"].Value
- : "").StrToDt()
- };
- if (!RecordIddDictionary.ContainsKey(record.IdType))
- {
- RecordIddDictionary.TryAdd(record.IdType, record);
- }
- }
- }
- //触发定时器执行
- ExecuteTimer();
- }
- catch (Exception e)
- {
- LogHelper.Logger.Error("Guid文件解析失败,请检查配置!", e);
- //LogHelper.LogException(e);
- //throw new Exception($"Guid文件解析失败,请检查配置!({e.Message})");
- }
- }
- /// <summary>
- /// 执行定时器
- /// </summary>
- private void ExecuteTimer()
- {
- if (_timer != null)
- {
- _timer.Stop();
- _timer.Close();
- _timer = null;
- }
- //分钟转换成毫秒
- var interval = Convert.ToDouble(Interval * 60 * 1000);
- _timer = new Timer(interval);
- _timer.Elapsed += Save;
- _timer.Enabled = true;
- _timer.AutoReset = true;
- _timer.Start();
- }
- /// <summary>
- /// 定时保存数据
- /// </summary>
- /// <param name="source"></param>
- /// <param name="ev"></param>
- private void Save(object source, ElapsedEventArgs ev)
- {
- if (!HasChanged)
- return;
- if (RecordIddDictionary == null)
- return;
- var str = "";
- foreach (var dic in RecordIddDictionary)
- {
- var record = dic.Value;
- str += $"<Guid type=\"{record.IdType}\" step=\"{record.Step}\" last-id=\"{record.LastId}\" last-date=\"{record.LastDate}\"></Guid>";
- }
- str = $"<Guids>{str}</Guids>";
- try
- {
- var xDoc = new XmlDocument { InnerXml = str };
- xDoc.Save(FileName);
- }
- catch (Exception e)
- {
- LogHelper.LogException(e);
- }
- HasChanged = false;
- }
- }
|