123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using CallCenter.Calls;
- using CallCenter.NewRock.Handlers;
- using CallCenter.Notifications;
- using CallCenter.Share.Dtos;
- using CallCenter.Share.Enums;
- using MediatR;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using NewRock.Sdk.Extensions;
- using System.Text;
- using System.Text.Json;
- using XF.Domain.Exceptions;
- namespace CallCenter.Application.Handlers
- {
- public class CdrNotificationHandler:INotificationHandler<CdrNotification>
- {
- private readonly ICallRecordRepository _callRecordRepository;
- private readonly ICallDetailRepository _callDetailRepository;
- private readonly ICallRepository _callRepository;
- private readonly IOptionsSnapshot<SendCallRecord> _sendCallRecordOptions;
- private readonly IHttpClientFactory _httpClientFactory;
- private readonly ILogger<CdrNotificationHandler> _logger;
- public CdrNotificationHandler(ICallRecordRepository callRecordRepository, ICallDetailRepository callDetailRepository, ICallRepository callRepository, IOptionsSnapshot<SendCallRecord> sendCallRecordOptions, IHttpClientFactory httpClientFactory,ILogger<CdrNotificationHandler> logger)
- {
- _callRecordRepository = callRecordRepository;
- _callDetailRepository = callDetailRepository;
- _callRepository = callRepository;
- _sendCallRecordOptions = sendCallRecordOptions;
- _httpClientFactory = httpClientFactory;
- _logger = logger;
- }
- public async Task Handle(CdrNotification notification, CancellationToken cancellationToken)
- {
- var callDetail = await
- _callDetailRepository.GetAsync(x => x.OMCallId == notification.CallId, cancellationToken);
-
- if (callDetail!=null)
- {
- var model = new CallRecord()
- {
- CallId = callDetail.CallId,
- CdrId = notification.Id,
- CDRCallId = notification.CallId,
- TimeStart = notification.TimeStart,
- Group = notification.Group,
- Type = (ECDRType)Enum.Parse(typeof(ECDRType), notification.Type),
- Route = (ECDRRoute)Enum.Parse(typeof(ECDRRoute), notification.Route),
- CPN = notification.CPN,
- CDPN = notification.CDPN,
- TimeEnd = notification.TimeEnd,
- Duration = notification.Duration,
- TrunkNumber = notification.TrunkNumber,
- Recording = notification.Recording,
- RecCodec = notification.RecCodec,
- IsDown = false
- };
- if (!string.IsNullOrEmpty(notification.VisitorId))
- model.VisitorId = notification.VisitorId;
- if (!string.IsNullOrEmpty(notification.OuterId))
- model.OuterId = notification.OuterId;
- await _callRecordRepository.AddAsync(model,cancellationToken);
- var ishave = await _callDetailRepository.AnyAsync(x => x.EventName == "ALERT" && x.OMCallId == notification.CallId && x.FromNo == notification.CPN && x.ToNo == notification.CDPN);
- if (ishave)
- {
- var callModel = await _callRepository.GetAsync(x => x.Id == callDetail.CallId, cancellationToken);
- if (callModel != null)
- {
- callModel.Duration = double.Parse(model.Duration);
- await _callRepository.UpdateAsync(callModel, cancellationToken);
- var call = await _callRepository.GetExtAsync(callModel.Id, x => x.Includes(d => d.CallDetails));
- //TODO 推送通话报告
- OutCallDto callDto = new OutCallDto();
- callDto.CallId = callDetail.CallId;
- callDto.InfoType = EInfoType.Call;
- callDto.Direction = callModel.CallDirection;
- callDto.Cpn = callModel.FromNo ?? "";
- callDto.Cdpn = callModel.ToNo ?? "";
- if (callDto.Direction == ECallDirection.In)
- {
- callDto.Answered = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWER")?.AnswerNo ?? call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.AnswerNo ?? "";
- callDto.OnTime = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWER")?.CreationTime ?? call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.CreationTime;
- callDto.OnState = call.CallDetails?.Any(x => x.EventName == "ANSWER" || x.EventName == "ANSWERED") == true ? EOnState.On : EOnState.NoOn;
- }
- else
- {
- //callDto.Answered = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.AnswerNo ?? "";
- //callDto.OnTime = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.CreationTime;
- //callDto.OnState = call.CallDetails?.Any(x => x.EventName == "ANSWERED") == true ? EOnState.On : EOnState.NoOn;
- callDto.Answered = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWER")?.AnswerNo ?? call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.AnswerNo ?? "";
- callDto.OnTime = call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWER")?.CreationTime ?? call.CallDetails?.FirstOrDefault(x => x.EventName == "ANSWERED")?.CreationTime;
- callDto.OnState = call.CallDetails?.Any(x => x.EventName == "ANSWER" || x.EventName == "ANSWERED") == true ? EOnState.On : EOnState.NoOn;
- }
- callDto.BeginTime = call.CreationTime;
- callDto.ByeTime = call.CallDetails?.FirstOrDefault(x => x.EventName == "BYE").CreationTime;
- callDto.TalkTime = call.Duration;
- callDto.SoundFileName = call.CallDetails?.FirstOrDefault(x => x.EventName == "BYE")?.Recording ?? model.Recording;
- callDto.EvaluateResult = "";
- callDto.EndBy = call.EndBy;
- callDto.InIvrTime = call.InIvrTime;
- callDto.OutIvrTime = call.OutIvrTime;
- callDto.InGroupTime = call.InGroupTime;
- callDto.OutGroupTime = call.OutGroupTime;
- callDto.InSeaTime = call.InSeaTime;
- callDto.ConnSeaTime = call.ConnSeaTime;
- var list = new List<OutCallDto>();
- list.Add(callDto);
- try
- {
- var client = _httpClientFactory.CreateClient();
- client.DefaultRequestHeaders.ConnectionClose = true;
- var requestContent = JsonSerializer.Serialize(list);
- _logger.LogInformation(requestContent);
- var content = new StringContent(requestContent, Encoding.UTF8, "application/json");
- var responseMessage = await client.PostAsync(_sendCallRecordOptions.Value.FwUrl, content, cancellationToken);
- var respContent = responseMessage.Content;
- var respContentString = await respContent.ReadAsStringAsync(cancellationToken);
- var result = JsonSerializer.Deserialize<FwResult>(respContentString);
- //成功
- if (result.code == 1)
- {
- }
- }
- catch (Exception ex)
- {
- throw new UserFriendlyException(ex.Message);
- }
- //HttpContent content = new
- }
- }
- }
- }
- }
- public class FwResult
- {
- public int code { get; set; }
- public string msg { get; set; }
- }
- }
|