YiBinCallReportApplication.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.Repository.SqlSugar.Extensions;
  3. using Hotline.CallCenter.Calls;
  4. using Hotline.CallCenter.Tels;
  5. using Hotline.DI;
  6. using Hotline.Repository.SqlSugar.CallCenter;
  7. using Hotline.Settings;
  8. using Hotline.Share.Dtos.CallCenter;
  9. using Hotline.Share.Dtos.TrCallCenter;
  10. using Hotline.Share.Enums.CallCenter;
  11. using Hotline.Share.Enums.User;
  12. using Hotline.Share.Requests;
  13. using Hotline.Users;
  14. using SqlSugar;
  15. using XF.Domain.Dependency;
  16. using XF.Domain.Exceptions;
  17. using XF.Domain.Repository;
  18. using Microsoft.AspNetCore.Http;
  19. using Hotline.Share.Dtos;
  20. using MapsterMapper;
  21. namespace Hotline.Application.StatisticalReport.CallReport;
  22. [Injection(AppScopes = EAppScope.YiBin)]
  23. public class YiBinCallReportApplication : CallReportApplicationBase, ICallReportApplication, IScopeDependency
  24. {
  25. private readonly IRepository<TrCallRecord> _trCallRecordRepository;
  26. private readonly IMapper _mapper;
  27. private readonly ITrCallRecordRepository _trCallRecordRepositoryEx;
  28. private readonly ISystemSettingCacheManager _systemSettingCacheManager;
  29. private readonly IRepository<User> _userRepository;
  30. private readonly IRepository<Work> _workRepository;
  31. private readonly IRepository<TelRest> _telRestRepository;
  32. private readonly ICallNativeRepository _callNativeRepository;
  33. public YiBinCallReportApplication(
  34. IRepository<TrCallRecord> trCallRecordRepository,
  35. ISystemSettingCacheManager systemSettingCacheManager,
  36. IRepository<User> userRepository,
  37. IRepository<Work> workRepository,
  38. IRepository<TelRest> telRestRepository,
  39. ICallNativeRepository callNativeRepository,
  40. ITrCallRecordRepository trCallRecordRepositoryEx,
  41. IMapper mapper,
  42. ISystemDicDataCacheManager systemDicDataCacheManager) : base(systemSettingCacheManager, callNativeRepository, userRepository, workRepository, telRestRepository, systemDicDataCacheManager)
  43. {
  44. _trCallRecordRepository = trCallRecordRepository;
  45. _systemSettingCacheManager = systemSettingCacheManager;
  46. _userRepository = userRepository;
  47. _workRepository = workRepository;
  48. _telRestRepository = telRestRepository;
  49. _callNativeRepository = callNativeRepository;
  50. _trCallRecordRepositoryEx = trCallRecordRepositoryEx;
  51. _mapper = mapper;
  52. }
  53. public override async Task<List<BiCallDto>> QueryCallsAsync(BiQueryCallsDto dto, CancellationToken cancellationToken)
  54. {
  55. return await _trCallRecordRepositoryEx.GetQueryCalls(dto.StartTime.Value, dto.EndTime.Value, dto.Keyword);
  56. }
  57. /// <summary>
  58. /// 话务日期明细
  59. /// </summary>
  60. /// <param name="dto"></param>
  61. /// <returns></returns>
  62. public override async Task<List<QueryCallsDetailDto>> QueryCallsDetailAsync(BiQueryCallsDto dto)
  63. {
  64. //超时接通量
  65. int CallInOverConnRingTime = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.CallInOverConnRingTime)?.SettingValue[0]);
  66. //坐席超时挂断时间
  67. int SeatChaoTime = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.SeatChaoTime)?.SettingValue[0]);
  68. //未接秒挂时间
  69. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  70. //呼入有效时间
  71. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  72. //接通秒挂时间
  73. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  74. var callData = await _trCallRecordRepository.Queryable()
  75. .Where(p => p.CreatedTime >= dto.StartTime && p.CreatedTime <= dto.EndTime)
  76. // .Where(p => p.Gateway != "82826886" && SqlFunc.Length(p.Gateway) != 4)
  77. .WhereIF(!string.IsNullOrEmpty(dto.Keyword), p => p.Gateway == dto.Keyword)
  78. .GroupBy(p => p.CreatedTime.ToString("yyyy-MM-dd"))
  79. .Select(p => new QueryCallsDetailDto
  80. {
  81. Date = p.CreatedTime.ToString("yyyy-MM-dd"),
  82. InTotal = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In, 1, 0)),//呼入总量
  83. InConnectionQuantity = SqlFunc.AggregateSum(SqlFunc.IIF(p.OnState == EOnState.On && p.CallDirection == ECallDirection.In && p.AnsweredTime != null, 1, 0)),//呼入接通量
  84. NotAcceptedHang = SqlFunc.AggregateSum(SqlFunc.IIF(p.Duration == 0 && p.RingTimes <= noConnectByeTimes && p.RingTimes > 0 && p.CallDirection == ECallDirection.In, 1, 0)), //未接通秒挂
  85. TotalDurationIncomingCalls = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.AnsweredTime != null && p.OnState == EOnState.On, p.Duration, 0)), //呼入总时长
  86. InAvailableAnswer = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.AnsweredTime != null && p.Duration >= effectiveTimes, 1, 0)),//有效接通量
  87. InHangupImmediateWhenAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.Duration > 0 && p.Duration <= connectByeTimes, 1, 0)), //呼入接通秒挂
  88. TimeoutConnection = SqlFunc.AggregateSum(SqlFunc.IIF(p.OnState == EOnState.On && p.CallDirection == ECallDirection.In && p.AnsweredTime != null && p.RingTimes >= CallInOverConnRingTime, 1, 0)),//超时接通量
  89. TimeoutSuspension = SqlFunc.AggregateSum(SqlFunc.IIF(p.OnState == EOnState.On && p.CallDirection == ECallDirection.In && p.AnsweredTime != null && p.Duration >= SeatChaoTime, 1, 0)),//超时挂断量
  90. QueueByeCount = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.QueueTims > 0 && p.RingTimes == 0 && p.OnState == EOnState.NoOn, 1, 0)), //队列挂断
  91. IvrByeCount = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.BeginIvrTime.HasValue && !p.BeginQueueTime.HasValue && !p.BeginRingTime.HasValue && p.OnState == EOnState.NoOn, 1, 0)), //IVR挂断
  92. OutTotal = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.Out, 1, 0)),//呼出总量
  93. OutConnectionQuantity = SqlFunc.AggregateSum(SqlFunc.IIF(p.OnState == EOnState.On && p.CallDirection == ECallDirection.Out && p.AnsweredTime != null, 1, 0))
  94. })
  95. .OrderBy(p => p.Date)
  96. .ToListAsync();
  97. return callData;
  98. }
  99. /// <summary>
  100. /// 话务日期明细-呼入总量/接通总量
  101. /// </summary>
  102. /// <param name="dto"></param>
  103. /// <returns></returns>
  104. public override async Task<(int, List<CallRecordOutDto>)> QueryCallsDetailInTotalAsync(BiQueryCallsDto dto, bool isAll)
  105. {
  106. var query = _trCallRecordRepository.Queryable()
  107. .Includes(p => p.Order)
  108. .Where(p => p.CreatedTime >= dto.StartTime && p.CreatedTime <= dto.EndTime && p.CallDirection == ECallDirection.In)
  109. .WhereIF(dto.TypeCode == "2", p => p.OnState == EOnState.On && p.AnsweredTime != null)
  110. .WhereIF(!string.IsNullOrEmpty(dto.Keyword), p => p.Gateway == dto.Keyword)
  111. .OrderByDescending(p => p.CreatedTime)
  112. .Select(m => new CallRecordOutDto()
  113. {
  114. FromNo = m.CPN,
  115. ToNo = m.CDPN,
  116. Direction = m.CallDirection,
  117. EndTime = m.OverTime,
  118. OrderId = m.Order.Id,
  119. OrderTitle = m.Order.Title,
  120. OrderNo = m.Order.No
  121. }, true);
  122. if (isAll)
  123. {
  124. return (0, await query.ToListAsync());
  125. }
  126. return await query.ToPagedListAsync(dto.PageIndex, dto.PageSize);
  127. }
  128. /// <summary>
  129. /// 坐席话务统计分析
  130. /// </summary>
  131. /// <param name="dto"></param>
  132. /// <returns></returns>
  133. public override async Task<List<BiSeatCallsDto>> QuerySeatCallAsync(ReportRequiredPagedRequest dto, CancellationToken cancellationToken)
  134. {
  135. //获取配置
  136. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  137. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  138. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  139. var list = await _userRepository.Queryable()
  140. .LeftJoin<TrCallRecord>((u, c) => u.Id == c.UserId)
  141. .Where(u => !u.IsDeleted && u.UserType == EUserType.Seat)
  142. .Where((u, c) => c.CreatedTime >= dto.StartTime)
  143. .Where((u, c) => c.CreatedTime <= dto.EndTime)
  144. .GroupBy((u, c) => new { c.UserName, c.UserId })
  145. .Select((u, c) => new BiSeatCallsDto
  146. {
  147. Name = c.UserName,
  148. UserId = c.UserId,
  149. InTotal = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In, 1, 0)),
  150. OutTotal = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.Out, 1, 0)),
  151. InAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime != null, 1, 0)),
  152. OutAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.Out && c.AnsweredTime != null, 1, 0)),
  153. InHangupImmediate = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime == null && c.RingTimes < noConnectByeTimes, 1, 0)),
  154. InHanguped = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime == null, 1, 0)),
  155. InDurationAvg = SqlFunc.Ceil(SqlFunc.AggregateAvg(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime != null, c.Duration, 0))),
  156. OutDurationAvg = SqlFunc.Ceil(SqlFunc.AggregateAvg(SqlFunc.IIF(c.CallDirection == ECallDirection.Out && c.AnsweredTime != null, c.Duration, 0))),
  157. InAvailableAnswer = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime != null && c.Duration >= effectiveTimes, 1, 0)),
  158. InHangupImmediateWhenAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime != null && c.Duration < connectByeTimes, 1, 0)),
  159. })
  160. .MergeTable()
  161. .ToListAsync(cancellationToken);
  162. list.ForEach(d =>
  163. {
  164. d.LoginDuration = _workRepository.Queryable().Where(q => q.UserId == d.UserId && q.CreationTime >= dto.StartTime && q.CreationTime <= dto.EndTime).Sum(q => q.WorkingDuration);
  165. if (d.LoginDuration != null)
  166. {
  167. d.LoginDuration = Math.Round(d.LoginDuration.Value, digits: 2);
  168. }
  169. d.RestDuration = _telRestRepository.Queryable().Where(q => q.UserId == d.UserId && q.CreationTime >= dto.StartTime && q.CreationTime <= dto.EndTime).Sum(q => q.RestDuration);
  170. d.RestDuration = Math.Round(d.RestDuration, digits: 2);
  171. });
  172. return list;
  173. }
  174. public override async Task<List<QueryCallsDetailDto>> QueryCallsHourDetailAsync(BiQueryCallsDto dto, CancellationToken cancellationToken)
  175. {
  176. //超时接通量
  177. int CallInOverConnRingTime = _systemSettingCacheManager.CallInOverConnRingTime;
  178. //坐席超时挂断时间
  179. int SeatChaoTime = _systemSettingCacheManager.SeatChaoTime;
  180. //未接秒挂时间
  181. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  182. //呼入有效时间
  183. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  184. //接通秒挂时间
  185. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  186. return await _trCallRecordRepositoryEx.QueryCallsHourDetail(dto.StartTime.Value, dto.EndTime.Value, noConnectByeTimes, effectiveTimes
  187. , connectByeTimes, CallInOverConnRingTime, SeatChaoTime, dto.Keyword);
  188. }
  189. public override async Task<(int, List<BiSeatSwitchDto>)> QuerySeatSwitchAsync(QuerySeatSwitchRequest dto, CancellationToken cancellationToken)
  190. {
  191. return await _trCallRecordRepository.Queryable()
  192. .Where(x => !string.IsNullOrEmpty(x.AgentTransferNumber))
  193. .WhereIF(!string.IsNullOrEmpty(dto.UserName), x => x.UserName.Contains(dto.UserName))
  194. .WhereIF(!string.IsNullOrEmpty(dto.CDPN), x => x.CDPN.Contains(dto.CDPN))
  195. .Where(x => x.CreatedTime >= dto.StartTime)
  196. .Where(x => x.CreatedTime <= dto.EndTime)
  197. .Select(x => new BiSeatSwitchDto
  198. {
  199. UserId = x.UserId,
  200. CPN = x.CPN,
  201. CDPN = x.CDPN,
  202. CreatedTime = x.CreatedTime,
  203. TelNo = x.AgentTransferNumber,
  204. UserName = x.UserName,
  205. })
  206. .OrderByDescending(x => x.CreatedTime)
  207. .ToPagedListAsync(dto.PageIndex, dto.PageSize, cancellationToken);
  208. }
  209. public override async Task<List<TrCallHourDto>> GetCallHourListAsync(BiQueryHourCallDto dto, CancellationToken requestAborted)
  210. {
  211. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  212. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  213. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  214. return await _trCallRecordRepositoryEx.GetCallHourList(dto.StartTime, dto.EndTime, noConnectByeTimes, effectiveTimes, connectByeTimes, dto.Source);
  215. }
  216. public override async Task<List<CallHotLineDto>> GetCallHotLineListAsync(BiQueryGateWayDto dto, CancellationToken requestAborted)
  217. {
  218. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  219. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  220. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  221. var ringTims = _systemSettingCacheManager.RingTimes;
  222. return await _trCallRecordRepositoryEx.GetCallHotLineList(dto.StartTime, dto.EndTime, dto.Gateway, noConnectByeTimes, effectiveTimes, connectByeTimes, ringTims); ;
  223. }
  224. public override async Task<TotalData<BiSeatSwitchDto>> GetCallListAsync(QueryCallListDto dto, CancellationToken requestAborted)
  225. {
  226. //获取配置
  227. int noConnectByeTimes = _systemSettingCacheManager.NoConnectByeTimes;
  228. int effectiveTimes = _systemSettingCacheManager.EffectiveTimes;
  229. int connectByeTimes = _systemSettingCacheManager.ConnectByeTimes;
  230. var list = await _trCallRecordRepositoryEx.GetCallList(dto, noConnectByeTimes, effectiveTimes, connectByeTimes);
  231. return list;
  232. }
  233. /// <summary>
  234. /// 话务日期统计明细
  235. /// </summary>
  236. /// <param name="dto"></param>
  237. /// <returns></returns>
  238. public override async Task<List<QueryCallDateStatisticsDetailResp>> QueryCallDateStatisticsDetail(QueryCallDateStatisticsDetailDto dto)
  239. {
  240. return await _trCallRecordRepositoryEx.QueryCallDateStatisticsDetail(dto.StartTime.Value,dto.EndTime.Value);
  241. }
  242. /// <summary>
  243. /// 个人服务话务明细
  244. /// </summary>
  245. /// <param name="dto"></param>
  246. /// <returns></returns>
  247. public override async Task<List<QueryPersonCallDateStatisticsDetailResp>> QueryPersonCallDateStatisticsDetail(QueryCallDateStatisticsDetailDto dto)
  248. {
  249. return await _trCallRecordRepositoryEx.QueryPersonCallDateStatisticsDetail(dto.StartTime.Value, dto.EndTime.Value);
  250. }
  251. /// <summary>
  252. /// 企业服务话务明细
  253. /// </summary>
  254. /// <param name="dto"></param>
  255. /// <returns></returns>
  256. public override async Task<List<QueryEnterpriseCallDateStatisticsDetailResp>> QueryEnterpriseCallDateStatisticsDetail(QueryCallDateStatisticsDetailDto dto)
  257. {
  258. return await _trCallRecordRepositoryEx.QueryEnterpriseCallDateStatisticsDetail(dto.StartTime.Value, dto.EndTime.Value);
  259. }
  260. /// <summary>
  261. /// 呼出话务统计明细
  262. /// </summary>
  263. /// <param name="dto"></param>
  264. /// <returns></returns>
  265. public override async Task<List<QueryCallOutDateStatisticsDetailResp>> QueryCallOutDateStatisticsDetail(QueryCallDateStatisticsDetailDto dto)
  266. {
  267. return null;
  268. }
  269. //public override async Task<PagedDto<TrCallDto>> GetCallDetailListAsync(GetCallListDto dto, CancellationToken cancellationToken)
  270. //{
  271. // var (total, items) = await _trCallRecordRepository.Queryable()
  272. // .Includes(x => x.Order)
  273. // .WhereIF(!string.IsNullOrEmpty(dto.CPN), x => x.CPN.Contains(dto.CPN))
  274. // .WhereIF(!string.IsNullOrEmpty(dto.CDPN), x => x.CDPN.Contains(dto.CDPN))
  275. // .WhereIF(!string.IsNullOrEmpty(dto.TelNo), x => x.TelNo.Contains(dto.TelNo))
  276. // .WhereIF(!string.IsNullOrEmpty(dto.UserName), x => x.UserName.Contains(dto.UserName))
  277. // .WhereIF(dto.CallDirection != null, x => x.CallDirection == dto.CallDirection)
  278. // .WhereIF(dto.OnState != null, x => x.OnState == dto.OnState)
  279. // .WhereIF(dto.EndBy != null, x => x.EndBy == dto.EndBy)
  280. // .WhereIF(dto.BeginIvrTimeStart.HasValue, x => x.BeginIvrTime >= dto.BeginIvrTimeStart)
  281. // .WhereIF(dto.BeginIvrTimeEnd.HasValue, x => x.BeginIvrTime <= dto.BeginIvrTimeEnd)
  282. // .WhereIF(dto.EndIvrTimeStart.HasValue, x => x.EndIvrTime >= dto.EndIvrTimeStart)
  283. // .WhereIF(dto.EndIvrTimeEnd.HasValue, x => x.EndIvrTime <= dto.EndIvrTimeEnd)
  284. // .WhereIF(dto.BeginQueueTimeStart.HasValue, x => x.BeginQueueTime >= dto.BeginQueueTimeEnd)
  285. // .WhereIF(dto.BeginQueueTimeEnd.HasValue, x => x.BeginQueueTime <= dto.BeginQueueTimeEnd)
  286. // .WhereIF(dto.EndQueueTimeStart.HasValue, x => x.EndQueueTime >= dto.EndQueueTimeStart)
  287. // .WhereIF(dto.EndQueueTimeEnd.HasValue, x => x.EndQueueTime <= dto.EndQueueTimeEnd)
  288. // .WhereIF(dto.AnsweredTimeStart.HasValue, x => x.AnsweredTime >= dto.AnsweredTimeStart)
  289. // .WhereIF(dto.AnsweredTimeEnd.HasValue, x => x.AnsweredTime <= dto.AnsweredTimeEnd)
  290. // .WhereIF(dto.OverTimeStart.HasValue, x => x.OverTime >= dto.OverTimeStart)
  291. // .WhereIF(dto.OverTimeEnd.HasValue, x => x.OverTime <= dto.OverTimeEnd)
  292. // .WhereIF(dto.BeginRingTimeStart.HasValue, x => x.BeginRingTime >= dto.BeginRingTimeStart)
  293. // .WhereIF(dto.BeginRingTimeEnd.HasValue, x => x.BeginRingTime <= dto.BeginRingTimeEnd)
  294. // .WhereIF(dto.EndRingTimeStart.HasValue, x => x.EndRingTimg >= dto.EndRingTimeStart)
  295. // .WhereIF(dto.EndRingTimeEnd.HasValue, x => x.EndRingTimg <= dto.EndRingTimeEnd)
  296. // .WhereIF(dto.CallTimeStart.HasValue, x => x.CreatedTime >= dto.CallTimeStart)
  297. // .WhereIF(dto.CallTimeEnd.HasValue, x => x.CreatedTime <= dto.CallTimeEnd)
  298. // .WhereIF(!string.IsNullOrEmpty(dto.OrderNo), x => x.Order.No.Contains(dto.OrderNo))
  299. // .WhereIF(!string.IsNullOrEmpty(dto.Title), x => x.Order.Title.Contains(dto.Title))
  300. // .WhereIF(!string.IsNullOrEmpty(dto.Gateway), x => x.Gateway.Contains(dto.Gateway))
  301. // .WhereIF(dto.IsAiAnswered == true, x => string.IsNullOrEmpty(x.UserId) == true && x.BeginIvrTime.HasValue && x.EndIvrTime.HasValue)
  302. // .WhereIF(dto.PhoneTypes != null, x => x.PhoneTypes == dto.PhoneTypes)
  303. // .OrderByDescending(x => x.CreatedTime)
  304. // .ToPagedListAsync(dto.PageIndex, dto.PageSize);
  305. // return new PagedDto<TrCallDto>(total, _mapper.Map<IReadOnlyList<TrCallDto>>(items));
  306. //}
  307. }