| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using Abp.Configuration;
- using Abp.Dependency;
- using Abp.UI;
- using VberZero.Configuration;
- using VberZero.Settings;
- using VberZero.Tools.StringModel;
- namespace VberZero.DomainService.AppGuids;
- public class AppGuidManager : VzDomainServiceBase, IAppGuidManager, ISingletonDependency
- {
- public AppGuidManager(ISettingManager settingManager, RecordIdManager riManager, IWebHostEnvironment env)
- {
- _settingManager = settingManager;
- _riManager = riManager;
- _configuration = env.GetAppConfiguration();
- }
- private readonly IConfigurationRoot _configuration;
- private readonly ISettingManager _settingManager;
- private readonly RecordIdManager _riManager;
- /// <summary>
- /// 获取下一记录ID
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public string GetNextRecordId(int type)
- {
- var id = GetGuidFromFile(AppGuidType.DataLib);
- return FormatterRecordId(id, type);
- }
- /// <summary>
- /// 获取下一记录ID
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public async Task<string> GetNextRecordIdAsync(int type)
- {
- var id = await GetGuidFromFileAsync(AppGuidType.DataLib);
- return FormatterRecordId(id, type);
- }
- public int GetRecordIdType(string no)
- {
- var pre = GetIdPrefix();
- var typeIntStr = no.Substring(pre.Length, 3);
- if (int.TryParse(typeIntStr, out int typeInt))
- {
- return typeInt;
- }
- return default;
- }
- /// <summary>
- /// 格式化Id
- /// </summary>
- /// <param name="id"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- private string FormatterRecordId(int id, int type)
- {
- if (id == 0)
- {
- throw new UserFriendlyException("获取RecordId失败");
- }
- string prefix = GetIdPrefix();
- return $"{prefix}{type.ToInt().LeftPad(3)}{id.LeftPad(9)}";
- }
- /// <summary>
- /// 获取前缀
- /// </summary>
- /// <returns></returns>
- private string GetIdPrefix()
- {
- string str = _settingManager.GetSettingValue(VzSettingNames.RecordIdPrefix);
- if (str.Empty())
- {
- str = _configuration["RecordId:Prefix"] ?? "VB";
- }
- return str;
- }
- /// <summary>
- /// 从文件获取自定义Guid
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public int GetGuidFromFile(AppGuidType type)
- {
- int guid = _riManager.GetNextRecordId(type);
- return guid;
- }
- /// <summary>
- /// 从文件获取自定义Guid
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public Task<int> GetGuidFromFileAsync(AppGuidType type)
- {
- int guid = _riManager.GetNextRecordId(type);
- return Task.FromResult(guid);
- }
- #region 从DB获取自定义Guid
- ///// <summary>
- ///// 获取自定义Guid
- ///// </summary>
- ///// <param name="type"></param>
- ///// <returns></returns>
- //public int GetAppGuid(AppGuidType type)
- //{
- // var sqlParams = new object[3];
- // sqlParams[0] = new SqlParameter("@idtype", (int)type);
- // sqlParams[1] = new SqlParameter("@nextid", SqlDbType.Int) { Direction = ParameterDirection.Output };
- // sqlParams[2] = new SqlParameter("@maxid", SqlDbType.Int) { Direction = ParameterDirection.Output };
- // SqlManager.Execute(@"exec [dbo].[Sp_AppGuid] @idtype,@nextid out,@maxid out", sqlParams);
- // int guid = (int)((SqlParameter)sqlParams[2]).Value;
- // return guid;
- //}
- ///// <summary>
- ///// 获取自定义Guid
- ///// </summary>
- ///// <param name="type"></param>
- ///// <returns></returns>
- //public async Task<int> GetAppGuidAsync(AppGuidType type)
- //{
- // var sqlParams = new object[3];
- // sqlParams[0] = new SqlParameter("@idtype", (int)type);
- // sqlParams[1] = new SqlParameter("@nextid", SqlDbType.Int) { Direction = ParameterDirection.Output };
- // sqlParams[2] = new SqlParameter("@maxid", SqlDbType.Int) { Direction = ParameterDirection.Output };
- // await SqlManager.ExecuteAsync(@"exec [dbo].[Sp_AppGuid] @idtype,@nextid out,@maxid out", sqlParams);
- // int guid = (int)((SqlParameter)sqlParams[2]).Value;
- // return guid;
- //}
- #endregion 从DB获取自定义Guid
- }
|