CommonPController.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.FlowEngine.Workflows;
  14. using Hotline.Settings.TimeLimits;
  15. using Hotline.Share.Enums.Order;
  16. using XF.Domain.Authentications;
  17. using XF.Domain.Exceptions;
  18. using XF.Domain.Repository;
  19. namespace Hotline.Api.Controllers
  20. {
  21. /// <summary>
  22. /// 常用意见接口
  23. /// </summary>
  24. public class CommonPController: BaseController
  25. {
  26. private readonly ISystemCommonOpinionDomainService _commonOpinionDomainService;
  27. private readonly ISystemAreaDomainService _systemAreaDomainService;
  28. private readonly IMapper _mapper;
  29. private readonly ISessionContext _sessionContext;
  30. private readonly IRepository<TrCallRecord> _trCallRecordRepository;
  31. private readonly IOrderRepository _orderRepository;
  32. private readonly IOrderDelayRepository _orderDelayRepository;
  33. private readonly ITimeLimitDomainService _timeLimitDomainService;
  34. private readonly IOrderScreenRepository _orderScreenRepository;
  35. private readonly IRepository<OrderVisitDetail> _orderVisitedDetailRepository;
  36. public CommonPController(
  37. ISystemCommonOpinionDomainService commonOpinionDomainService,
  38. ISystemAreaDomainService systemAreaDomainService,
  39. ISessionContext sessionContext,
  40. IRepository<TrCallRecord> trCallRecordRepository,
  41. IOrderRepository orderRepository,
  42. IMapper mapper,
  43. IOrderDelayRepository orderDelayRepository,
  44. ITimeLimitDomainService timeLimitDomainService,
  45. IOrderScreenRepository orderScreenRepository,
  46. IRepository<OrderVisitDetail> orderVisitedDetailRepository)
  47. {
  48. _commonOpinionDomainService = commonOpinionDomainService;
  49. _systemAreaDomainService= systemAreaDomainService;
  50. _mapper = mapper;
  51. _sessionContext = sessionContext;
  52. _trCallRecordRepository = trCallRecordRepository;
  53. _orderRepository = orderRepository;
  54. _orderDelayRepository = orderDelayRepository;
  55. _timeLimitDomainService = timeLimitDomainService;
  56. _orderScreenRepository = orderScreenRepository;
  57. _orderVisitedDetailRepository= orderVisitedDetailRepository;
  58. }
  59. #region 省市区
  60. /// <summary>
  61. /// 获取省市区树形
  62. /// </summary>
  63. /// <returns></returns>
  64. [HttpGet("tree-area")]
  65. [Obsolete("请调用sys/area/tree")]
  66. public async Task<List<SystemArea>> GetAearTree()
  67. {
  68. return await _systemAreaDomainService.GetAreaTree();
  69. }
  70. #endregion
  71. /// <summary>
  72. /// 首页基础数据
  73. /// </summary>
  74. /// <returns></returns>
  75. [HttpGet("home_data")]
  76. public async Task<Object> GetHomeData()
  77. {
  78. var tadayTime = DateTime.Now.ToString("yyyy-MM-dd");
  79. //中心
  80. if (_sessionContext.OrgIsCenter)
  81. {
  82. var orderQuery = _orderRepository.Queryable(false, false, false)
  83. .Includes(o => o.Workflow, w => w.Steps);
  84. //今日来电
  85. var tadayCalls = await _trCallRecordRepository.Queryable()
  86. .Where(x => x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && tadayTime.Equals(x.CreatedTime.ToString("yyyy-MM-dd"))).ToListAsync();
  87. var callNum = tadayCalls.Count();
  88. var validCallNum = tadayCalls.Where(x => x.Duration > 0 || x.QueueTims > 0 || x.RingTimes > 0).Count();
  89. //今日接通率
  90. var answeredNum = tadayCalls.Where(x=>x.Duration > 0).Count();
  91. var answeredRate = validCallNum > 0 ? (answeredNum / validCallNum * 100) + "%" : "100%";
  92. //今日受理工单
  93. var tadayOrders = await orderQuery
  94. .Where(o => tadayTime.Equals(o.CreationTime.ToString("yyyy-MM-dd"))).ToListAsync();
  95. var orderNum = tadayOrders.Count();
  96. var directlyNum = tadayOrders.Where(o=> o.ProcessType == Share.Enums.Order.EProcessType.Zhiban).Count();
  97. return new { CallNum = callNum, ValidCallNum = validCallNum, AnsweredNum = answeredNum, AnsweredRate = answeredRate, OrderNum = orderNum, DirectlyNum = directlyNum };
  98. }
  99. //部门
  100. //今日待办 tasksOkNum
  101. var time = DateTime.Parse(tadayTime);
  102. //工单
  103. var order = await _orderRepository.Queryable()
  104. .Includes(o => o.Workflow, w => w.Steps)
  105. .Where(o => SqlFunc.JsonListObjectAny(o.Workflow.HandlerUsers, "Key", _sessionContext.RequiredUserId) || SqlFunc.JsonListObjectAny(o.Workflow.HandlerOrgs, "Key", _sessionContext.RequiredOrgId))
  106. .GroupBy(o => o.Id)
  107. .Select(o => new
  108. {
  109. aboutExpire = SqlFunc.AggregateSum(SqlFunc.IIF(time > o.NearlyExpiredTime!.Value && time < o.ExpiredTime!.Value, 1, 0)),
  110. havExpired = SqlFunc.AggregateSum(SqlFunc.IIF( time > o.ExpiredTime!.Value, 1, 0)),
  111. countersignHandle = SqlFunc.AggregateSum(SqlFunc.IIF(o.CounterSignType.HasValue, 1, 0)),
  112. }).FirstAsync();
  113. var aboutExpire = order?.aboutExpire ?? 0;
  114. var havExpired = order?.havExpired ?? 0;
  115. var countersignHandle = order?.countersignHandle ?? 0;
  116. //延期
  117. var delay = await _orderDelayRepository.Queryable()
  118. .Includes(x=>x.Workflow)
  119. .Where(x=> SqlFunc.JsonListObjectAny(x.Workflow.HandlerUsers, "Key", _sessionContext.RequiredUserId) || SqlFunc.JsonListObjectAny(x.Workflow.HandlerOrgs, "Key", _sessionContext.RequiredOrgId))
  120. .Where(x => x.DelayState == EDelayState.Examining).CountAsync();
  121. //甄别
  122. var screenAudit = await _orderScreenRepository.Queryable()
  123. .Includes(x=>x.Workflow)
  124. .Where(x => SqlFunc.JsonListObjectAny(x.Workflow.HandlerUsers, "Key", _sessionContext.RequiredUserId) || SqlFunc.JsonListObjectAny(x.Workflow.HandlerOrgs, "Key", _sessionContext.RequiredOrgId))
  125. .Where(x => x.Status == EScreenStatus.Apply)
  126. .CountAsync();
  127. var workTime = _timeLimitDomainService.CalcWorkTimeReduce(DateTime.Now, 5);
  128. var screenHandle = await _orderVisitedDetailRepository.Queryable(false, true)
  129. .Includes(x => x.OrderVisit)
  130. .LeftJoin<OrderScreen>((x, s) => x.Id == s.VisitDetailId && s.Status < EScreenStatus.End && s.IsDeleted == false)
  131. .Where((x, s) => s.Id == null)
  132. .Where(x => x.OrderVisit.VisitTime < time && x.OrderVisit.VisitTime > workTime)
  133. .Where((x, s) => x.OrderVisit.VisitState != EVisitState.None && x.OrderVisit.IsCanHandle)
  134. .Where((x, s) => x.VisitTarget == EVisitTarget.Org && x.VisitOrgCode.StartsWith(_sessionContext.OrgId) && (
  135. SqlFunc.JsonField(x.OrgProcessingResults, "Key") == "1" || SqlFunc.JsonField(x.OrgProcessingResults, "Key") == "2" ||
  136. SqlFunc.JsonField(x.OrgHandledAttitude, "Key") == "1" || SqlFunc.JsonField(x.OrgHandledAttitude, "Key") == "2")).CountAsync();
  137. return new { AboutExpire = aboutExpire, HavExpired = havExpired, CountersignHandle = countersignHandle, ScreenAudit = screenAudit, Delay = delay, ScreenHandle = screenHandle };
  138. }
  139. }
  140. }