SeatStateDataServiceBase.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Hotline.CallCenter.Calls;
  7. using Hotline.Orders;
  8. using SqlSugar;
  9. using XF.Domain.Dependency;
  10. using XF.Domain.Repository;
  11. namespace Hotline.Application.Bigscreen
  12. {
  13. public abstract class SeatStateDataServiceBase
  14. {
  15. private readonly IRepository<CallNative> _callRepository;
  16. public SeatStateDataServiceBase(IRepository<CallNative> callRepository)
  17. {
  18. _callRepository = callRepository;
  19. }
  20. public virtual async Task<object> GetCall24(CancellationToken stoppingToken)
  21. {
  22. List<string> timeList = new List<string>();
  23. for (int i = 0;i < 24;i++)
  24. {
  25. var time = i < 10 ? $"0{i}" : i.ToString();
  26. timeList.Add(time);
  27. }
  28. var call24 = await _callRepository.Queryable()
  29. .LeftJoin<Order>((x, o) => x.Id == o.CallId)
  30. .Where((x, o) => x.CreationTime.Date == DateTime.Now.Date
  31. && string.IsNullOrEmpty(o.Id) == false
  32. )
  33. .Select(x => new { time = x.CreationTime.ToString("hh"), x.Direction }).MergeTable()
  34. .GroupBy(x => x.time)
  35. .Select(x => new
  36. {
  37. Time = x.time.ToString(),
  38. In = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.In, 1, 0)),
  39. Out = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)),
  40. }).MergeTable().ToListAsync(stoppingToken);
  41. var call24List = (from t1 in timeList
  42. join t2 in call24 on t1 equals t2.Time into t1_t2
  43. from item in t1_t2.DefaultIfEmpty()
  44. select new
  45. {
  46. Time = t1 + ":00",
  47. In = t1_t2.Select(x => x.In).FirstOrDefault(),
  48. Out = t1_t2.Select(x => x.Out).FirstOrDefault()
  49. }).ToList();
  50. return call24List;
  51. }
  52. public virtual async Task<object> GetCallTop10(CancellationToken stoppingToken)
  53. {
  54. var callTop10 = await _callRepository.Queryable()
  55. .LeftJoin<Order>((x, o) => x.Id == o.CallId)
  56. .Where((x, o) => x.CreationTime.Date == DateTime.Now.Date
  57. && string.IsNullOrEmpty(o.Id) == false
  58. )
  59. .GroupBy(x => x.UserName)
  60. .Select(x => new
  61. {
  62. UserName = x.UserName,
  63. In = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.In, 1, 0))
  64. }).MergeTable().OrderByDescending(x => x.In).Take(10).ToListAsync(stoppingToken);
  65. return callTop10;
  66. }
  67. public virtual async Task<object> GetCallList(CancellationToken stoppingToken)
  68. {
  69. var callList = await _callRepository.Queryable()
  70. .LeftJoin<Order>((x, o) => x.Id == o.CallId)
  71. .Where((x, o) => x.CreationTime.Date == DateTime.Now.Date
  72. && string.IsNullOrEmpty(o.Id) == false
  73. )
  74. .Select(x => new
  75. {
  76. InOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.In && x.AnsweredTime != null, 1, 0)),
  77. ValidOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.In && x.AnsweredTime != null && x.Duration > 5, 1, 0)),
  78. InNoOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.In && x.AnsweredTime == null, 1, 0)),
  79. OutOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.Out && x.AnsweredTime != null, 1, 0)),
  80. InQueueNoOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.In && x.AnsweredTime == null && x.WaitDuration > 0, 1, 0)),
  81. }).MergeTable().ToListAsync(stoppingToken);
  82. return callList;
  83. }
  84. public virtual async Task<object> GetCallAverage(CancellationToken stoppingToken)
  85. {
  86. List<string> timeList = new List<string>();
  87. for (int i = 0;i < 24;i++)
  88. {
  89. var time = i < 10 ? $"0{i}" : i.ToString();
  90. timeList.Add(time);
  91. }
  92. var callAverage = await _callRepository.Queryable()
  93. .LeftJoin<Order>((x, o) => x.Id == o.CallId)
  94. .Where((x, o) => x.CreationTime.Date == DateTime.Now.Date
  95. && string.IsNullOrEmpty(o.Id) == false
  96. )
  97. .Select(x => new { time = x.CreationTime.ToString("hh"), x.Direction }).MergeTable()
  98. .GroupBy(x => x.time)
  99. .Select(x => new
  100. {
  101. Time = x.time.ToString(),
  102. In = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.In, 1, 0)),
  103. InAverag = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.In, 1, 0)) / 60,
  104. Out = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)),
  105. OutAverag = SqlFunc.AggregateSum(SqlFunc.IIF(x.Direction == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)) / 60,
  106. }).MergeTable().ToListAsync(stoppingToken);
  107. var callAverageList = (from t1 in timeList
  108. join t2 in callAverage on t1 equals t2.Time into t1_t2
  109. from item in t1_t2.DefaultIfEmpty()
  110. select new
  111. {
  112. Time = t1 + ":00",
  113. In = t1_t2.Select(x => x.In).FirstOrDefault(),
  114. InAverag = t1_t2.Select(x => x.InAverag).FirstOrDefault(),
  115. Out = t1_t2.Select(x => x.Out).FirstOrDefault(),
  116. OutAverag = t1_t2.Select(x => x.OutAverag).FirstOrDefault()
  117. }).ToList();
  118. return callAverageList;
  119. }
  120. }
  121. }