RingExtToOuterNotificationHandler.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.CallCenter.Calls;
  3. using Hotline.EventBus;
  4. using Hotline.Share.Enums.CallCenter;
  5. using Hotline.Share.Notifications.NewRockCallCenter;
  6. using Hotline.Tools;
  7. using MediatR;
  8. namespace Hotline.Application.Handlers.CallCenter.CallState
  9. {
  10. [HandlerInject(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
  11. public class RingExtToOuterNotificationHandler : INotificationHandler<RingExtToOuterNotification>
  12. {
  13. private readonly ICallRepository _callRepository;
  14. private readonly ICallDetailRepository _callDetailRepository;
  15. private readonly IUserCacheManager _userCacheManager;
  16. public RingExtToOuterNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository, IUserCacheManager userCacheManager)
  17. {
  18. _callRepository = callRepository;
  19. _callDetailRepository = callDetailRepository;
  20. _userCacheManager = userCacheManager;
  21. }
  22. public async Task Handle(RingExtToOuterNotification notification, CancellationToken cancellationToken)
  23. {
  24. var model = await _callRepository.GetAsync(x =>
  25. x.FromNo == notification.TelNo && x.ToNo == notification.Outer.To && x.CreationTime >= DateTime.Now.AddHours(-2), true, x => x.CreationTime, cancellationToken);
  26. if (model == null)
  27. {
  28. model = await _callRepository.GetAsync(
  29. x => x.ConversationId == notification.Outer.Id && x.ToNo == notification.Outer.To &&
  30. x.Trunk == notification.Outer.Trunk, true, x => x.CreationTime, cancellationToken);
  31. }
  32. if (model != null)
  33. {
  34. var workModel = _userCacheManager.GetWorkByTel(notification.TelNo);
  35. model.UserId = workModel.UserId;
  36. model.UserName = workModel.UserName;
  37. model.CallStatus = ECallStatus.Ring;
  38. await _callRepository.UpdateAsync(model, cancellationToken);
  39. var detail = new CallDetail()
  40. {
  41. CallId = model.Id,
  42. CallStatus = ECallStatus.Ring,
  43. EventName = notification.Attribute,
  44. ConversationId = notification.Outer.Id,
  45. OMCallId = notification.Outer.CallId,
  46. FromNo = notification.Outer.From,
  47. ToNo = notification.Outer.To,
  48. };
  49. if (string.IsNullOrEmpty(detail.FromNo))
  50. detail.FromNo = notification.TelNo;
  51. await _callDetailRepository.AddAsync(detail, cancellationToken);
  52. }
  53. else
  54. {
  55. var isp = PhoneIspTool.GetPhoneIsp(notification.Outer.To);
  56. var callModel = new Call()
  57. {
  58. CallStatus = ECallStatus.Alert,
  59. CallDirection = ECallDirection.Out,
  60. CallType = ECallType.ExtToOuter,
  61. ConversationId = notification.Outer.Id,
  62. FromNo = notification.Outer.From,
  63. ToNo = notification.Outer.To,
  64. Trunk = notification.Outer.Trunk,
  65. PhoneIsp = isp
  66. };
  67. callModel.Modified();
  68. var callId = await _callRepository.AddAsync(callModel, cancellationToken);
  69. //写入明细
  70. var detail = new CallDetail()
  71. {
  72. CallId = callId,
  73. CallStatus = ECallStatus.Alert,
  74. ConversationId = notification.Outer.Id,
  75. OMCallId = notification.Outer.CallId,
  76. EventName = notification.Attribute,
  77. FromNo = notification.Outer.From,
  78. ToNo = notification.Outer.To,
  79. };
  80. await _callDetailRepository.AddAsync(detail, cancellationToken);
  81. }
  82. }
  83. }
  84. }