AnswerExtToOuterNotificationHandler.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using CallCenter.Share.Notifications;
  2. using CallCenter.Calls;
  3. using CallCenter.Share.Enums;
  4. using MediatR;
  5. using CallCenter.Realtimes;
  6. using CallCenter.Caches;
  7. namespace CallCenter.Application.Handlers
  8. {
  9. public class AnswerExtToOuterNotificationHandler:INotificationHandler<AnswerExtToOuterNotification>
  10. {
  11. private readonly ICallRepository _callRepository;
  12. private readonly ICallDetailRepository _callDetailRepository;
  13. private readonly IUserCacheManager _userCacheManager;
  14. private readonly IRealtimeService _realtimeService;
  15. public AnswerExtToOuterNotificationHandler(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(AnswerExtToOuterNotification notification, CancellationToken cancellationToken)
  23. {
  24. var model =await _callRepository.GetAsync(
  25. x => x.ConversationId == notification.Outer.Id && x.ToNo == notification.Outer.To &&
  26. x.Trunk == notification.Outer.Trunk && x.CreationTime >= DateTime.Now.AddHours(-2),
  27. cancellationToken);
  28. if (model!=null)
  29. {
  30. model.CallStatus = ECallStatus.Answer;
  31. await _callRepository.UpdateAsync(model, cancellationToken);
  32. var detail = new CallDetail()
  33. {
  34. CallId = model.Id,
  35. CallStatus = ECallStatus.Answer,
  36. OMCallId = notification.Outer.CallId,
  37. ConversationId =notification.Outer.Id,
  38. EventName = notification.Attribute,
  39. FromNo = notification.Outer.From,
  40. ToNo = notification.Outer.To,
  41. AnswerNo = notification.TelNo
  42. };
  43. await _callDetailRepository.AddAsync(detail, cancellationToken);
  44. //调用业务通知 通知前端
  45. var workModel = _userCacheManager.GetWorkByTel(notification.Outer.From);
  46. if (workModel != null)
  47. {
  48. 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);
  49. }
  50. }
  51. }
  52. }
  53. }