AppGuidManager.cs 4.5 KB

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