CommonPController.cs 10 KB

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