AnsweredExtToOuterNotificationHandler.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 AnsweredExtToOuterNotificationHandler:INotificationHandler<AnsweredExtToOuterNotification>
  10. {
  11. private readonly ICallRepository _callRepository;
  12. private readonly ICallDetailRepository _callDetailRepository;
  13. private readonly IUserCacheManager _userCacheManager;
  14. private readonly IRealtimeService _realtimeService;
  15. public AnsweredExtToOuterNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository, IUserCacheManager userCacheManager, IRealtimeService realtimeService)
  16. {
  17. _callRepository = callRepository;
  18. _callDetailRepository = callDetailRepository;
  19. _userCacheManager = userCacheManager;
  20. _realtimeService = realtimeService;
  21. }
  22. public async Task Handle(AnsweredExtToOuterNotification notification, CancellationToken cancellationToken)
  23. {
  24. var model = await _callRepository.GetAsync(
  25. x => x.ConversationId == notification.Outer.Id &&
  26. x.FromNo == notification.Outer.From && x.ToNo == notification.Outer.To && x.CreationTime >= DateTime.Now.AddHours(-2), cancellationToken);
  27. if (model != null)
  28. {
  29. model.CallStatus = ECallStatus.Answered;
  30. await _callRepository.UpdateAsync(model, cancellationToken);
  31. var detail = new CallDetail()
  32. {
  33. CallId = model.Id,
  34. CallStatus = ECallStatus.Answered,
  35. OMCallId = notification.Outer.CallId,
  36. ConversationId = notification.Outer.Id,
  37. EventName = notification.Attribute,
  38. AnswerNo = notification.TelNo,
  39. FromNo = notification.Outer.From,
  40. ToNo = notification.Outer.To
  41. };
  42. await _callDetailRepository.AddAsync(detail, cancellationToken);
  43. //调用业务通知 通知前端
  44. var workModel = _userCacheManager.GetWorkByTel(notification.Outer.From);
  45. if (workModel != null)
  46. {
  47. await _realtimeService.AnsweredAsync(workModel.UserId, new AnseredDto() { ConversationId = notification.Outer.Id, From = notification.Outer.From, Id = model.Id, To = notification.Outer.To, CallType = ECallType.ExtToOuter }, cancellationToken);
  48. }
  49. }
  50. }
  51. }
  52. }