CdrNotificationHandler.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using CallCenter.Calls;
  2. using CallCenter.NewRock.Handlers;
  3. using CallCenter.Notifications;
  4. using CallCenter.Share.Dtos;
  5. using CallCenter.Share.Enums;
  6. using MediatR;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using NewRock.Sdk.Extensions;
  10. using System.Text;
  11. using System.Text.Json;
  12. using XF.Domain.Exceptions;
  13. namespace CallCenter.Application.Handlers
  14. {
  15. public class CdrNotificationHandler:INotificationHandler<CdrNotification>
  16. {
  17. private readonly ICallRecordRepository _callRecordRepository;
  18. private readonly ICallDetailRepository _callDetailRepository;
  19. private readonly ICallRepository _callRepository;
  20. private readonly IOptionsSnapshot<SendCallRecord> _sendCallRecordOptions;
  21. private readonly IHttpClientFactory _httpClientFactory;
  22. private readonly ILogger<CdrNotificationHandler> _logger;
  23. public CdrNotificationHandler(ICallRecordRepository callRecordRepository, ICallDetailRepository callDetailRepository, ICallRepository callRepository, IOptionsSnapshot<SendCallRecord> sendCallRecordOptions, IHttpClientFactory httpClientFactory,ILogger<CdrNotificationHandler> logger)
  24. {
  25. _callRecordRepository = callRecordRepository;
  26. _callDetailRepository = callDetailRepository;
  27. _callRepository = callRepository;
  28. _sendCallRecordOptions = sendCallRecordOptions;
  29. _httpClientFactory = httpClientFactory;
  30. _logger = logger;
  31. }
  32. public async Task Handle(CdrNotification notification, CancellationToken cancellationToken)
  33. {
  34. var callDetail = await
  35. _callDetailRepository.GetAsync(x => x.OMCallId == notification.CallId, cancellationToken);
  36. if (callDetail!=null)
  37. {
  38. var model = new CallRecord()
  39. {
  40. CallId = callDetail.CallId,
  41. CdrId = notification.Id,
  42. CDRCallId = notification.CallId,
  43. TimeStart = notification.TimeStart,
  44. Group = notification.Group,
  45. Type = (ECDRType)Enum.Parse(typeof(ECDRType), notification.Type),
  46. Route = (ECDRRoute)Enum.Parse(typeof(ECDRRoute), notification.Route),
  47. CPN = notification.CPN,
  48. CDPN = notification.CDPN,
  49. TimeEnd = notification.TimeEnd,
  50. Duration = notification.Duration,
  51. TrunkNumber = notification.TrunkNumber,
  52. Recording = notification.Recording,
  53. RecCodec = notification.RecCodec,
  54. IsDown = false
  55. };
  56. if (!string.IsNullOrEmpty(notification.VisitorId))
  57. model.VisitorId = notification.VisitorId;
  58. if (!string.IsNullOrEmpty(notification.OuterId))
  59. model.OuterId = notification.OuterId;
  60. await _callRecordRepository.AddAsync(model,cancellationToken);
  61. var ishave = await _callDetailRepository.AnyAsync(x => x.EventName == "ALERT" && x.OMCallId == notification.CallId && x.FromNo == notification.CPN && x.ToNo == notification.CDPN);
  62. if (ishave)
  63. {
  64. var callModel = await _callRepository.GetAsync(x => x.Id == callDetail.CallId, cancellationToken);
  65. if (callModel != null)
  66. {
  67. callModel.Duration = double.Parse(model.Duration);
  68. await _callRepository.UpdateAsync(callModel, cancellationToken);
  69. var call = await _callRepository.GetExtAsync(callModel.Id, x => x.Includes(d => d.CallDetails));
  70. //TODO 推送通话报告
  71. OutCallDto callDto = new OutCallDto();
  72. callDto.CallId = callDetail.CallId;
  73. callDto.InfoType = EInfoType.Call;
  74. callDto.Direction = callModel.CallDirection;
  75. callDto.Cpn = callModel.FromNo ?? "";
  76. callDto.Cdpn = callModel.ToNo ?? "";
  77. if (callDto.Direction == ECallDirection.In)
  78. {
  79. callDto.Answered = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWER")?.AnswerNo ?? call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.AnswerNo ?? "";
  80. callDto.OnTime = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWER")?.CreationTime ?? call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.CreationTime;
  81. callDto.OnState = call.CallDetails?.Any(x => x.EventName == "ANSWER" || x.EventName == "ANSWERED") == true ? EOnState.On : EOnState.NoOn;
  82. }
  83. else
  84. {
  85. //callDto.Answered = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.AnswerNo ?? "";
  86. //callDto.OnTime = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.CreationTime;
  87. //callDto.OnState = call.CallDetails?.Any(x => x.EventName == "ANSWERED") == true ? EOnState.On : EOnState.NoOn;
  88. callDto.Answered = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWER")?.AnswerNo ?? call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.AnswerNo ?? "";
  89. callDto.OnTime = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWER")?.CreationTime ?? call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.CreationTime;
  90. callDto.OnState = call.CallDetails?.Any(x => x.EventName == "ANSWER" || x.EventName == "ANSWERED") == true ? EOnState.On : EOnState.NoOn;
  91. }
  92. callDto.BeginTime = call.CreationTime;
  93. callDto.ByeTime = call.CallDetails?.FirstOrDefault(x => x.EventName == "BYE").CreationTime;
  94. callDto.TalkTime = call.Duration;
  95. callDto.SoundFileName = call.CallDetails?.FirstOrDefault(x => x.EventName == "BYE")?.Recording ?? model.Recording;
  96. callDto.EvaluateResult = "";
  97. callDto.EndBy = call.EndBy;
  98. callDto.InIvrTime = call.InIvrTime;
  99. callDto.OutIvrTime = call.OutIvrTime;
  100. callDto.InGroupTime = call.InGroupTime;
  101. callDto.OutGroupTime = call.OutGroupTime;
  102. callDto.InSeaTime = call.InSeaTime;
  103. callDto.ConnSeaTime = call.ConnSeaTime;
  104. var list = new List<OutCallDto>();
  105. list.Add(callDto);
  106. try
  107. {
  108. var client = _httpClientFactory.CreateClient();
  109. client.DefaultRequestHeaders.ConnectionClose = true;
  110. var requestContent = JsonSerializer.Serialize(list);
  111. _logger.LogInformation(requestContent);
  112. var content = new StringContent(requestContent, Encoding.UTF8, "application/json");
  113. var responseMessage = await client.PostAsync(_sendCallRecordOptions.Value.FwUrl, content, cancellationToken);
  114. var respContent = responseMessage.Content;
  115. var respContentString = await respContent.ReadAsStringAsync(cancellationToken);
  116. var result = JsonSerializer.Deserialize<FwResult>(respContentString);
  117. //成功
  118. if (result.code == 1)
  119. {
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. throw new UserFriendlyException(ex.Message);
  125. }
  126. //HttpContent content = new
  127. }
  128. }
  129. }
  130. }
  131. }
  132. public class FwResult
  133. {
  134. public int code { get; set; }
  135. public string msg { get; set; }
  136. }
  137. }