ByeVisitorAndExtNotificationHandler.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.CallCenter.Calls;
  3. using Hotline.DI;
  4. using Hotline.Realtimes;
  5. using Hotline.Share.Enums.CallCenter;
  6. using Hotline.Share.Notifications.NewRockCallCenter;
  7. using MediatR;
  8. namespace Hotline.Application.Handlers.CallCenter.FlowControl
  9. {
  10. [Injection(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
  11. public class ByeVisitorAndExtNotificationHandler : INotificationHandler<ByeVisitorAndExtNotification>
  12. {
  13. private readonly ICallRepository _callRepository;
  14. private readonly ICallDetailRepository _callDetailRepository;
  15. private readonly IUserCacheManager _userCacheManager;
  16. private readonly IRealtimeService _realtimeService;
  17. private readonly ICallCacheManager _callCacheManager;
  18. public ByeVisitorAndExtNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository, IUserCacheManager userCacheManager, IRealtimeService realtimeService, ICallCacheManager callCacheManager)
  19. {
  20. _callRepository = callRepository;
  21. _callDetailRepository = callDetailRepository;
  22. _userCacheManager = userCacheManager;
  23. _realtimeService = realtimeService;
  24. _callCacheManager = callCacheManager;
  25. }
  26. public async Task Handle(ByeVisitorAndExtNotification notification, CancellationToken cancellationToken)
  27. {
  28. var model = await _callRepository.GetAsync(
  29. x => x.ConversationId == notification.Visitor.Id &&
  30. x.FromNo == notification.Visitor.From && x.CreationTime >= DateTime.Now.AddHours(-2), true, x => x.CreationTime, cancellationToken);
  31. if (model != null)
  32. {
  33. model.CallStatus = ECallStatus.Bye;
  34. model.EndBy = EEndBy.From;
  35. model.RingOffType = ERingOffType.Normal;
  36. model.ByeTime = DateTime.Now;
  37. await _callRepository.UpdateAsync(model, cancellationToken);
  38. var detail = new CallDetail()
  39. {
  40. CallId = model.Id,
  41. CallStatus = ECallStatus.Bye,
  42. OMCallId = notification.Visitor.CallId,
  43. ConversationId = notification.Visitor.Id,
  44. EventName = notification.Attribute,
  45. FromNo = notification.Visitor.From,
  46. ToNo = notification.Visitor.To,
  47. Recording = notification.Recording,
  48. };
  49. await _callDetailRepository.AddAsync(detail, cancellationToken);
  50. //处理队列记录
  51. _callCacheManager.RemoveCallCache(model.Id);
  52. await _realtimeService.CallQueueAsync(_callCacheManager.GetCallQueueList(), cancellationToken);
  53. //查询应答分机分机号
  54. var callDetailAnswer = await _callDetailRepository.GetAsync(x => x.CallId == model.Id && x.EventName == "ANSWER", true, d => d.CreationTime, cancellationToken);
  55. if (callDetailAnswer != null)
  56. {
  57. var workModel = _userCacheManager.GetWorkByTel(callDetailAnswer.AnswerNo);
  58. if (workModel != null)
  59. {
  60. //调用业务通知 通知前端
  61. await _realtimeService.ByeAsync(workModel.UserId, new Share.Dtos.Realtime.ByeDto() { Id = model.Id }, cancellationToken);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }