ソースを参照

通话队列通知

dss 2 年 前
コミット
db2d62c161

+ 14 - 0
src/CallCenter.Api/Realtimes/RealtimeMethods.cs

@@ -2,10 +2,24 @@
 {
     public static class RealtimeMethods
     {
+        /// <summary>
+        /// 电话振铃通知
+        /// </summary>
         public static string Ring = "Ring";
 
+        /// <summary>
+        /// 电话应答通知
+        /// </summary>
         public static string Answered = "Answered";
 
+        /// <summary>
+        /// 电话挂断通知
+        /// </summary>
         public static string Bye = "Bye";
+
+        /// <summary>
+        /// 电话队列通知
+        /// </summary>
+        public static string CallQueue = "CallQueue";
     }
 }

+ 16 - 0
src/CallCenter.Api/Realtimes/RealtimeService.cs

@@ -64,5 +64,21 @@ namespace CallCenter.Api.Realtimes
                 throw new UserFriendlyException("无效signalr.connectionId");
             await _hubContext.Clients.Client(work.SignalRId).SendAsync(RealtimeMethods.Bye, dto, cancellationToken);
         }
+
+        /// <summary>
+        /// 通话队列通知
+        /// </summary>
+        /// <param name="userId"></param>
+        /// <param name="count"></param>
+        /// <param name="cancellationToken"></param>
+        /// <returns></returns>
+        public async Task CallQueueAsync(CancellationToken cancellationToken)
+        {
+            var works = _userCacheManager.GetWorks();
+            if (works!=null)
+            {
+                await _hubContext.Clients.Clients(works.Select(x => x.SignalRId).ToList()).SendAsync(RealtimeMethods.CallQueue, 1, cancellationToken);
+            }
+        }
     }
 }

+ 41 - 0
src/CallCenter.CacheManager/CallQueueManager.cs

@@ -0,0 +1,41 @@
+using CallCenter.Caches;
+using CallCenter.Realtimes;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace CallCenter.CacheManager
+{
+    public class CallQueueManager : BackgroundService
+    {
+        private readonly IServiceScopeFactory _serviceScopeFactory;
+
+        public CallQueueManager(IServiceScopeFactory serviceScopeFactory)
+        {
+            _serviceScopeFactory = serviceScopeFactory;
+        }
+
+
+        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
+        {
+            //work缓存注入
+            var userCacheManager = _serviceScopeFactory.CreateScope().ServiceProvider.GetService<IUserCacheManager>();
+            //消息通知注入
+            var realtimeService = _serviceScopeFactory.CreateScope().ServiceProvider.GetService<IRealtimeService>();
+
+            var time = TimeSpan.FromSeconds(1);
+            while (!cancellationToken.IsCancellationRequested)
+            {
+                //通知
+                await realtimeService.CallQueueAsync(cancellationToken);
+                await Task.Delay(time, cancellationToken);
+            }
+
+            
+        }
+    }
+}

+ 6 - 0
src/CallCenter/Caches/IUserCacheManager.cs

@@ -45,5 +45,11 @@ namespace CallCenter.Caches
         /// <param name="work"></param>
         /// <returns></returns>
         void UpdateWorkByUser(Work work);
+
+        /// <summary>
+        /// 查询所有在工作的记录
+        /// </summary>
+        /// <returns></returns>
+        List<Work> GetWorks();
     }
 }

+ 46 - 3
src/CallCenter/Caches/UserCacheManager.cs

@@ -1,4 +1,5 @@
-using CallCenter.Users;
+using CallCenter.Share.Requests;
+using CallCenter.Users;
 using XF.Domain.Cache;
 using XF.Domain.Dependency;
 using XF.Domain.Exceptions;
@@ -9,13 +10,16 @@ public class UserCacheManager : IUserCacheManager, IScopeDependency
 {
     private readonly ITypedCache<Work> _cacheWork;
     private readonly IWorkRepository _workRepository;
-
+    private readonly ITypedCache<List<Work>> _cacheWorks;
+    private const string WorkKey = "Works";
     public UserCacheManager(
         ITypedCache<Work> cacheWork,
-        IWorkRepository workRepository)
+        IWorkRepository workRepository,
+        ITypedCache<List<Work>> cacheWorks)
     {
         _cacheWork = cacheWork;
         _workRepository = workRepository;
+        _cacheWorks = cacheWorks;
     }
 
     /// <summary>
@@ -55,6 +59,42 @@ public class UserCacheManager : IUserCacheManager, IScopeDependency
         return work;
     }
 
+    /// <summary>
+    /// 查询所有在工作的记录
+    /// </summary>
+    /// <returns></returns>
+    public List<Work> GetWorks()
+    {
+        var works = _cacheWorks.GetOrAdd(WorkKey, k =>
+        {
+            var works = _workRepository.QueryAsync(x => x.EndTime == null).GetAwaiter().GetResult();
+            return works;
+        });
+        return works;
+    }
+
+    public void AddOrUpdateWorksCache(List<Work> list)
+    {
+        _cacheWorks.AddOrUpdate(WorkKey, list);
+    }
+
+
+    public void RemoveCallCache(string id)
+    {
+        var list = _cacheWorks.Get(WorkKey)?.ToList();
+        if (list!=null)
+        {
+            var model = list.First(x => x.Id == id);
+            if (model!=null)
+            {
+                list.Remove(model);
+                AddOrUpdateWorksCache(list);
+            }
+        }
+    }
+
+
+
     /// <summary>
     /// 查询分机是否处于工作
     /// </summary>
@@ -108,4 +148,7 @@ public class UserCacheManager : IUserCacheManager, IScopeDependency
     {
         _cacheWork.Update(Work.GetKey(KeyMode.UserId, work.UserId), d => work);
     }
+
+    
+
 }

+ 2 - 0
src/CallCenter/Realtimes/IRealtimeService.cs

@@ -14,6 +14,8 @@ namespace CallCenter.Realtimes
         Task AnsweredAsync(string userId, AnseredDto dto, CancellationToken cancellationToken);
 
         Task ByeAsync(string userId, ByeDto dto, CancellationToken cancellationToken);
+
+        Task CallQueueAsync(CancellationToken cancellationToken);
     }
 
     public record ByeDto