UserDomainService.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.CallCenter.Devices;
  3. using Hotline.CallCenter.Tels;
  4. using Hotline.Share.Dtos.CallCenter;
  5. using Hotline.Share.Dtos.TrCallCenter;
  6. using Hotline.Share.Dtos.Users;
  7. using MapsterMapper;
  8. using System.Security.Cryptography;
  9. using XF.Domain.Authentications;
  10. using XF.Domain.Cache;
  11. using XF.Domain.Constants;
  12. using XF.Domain.Dependency;
  13. using XF.Domain.Exceptions;
  14. using XF.Domain.Repository;
  15. namespace Hotline.Users
  16. {
  17. public class UserDomainService : IUserDomainService, IScopeDependency
  18. {
  19. private readonly IRepository<User> _userRepository;
  20. private readonly IWorkRepository _workRepository;
  21. private readonly IDeviceManager _deviceManager;
  22. private readonly IUserCacheManager _userCacheManager;
  23. private readonly ITelCacheManager _telCacheManager;
  24. private readonly ITypedCache<Work> _cacheWork;
  25. private readonly IMapper _mapper;
  26. private readonly ISystemSettingCacheManager _systemSettingCacheManager;
  27. private readonly ISessionContext _sessionContext;
  28. public UserDomainService(
  29. IRepository<User> userRepository,
  30. IWorkRepository workRepository,
  31. IDeviceManager deviceManager,
  32. ITypedCache<Work> cacheWork,
  33. IUserCacheManager userCacheManager,
  34. IMapper mapper,
  35. ITelCacheManager telCacheManager,
  36. ISystemSettingCacheManager systemSettingCacheManager,
  37. ISessionContext sessionContext
  38. )
  39. {
  40. _userRepository = userRepository;
  41. _workRepository = workRepository;
  42. _deviceManager = deviceManager;
  43. _userCacheManager = userCacheManager;
  44. _cacheWork = cacheWork;
  45. _mapper = mapper;
  46. _telCacheManager = telCacheManager;
  47. _systemSettingCacheManager = systemSettingCacheManager;
  48. _sessionContext = sessionContext;
  49. }
  50. /// <summary>
  51. /// 上班
  52. /// </summary>
  53. /// <param name="userId">userId</param>
  54. /// <param name="tel"></param>
  55. /// <param name="cancellationToken"></param>
  56. /// <returns></returns>
  57. public async Task OnDutyAsync(string userId, Tel tel, CancellationToken cancellationToken)
  58. {
  59. var user = await _userRepository.GetAsync(userId, cancellationToken);
  60. if (user is null)
  61. throw UserFriendlyException.SameMessage("无效的用户编号");
  62. var workingTel = await _workRepository.GetCurrentWorkByUserAsync(userId, cancellationToken);
  63. if (workingTel is not null)
  64. throw UserFriendlyException.SameMessage($"当前用户已上班,userId:{userId}");
  65. var telIsWorking = await _workRepository.AnyAsync(d => d.TelId == tel.Id && !d.EndTime.HasValue, cancellationToken);
  66. if (telIsWorking)
  67. throw UserFriendlyException.SameMessage("当前分机已被占用");
  68. var work = new Work(userId, user.Name, tel.Id, tel.No);
  69. await _workRepository.AddAsync(work, cancellationToken);
  70. if (!string.IsNullOrEmpty(user.StaffNo))
  71. await _deviceManager.UpdateStaffNoAsync(tel.No, user.StaffNo, tel.LineId, cancellationToken);
  72. //更新分机组
  73. foreach (var group in tel.Groups)
  74. {
  75. await _deviceManager.AssginConfigGroupAsync(group.No, group.Distribution, new List<string>() { tel.No }, group.Voice, cancellationToken);
  76. }
  77. }
  78. /// <summary>
  79. /// 下班
  80. /// </summary>
  81. /// <param name="userId"></param>
  82. /// <param name="cancellationToken"></param>
  83. /// <returns></returns>
  84. public async Task<WorkDto?> OffDutyAsync(string userId, CancellationToken cancellationToken)
  85. {
  86. var work = _userCacheManager.GetWorkByUser(userId);
  87. var tel = _telCacheManager.GetTel(work.TelNo);
  88. work.OffDuty();
  89. await _workRepository.UpdateAsync(work, cancellationToken);
  90. await _deviceManager.UpdateStaffNoAsync(work.TelNo, string.Empty, tel.LineId, cancellationToken);
  91. _cacheWork.Remove(work.GetKey(KeyMode.UserId));
  92. _cacheWork.Remove(work.GetKey(KeyMode.TelNo));
  93. #region 初始化话机
  94. //初始化解除静音
  95. await _deviceManager.UnMuteAsync(tel.No, cancellationToken);
  96. foreach (var group in tel.Groups)
  97. {
  98. await _deviceManager.ModifyGroupExtAsync(group.No, group.Distribution, group.Voice, "", false, cancellationToken);
  99. }
  100. #endregion
  101. return _mapper.Map<WorkDto>(work);
  102. }
  103. #region 添添呼
  104. /// <summary>
  105. /// 上班
  106. /// </summary>
  107. /// <param name="userId"></param>
  108. /// <param name="telNo"></param>
  109. /// <param name="cancellationToken"></param>
  110. /// <returns></returns>
  111. public async Task<TrOnDutyResponseDto> TrOnDutyAsync(string userId, string telNo, CancellationToken cancellationToken)
  112. {
  113. var work = _userCacheManager.GetWorkByUserNoExp(userId);
  114. if(work is not null)
  115. {
  116. if(work.TelNo == telNo)
  117. {
  118. return new TrOnDutyResponseDto() { TelNo = work.TelNo, TelPwd = work.TelPwd, Description = work.Description };
  119. }
  120. else
  121. {
  122. throw UserFriendlyException.SameMessage("当前用户已签入其他分机");
  123. }
  124. }
  125. var telWork = _userCacheManager.GetWorkByTelNoExp(telNo);
  126. if (telWork is not null)
  127. {
  128. throw UserFriendlyException.SameMessage("当前分机已被占用");
  129. }
  130. bool IsTelNeedVerify = bool.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.IsTelNeedVerify).SettingValue[0]);
  131. //var telModel = await _trClient.QueryTelsAsync(new Tr.Sdk.Tels.QueryTelRequest() { TelNo = telNo }, cancellationToken);
  132. //if (telModel != null && telModel.Count > 0)
  133. //{
  134. // work = new Work(userId, _sessionContext.UserName, telModel[0].Id, telNo, telModel[0].Password, telModel[0].Description);
  135. // await _workRepository.AddAsync(work, cancellationToken);
  136. // if (IsTelNeedVerify)
  137. // {
  138. // return new TrOnDutyResponseDto() { TelNo = telNo, TelPwd = "", Description = telModel[0].Description };
  139. // }
  140. // else
  141. // {
  142. // return new TrOnDutyResponseDto() { TelNo = telNo, TelPwd = telModel[0].Password, Description = telModel[0].Description };
  143. // }
  144. //}
  145. throw UserFriendlyException.SameMessage("签入异常,未查询到对应分机信息");
  146. /*
  147. *var work = 缓存.get(userId);
  148. * if(work.TelNo == telNo)
  149. * t: return work f: throw
  150. *
  151. * 获取配置
  152. *IsNeedTelNo
  153. * IsTelNeedVerify
  154. *
  155. * user
  156. * tel = trClient.get(telNo);
  157. * work
  158. *
  159. * 根据配置 return new();
  160. *
  161. */
  162. }
  163. /// <summary>
  164. /// 下班
  165. /// </summary>
  166. /// <param name="userId"></param>
  167. /// <param name="cancellationToken"></param>
  168. /// <returns></returns>
  169. public async Task TrOffDutyAsync(string userId, CancellationToken cancellationToken)
  170. {
  171. var work = _userCacheManager.GetWorkByUser(userId);
  172. work.OffDuty();
  173. await _workRepository.UpdateAsync(work, cancellationToken);
  174. _cacheWork.Remove(work.GetKey(KeyMode.UserId));
  175. _cacheWork.Remove(work.GetKey(KeyMode.TelNo));
  176. }
  177. #endregion
  178. }
  179. }