IdleNotificationHandler.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.Caching.Services;
  3. using Hotline.CallCenter.Configs;
  4. using Hotline.CallCenter.Devices;
  5. using Hotline.CallCenter.Tels;
  6. using Hotline.EventBus;
  7. using Hotline.NewRock;
  8. using Hotline.Realtimes;
  9. using Hotline.Repository.SqlSugar.CallCenter;
  10. using Hotline.Share.Enums.CallCenter;
  11. using Hotline.Share.Notifications.NewRockCallCenter;
  12. using MediatR;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using Microsoft.Extensions.Options;
  15. using XF.Domain.Cache;
  16. using XF.Domain.Exceptions;
  17. namespace Hotline.Application.Handlers.CallCenter.ExtState
  18. {
  19. [HandlerInject(EventHandlerType = EEventHandlerType.CallCenter, Label = AppDefaults.CallCenterType.XunShi)]
  20. public class IdleNotificationHandler : INotificationHandler<IdleNotification>
  21. {
  22. private readonly ITelRepository _telRepository;
  23. private readonly ITelCacheManager _telCacheManager;
  24. private readonly ITypedCache<Tel> _typedCache;
  25. private readonly IUserCacheManager _userCacheManager;
  26. private readonly IRealtimeService _realtimeService;
  27. private readonly INewRockDeviceManager _newRockDeviceManager;
  28. private readonly ITelRestRepository _telRestRepository;
  29. private readonly IOptionsSnapshot<AppConfiguration> _appOptions;
  30. private readonly IOptionsSnapshot<CallCenterConfiguration> _callcenterOptions;
  31. public IdleNotificationHandler(
  32. IServiceProvider serviceProvider,
  33. ITelRepository telRepository, ITelCacheManager telCacheManager,
  34. ITypedCache<Tel> typedCache,IUserCacheManager userCacheManager,
  35. IRealtimeService realtimeService,
  36. ITelRestRepository telRestRepository,
  37. IOptionsSnapshot<AppConfiguration> appOptions,
  38. IOptionsSnapshot<CallCenterConfiguration> callcenterOptions)
  39. {
  40. _telRepository = telRepository;
  41. _telCacheManager = telCacheManager;
  42. _typedCache = typedCache;
  43. _userCacheManager = userCacheManager;
  44. _realtimeService = realtimeService;
  45. _telRestRepository = telRestRepository;
  46. _appOptions = appOptions;
  47. _callcenterOptions = callcenterOptions;
  48. if (appOptions.Value.GetDefaultAppScopeConfiguration().CallCenterType == AppDefaults.CallCenterType.XunShi)
  49. {
  50. _newRockDeviceManager = serviceProvider.GetRequiredService<INewRockDeviceManager>();
  51. }
  52. }
  53. public async Task Handle(IdleNotification notification, CancellationToken cancellationToken)
  54. {
  55. var telModel = _telCacheManager.GetTel(notification.TelNo);
  56. telModel.TelStatus = ETelStatus.Ready;
  57. //await _telRepository.UpdateAsync(telModel, cancellationToken);
  58. _typedCache.Set(notification.TelNo, telModel);
  59. var iswork = await _userCacheManager.IsWorkingByTelAsync(notification.TelNo, cancellationToken);
  60. if (!iswork)
  61. throw new UserFriendlyException(notification.TelNo + "未查询到工作记录");
  62. var workModel = _userCacheManager.GetWorkByTel(notification.TelNo);
  63. if (workModel != null)
  64. {
  65. await _realtimeService.IdleAsync(workModel.UserId, cancellationToken);
  66. }
  67. var restingTel = await _telRestRepository.GetAsync(d => d.TelId == telModel.Id && !d.EndTime.HasValue && d.ApplyStatus== ETelRestApplyStatus.Resting, cancellationToken);
  68. if (restingTel == null)
  69. {
  70. foreach (var group in telModel.Groups)
  71. {
  72. await _newRockDeviceManager.AssginConfigGroupAsync(_callcenterOptions.Value.NewRock, group.No, group.Distribution, new List<string>() { notification.TelNo }, group.Voice, cancellationToken);
  73. }
  74. }
  75. }
  76. }
  77. }