CallReportApplicationBase.cs 19 KB

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