DefaultCallApplication.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.CallCenter.BlackLists;
  3. using Hotline.CallCenter.Calls;
  4. using Hotline.CallCenter.Tels;
  5. using Hotline.Orders;
  6. using Hotline.Repository.SqlSugar.Extensions;
  7. using Hotline.Share.Dtos;
  8. using Hotline.Share.Dtos.CallCenter;
  9. using Hotline.Share.Dtos.TrCallCenter;
  10. using Hotline.Share.Enums.CallCenter;
  11. using Hotline.Users;
  12. using MapsterMapper;
  13. using Microsoft.Extensions.Logging;
  14. using SqlSugar;
  15. using XF.Domain.Authentications;
  16. using XF.Domain.Cache;
  17. using XF.Domain.Exceptions;
  18. using XF.Domain.Repository;
  19. namespace Hotline.Application.CallCenter;
  20. public abstract class DefaultCallApplication : ICallApplication
  21. {
  22. private readonly IRepository<Tel> _telRepository;
  23. private readonly IRepository<TelGroup> _telGroupRepository;
  24. private readonly IWorkRepository _workRepository;
  25. private readonly ITelRestRepository _telRestRepository;
  26. private readonly IRepository<CallNative> _callNativeRepository;
  27. private readonly IRepository<TelOperation> _telOperationRepository;
  28. private readonly IRepository<CallidRelation> _callIdRelationRepository;
  29. private readonly ITypedCache<Work> _cacheWork;
  30. private readonly IUserCacheManager _userCacheManager;
  31. private readonly ISessionContext _sessionContext;
  32. private readonly IMapper _mapper;
  33. private readonly ILogger<DefaultCallApplication> _logger;
  34. public DefaultCallApplication(
  35. IRepository<Tel> telRepository,
  36. IRepository<TelGroup> telGroupRepository,
  37. IWorkRepository workRepository,
  38. ITelRestRepository telRestRepository,
  39. IRepository<CallNative> callNativeRepository,
  40. IRepository<TelOperation> telOperationRepository,
  41. IRepository<CallidRelation> callIdRelationRepository,
  42. ITypedCache<Work> cacheWork,
  43. IUserCacheManager userCacheManager,
  44. ISessionContext sessionContext,
  45. IMapper mapper,
  46. ILogger<DefaultCallApplication> logger)
  47. {
  48. _telRepository = telRepository;
  49. _telGroupRepository = telGroupRepository;
  50. _workRepository = workRepository;
  51. _telRestRepository = telRestRepository;
  52. _callNativeRepository = callNativeRepository;
  53. _telOperationRepository = telOperationRepository;
  54. _callIdRelationRepository = callIdRelationRepository;
  55. _cacheWork = cacheWork;
  56. _userCacheManager = userCacheManager;
  57. _sessionContext = sessionContext;
  58. _mapper = mapper;
  59. _logger = logger;
  60. }
  61. public DefaultCallApplication()
  62. {
  63. }
  64. /// <summary>
  65. /// 查询分机
  66. /// </summary>
  67. public virtual async Task<IReadOnlyList<TelDto>> QueryTelsAsync(CancellationToken cancellationToken)
  68. {
  69. return _mapper.Map<IReadOnlyList<TelDto>>(await _telRepository.Queryable()
  70. .Includes(x => x.Groups)
  71. .ToListAsync(cancellationToken));
  72. }
  73. /// <summary>
  74. /// 查询分机组
  75. /// </summary>
  76. public virtual async Task<IReadOnlyList<TelGroupDto>> QueryTelGroupsAsync(CancellationToken cancellationToken)
  77. {
  78. return await _telGroupRepository.Queryable()
  79. .Select<TelGroupDto>()
  80. .ToListAsync(cancellationToken);
  81. }
  82. /// <summary>
  83. /// 新增黑名单
  84. /// </summary>
  85. public abstract Task<string> AddBlackListAsync(AddBlacklistDto dto, CancellationToken cancellationToken);
  86. /// <summary>
  87. /// 删除黑名单
  88. /// </summary>
  89. public abstract Task RemoveBlackListAsync(string id, CancellationToken cancellationToken);
  90. /// <summary>
  91. /// 查询黑名单
  92. /// </summary>
  93. public abstract Task<List<Blacklist>> QueryBlackListsAsync(CancellationToken cancellationToken);
  94. /// <summary>
  95. /// 签入
  96. /// </summary>
  97. public virtual async Task<TrOnDutyResponseDto> SignInAsync(SignInDto dto, CancellationToken cancellationToken)
  98. {
  99. if (string.IsNullOrEmpty(dto.TelNo))
  100. throw UserFriendlyException.SameMessage("无效分机号");
  101. var work = _userCacheManager.GetWorkByUserNoExp(_sessionContext.RequiredUserId);
  102. if (work is not null)
  103. {
  104. //if (work.TelNo != dto.TelNo)
  105. //{
  106. // throw UserFriendlyException.SameMessage("当前用户已签入其他分机");
  107. //}
  108. throw UserFriendlyException.SameMessage("当前用户已签入");
  109. }
  110. var telWork = _userCacheManager.GetWorkByTelNoExp(dto.TelNo);
  111. if (telWork is not null)
  112. {
  113. throw UserFriendlyException.SameMessage("当前分机已被占用");
  114. }
  115. work = new Work(_sessionContext.RequiredUserId, _sessionContext.UserName,
  116. dto.TelNo, dto.TelNo, null, null,
  117. dto.GroupId, _sessionContext.StaffNo, null);
  118. await _workRepository.AddAsync(work, cancellationToken);
  119. return new TrOnDutyResponseDto
  120. {
  121. TelNo = dto.TelNo,
  122. QueueId = dto.GroupId,
  123. StartTime = work.StartTime,
  124. };
  125. }
  126. /// <summary>
  127. /// 签出
  128. /// </summary>
  129. public virtual async Task SingOutAsync(CancellationToken cancellationToken)
  130. {
  131. var work = _userCacheManager.GetWorkByUserNoExp(_sessionContext.RequiredUserId);
  132. if (work is null) return;
  133. var telRest =
  134. await _telRestRepository.GetAsync(x => x.TelNo == work.TelNo && !x.EndTime.HasValue, cancellationToken);
  135. if (telRest is not null)
  136. {
  137. telRest.EndRest();
  138. await _telRestRepository.UpdateAsync(telRest, cancellationToken);
  139. }
  140. work.OffDuty();
  141. await _workRepository.UpdateAsync(work, cancellationToken);
  142. await _cacheWork.RemoveAsync(work.GetKey(KeyMode.UserId), cancellationToken);
  143. await _cacheWork.RemoveAsync(work.GetKey(KeyMode.TelNo), cancellationToken);
  144. }
  145. /// <summary>
  146. /// 签出
  147. /// </summary>
  148. public virtual async Task SingOutAsync(string telNo, CancellationToken cancellationToken)
  149. {
  150. var work = _userCacheManager.GetWorkByTelNoExp(telNo);
  151. if (work is null) return;
  152. var telRest =
  153. await _telRestRepository.GetAsync(x => x.TelNo == work.TelNo && !x.EndTime.HasValue, cancellationToken);
  154. if (telRest is not null)
  155. {
  156. telRest.EndRest();
  157. await _telRestRepository.UpdateAsync(telRest, cancellationToken);
  158. }
  159. work.OffDuty();
  160. await _workRepository.UpdateAsync(work, cancellationToken);
  161. await _cacheWork.RemoveAsync(work.GetKey(KeyMode.UserId), cancellationToken);
  162. await _cacheWork.RemoveAsync(work.GetKey(KeyMode.TelNo), cancellationToken);
  163. }
  164. /// <summary>
  165. /// 查询当前用户的分机状态
  166. /// </summary>
  167. /// <param name="cancellationToken"></param>
  168. /// <returns></returns>
  169. public virtual async Task<TrOnDutyResponseDto> GetTelStateAsync(CancellationToken cancellationToken)
  170. {
  171. var work = _userCacheManager.GetWorkByUserNoExp(_sessionContext.RequiredUserId);
  172. if (work is null) return null;
  173. return await Task.FromResult(new TrOnDutyResponseDto
  174. {
  175. TelNo = work.TelNo,
  176. QueueId = work.QueueId,
  177. StartTime = work.StartTime,
  178. StaffNo = work.StaffNo
  179. });
  180. }
  181. /// <summary>
  182. /// 定量查询通话记录
  183. /// </summary>
  184. public virtual async Task<IReadOnlyList<CallNativeDto>> QueryCallsFixedAsync(QueryCallsFixedDto dto, CancellationToken cancellationToken)
  185. {
  186. var query = _callNativeRepository.Queryable(includeDeleted: true)
  187. .LeftJoin<Order>((d, o) => d.Id == o.CallId)
  188. .Where((d, o) => d.GroupId == "1")
  189. .WhereIF(!string.IsNullOrEmpty(dto.OrderNo), (d, o) => o.No == dto.OrderNo)
  190. .WhereIF(!string.IsNullOrEmpty(dto.FromNo), d => d.FromNo == dto.FromNo)
  191. .WhereIF(!string.IsNullOrEmpty(dto.ToNo), d => d.ToNo == dto.ToNo)
  192. .WhereIF(!string.IsNullOrEmpty(dto.UserName), d => d.UserName == dto.UserName)
  193. .WhereIF(!string.IsNullOrEmpty(dto.TelNo), d => d.TelNo == dto.TelNo)
  194. .WhereIF(dto.EndBy != null, d => d.EndBy == dto.EndBy)
  195. .WhereIF(dto.CallStartTimeStart != null, d => d.BeginIvrTime >= dto.CallStartTimeStart)
  196. .WhereIF(dto.CallStartTimeEnd != null, d => d.BeginIvrTime <= dto.CallStartTimeEnd)
  197. .WhereIF(dto.IsConnected != null, d => d.AnsweredTime != null)
  198. .WhereIF(dto.Direction != null, d => d.Direction == dto.Direction)
  199. .WhereIF(dto.WaitDurationStart != null && dto.WaitDurationStart > 0, d => d.WaitDuration >= dto.WaitDurationStart)
  200. .WhereIF(dto.WaitDurationEnd != null && dto.WaitDurationEnd > 0, d => d.WaitDuration <= dto.WaitDurationEnd)
  201. .WhereIF(dto.IsMissOrder != null && dto.IsMissOrder.Value == true, (d, o) => string.IsNullOrEmpty(o.Id) == true)
  202. .WhereIF(dto.IsMissOrder != null && dto.IsMissOrder.Value == false, (d, o) => string.IsNullOrEmpty(o.Id) == false)
  203. .OrderByDescending(d => d.Id);
  204. query = query.WhereIF(dto.Type == 3, (d, o) => d.AnsweredTime == null);
  205. query = query.WhereIF(dto.Type == 1, (d, o) => d.Direction == ECallDirection.In);
  206. query = query.WhereIF(dto.Type == 2, (d, o) => d.Direction == ECallDirection.Out);
  207. var items = await query.Select((d, o) => new CallNativeDto
  208. {
  209. OrderId = o.Id,
  210. OrderNo = o.No,
  211. Title = o.Title,
  212. Gateway = SqlFunc.Subqueryable<CallNative>()
  213. .Where(m => m.GroupId == "0" && m.CallNo == d.CallNo)
  214. .Select(m => m.ToNo)
  215. }, true)
  216. .ToFixedListAsync(dto, cancellationToken);
  217. items.Where(m => m.Gateway != null)
  218. .ToList().ForEach(m =>
  219. {
  220. var toNo = m.Gateway;
  221. m.Gateway = m.ToNo;
  222. m.ToNo = toNo;
  223. });
  224. return items;
  225. }
  226. /// <summary>
  227. /// 查询分机操作记录(定量)
  228. /// </summary>
  229. public virtual async Task<IReadOnlyList<TelOperation>> QueryTelOperationsAsync(QueryTelOperationsFixedDto dto, CancellationToken cancellationToken)
  230. {
  231. return await _telOperationRepository.Queryable()
  232. .WhereIF(!string.IsNullOrEmpty(dto.UserName), d => d.UserName == dto.UserName)
  233. .WhereIF(!string.IsNullOrEmpty(dto.StaffNo), d => d.StaffNo == dto.StaffNo)
  234. .WhereIF(!string.IsNullOrEmpty(dto.GroupId), d => d.GroupId == dto.GroupId)
  235. .WhereIF(dto.OperateState != null, d => d.OperateState == dto.OperateState)
  236. .OrderByDescending(d => d.Id)
  237. .ToFixedListAsync(dto, cancellationToken);
  238. }
  239. /// <summary>
  240. /// 依据通话记录编号获取映射后的callId
  241. /// </summary>
  242. public virtual async Task<string> GetOrSetCallIdAsync(string callNo, CancellationToken cancellationToken)
  243. {
  244. var callOrder = await _callIdRelationRepository.GetAsync(callNo, cancellationToken);
  245. if (callOrder == null)
  246. {
  247. callOrder = new CallidRelation
  248. {
  249. Id = callNo,
  250. CallId = Ulid.NewUlid().ToString(),
  251. };
  252. try
  253. {
  254. await _callIdRelationRepository.AddAsync(callOrder, cancellationToken);
  255. }
  256. catch (Exception e)
  257. {
  258. _logger.LogError($"写入callidRelation失败:{e.Message}");
  259. return await GetOrSetCallIdAsync(callNo, cancellationToken);
  260. }
  261. }
  262. return callOrder.CallId;
  263. }
  264. public async Task<CallidRelation> GetRelationAsync(string callNo, CancellationToken cancellation)
  265. {
  266. return await _callIdRelationRepository.GetAsync(callNo, cancellation);
  267. }
  268. public async Task AddRelationAsync(CallidRelation relation, CancellationToken cancellation)
  269. {
  270. await _callIdRelationRepository.AddAsync(relation, cancellation);
  271. }
  272. /// <summary>
  273. /// 乐观并发更新映射关系
  274. /// </summary>
  275. public virtual async Task<int> UpdateRelationOptLockAsync(CallidRelation relation, CancellationToken cancellationToken)
  276. {
  277. return await _callIdRelationRepository.Updateable(relation).ExecuteCommandWithOptLockAsync();
  278. }
  279. /// <summary>
  280. /// 查询通话记录
  281. /// </summary>
  282. public virtual async Task<CallNative> GetCallAsync(string callId, CancellationToken cancellationToken)
  283. {
  284. if (string.IsNullOrEmpty(callId)) return null;
  285. return await _callNativeRepository.GetAsync(callId, cancellationToken);
  286. }
  287. /// <summary>
  288. /// 查询通话记录列表
  289. /// </summary>
  290. /// <param name="callId"></param>
  291. /// <param name="cancellationToken"></param>
  292. /// <returns></returns>
  293. public virtual async Task<List<CallNative>> GetCallListAsync(string callId, CancellationToken cancellationToken)
  294. {
  295. if (string.IsNullOrEmpty(callId)) return null;
  296. return await _callNativeRepository.Queryable().Where(x => x.Id == callId).ToListAsync(cancellationToken);
  297. }
  298. /// <summary>
  299. /// 查询通话记录
  300. /// </summary>
  301. public virtual async Task<List<CallNative>> QueryCallsAsync(
  302. string? phone,
  303. ECallDirection? direction,
  304. DateTime? callStartTimeStart,
  305. DateTime? callStartTimeEnd,
  306. CancellationToken cancellationToken)
  307. {
  308. if (string.IsNullOrEmpty(phone))
  309. return new List<CallNative>();
  310. return await _callNativeRepository.Queryable()
  311. .WhereIF(direction.HasValue, d => d.Direction == direction)
  312. .WhereIF(callStartTimeStart != null, d => d.BeginIvrTime >= callStartTimeStart)
  313. .WhereIF(callStartTimeEnd != null, d => d.BeginIvrTime <= callStartTimeEnd)
  314. .Where(d => d.FromNo == phone || d.ToNo == phone)
  315. .OrderBy(d => d.CreationTime)
  316. .ToListAsync(cancellationToken);
  317. }
  318. #region tianrun 临时方案
  319. public virtual Task<TrCallRecord?> GetTianrunCallAsync(string callId, CancellationToken cancellationToken)
  320. {
  321. throw new NotImplementedException();
  322. }
  323. /// <summary>
  324. /// 根据转写ID获取通话信息
  325. /// </summary>
  326. /// <returns></returns>
  327. public virtual async Task<TrCallRecord?> GetTianrunCallTransliterationAsync(string transliterationId, CancellationToken cancellationToken)
  328. {
  329. throw new NotImplementedException();
  330. }
  331. public virtual async Task EditTransliterationStateAsync(string callId, ECallTransliterationState state, string transliterationId, CancellationToken cancellationToken)
  332. {
  333. throw new NotImplementedException();
  334. }
  335. /// <summary>
  336. /// 关联通话记录与order(添润)
  337. /// </summary>
  338. public virtual Task RelateTianrunCallWithOrderAsync(string callId, string orderId,
  339. CancellationToken cancellationToken)
  340. {
  341. throw new NotImplementedException();
  342. }
  343. /// <summary>
  344. /// 查询通话记录
  345. /// </summary>
  346. public virtual Task<List<TrCallRecord>> QueryTianrunCallsAsync(
  347. string? phone = null,
  348. ECallDirection? direction = null,
  349. DateTime? callStartTimeStart = null,
  350. DateTime? callStartTimeEnd = null,
  351. CancellationToken cancellationToken = default)
  352. {
  353. throw new NotImplementedException();
  354. }
  355. /// <summary>
  356. /// 查询分机操作选项
  357. /// </summary>
  358. /// <returns></returns>
  359. public abstract List<Kv> GetTelOperationOptions();
  360. #endregion
  361. }