TelDomainService.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using Hotline.Caches;
  2. using Hotline.CallCenter.Devices;
  3. using Hotline.Realtimes;
  4. using Hotline.Users;
  5. using System.Reflection;
  6. using XF.Domain.Constants;
  7. using XF.Domain.Dependency;
  8. using XF.Domain.Exceptions;
  9. namespace Hotline.CallCenter.Tels;
  10. public class TelDomainService : ITelDomainService, IScopeDependency
  11. {
  12. private readonly IDeviceManager _deviceManager;
  13. private readonly ITelRepository _telRepository;
  14. private readonly ITelRestRepository _telRestRepository;
  15. private readonly ITelHoldRepository _telHoldRepository;
  16. private readonly IRealtimeService _realtimeService;
  17. public TelDomainService(
  18. IDeviceManager deviceManager,
  19. ITelRepository telRepository,
  20. ITelRestRepository telRestRepository,
  21. ITelHoldRepository telHoldRepository,
  22. IRealtimeService realtimeService)
  23. {
  24. _deviceManager = deviceManager;
  25. _telRepository = telRepository;
  26. _telRestRepository = telRestRepository;
  27. _telHoldRepository = telHoldRepository;
  28. _realtimeService = realtimeService;
  29. }
  30. /// <summary>
  31. /// 查询所有分机
  32. /// </summary>
  33. /// <param name="cancellationToken"></param>
  34. /// <returns></returns>
  35. public Task<List<Tel>> QueryTelsAsync(CancellationToken cancellationToken)
  36. => _deviceManager.QueryTelsAsync(cancellationToken);
  37. /// <summary>
  38. /// 查询所有分机组
  39. /// </summary>
  40. /// <param name="cancellationToken"></param>
  41. /// <returns></returns>
  42. public Task<List<TelGroup>> QueryTelGroupsAsync(CancellationToken cancellationToken)
  43. => _deviceManager.QueryTelGroupsAsync(cancellationToken);
  44. /// <summary>
  45. /// 分机休息
  46. /// </summary>
  47. /// <param name="currentWork"></param>
  48. /// <param name="cancellationToken"></param>
  49. /// <returns></returns>
  50. public async Task<string> RestAsync(Work currentWork,string reason,bool isApply, CancellationToken cancellationToken)
  51. {
  52. var isResting = await _telRepository.IsRestingAsync(currentWork.TelNo, cancellationToken);
  53. if (!isResting)
  54. {
  55. await _deviceManager.TelRestAsync(currentWork.TelNo, cancellationToken);
  56. return await _telRestRepository.AddAsync(new TelRest(currentWork.TelId, currentWork.TelNo, currentWork.UserId, currentWork.UserName,reason, isApply),
  57. cancellationToken);
  58. }
  59. throw new UserFriendlyException("当前坐席正在休息");
  60. }
  61. /// <summary>
  62. /// 分机审批通过
  63. /// </summary>
  64. /// <param name="id"></param>
  65. /// <param name="cancellationToken"></param>
  66. /// <returns></returns>
  67. public async Task RestApplyPass(string id, CancellationToken cancellationToken)
  68. {
  69. var telrest = await _telRestRepository.GetAsync(id, cancellationToken);
  70. if(telrest!=null)
  71. {
  72. telrest.ApplyStatus = Share.Enums.Order.ERestApplyStatus.Resting;
  73. telrest.StartTime = DateTime.Now;
  74. await _telRestRepository.UpdateAsync(telrest, cancellationToken);
  75. //通知前端休息通过
  76. await _realtimeService.RestApplyPassAsync(telrest.UserId, cancellationToken);
  77. }
  78. }
  79. /// <summary>
  80. /// 分机结束休息
  81. /// </summary>
  82. /// <param name="telId"></param>
  83. /// <param name="cancellationToken"></param>
  84. /// <returns></returns>
  85. public async Task<TelRest> UnRestAsync(string telId, CancellationToken cancellationToken)
  86. {
  87. var tel = await _telRepository.GetAsync(telId, cancellationToken);
  88. if (tel is null)
  89. throw new UserFriendlyException("无效分机编号");
  90. await _deviceManager.TelEndRestAsync(tel.No, cancellationToken);
  91. var restingTel = await _telRestRepository.GetAsync(d => d.TelId == telId && !d.EndTime.HasValue, cancellationToken);
  92. if (restingTel is null)
  93. throw new UserFriendlyException("未查询到分机休息信息");
  94. restingTel.EndRest();
  95. await _telRestRepository.UpdateAsync(restingTel, cancellationToken);
  96. return restingTel;
  97. }
  98. /// <summary>
  99. /// 保持通话
  100. /// </summary>
  101. /// <param name="telId"></param>
  102. /// <param name="cancellationToken"></param>
  103. /// <returns></returns>
  104. public async Task HoldAsync(string telId,string userId,string userName,string callId, CancellationToken cancellationToken)
  105. {
  106. var isHolding = await _telHoldRepository.IsHoldingAsync(telId, userId, callId, cancellationToken);
  107. if (!isHolding)
  108. {
  109. var tel = await _telRepository.GetAsync(telId, cancellationToken);
  110. if (tel is null)
  111. throw new UserFriendlyException("无效分机编号");
  112. await _deviceManager.HoldAsync(tel.No, cancellationToken);
  113. await _telHoldRepository.AddAsync(new Calls.TelHold() { TelId = telId, TelNo = tel.No, UserId = userId, UserName = userName, CallId = callId }, cancellationToken);
  114. }
  115. }
  116. /// <summary>
  117. /// 恢复通话(解除hold状态)
  118. /// </summary>
  119. /// <param name="telId"></param>
  120. /// <param name="cancellationToken"></param>
  121. /// <returns></returns>
  122. public async Task UnHoldAsync(string telId,string userId,string callId, CancellationToken cancellationToken)
  123. {
  124. var tel = await _telRepository.GetAsync(telId, cancellationToken);
  125. if (tel is null)
  126. throw new UserFriendlyException("无效分机编号");
  127. await _deviceManager.UnHoldAsync(tel.No, cancellationToken);
  128. var holdingTel = await _telHoldRepository.GetAsync(d => d.TelId == telId && d.TelNo == tel.No && d.UserId == userId && d.CallId == callId && !d.EndTime.HasValue );
  129. if (holdingTel is null)
  130. throw new UserFriendlyException("未找到分机保持信息");
  131. holdingTel.EndHold();
  132. await _telHoldRepository.UpdateAsync(holdingTel, cancellationToken);
  133. }
  134. /// <summary>
  135. /// 开启静音
  136. /// </summary>
  137. /// <param name="telId"></param>
  138. /// <param name="cancellationToken"></param>
  139. /// <returns></returns>
  140. /// <exception cref="UserFriendlyException"></exception>
  141. public async Task MuteAsync(string telId, CancellationToken cancellationToken)
  142. {
  143. var tel = await _telRepository.GetAsync(telId, cancellationToken);
  144. if (tel is null)
  145. throw new UserFriendlyException("无效分机编号");
  146. await _deviceManager.MuteAsync(tel.No, cancellationToken);
  147. }
  148. /// <summary>
  149. /// 解除静音
  150. /// </summary>
  151. /// <param name="telId"></param>
  152. /// <param name="cancellationToken"></param>
  153. /// <returns></returns>
  154. public async Task UnMuteAsync(string telId, CancellationToken cancellationToken)
  155. {
  156. var tel = await _telRepository.GetAsync(telId, cancellationToken);
  157. if (tel is null)
  158. throw new UserFriendlyException("无效分机编号");
  159. await _deviceManager.MuteAsync(tel.No, cancellationToken);
  160. }
  161. }