ByeVisitorAndExtNotificationHandler.cs 3.1 KB

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