CommonPController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using Hotline.CallCenter.Calls;
  2. using Hotline.Orders;
  3. using Hotline.Permissions;
  4. using Hotline.Settings;
  5. using Hotline.Settings.CommonOpinions;
  6. using Hotline.Share.Dtos.Settings;
  7. using Hotline.Share.Enums.FlowEngine;
  8. using MapsterMapper;
  9. using Microsoft.AspNetCore.Mvc;
  10. using MongoDB.Driver;
  11. using SqlSugar;
  12. using System.Reflection.Metadata;
  13. using Hotline.Application.CallCenter;
  14. using Hotline.CallCenter.Configs;
  15. using Hotline.FlowEngine.Workflows;
  16. using Hotline.Settings.TimeLimits;
  17. using Hotline.Share.Enums.CallCenter;
  18. using Hotline.Share.Enums.Order;
  19. using Microsoft.Extensions.Options;
  20. using XF.Domain.Authentications;
  21. using XF.Domain.Exceptions;
  22. using XF.Domain.Repository;
  23. namespace Hotline.Api.Controllers
  24. {
  25. /// <summary>
  26. /// 常用意见接口
  27. /// </summary>
  28. public class CommonPController : BaseController
  29. {
  30. private readonly ISystemCommonOpinionDomainService _commonOpinionDomainService;
  31. private readonly ISystemAreaDomainService _systemAreaDomainService;
  32. private readonly IMapper _mapper;
  33. private readonly ISessionContext _sessionContext;
  34. // private readonly IRepository<TrCallRecord> _trCallRecordRepository;
  35. private readonly IOrderRepository _orderRepository;
  36. private readonly IOrderDelayRepository _orderDelayRepository;
  37. private readonly ITimeLimitDomainService _timeLimitDomainService;
  38. private readonly IOrderScreenRepository _orderScreenRepository;
  39. private readonly IRepository<OrderVisitDetail> _orderVisitedDetailRepository;
  40. private readonly ICallApplication _callApplication;
  41. private readonly IOptionsSnapshot<CallCenterConfiguration> _callcenterOptions;
  42. public CommonPController(
  43. ISystemCommonOpinionDomainService commonOpinionDomainService,
  44. ISystemAreaDomainService systemAreaDomainService,
  45. ISessionContext sessionContext,
  46. // IRepository<TrCallRecord> trCallRecordRepository,
  47. IOrderRepository orderRepository,
  48. IMapper mapper,
  49. IOrderDelayRepository orderDelayRepository,
  50. ITimeLimitDomainService timeLimitDomainService,
  51. IOrderScreenRepository orderScreenRepository,
  52. IRepository<OrderVisitDetail> orderVisitedDetailRepository,
  53. ICallApplication callApplication,
  54. IOptionsSnapshot<CallCenterConfiguration> callcenterOptions)
  55. {
  56. _commonOpinionDomainService = commonOpinionDomainService;
  57. _systemAreaDomainService = systemAreaDomainService;
  58. _mapper = mapper;
  59. _sessionContext = sessionContext;
  60. // _trCallRecordRepository = trCallRecordRepository;
  61. _orderRepository = orderRepository;
  62. _orderDelayRepository = orderDelayRepository;
  63. _timeLimitDomainService = timeLimitDomainService;
  64. _orderScreenRepository = orderScreenRepository;
  65. _orderVisitedDetailRepository = orderVisitedDetailRepository;
  66. _callApplication = callApplication;
  67. _callcenterOptions = callcenterOptions;
  68. }
  69. #region 省市区
  70. /// <summary>
  71. /// 获取省市区树形
  72. /// </summary>
  73. /// <returns></returns>
  74. [HttpGet("tree-area")]
  75. [Obsolete("请调用sys/area/tree")]
  76. public async Task<List<SystemArea>> GetAearTree()
  77. {
  78. return await _systemAreaDomainService.GetAreaTree();
  79. }
  80. #endregion
  81. /// <summary>
  82. /// 首页基础数据
  83. /// </summary>
  84. /// <returns></returns>
  85. [HttpGet("home_data")]
  86. public async Task<Object> GetHomeData()
  87. {
  88. var tadayTime = DateTime.Now.ToString("yyyy-MM-dd");
  89. //中心
  90. if (_sessionContext.OrgIsCenter)
  91. {
  92. var orderQuery = _orderRepository.Queryable(false, false, false)
  93. .Includes(o => o.Workflow, w => w.Steps);
  94. //今日来电
  95. // var tadayCalls = await _trCallRecordRepository.Queryable()
  96. // .Where(x => x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && tadayTime.Equals(x.CreatedTime.ToString("yyyy-MM-dd"))).ToListAsync();
  97. // var callNum = tadayCalls.Count();
  98. // var validCallNum = tadayCalls.Where(x => x.Duration > 0 || x.QueueTims > 0 || x.RingTimes > 0).Count();
  99. // //今日接通率
  100. // var answeredNum = tadayCalls.Where(x => x.Duration > 0).Count();
  101. // var answeredRate = validCallNum > 0 ? Math.Round((double.Parse(answeredNum.ToString()) / double.Parse(validCallNum.ToString())) * 100, 2) + "%" : "-";
  102. int callNum = 0, validCallNum = 0, answeredNum = 0;
  103. var answeredRate = string.Empty;
  104. var today = DateTime.Today.Date;
  105. if (_callcenterOptions.Value.CallCenterType == AppDefaults.CallCenterType.TianRun)
  106. {
  107. var calls = await _callApplication.QueryTianrunCallsAsync(
  108. direction: ECallDirection.In,
  109. callStartTimeStart: today,
  110. callStartTimeEnd: today.AddDays(1).AddSeconds(-1),
  111. cancellationToken: HttpContext.RequestAborted);
  112. callNum = calls.Count();
  113. validCallNum = calls.Where(x => x.Duration > 0 || x.QueueTims > 0 || x.RingTimes > 0).Count();
  114. //今日接通率
  115. answeredNum = calls.Where(x => x.Duration > 0).Count();
  116. answeredRate = validCallNum > 0
  117. ? Math.Round((double.Parse(answeredNum.ToString()) / double.Parse(validCallNum.ToString())) * 100, 2) + "%"
  118. : "-";
  119. }
  120. else if (_callcenterOptions.Value.CallCenterType == AppDefaults.CallCenterType.XingTang)
  121. {
  122. var calls = await _callApplication.QueryCallsAsync(
  123. callStartTimeStart: today,
  124. callStartTimeEnd: today.AddDays(1).AddSeconds(-1),
  125. cancellationToken: HttpContext.RequestAborted);
  126. callNum = calls.Count();
  127. validCallNum = calls.Where(x => x.Duration > 0 || x.WaitDuration > 0 || x.RingDuration > 0).Count();
  128. //今日接通率
  129. answeredNum = calls.Where(x => x.Duration > 0).Count();
  130. answeredRate = validCallNum > 0
  131. ? Math.Round((double.Parse(answeredNum.ToString()) / double.Parse(validCallNum.ToString())) * 100, 2) + "%"
  132. : "-";
  133. }
  134. //今日受理工单
  135. var tadayOrders = await orderQuery
  136. .Where(o => tadayTime.Equals(o.CreationTime.ToString("yyyy-MM-dd"))).ToListAsync();
  137. var orderNum = tadayOrders.Count();
  138. var directlyNum = tadayOrders.Where(o => o.ProcessType == Share.Enums.Order.EProcessType.Zhiban).Count();
  139. return new
  140. {
  141. CallNum = callNum, ValidCallNum = validCallNum, AnsweredNum = answeredNum, AnsweredRate = answeredRate, OrderNum = orderNum,
  142. DirectlyNum = directlyNum
  143. };
  144. }
  145. //部门
  146. //今日待办 tasksOkNum
  147. //var time = DateTime.Parse(tadayTime);
  148. //工单
  149. var order = await _orderRepository.Queryable(hasHandled: false)
  150. .Includes(d => d.OrderSpecials)
  151. .Where(d => d.Status != EOrderStatus.WaitForAccept && d.Status != EOrderStatus.BackToUnAccept && d.Status != EOrderStatus.SpecialToUnAccept)
  152. .Where(d => d.Source < ESource.MLSQ || d.Source > ESource.WZSC)
  153. .Where(d => d.Status != EOrderStatus.BackToProvince && d.Status < EOrderStatus.Filed)
  154. .Where(d => d.OrderSpecials.Any() == false || d.OrderSpecials.Any(s => s.State > 0))
  155. .GroupBy(o => new { o.Id,o.Status })
  156. .Select(o => new
  157. {
  158. aboutExpire = SqlFunc.AggregateSum(SqlFunc.IIF(DateTime.Now > o.NearlyExpiredTime!.Value && DateTime.Now < o.ExpiredTime!.Value,
  159. 1, 0)),
  160. havExpired = SqlFunc.AggregateSum(SqlFunc.IIF(DateTime.Now > o.ExpiredTime!.Value , 1, 0)),
  161. countersignHandle = SqlFunc.AggregateSum(SqlFunc.IIF(o.CounterSignType.HasValue, 1, 0)),
  162. }).ToListAsync();
  163. var aboutExpire = order?.Sum(x=>x.aboutExpire) ?? 0;
  164. var havExpired = order?.Sum(x=>x.havExpired) ?? 0;
  165. var countersignHandle = order?.Sum(x=> x.countersignHandle) ?? 0;
  166. //延期
  167. var delay = await _orderDelayRepository.Queryable(hasHandled: false)
  168. .Where(d => d.DelayState == EDelayState.Examining).CountAsync();
  169. //甄别
  170. var screenAudit = await _orderScreenRepository.Queryable(hasHandled: false)
  171. .Where(d => d.Status == EScreenStatus.Apply)
  172. .CountAsync();
  173. //var workTime = _timeLimitDomainService.CalcWorkTimeReduce(DateTime.Now, 5);
  174. var screenHandle = await _orderVisitedDetailRepository.Queryable(false, true)
  175. .Includes(x => x.OrderVisit)
  176. .LeftJoin<OrderScreen>((x, s) => x.Id == s.VisitDetailId && s.IsDeleted == false)
  177. //&& s.Status < EScreenStatus.End
  178. .Where((x, s) => s.Id == null)
  179. //.Where(x => x.OrderVisit.VisitTime < DateTime.Now && x.OrderVisit.VisitTime > workTime)
  180. .Where((x, s) => x.OrderVisit.VisitState == EVisitState.Visited && x.OrderVisit.IsCanHandle)
  181. .Where((x, s) => x.VisitTarget == EVisitTarget.Org && x.VisitOrgCode == _sessionContext.OrgId && (
  182. SqlFunc.JsonField(x.OrgProcessingResults, "Key") == "1" || SqlFunc.JsonField(x.OrgProcessingResults, "Key") == "2" ||
  183. SqlFunc.JsonField(x.OrgHandledAttitude, "Key") == "1" || SqlFunc.JsonField(x.OrgHandledAttitude, "Key") == "2")).CountAsync();
  184. return new
  185. {
  186. AboutExpire = aboutExpire, HavExpired = havExpired, CountersignHandle = countersignHandle, ScreenAudit = screenAudit, Delay = delay,
  187. ScreenHandle = screenHandle
  188. };
  189. }
  190. }
  191. }