InviteNotificationHandler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.CallCenter.BlackLists;
  3. using Hotline.CallCenter.Calls;
  4. using Hotline.CallCenter.Devices;
  5. using Hotline.Settings;
  6. using Hotline.Share.Enums.CallCenter;
  7. using Hotline.Share.Notifications;
  8. using Hotline.Tools;
  9. using MediatR;
  10. using Microsoft.Extensions.Options;
  11. using NewRock.Sdk;
  12. using NewRock.Sdk.Accept.Request;
  13. using NewRock.Sdk.Control.Request;
  14. namespace Hotline.Application.Handlers.CallCenter.FlowControl
  15. {
  16. public class InviteNotificationHandler : INotificationHandler<InviteNotification>
  17. {
  18. private readonly ICallRepository _callRepository;
  19. private readonly ICallDetailRepository _callDetailRepository;
  20. private readonly IUserCacheManager _userCacheManager;
  21. private readonly IBlacklistDomainService _blacklistDomainService;
  22. private readonly INewRockClient _newRockClient;
  23. private readonly IOptionsSnapshot<CallCenterConfiguration> _options;
  24. private readonly IOptionsSnapshot<WorkTimeSettings> _workTimeOptions;
  25. public InviteNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository, IUserCacheManager userCacheManager, IBlacklistDomainService blacklistDomainService, INewRockClient newRockClient, IOptionsSnapshot<CallCenterConfiguration> options, IOptionsSnapshot<WorkTimeSettings> workTimeOptions)
  26. {
  27. _callRepository = callRepository;
  28. _callDetailRepository = callDetailRepository;
  29. _userCacheManager = userCacheManager;
  30. _blacklistDomainService = blacklistDomainService;
  31. _newRockClient = newRockClient;
  32. _options = options;
  33. _workTimeOptions = workTimeOptions;
  34. }
  35. public bool IsInWrokTime()
  36. {
  37. return false;
  38. }
  39. public async Task Handle(InviteNotification notification, CancellationToken cancellationToken)
  40. {
  41. //验证来电是否在黑名单中
  42. bool isblacklist = _blacklistDomainService.IsInBlacklist(notification.Visitor.From);
  43. //获取星期数
  44. //string weeekNum = WeekTool.GetWeekNum();
  45. //TODO 去掉绑定用户信息
  46. //var workModel = _userCacheManager.GetWorkByTel(notification.Visitor.To);
  47. var isp = PhoneIspTool.GetPhoneIsp(notification.Visitor.From);
  48. var model = new Call()
  49. {
  50. CallStatus = ECallStatus.PreIncoming,
  51. CallDirection = ECallDirection.In,
  52. CallType = ECallType.VisitorCallIn,
  53. ConversationId = notification.Visitor.Id,
  54. FromNo = notification.Visitor.From,
  55. ToNo = notification.Visitor.To,
  56. Trunk = notification.TrunkId,
  57. //UserId = workModel.UserId,
  58. //UserName = workModel.UserName,
  59. PhoneIsp = isp,
  60. };
  61. if (isblacklist)
  62. model.RingOffType = ERingOffType.BlackList;
  63. model.Modified();
  64. var callid = await _callRepository.AddAsync(model, cancellationToken);
  65. var detail = new CallDetail()
  66. {
  67. CallId = callid,
  68. CallStatus = ECallStatus.PreIncoming,
  69. OMCallId = notification.Visitor.CallId,
  70. ConversationId = notification.Visitor.Id,
  71. EventName = notification.Attribute,
  72. FromNo = notification.Visitor.From,
  73. ToNo = notification.Visitor.To,
  74. };
  75. await _callDetailRepository.AddAsync(detail, cancellationToken);
  76. //如果是在黑名单中直接挂断
  77. if (isblacklist)
  78. {
  79. await _newRockClient.ClearCall(new ClearCallRequest()
  80. {
  81. Attribute = "Clear",
  82. Visitor = new ClearCallVisitor()
  83. {
  84. Id = notification.Visitor.Id
  85. }
  86. }, _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  87. }
  88. else
  89. {
  90. await _newRockClient.AcceptVisitor(
  91. new AcceptVisitorRequest()
  92. { Attribute = "Accept", Visitor = new AcceptVisitorModel() { Id = notification.Visitor.Id } },
  93. _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  94. }
  95. }
  96. }
  97. }