1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using CallCenter.Caches;
- using CallCenter.Calls;
- using CallCenter.Notifications;
- using CallCenter.Realtimes;
- using CallCenter.Share.Enums;
- using MediatR;
- namespace CallCenter.Application.Handlers
- {
- public class ByeVisitorAndExtNotificationHandler:INotificationHandler<ByeVisitorAndExtNotification>
- {
- private readonly ICallRepository _callRepository;
- private readonly ICallDetailRepository _callDetailRepository;
- private readonly IRealtimeService _realtimeService;
- private readonly IUserCacheManager _userCacheManager;
- private readonly ICallCacheManager _callCacheManager;
- public ByeVisitorAndExtNotificationHandler(ICallRepository callRepository, ICallDetailRepository callDetailRepository,IRealtimeService realtimeService, IUserCacheManager userCacheManager, ICallCacheManager callCacheManager)
- {
- _callRepository = callRepository;
- _callDetailRepository = callDetailRepository;
- _realtimeService = realtimeService;
- _userCacheManager = userCacheManager;
- _callCacheManager = callCacheManager;
- }
- public async Task Handle(ByeVisitorAndExtNotification notification, CancellationToken cancellationToken)
- {
- var model = await _callRepository.GetAsync(
- x => x.ConversationId == notification.Visitor.Id &&
- x.FromNo == notification.Visitor.From && x.CreationTime>=DateTime.Now.AddHours(-2), cancellationToken);
- if (model != null)
- {
- model.CallStatus = ECallStatus.Bye;
- model.EndBy = EEndBy.From;
- model.RingOffType = ERingOffType.Normal;
- await _callRepository.UpdateAsync(model, cancellationToken);
- var detail = new CallDetail()
- {
- CallId = model.Id,
- CallStatus = ECallStatus.Bye,
- OMCallId = notification.Visitor.CallId,
- ConversationId = notification.Visitor.Id,
- EventName = notification.Attribute,
- FromNo = notification.Visitor.From,
- ToNo = notification.Visitor.To,
- Recording = notification.Recording,
- };
- await _callDetailRepository.AddAsync(detail, cancellationToken);
- //处理队列记录
- _callCacheManager.RemoveCallCache(model.Id);
- //查询应答分机分机号
- var callDetailAnswer = await _callDetailRepository.GetAsync(x => x.CallId == model.Id && x.EventName == "ANSWER", true, d => d.CreationTime);
- if (callDetailAnswer != null)
- {
- //调用业务通知 通知前端
- var workModel = _userCacheManager.GetWorkByTel(callDetailAnswer.AnswerNo);
- if (workModel != null)
- {
- await _realtimeService.ByeAsync(workModel.UserId, new ByeDto() { Id = model.Id }, cancellationToken);
- }
- }
- }
- }
- }
- }
|