ByeVisitorAndExtNotificationHandler.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 workModel = _userCacheManager.GetWorkByTel(notification.TelNo);
  51. if (workModel!=null)
  52. {
  53. await _realtimeService.ByeAsync(workModel.UserId, new ByeDto() { Id = model.Id }, cancellationToken);
  54. }
  55. }
  56. }
  57. }
  58. }