瀏覽代碼

部分线程安全问题处理

yuanrf 3 天之前
父節點
當前提交
5cac2649ed
共有 1 個文件被更改,包括 22 次插入6 次删除
  1. 22 6
      OASystem/OASystem.Infrastructure/Repositories/CRM/NewClientDataRepository.cs

+ 22 - 6
OASystem/OASystem.Infrastructure/Repositories/CRM/NewClientDataRepository.cs

@@ -38,6 +38,7 @@ using System.Xml.Linq;
 using System.Runtime.Serialization;
 using static Google.Protobuf.Reflection.SourceCodeInfo.Types;
 using static OASystem.Domain.Dtos.CRM.NewClientDataQueryDto;
+using Autofac;
 
 namespace OASystem.Infrastructure.Repositories.CRM
 {
@@ -48,12 +49,14 @@ namespace OASystem.Infrastructure.Repositories.CRM
     {
         private readonly IMapper _mapper;
         private readonly IRedisHelper _redisHelper;
-        
-        public NewClientDataRepository(SqlSugarClient sqlSugar, IMapper mapper) :
+        private readonly ILifetimeScope _scope;
+
+        public NewClientDataRepository(SqlSugarClient sqlSugar, IMapper mapper, ILifetimeScope scope) :
          base(sqlSugar)
         {
             _mapper = mapper;
             _redisHelper = RedisFactory.CreateRedisRepository();
+            this._scope = scope;
         }
 
         /// <summary>
@@ -95,9 +98,15 @@ namespace OASystem.Infrastructure.Repositories.CRM
                 return cachedData;
             }
 
-            // 缓存未命中,从数据库获取
-            var setDatas = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0).ToList();
-            
+            var setDatas = new List<Sys_SetData>();
+            using var newScope = _scope.BeginLifetimeScope();
+            using (SqlSugarClient sc = newScope.Resolve<SqlSugarClient>())
+            {
+                setDatas = await sc.Queryable<Sys_SetData>()
+                .Where(x => x.IsDel == 0)
+                .ToListAsync();
+            }
+
             var dropdownData = new DropdownData();
             
             //负责人下拉框
@@ -122,6 +131,7 @@ namespace OASystem.Infrastructure.Repositories.CRM
             List<Sys_SetData> CustomerClass = setDatas
                 .Where(u => u.STid == 37 && u.IsDel == 0)
                 .ToList();
+
             foreach (Sys_SetData item in CustomerClass)
             {
                 customerClassList.Add(new DropdownItem
@@ -286,8 +296,14 @@ namespace OASystem.Infrastructure.Repositories.CRM
                     break;
             }
 
-            var rangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
+            var rangeSetDataList = new List<int>();
+            using var newScope = _scope.BeginLifetimeScope();
+            using (SqlSugarClient sc = newScope.Resolve<SqlSugarClient>())
+            {
+                rangeSetDataList = sc.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
+            }
             
+
             // 缓存1小时
             await _redisHelper.StringSetAsync(cacheKey, rangeSetDataList, TimeSpan.FromHours(1));