| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using System.Configuration;
- using System.Threading.Tasks;
- using Abp.Configuration;
- using Abp.Dependency;
- using Abp.UI;
- using IwbZero.ToolCommon.StringModel;
- using WeOnlineApp.BaseInfo;
- using WeOnlineApp.CommonManager.Sqls;
- using WeOnlineApp.Configuration;
- namespace WeOnlineApp.CommonManager.AppGuids
- {
- public class AppGuidManager : IAppGuidManager, ISingletonDependency
- {
- public AppGuidManager(ISettingManager settingManager)
- {
- SettingManager = settingManager;
- SqlManager = NullSqlManager.Instance;
- }
- public ISqlManager SqlManager { get; set; }
- private ISettingManager SettingManager { get; }
- /// <summary>
- /// 获取下一记录ID
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public string GetNextRecordId(DataLibType type)
- {
- var id = GetGuidFromFile(AppGuidType.DataLib);
- return FormatterRecordId(id, type);
- }
- /// <summary>
- /// 获取下一记录ID
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public async Task<string> GetNextRecordIdAsync(DataLibType type)
- {
- var id = await GetGuidFromFileAsync(AppGuidType.DataLib);
- return FormatterRecordId(id, type);
- }
- /// <summary>
- /// 格式化Id
- /// </summary>
- /// <param name="id"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- private string FormatterRecordId(int id, DataLibType 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(IwbSettingNames.RecordIdPrefix);
- if (str.IsEmpty())
- {
- str = ConfigurationManager.AppSettings["WeOnlineApp.RecordId.Prefix"] ?? "JY";
- }
- return str;
- }
- /// <summary>
- /// 从文件获取自定义Guid
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public int GetGuidFromFile(AppGuidType type)
- {
- int guid = RecordIdManager.GetNextRecordId(type);
- return guid;
- }
- /// <summary>
- /// 从文件获取自定义Guid
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public Task<int> GetGuidFromFileAsync(AppGuidType type)
- {
- int guid = RecordIdManager.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
- }
- }
|