dss 2 éve
szülő
commit
9a4393cceb

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

@@ -21,5 +21,15 @@
         /// 电话队列通知
         /// </summary>
         public static string CallQueue = "CallQueue";
+
+        /// <summary>
+        /// 回铃(去电)
+        /// </summary>
+        public static string Alert = "Alert";
+
+        /// <summary>
+        /// 话机空闲
+        /// </summary>
+        public static string Idle = "Idle";
     }
 }

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

@@ -1,5 +1,6 @@
 using CallCenter.Caches;
 using CallCenter.Realtimes;
+using CallCenter.Users;
 using Microsoft.AspNetCore.SignalR;
 using XF.Domain.Dependency;
 using XF.Domain.Exceptions;
@@ -85,5 +86,36 @@ namespace CallCenter.Api.Realtimes
                 }
             }
         }
+
+        /// <summary>
+        /// 回铃通知(去电)
+        /// </summary>
+        /// <param name="userId"></param>
+        /// <param name="dto"></param>
+        /// <param name="cancellationToken"></param>
+        /// <returns></returns>
+        /// <exception cref="UserFriendlyException"></exception>
+        public async Task AlertAsync(string userId, AlertDto dto, CancellationToken cancellationToken)
+        {
+            var work = _userCacheManager.GetWorkByUser(userId);
+            if(string.IsNullOrEmpty(work.SignalRId))
+                throw new UserFriendlyException("无效signalr.connectionId");
+            await _hubContext.Clients.Client(work.SignalRId).SendAsync(RealtimeMethods.Alert, dto, cancellationToken);
+        }
+
+
+        /// <summary>
+        /// 话机空闲通知
+        /// </summary>
+        /// <param name="userId"></param>
+        /// <param name="cancellationToken"></param>
+        /// <returns></returns>
+        public async Task IdleAsync(string userId, CancellationToken cancellationToken)
+        {
+            var work = _userCacheManager.GetWorkByUser(userId);
+            if (string.IsNullOrEmpty(work.SignalRId))
+                throw new UserFriendlyException("无效signalr.connectionId");
+            await _hubContext.Clients.Client(work.SignalRId).SendAsync(RealtimeMethods.Idle, new { EventName = "Idle" }, cancellationToken);
+        }
     }
 }

+ 12 - 7
src/CallCenter.Application/Handlers/CallState/AlertExtToOuterNotificationHandler.cs

@@ -1,6 +1,7 @@
 using CallCenter.Caches;
 using CallCenter.Calls;
 using CallCenter.Notifications;
+using CallCenter.Realtimes;
 using CallCenter.Share.Enums;
 using CallCenter.Tools;
 using MediatR;
@@ -12,21 +13,24 @@ namespace CallCenter.Application.Handlers
         private readonly ICallRepository _callRepository;
         private readonly ICallDetailRepository _callDetailRepository;
         private readonly IUserCacheManager _userCacheManager;
+        private readonly IRealtimeService _realtimeService;
 
 
-        public AlertExtToOuterNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository,IUserCacheManager userCacheManager)
+        public AlertExtToOuterNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository,IUserCacheManager userCacheManager,IRealtimeService realtimeService)
         {
             _callRepository = callRepository;
             _callDetailRepository = callDetailRepository;
             _userCacheManager = userCacheManager;
+            _realtimeService = realtimeService;
         }
 
         public async Task Handle(AlertExtToOuterNotification notification, CancellationToken cancellationToken)
         {
-            if (!string.IsNullOrEmpty(notification.TelNo))
+            string telNo = notification.Outer.From != "" ? notification.Outer.From : notification.TelNo;
+            if (!string.IsNullOrEmpty(telNo))
             {
                 var model =await _callRepository.GetAsync(x => x.ConversationId==notification.Outer.Id && x.ToNo==notification.Outer.To && x.Trunk==notification.Outer.Trunk && x.CreationTime>=DateTime.Now.AddHours(-2), cancellationToken);
-
+                var workModel = _userCacheManager.GetWorkByTel(telNo);
                 if (model!=null)
                 {
                     model.CallStatus = ECallStatus.Alert;
@@ -42,12 +46,12 @@ namespace CallCenter.Application.Handlers
                         ToNo = notification.Outer.To
                     };
                     await _callDetailRepository.AddAsync(detail, cancellationToken);
+
+                    await _realtimeService.AlertAsync(workModel.UserId, new AlertDto() { Id = model.Id, From = telNo, To = notification.Outer.To, CallType = ECallType.ExtToOuter, ConversationId = notification.Outer.Id }, cancellationToken);
                 }
                 //无记录的情况下
                 else
                 {
-                    string telNo = notification.Outer.From != "" ? notification.Outer.From : notification.TelNo;
-                    var workModel = _userCacheManager.GetWorkByTel(telNo);
                     if (workModel!=null) 
                     {
                         var isp = PhoneIspTool.GetPhoneIsp(notification.Outer.To);
@@ -73,15 +77,16 @@ namespace CallCenter.Application.Handlers
                             CallStatus = ECallStatus.Alert,
                             ConversationId = notification.Outer.Id,
                             OMCallId = notification.Outer.CallId,
-                            EventName = "Alert",
+                            EventName = "ALERT",
                             FromNo = telNo,
                             ToNo = notification.Outer.To,
                         };
                         await _callDetailRepository.AddAsync(detail, cancellationToken);
+
+                        await _realtimeService.AlertAsync(workModel.UserId, new AlertDto() { Id = model.Id, From = telNo, To = notification.Outer.To, CallType = ECallType.ExtToOuter, ConversationId = notification.Outer.Id }, cancellationToken);
                     }
                 }
             }
-
         }
     }
 }

+ 12 - 1
src/CallCenter.Application/Handlers/ExtState/IdleNotificationHandler.cs

@@ -1,5 +1,6 @@
 using CallCenter.Caches;
 using CallCenter.Notifications;
+using CallCenter.Realtimes;
 using CallCenter.Tels;
 using MediatR;
 using XF.Domain.Cache;
@@ -11,11 +12,15 @@ namespace CallCenter.Application.Handlers
         private readonly ITelRepository _telRepository;
         private readonly ITelCacheManager _telCacheManager;
         private readonly ITypedCache<Tel> _typedCache;
-        public IdleNotificationHandler(ITelRepository telRepository, ITelCacheManager telCacheManager, ITypedCache<Tel> typedCache)
+        private readonly IUserCacheManager _userCacheManager;
+        private readonly IRealtimeService _realtimeService;
+        public IdleNotificationHandler(ITelRepository telRepository, ITelCacheManager telCacheManager, ITypedCache<Tel> typedCache,IUserCacheManager userCacheManager,IRealtimeService realtimeService)
         {
             _telRepository=telRepository;
             _telCacheManager = telCacheManager;
             _typedCache = typedCache;
+            _userCacheManager = userCacheManager;
+            _realtimeService = realtimeService;
         }
 
         public async Task Handle(IdleNotification notification, CancellationToken cancellationToken)
@@ -24,6 +29,12 @@ namespace CallCenter.Application.Handlers
             telModel.TelStatus = ETelStatus.Ready;
             //await _telRepository.UpdateAsync(telModel, cancellationToken);
             _typedCache.Update(notification.TelNo, x => telModel);
+
+            var workModel = _userCacheManager.GetWorkByTel(notification.TelNo);
+            if (workModel!=null)
+            {
+                await _realtimeService.IdleAsync(workModel.UserId, cancellationToken);
+            }
         }
     }
 }

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

@@ -16,8 +16,26 @@ namespace CallCenter.Realtimes
         Task ByeAsync(string userId, ByeDto dto, CancellationToken cancellationToken);
 
         Task CallQueueAsync(CancellationToken cancellationToken);
+
+        Task AlertAsync(string userId, AlertDto dto, CancellationToken cancellationToken);
+
+        Task IdleAsync(string userId, CancellationToken cancellationToken);
     }
 
+    public record AlertDto
+    {
+        public string Id { get; set; }
+
+        public string From { get; set; }
+
+        public string To { get; set; }
+
+        public ECallType CallType { get; set; }
+
+        public string ConversationId { get; set; }
+    }
+
+
     public record ByeDto
     {
         public string Id { get; set; }

+ 2 - 2
src/CallCenter/Users/UserDomainService.cs

@@ -60,7 +60,7 @@ namespace CallCenter.Users
 
             var work = new Work(userId, user.Name, tel.Id, tel.No);
             await _workRepository.AddAsync(work, cancellationToken);
-            _userCacheManager.RemoveCallCache(work.Id);
+            //_userCacheManager.RemoveCallCache(work.Id);
             if (!string.IsNullOrEmpty(user.StaffNo))
                 await _deviceManager.UpdateStaffNoAsync(tel.No, user.StaffNo, tel.LineId, cancellationToken);
 
@@ -86,7 +86,7 @@ namespace CallCenter.Users
             await _deviceManager.UpdateStaffNoAsync(work.TelNo, string.Empty, tel.LineId, cancellationToken);
             _cacheWork.Remove(work.GetKey(KeyMode.UserId));
             _cacheWork.Remove(work.GetKey(KeyMode.TelNo));
-            _userCacheManager.RemoveCallCache(work.Id);
+            //_userCacheManager.RemoveCallCache(work.Id);
             //foreach (var group in tel.Groups)
             //{
             //    await _deviceManager.ModifyGroupExtAsync(group.No, tel.No, group.Voice, false, cancellationToken);