CdrNotificationHandler.cs 9.0 KB

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