CallReportApplicationBase.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.Repository.SqlSugar.Extensions;
  3. using Hotline.CallCenter.Calls;
  4. using Hotline.Repository.SqlSugar.CallCenter;
  5. using Hotline.Settings;
  6. using Hotline.Share.Dtos.CallCenter;
  7. using Hotline.Share.Dtos.TrCallCenter;
  8. using Hotline.Share.Enums.CallCenter;
  9. using Hotline.Share.Requests;
  10. using SqlSugar;
  11. using XF.Domain.Exceptions;
  12. using Hotline.Orders;
  13. using Hotline.Share.Enums.User;
  14. using Hotline.Users;
  15. using XF.Domain.Repository;
  16. using Hotline.CallCenter.Tels;
  17. using Hotline.Share.Dtos;
  18. using Hotline.Share.Tools;
  19. using JiebaNet.Segmenter.Common;
  20. using XingTang.Sdk;
  21. using Mapster;
  22. namespace Hotline.Application.StatisticalReport.CallReport;
  23. public abstract class CallReportApplicationBase : ICallReportApplication
  24. {
  25. private readonly ISystemSettingCacheManager _systemSettingCacheManager;
  26. private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;
  27. private readonly ICallNativeRepository _callNativeRepository;
  28. private readonly IRepository<User> _userRepository;
  29. private readonly IRepository<Work> _workRepository;
  30. private readonly IRepository<TelRest> _telRestRepository;
  31. protected CallReportApplicationBase(ISystemSettingCacheManager systemSettingCacheManager, ICallNativeRepository callNativeRepository, IRepository<User> userRepository, IRepository<Work> workRepository, IRepository<TelRest> telRestRepository, ISystemDicDataCacheManager systemDicDataCacheManager)
  32. {
  33. _systemSettingCacheManager = systemSettingCacheManager;
  34. _callNativeRepository = callNativeRepository;
  35. _userRepository = userRepository;
  36. _workRepository = workRepository;
  37. _telRestRepository = telRestRepository;
  38. _systemDicDataCacheManager = systemDicDataCacheManager;
  39. }
  40. public virtual async Task<List<CallHotLineDto>> GetCallHotLineListAsync(BiQueryGateWayDto dto, CancellationToken requestAborted)
  41. {
  42. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  43. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  44. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  45. int ringTimes = _systemSettingCacheManager.RingTimes;
  46. var hotlines = _systemDicDataCacheManager.CallForwardingSource.Select(m => m.DicDataValue).ToList();
  47. return await _callNativeRepository.GetCallHotLineListAsync(dto, noConnectByeTimes, effectiveTimes, connectByeTimes, ringTimes, hotlines);
  48. }
  49. public virtual async Task<List<TrCallHourDto>> GetCallHourListAsync(BiQueryHourCallDto dto, CancellationToken requestAborted)
  50. {
  51. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  52. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  53. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  54. return await _callNativeRepository.GetCallHourList(dto.StartTime, dto.EndTime, noConnectByeTimes, effectiveTimes, connectByeTimes, dto.Source);
  55. }
  56. public virtual async Task<TotalData<BiSeatSwitchDto>> GetCallListAsync(QueryCallListDto dto, CancellationToken requestAborted)
  57. {
  58. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  59. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  60. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  61. return await _callNativeRepository.GetCallList(dto, noConnectByeTimes, effectiveTimes, connectByeTimes);
  62. }
  63. public virtual async Task<List<BiCallDto>> QueryCallsAsync(BiQueryCallsDto dto, CancellationToken cancellationToken)
  64. {
  65. return await _callNativeRepository.GetQueryCalls(dto.StartTime.Value, dto.EndTime.Value, dto.Line);
  66. }
  67. /// <summary>
  68. /// 话务日期明细
  69. /// </summary>
  70. /// <param name="dto"></param>
  71. /// <returns></returns>
  72. public virtual async Task<List<QueryCallsDetailDto>> QueryCallsDetailAsync(BiQueryCallsDto dto)
  73. {
  74. //超时接通量
  75. int CallInOverConnRingTime = _systemSettingCacheManager.CallInOverConnRingTime;
  76. //坐席超时挂断时间
  77. int SeatChaoTime = _systemSettingCacheManager.SeatChaoTime;
  78. //未接秒挂时间
  79. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  80. //呼入有效时间
  81. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  82. //接通秒挂时间
  83. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  84. var callData = await _callNativeRepository.Queryable()
  85. .Where(p => p.BeginIvrTime >= dto.StartTime && p.BeginIvrTime <= dto.EndTime && p.CallState != ECallState.Invalid)
  86. .WhereIF(!string.IsNullOrEmpty(dto.Keyword), p => p.ToNo == dto.Keyword)
  87. .GroupBy(p => p.BeginIvrTime.Value.ToString("yyyy-MM-dd"))
  88. .Select(p => new QueryCallsDetailDto
  89. {
  90. Date = p.BeginIvrTime.Value.ToString("yyyy-MM-dd"),
  91. InTotal = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In, 1, 0)),//呼入总量
  92. InConnectionQuantity = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.AnsweredTime != null, 1, 0)),//呼入接通量
  93. NotAcceptedHang = SqlFunc.AggregateSum(SqlFunc.IIF(p.RingDuration <= noConnectByeTimes && p.RingDuration > 0 && p.Direction == ECallDirection.In, 1, 0)), //未接通秒挂
  94. TotalDurationIncomingCalls = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.AnsweredTime != null, p.Duration, 0)), //呼入总时长
  95. InAvailableAnswer = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.AnsweredTime != null && p.Duration >= effectiveTimes, 1, 0)),//有效接通量
  96. InHangupImmediateWhenAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.Duration > 0 && p.Duration <= connectByeTimes, 1, 0)), //呼入接通秒挂
  97. TimeoutConnection = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.AnsweredTime != null && p.RingDuration >= CallInOverConnRingTime, 1, 0)),//超时接通量
  98. TimeoutSuspension = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.AnsweredTime != null && p.Duration >= SeatChaoTime, 1, 0)),//超时挂断量
  99. QueueByeCount = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.CallState == ECallState.NotAcceptedHang, 1, 0)), //队列挂断
  100. IvrByeCount = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.CallState == ECallState.IVRNoAccept, 1, 0)), //IVR挂断
  101. OutTotal = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.Out, 1, 0)),//呼出总量
  102. OutConnectionQuantity = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.Out && p.AnsweredTime != null, 1, 0))
  103. })
  104. .OrderBy(p => p.Date)
  105. .ToListAsync();
  106. return callData;
  107. }
  108. /// <summary>
  109. /// 话务日期明细-呼入总量/接通总量
  110. /// </summary>
  111. /// <param name="dto"></param>
  112. /// <returns></returns>
  113. public virtual async Task<(int, List<CallRecordOutDto>)> QueryCallsDetailInTotalAsync(BiQueryCallsDto dto, bool isAll)
  114. {
  115. var recordPrefix = _systemSettingCacheManager.RecordPrefix;
  116. var query = _callNativeRepository.Queryable(includeDeleted: true)
  117. .LeftJoin<Order>((p, o) => p.Id == o.CallId)
  118. .Where((p, o) => p.BeginIvrTime >= dto.StartTime && p.BeginIvrTime <= dto.EndTime && p.Direction == ECallDirection.In && p.CallState != ECallState.Invalid)
  119. .WhereIF(dto.TypeCode == "2", (p, o) => p.AnsweredTime != null)
  120. .WhereIF(!string.IsNullOrEmpty(dto.Keyword), (p, o) => p.TelNo == dto.Keyword)
  121. .OrderByDescending((p, o) => p.BeginIvrTime)
  122. .Select((p, o) => new CallRecordOutDto
  123. {
  124. OtherAccept = p.Id,
  125. OrderId = o.Id,
  126. OrderNo = o.No,
  127. OrderTitle = o.Title,
  128. Cdpn = p.ToNo,
  129. Cpn = p.FromNo,
  130. RecordingFileUrl = recordPrefix + p.AudioFile,
  131. RecordingFileName = p.AudioFile,
  132. RecordingBaseAddress = recordPrefix,
  133. RecordingAbsolutePath = p.AudioFile
  134. }, true);
  135. if (isAll)
  136. {
  137. return (0, await query.ToListAsync());
  138. }
  139. return await query.ToPagedListAsync(dto.PageIndex, dto.PageSize);
  140. }
  141. public virtual async Task<List<QueryCallsDetailStatistics>> QueryCallsDetailStatisticsAsync(StartEndTimeDto dto, CancellationToken cancellationToken)
  142. {
  143. //超时接通量
  144. int CallInOverConnRingTime = _systemSettingCacheManager.CallInOverConnRingTime;
  145. //坐席超时挂断时间
  146. int SeatChaoTime = _systemSettingCacheManager.SeatChaoTime;
  147. //未接秒挂时间
  148. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  149. //呼入有效时间
  150. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  151. //接通秒挂时间
  152. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  153. var callData = await _callNativeRepository.Queryable()
  154. .Where(p => p.BeginIvrTime >= dto.StartTime && p.BeginIvrTime <= dto.EndTime && p.CallState != ECallState.Invalid)
  155. .GroupBy(p => p.BeginIvrTime.Value.ToString("yyyy-MM-dd"))
  156. .Select(p => new QueryCallsDetailStatistics
  157. {
  158. Date = p.BeginIvrTime.Value.ToString("yyyy-MM-dd"),
  159. //InTotal = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In, 1, 0)),//呼入总量
  160. InConnectionQuantity = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.AnsweredTime != null, 1, 0)),//呼入接通量
  161. NotAcceptedHang = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallState == ECallState.NotAcceptedHang && p.Direction == ECallDirection.In, 1, 0)), //呼入队列挂断
  162. InNotAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(p.Duration == 0 && p.TelNo != "0" && p.Direction == ECallDirection.In, 1, 0)), // 挂机量
  163. IvrByeCount = SqlFunc.AggregateSum(SqlFunc.IIF(p.Direction == ECallDirection.In && p.CallState == ECallState.IVRNoAccept && p.AnsweredTime == null, 1, 0)), //IVR挂断
  164. OutConnectionQuantity = SqlFunc.AggregateSum(SqlFunc.IIF(p.AnsweredTime != null && p.Direction == ECallDirection.Out, 1, 0)), // 呼出接通量
  165. OutNotAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(p.AnsweredTime == null && p.Direction == ECallDirection.Out, 1, 0)), // 呼出未接量
  166. })
  167. .OrderBy(p => p.Date)
  168. .ToListAsync(cancellationToken);
  169. return callData;
  170. }
  171. public virtual async Task<List<QueryCallsDetailDto>> QueryCallsHourDetailAsync(BiQueryCallsDto dto, CancellationToken cancellationToken)
  172. {
  173. //超时接通量
  174. int CallInOverConnRingTime = _systemSettingCacheManager.CallInOverConnRingTime;
  175. //坐席超时挂断时间
  176. int SeatChaoTime = _systemSettingCacheManager.SeatChaoTime;
  177. //未接秒挂时间
  178. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  179. //呼入有效时间
  180. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  181. //接通秒挂时间
  182. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  183. return await _callNativeRepository.QueryCallsHourDetail(dto.StartTime.Value, dto.EndTime.Value, noConnectByeTimes, effectiveTimes, connectByeTimes, CallInOverConnRingTime, SeatChaoTime, dto.Line);
  184. }
  185. public virtual async Task<(int, List<QueryCallsStatisticsDetailOutDto>)> QueryCallsStatisticsDetailAsync(QueryCallsStatisticsDetailInDto dto, CancellationToken cancellationToken)
  186. {
  187. dto.FieldName ??= string.Empty;
  188. dto.FieldName = dto.FieldName.ToLower();
  189. //超时接通量
  190. int CallInOverConnRingTime = _systemSettingCacheManager.CallInOverConnRingTime;
  191. //坐席超时挂断时间
  192. int SeatChaoTime = _systemSettingCacheManager.SeatChaoTime;
  193. //未接秒挂时间
  194. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  195. //呼入有效时间
  196. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  197. //接通秒挂时间
  198. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  199. var query = _callNativeRepository.Queryable(includeDeleted: true)
  200. .LeftJoin<Order>((c, o) => c.Id == o.CallId)
  201. .Where((c, o) => c.CallState != ECallState.Invalid)
  202. .WhereIF(dto.OrderNo.NotNullOrEmpty(), (c, o) => o.No == dto.OrderNo)
  203. .WhereIF(dto.FromNo.NotNullOrEmpty(), (c, o) => c.FromNo == dto.FromNo)
  204. .WhereIF(dto.ToNo.NotNullOrEmpty(), (c, o) => c.ToNo == dto.ToNo)
  205. .WhereIF(dto.TelNo.NotNullOrEmpty(), (c, o) => c.TelNo == dto.TelNo)
  206. .WhereIF(dto.EndBy.IsNotNull(), (c, o) => c.EndBy == dto.EndBy)
  207. .Where((c, o) => c.BeginIvrTime >= dto.StartTime && c.BeginIvrTime <= dto.EndTime);
  208. if (dto.FieldName == "intotal") // 呼入总量
  209. query = query.Where((c, o) => c.Direction == ECallDirection.In);
  210. if (dto.FieldName == "inconnectionquantity") // 呼入接通
  211. query = query.Where((c, o) => c.Direction == ECallDirection.In && c.AnsweredTime != null);
  212. if (dto.FieldName == "innotanswered") // 挂机量
  213. query = query.Where((c, o) => c.Direction == ECallDirection.In && c.Duration ==0 && c.TelNo != "0");
  214. if (dto.FieldName == "notacceptedhang") // 呼入队列挂断
  215. query = query.Where((c, o) => c.Duration == 0 && c.RingDuration <= noConnectByeTimes && c.RingDuration > 0 && c.Direction == ECallDirection.In);
  216. if (dto.FieldName == "ivrbyecount") // 呼入IVR挂断
  217. query = query.Where((c, o) => c.Direction == ECallDirection.In && c.BeginIvrTime.HasValue && !c.BeginQueueTime.HasValue && !c.BeginRingTime.HasValue && c.AnsweredTime == null);
  218. if (dto.FieldName == "outconnectionquantity") // 呼出接通量
  219. query = query.Where((c, o) => c.AnsweredTime != null && c.Direction == ECallDirection.Out);
  220. if (dto.FieldName == "outnotanswered") // 呼出未接通
  221. query = query.Where((c, o) => c.AnsweredTime == null && c.Direction == ECallDirection.Out);
  222. return await query
  223. .Select<QueryCallsStatisticsDetailOutDto>()
  224. .ToPagedListAsync(dto.PageIndex, dto.PageSize, cancellationToken);
  225. }
  226. /// <summary>
  227. /// 坐席话务统计分析
  228. /// </summary>
  229. /// <param name="dto"></param>
  230. /// <returns></returns>
  231. public virtual async Task<List<BiSeatCallsDto>> QuerySeatCallAsync(ReportRequiredPagedRequest dto, CancellationToken cancellationToken)
  232. {
  233. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  234. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  235. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  236. var list = await _userRepository.Queryable()
  237. .LeftJoin<CallNative>((u, c) => u.Id == c.UserId)
  238. .Where((u, c) => c.CallState != ECallState.Invalid)
  239. .Where(u => !u.IsDeleted && u.UserType == EUserType.Seat)
  240. .Where((u, c) => c.BeginIvrTime >= dto.StartTime)
  241. .Where((u, c) => c.BeginIvrTime <= dto.EndTime)
  242. .GroupBy((u, c) => new { c.UserName, c.UserId })
  243. .Select((u, c) => new BiSeatCallsDto
  244. {
  245. Name = c.UserName,
  246. UserId = c.UserId,
  247. InTotal = SqlFunc.AggregateSum(SqlFunc.IIF(c.Direction == ECallDirection.In, 1, 0)),
  248. OutTotal = SqlFunc.AggregateSum(SqlFunc.IIF(c.Direction == ECallDirection.Out, 1, 0)),
  249. InAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(c.Direction == ECallDirection.In && c.AnsweredTime != null, 1, 0)), // 呼入接通量
  250. OutAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(c.Direction == ECallDirection.Out && c.AnsweredTime != null, 1, 0)), // 呼出接通量
  251. InHangupImmediate = SqlFunc.AggregateSum(SqlFunc.IIF(c.Direction == ECallDirection.In && c.AnsweredTime == null && c.RingDuration < noConnectByeTimes, 1, 0)), // 呼入秒挂
  252. InHanguped = SqlFunc.AggregateSum(SqlFunc.IIF(c.Direction == ECallDirection.In && c.CallState == ECallState.Missed, 1, 0)), //呼入未接
  253. InDurationAvg = SqlFunc.Ceil(SqlFunc.AggregateAvg(SqlFunc.IIF(c.Direction == ECallDirection.In && c.AnsweredTime != null, c.Duration, 0))), // 呼入平均时长
  254. OutDurationAvg = SqlFunc.Ceil(SqlFunc.AggregateAvg(SqlFunc.IIF(c.Direction == ECallDirection.Out && c.AnsweredTime != null, c.Duration, 0))), // 呼出平均时长
  255. InAvailableAnswer = SqlFunc.AggregateSum(SqlFunc.IIF(c.Direction == ECallDirection.In && c.AnsweredTime != null && c.Duration >= effectiveTimes, 1, 0)), // 有效接通量
  256. InHangupImmediateWhenAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(c.Direction == ECallDirection.In && c.AnsweredTime != null && c.Duration < connectByeTimes, 1, 0)), // 呼入接通秒挂
  257. })
  258. .MergeTable()
  259. .ToListAsync();
  260. list.ForEach(d =>
  261. {
  262. d.LoginDuration = _workRepository.Queryable().Where(q => q.UserId == d.UserId && q.CreationTime >= dto.StartTime && q.CreationTime <= dto.EndTime).Sum(q => q.WorkingDuration);
  263. if (d.LoginDuration != null)
  264. {
  265. d.LoginDuration = Math.Round(d.LoginDuration.Value, digits: 2);
  266. }
  267. d.RestDuration = _telRestRepository.Queryable().Where(q => q.UserId == d.UserId && q.CreationTime >= dto.StartTime && q.CreationTime <= dto.EndTime).Sum(q => q.RestDuration);
  268. d.RestDuration = Math.Round(d.RestDuration, digits: 2);
  269. });
  270. return list;
  271. }
  272. public virtual async Task<(int, List<BiSeatSwitchDto>)> QuerySeatSwitchAsync(QuerySeatSwitchRequest dto, CancellationToken cancellationToken)
  273. {
  274. return await _callNativeRepository.Queryable()
  275. .Where(x => !string.IsNullOrEmpty(x.AgentTransferNumber) && x.CallState != ECallState.Invalid)
  276. .WhereIF(!string.IsNullOrEmpty(dto.UserName), x => x.UserName.Contains(dto.UserName))
  277. .WhereIF(!string.IsNullOrEmpty(dto.CDPN), x => x.ToNo.Contains(dto.CDPN))
  278. .Where(x => x.BeginIvrTime >= dto.StartTime)
  279. .Where(x => x.BeginIvrTime <= dto.EndTime)
  280. .Select(x => new BiSeatSwitchDto
  281. {
  282. UserId = x.UserId,
  283. CPN = x.FromNo,
  284. CDPN = x.ToNo,
  285. CreatedTime = x.CreationTime,
  286. TelNo = x.TelNo,
  287. UserName = x.UserName,
  288. })
  289. .OrderByDescending(x => x.CreatedTime)
  290. .ToPagedListAsync(dto.PageIndex, dto.PageSize, cancellationToken);
  291. }
  292. }