AppGuidManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Abp.Configuration;
  2. using Abp.Dependency;
  3. using Abp.UI;
  4. using VberZero.Configuration;
  5. using VberZero.Settings;
  6. using VberZero.Tools.StringModel;
  7. namespace VberZero.DomainService.AppGuids;
  8. public class AppGuidManager : VzDomainServiceBase, IAppGuidManager, ISingletonDependency
  9. {
  10. public AppGuidManager(ISettingManager settingManager, RecordIdManager riManager, IWebHostEnvironment env)
  11. {
  12. _settingManager = settingManager;
  13. _riManager = riManager;
  14. _configuration = env.GetAppConfiguration();
  15. }
  16. private readonly IConfigurationRoot _configuration;
  17. private readonly ISettingManager _settingManager;
  18. private readonly RecordIdManager _riManager;
  19. /// <summary>
  20. /// 获取下一记录ID
  21. /// </summary>
  22. /// <param name="type"></param>
  23. /// <returns></returns>
  24. public string GetNextRecordId(int type)
  25. {
  26. var id = GetGuidFromFile(AppGuidType.DataLib);
  27. return FormatterRecordId(id, type);
  28. }
  29. /// <summary>
  30. /// 获取下一记录ID
  31. /// </summary>
  32. /// <param name="type"></param>
  33. /// <returns></returns>
  34. public async Task<string> GetNextRecordIdAsync(int type)
  35. {
  36. var id = await GetGuidFromFileAsync(AppGuidType.DataLib);
  37. return FormatterRecordId(id, type);
  38. }
  39. public int GetRecordIdType(string no)
  40. {
  41. var pre = GetIdPrefix();
  42. var typeIntStr = no.Substring(pre.Length, 3);
  43. if (int.TryParse(typeIntStr, out int typeInt))
  44. {
  45. return typeInt;
  46. }
  47. return default;
  48. }
  49. /// <summary>
  50. /// 格式化Id
  51. /// </summary>
  52. /// <param name="id"></param>
  53. /// <param name="type"></param>
  54. /// <returns></returns>
  55. private string FormatterRecordId(int id, int type)
  56. {
  57. if (id == 0)
  58. {
  59. throw new UserFriendlyException("获取RecordId失败");
  60. }
  61. string prefix = GetIdPrefix();
  62. return $"{prefix}{type.ToInt().LeftPad(3)}{id.LeftPad(9)}";
  63. }
  64. /// <summary>
  65. /// 获取前缀
  66. /// </summary>
  67. /// <returns></returns>
  68. private string GetIdPrefix()
  69. {
  70. string str = _settingManager.GetSettingValue(VzSettingNames.RecordIdPrefix);
  71. if (str.Empty())
  72. {
  73. str = _configuration["RecordId:Prefix"] ?? "VB";
  74. }
  75. return str;
  76. }
  77. /// <summary>
  78. /// 从文件获取自定义Guid
  79. /// </summary>
  80. /// <param name="type"></param>
  81. /// <returns></returns>
  82. public int GetGuidFromFile(AppGuidType type)
  83. {
  84. int guid = _riManager.GetNextRecordId(type);
  85. return guid;
  86. }
  87. /// <summary>
  88. /// 从文件获取自定义Guid
  89. /// </summary>
  90. /// <param name="type"></param>
  91. /// <returns></returns>
  92. public Task<int> GetGuidFromFileAsync(AppGuidType type)
  93. {
  94. int guid = _riManager.GetNextRecordId(type);
  95. return Task.FromResult(guid);
  96. }
  97. #region 从DB获取自定义Guid
  98. ///// <summary>
  99. ///// 获取自定义Guid
  100. ///// </summary>
  101. ///// <param name="type"></param>
  102. ///// <returns></returns>
  103. //public int GetAppGuid(AppGuidType type)
  104. //{
  105. // var sqlParams = new object[3];
  106. // sqlParams[0] = new SqlParameter("@idtype", (int)type);
  107. // sqlParams[1] = new SqlParameter("@nextid", SqlDbType.Int) { Direction = ParameterDirection.Output };
  108. // sqlParams[2] = new SqlParameter("@maxid", SqlDbType.Int) { Direction = ParameterDirection.Output };
  109. // SqlManager.Execute(@"exec [dbo].[Sp_AppGuid] @idtype,@nextid out,@maxid out", sqlParams);
  110. // int guid = (int)((SqlParameter)sqlParams[2]).Value;
  111. // return guid;
  112. //}
  113. ///// <summary>
  114. ///// 获取自定义Guid
  115. ///// </summary>
  116. ///// <param name="type"></param>
  117. ///// <returns></returns>
  118. //public async Task<int> GetAppGuidAsync(AppGuidType type)
  119. //{
  120. // var sqlParams = new object[3];
  121. // sqlParams[0] = new SqlParameter("@idtype", (int)type);
  122. // sqlParams[1] = new SqlParameter("@nextid", SqlDbType.Int) { Direction = ParameterDirection.Output };
  123. // sqlParams[2] = new SqlParameter("@maxid", SqlDbType.Int) { Direction = ParameterDirection.Output };
  124. // await SqlManager.ExecuteAsync(@"exec [dbo].[Sp_AppGuid] @idtype,@nextid out,@maxid out", sqlParams);
  125. // int guid = (int)((SqlParameter)sqlParams[2]).Value;
  126. // return guid;
  127. //}
  128. #endregion 从DB获取自定义Guid
  129. }