Explorar o código

refactor: CommonPController

xfe hai 9 meses
pai
achega
988bdeeb44

+ 83 - 27
src/Hotline.Api/Controllers/CommonPController.cs

@@ -10,9 +10,13 @@ using Microsoft.AspNetCore.Mvc;
 using MongoDB.Driver;
 using SqlSugar;
 using System.Reflection.Metadata;
+using Hotline.Application.CallCenter;
+using Hotline.CallCenter.Configs;
 using Hotline.FlowEngine.Workflows;
 using Hotline.Settings.TimeLimits;
+using Hotline.Share.Enums.CallCenter;
 using Hotline.Share.Enums.Order;
+using Microsoft.Extensions.Options;
 using XF.Domain.Authentications;
 using XF.Domain.Exceptions;
 using XF.Domain.Repository;
@@ -34,34 +38,40 @@ namespace Hotline.Api.Controllers
         private readonly ITimeLimitDomainService _timeLimitDomainService;
         private readonly IOrderScreenRepository _orderScreenRepository;
         private readonly IRepository<OrderVisitDetail> _orderVisitedDetailRepository;
+        private readonly ICallApplication _callApplication;
+        private readonly IOptionsSnapshot<CallCenterConfiguration> _callcenterOptions;
 
         public CommonPController(
             ISystemCommonOpinionDomainService commonOpinionDomainService,
             ISystemAreaDomainService systemAreaDomainService,
             ISessionContext sessionContext,
-            IRepository<TrCallRecord> trCallRecordRepository,
+            // IRepository<TrCallRecord> trCallRecordRepository,
             IOrderRepository orderRepository,
             IMapper mapper,
             IOrderDelayRepository orderDelayRepository,
             ITimeLimitDomainService timeLimitDomainService,
             IOrderScreenRepository orderScreenRepository,
-            IRepository<OrderVisitDetail> orderVisitedDetailRepository)
+            IRepository<OrderVisitDetail> orderVisitedDetailRepository,
+            ICallApplication callApplication,
+            IOptionsSnapshot<CallCenterConfiguration> callcenterOptions)
         {
             _commonOpinionDomainService = commonOpinionDomainService;
             _systemAreaDomainService = systemAreaDomainService;
             _mapper = mapper;
             _sessionContext = sessionContext;
-            _trCallRecordRepository = trCallRecordRepository;
+            // _trCallRecordRepository = trCallRecordRepository;
             _orderRepository = orderRepository;
             _orderDelayRepository = orderDelayRepository;
             _timeLimitDomainService = timeLimitDomainService;
             _orderScreenRepository = orderScreenRepository;
             _orderVisitedDetailRepository = orderVisitedDetailRepository;
+            _callApplication = callApplication;
+            _callcenterOptions = callcenterOptions;
         }
 
 
-
         #region 省市区
+
         /// <summary>
         /// 获取省市区树形
         /// </summary>
@@ -89,46 +99,88 @@ namespace Hotline.Api.Controllers
                 var orderQuery = _orderRepository.Queryable(false, false, false)
                     .Includes(o => o.Workflow, w => w.Steps);
                 //今日来电
-                var tadayCalls = await _trCallRecordRepository.Queryable()
-                    .Where(x => x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && tadayTime.Equals(x.CreatedTime.ToString("yyyy-MM-dd"))).ToListAsync();
-                var callNum = tadayCalls.Count();
-                var validCallNum = tadayCalls.Where(x => x.Duration > 0 || x.QueueTims > 0 || x.RingTimes > 0).Count();
-                //今日接通率
-                var answeredNum = tadayCalls.Where(x => x.Duration > 0).Count();
-                var answeredRate = validCallNum > 0 ? Math.Round((double.Parse(answeredNum.ToString()) / double.Parse(validCallNum.ToString())) * 100, 2) + "%" : "-";
+                //  var tadayCalls = await _trCallRecordRepository.Queryable()
+                //      .Where(x => x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && tadayTime.Equals(x.CreatedTime.ToString("yyyy-MM-dd"))).ToListAsync();
+                //  var callNum = tadayCalls.Count();
+                //  var validCallNum = tadayCalls.Where(x => x.Duration > 0 || x.QueueTims > 0 || x.RingTimes > 0).Count();
+                //  //今日接通率
+                //  var answeredNum = tadayCalls.Where(x => x.Duration > 0).Count();
+                // var answeredRate = validCallNum > 0 ? Math.Round((double.Parse(answeredNum.ToString()) / double.Parse(validCallNum.ToString())) * 100, 2) + "%" : "-";
+
+                int callNum = 0, validCallNum = 0, answeredNum = 0;
+                var answeredRate = string.Empty;
+                var today = DateTime.Today.Date;
+                if (_callcenterOptions.Value.CallCenterType == AppDefaults.CallCenterType.TianRun)
+                {
+                    var calls = await _callApplication.QueryTianrunCallsAsync(
+                        direction: ECallDirection.In,
+                        callStartTimeStart: today,
+                        callStartTimeEnd: today.AddDays(1).AddSeconds(-1),
+                        cancellationToken: HttpContext.RequestAborted);
+                    callNum = calls.Count();
+                    validCallNum = calls.Where(x => x.Duration > 0 || x.QueueTims > 0 || x.RingTimes > 0).Count();
+                    //今日接通率
+                    answeredNum = calls.Where(x => x.Duration > 0).Count();
+                    answeredRate = validCallNum > 0
+                        ? Math.Round((double.Parse(answeredNum.ToString()) / double.Parse(validCallNum.ToString())) * 100, 2) + "%"
+                        : "-";
+                }
+                else if (_callcenterOptions.Value.CallCenterType == AppDefaults.CallCenterType.XingTang)
+                {
+                    var calls = await _callApplication.QueryCallsAsync(
+                        callStartTimeStart: today,
+                        callStartTimeEnd: today.AddDays(1).AddSeconds(-1),
+                        cancellationToken: HttpContext.RequestAborted);
+                    callNum = calls.Count();
+                    validCallNum = calls.Where(x => x.Duration > 0 || x.WaitDuration > 0 || x.RingDuration > 0).Count();
+                    //今日接通率
+                    answeredNum = calls.Where(x => x.Duration > 0).Count();
+                    answeredRate = validCallNum > 0
+                        ? Math.Round((double.Parse(answeredNum.ToString()) / double.Parse(validCallNum.ToString())) * 100, 2) + "%"
+                        : "-";
+                }
+
                 //今日受理工单
                 var tadayOrders = await orderQuery
                     .Where(o => tadayTime.Equals(o.CreationTime.ToString("yyyy-MM-dd"))).ToListAsync();
                 var orderNum = tadayOrders.Count();
                 var directlyNum = tadayOrders.Where(o => o.ProcessType == Share.Enums.Order.EProcessType.Zhiban).Count();
-                return new { CallNum = callNum, ValidCallNum = validCallNum, AnsweredNum = answeredNum, AnsweredRate = answeredRate, OrderNum = orderNum, DirectlyNum = directlyNum };
+                return new
+                {
+                    CallNum = callNum, ValidCallNum = validCallNum, AnsweredNum = answeredNum, AnsweredRate = answeredRate, OrderNum = orderNum,
+                    DirectlyNum = directlyNum
+                };
             }
+
             //部门
             //今日待办 tasksOkNum
             //var time = DateTime.Parse(tadayTime);
             //工单
             var order = await _orderRepository.Queryable()
-               .Where(o => SqlFunc.JsonListObjectAny(o.HandlerUsers, "Key", _sessionContext.RequiredUserId) || SqlFunc.JsonListObjectAny(o.HandlerOrgs, "Key", _sessionContext.RequiredOrgId))
-               .GroupBy(o => o.Id).MergeTable()
-               .Select(o => new
-               {
-                   aboutExpire = SqlFunc.AggregateSum(SqlFunc.IIF(DateTime.Now > o.NearlyExpiredTime!.Value && DateTime.Now < o.ExpiredTime!.Value, 1, 0)),
-                   havExpired = SqlFunc.AggregateSum(SqlFunc.IIF(DateTime.Now > o.ExpiredTime!.Value, 1, 0)),
-                   countersignHandle = SqlFunc.AggregateSum(SqlFunc.IIF(o.CounterSignType.HasValue, 1, 0)),
-
-               }).FirstAsync();
+                .Where(o => SqlFunc.JsonListObjectAny(o.HandlerUsers, "Key", _sessionContext.RequiredUserId) ||
+                            SqlFunc.JsonListObjectAny(o.HandlerOrgs, "Key", _sessionContext.RequiredOrgId))
+                .GroupBy(o => o.Id).MergeTable()
+                .Select(o => new
+                {
+                    aboutExpire = SqlFunc.AggregateSum(SqlFunc.IIF(DateTime.Now > o.NearlyExpiredTime!.Value && DateTime.Now < o.ExpiredTime!.Value,
+                        1, 0)),
+                    havExpired = SqlFunc.AggregateSum(SqlFunc.IIF(DateTime.Now > o.ExpiredTime!.Value, 1, 0)),
+                    countersignHandle = SqlFunc.AggregateSum(SqlFunc.IIF(o.CounterSignType.HasValue, 1, 0)),
+                }).FirstAsync();
             var aboutExpire = order?.aboutExpire ?? 0;
             var havExpired = order?.havExpired ?? 0;
             var countersignHandle = order?.countersignHandle ?? 0;
             //延期
             var delay = await _orderDelayRepository.Queryable()
                 .Includes(x => x.Workflow)
-                .Where(x => SqlFunc.JsonListObjectAny(x.HandlerUsers, "Key", _sessionContext.RequiredUserId) || SqlFunc.JsonListObjectAny(x.HandlerOrgs, "Key", _sessionContext.RequiredOrgId))
+                .Where(x => SqlFunc.JsonListObjectAny(x.HandlerUsers, "Key", _sessionContext.RequiredUserId) ||
+                            SqlFunc.JsonListObjectAny(x.HandlerOrgs, "Key", _sessionContext.RequiredOrgId))
                 .Where(x => x.DelayState == EDelayState.Examining).CountAsync();
             //甄别
             var screenAudit = await _orderScreenRepository.Queryable()
                 .Includes(x => x.Workflow)
-                 .Where(x => SqlFunc.JsonListObjectAny(x.HandlerUsers, "Key", _sessionContext.RequiredUserId) || SqlFunc.JsonListObjectAny(x.HandlerOrgs, "Key", _sessionContext.RequiredOrgId))
+                .Where(x => SqlFunc.JsonListObjectAny(x.HandlerUsers, "Key", _sessionContext.RequiredUserId) ||
+                            SqlFunc.JsonListObjectAny(x.HandlerOrgs, "Key", _sessionContext.RequiredOrgId))
                 .Where(x => x.Status == EScreenStatus.Apply)
                 .CountAsync();
             var workTime = _timeLimitDomainService.CalcWorkTimeReduce(DateTime.Now, 5);
@@ -139,9 +191,13 @@ namespace Hotline.Api.Controllers
                 .Where(x => x.OrderVisit.VisitTime < DateTime.Now && x.OrderVisit.VisitTime > workTime)
                 .Where((x, s) => x.OrderVisit.VisitState == EVisitState.Visited && x.OrderVisit.IsCanHandle)
                 .Where((x, s) => x.VisitTarget == EVisitTarget.Org && x.VisitOrgCode == _sessionContext.OrgId && (
-                SqlFunc.JsonField(x.OrgProcessingResults, "Key") == "1" || SqlFunc.JsonField(x.OrgProcessingResults, "Key") == "2" ||
-                SqlFunc.JsonField(x.OrgHandledAttitude, "Key") == "1" || SqlFunc.JsonField(x.OrgHandledAttitude, "Key") == "2")).CountAsync();
-            return new { AboutExpire = aboutExpire, HavExpired = havExpired, CountersignHandle = countersignHandle, ScreenAudit = screenAudit, Delay = delay, ScreenHandle = screenHandle };
+                    SqlFunc.JsonField(x.OrgProcessingResults, "Key") == "1" || SqlFunc.JsonField(x.OrgProcessingResults, "Key") == "2" ||
+                    SqlFunc.JsonField(x.OrgHandledAttitude, "Key") == "1" || SqlFunc.JsonField(x.OrgHandledAttitude, "Key") == "2")).CountAsync();
+            return new
+            {
+                AboutExpire = aboutExpire, HavExpired = havExpired, CountersignHandle = countersignHandle, ScreenAudit = screenAudit, Delay = delay,
+                ScreenHandle = screenHandle
+            };
         }
     }
-}
+}

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 246 - 140
src/Hotline.Api/Controllers/OrderController.cs


+ 24 - 11
src/Hotline.Application/CallCenter/DefaultCallApplication.cs

@@ -60,7 +60,9 @@ public abstract class DefaultCallApplication : ICallApplication
         _logger = logger;
     }
 
-    public DefaultCallApplication() { }
+    public DefaultCallApplication()
+    {
+    }
 
     /// <summary>
     /// 查询分机
@@ -197,8 +199,7 @@ public abstract class DefaultCallApplication : ICallApplication
     /// <summary>
     /// 定量查询通话记录
     /// </summary>
-    public async Task<IReadOnlyList<CallNativeDto>> QueryCallsFixedAsync(QueryCallsFixedDto dto,
-        CancellationToken cancellationToken)
+    public async Task<IReadOnlyList<CallNativeDto>> QueryCallsFixedAsync(QueryCallsFixedDto dto, CancellationToken cancellationToken)
     {
         return await _callNativeRepository.Queryable(includeDeleted: true)
             .LeftJoin<Order>((d, o) => d.CallNo == o.CallId)
@@ -208,10 +209,12 @@ public abstract class DefaultCallApplication : ICallApplication
             .WhereIF(!string.IsNullOrEmpty(dto.UserName), d => d.UserName == dto.UserName)
             .WhereIF(!string.IsNullOrEmpty(dto.TelNo), d => d.TelNo == dto.TelNo)
             .WhereIF(dto.EndBy != null, d => d.EndBy == dto.EndBy)
-            .WhereIF(dto.CallStartTimeBT != null, d => d.BeginIvrTime >= dto.CallStartTimeBT)
-            .WhereIF(dto.CallStartTimeLT != null, d => d.BeginIvrTime <= dto.CallStartTimeLT)
+            .WhereIF(dto.CallStartTimeStart != null, d => d.BeginIvrTime >= dto.CallStartTimeStart)
+            .WhereIF(dto.CallStartTimeEnd != null, d => d.BeginIvrTime <= dto.CallStartTimeEnd)
             .WhereIF(dto.IsConnected != null, d => d.AnsweredTime != null)
             .WhereIF(dto.Direction != null, d => d.Direction == dto.Direction)
+            .WhereIF(dto.WaitDurationStart != null && dto.WaitDurationStart > 0, d => d.WaitDuration >= dto.WaitDurationStart)
+            .WhereIF(dto.WaitDurationEnd != null && dto.WaitDurationEnd > 0, d => d.WaitDuration <= dto.WaitDurationEnd)
             .Select((d, o) => new CallNativeDto
             {
                 OrderId = o.Id,
@@ -224,8 +227,7 @@ public abstract class DefaultCallApplication : ICallApplication
     /// <summary>
     /// 查询分机操作记录(定量)
     /// </summary>
-    public async Task<IReadOnlyList<TelOperation>> QueryTelOperationsAsync(QueryTelOperationsFixedDto dto,
-        CancellationToken cancellationToken)
+    public async Task<IReadOnlyList<TelOperation>> QueryTelOperationsAsync(QueryTelOperationsFixedDto dto, CancellationToken cancellationToken)
     {
         return await _telOperationRepository.Queryable()
             .WhereIF(!string.IsNullOrEmpty(dto.UserName), d => d.UserName == dto.UserName)
@@ -308,13 +310,19 @@ public abstract class DefaultCallApplication : ICallApplication
     /// <summary>
     /// 查询通话记录
     /// </summary>
-    public async Task<List<CallNative>> QueryCallsAsync(string phone, ECallDirection? direction,
+    public async Task<List<CallNative>> QueryCallsAsync(
+        string? phone,
+        ECallDirection? direction,
+        DateTime? callStartTimeStart,
+        DateTime? callStartTimeEnd,
         CancellationToken cancellationToken)
     {
         if (string.IsNullOrEmpty(phone))
             return new List<CallNative>();
         return await _callNativeRepository.Queryable()
             .WhereIF(direction.HasValue, d => d.Direction == direction)
+            .WhereIF(callStartTimeStart != null, d => d.BeginIvrTime >= callStartTimeStart)
+            .WhereIF(callStartTimeEnd != null, d => d.BeginIvrTime <= callStartTimeEnd)
             .Where(d => d.FromNo == phone || d.ToNo == phone)
             .OrderBy(d => d.CreationTime)
             .ToListAsync(cancellationToken);
@@ -322,7 +330,7 @@ public abstract class DefaultCallApplication : ICallApplication
 
     #region tianrun 临时方案
 
-    public async Task<TrCallRecord?> GetTianrunCallAsync(string callId, CancellationToken cancellationToken)
+    public Task<TrCallRecord?> GetTianrunCallAsync(string callId, CancellationToken cancellationToken)
     {
         throw new NotImplementedException();
     }
@@ -330,7 +338,8 @@ public abstract class DefaultCallApplication : ICallApplication
     /// <summary>
     /// 关联通话记录与order(添润)
     /// </summary>
-    public async Task RelateTianrunCallWithOrderAsync(string callId, string orderId, CancellationToken cancellationToken)
+    public Task RelateTianrunCallWithOrderAsync(string callId, string orderId,
+        CancellationToken cancellationToken)
     {
         throw new NotImplementedException();
     }
@@ -338,7 +347,11 @@ public abstract class DefaultCallApplication : ICallApplication
     /// <summary>
     /// 查询通话记录
     /// </summary>
-    public async Task<List<TrCallRecord>> QueryTianrunCallsAsync(string phone, ECallDirection? direction = null,
+    public Task<List<TrCallRecord>> QueryTianrunCallsAsync(
+        string? phone, 
+        ECallDirection? direction = null,
+        DateTime? callStartTimeStart = null,
+        DateTime? callStartTimeEnd = null,
         CancellationToken cancellationToken = default)
     {
         throw new NotImplementedException();

+ 13 - 3
src/Hotline.Application/CallCenter/ICallApplication.cs

@@ -96,7 +96,12 @@ namespace Hotline.Application.CallCenter
         /// <summary>
         /// 查询通话记录
         /// </summary>
-        Task<List<CallNative>> QueryCallsAsync(string phone, ECallDirection? direction = null, CancellationToken cancellationToken = default);
+        Task<List<CallNative>> QueryCallsAsync(
+            string? phone = null,
+            ECallDirection? direction = null,
+            DateTime? callStartTimeStart = null,
+            DateTime? callStartTimeEnd = null,
+            CancellationToken cancellationToken = default);
 
         #region tianrun
 
@@ -110,8 +115,13 @@ namespace Hotline.Application.CallCenter
         /// <summary>
         /// 查询通话记录
         /// </summary>
-        Task<List<TrCallRecord>> QueryTianrunCallsAsync(string phone, ECallDirection? direction = null, CancellationToken cancellationToken = default);
+        Task<List<TrCallRecord>> QueryTianrunCallsAsync(
+            string? phone = null, 
+            ECallDirection? direction = null,
+            DateTime? callStartTimeStart = null,
+            DateTime? callStartTimeEnd = null,
+            CancellationToken cancellationToken = default);
 
         #endregion
     }
-}
+}

+ 13 - 5
src/Hotline.Application/CallCenter/TianRunCallApplication.cs

@@ -34,7 +34,7 @@ namespace Hotline.Application.CallCenter
             ITrApplication trApplication,
             ITelApplication telApplication,
             ISystemSettingCacheManager systemSettingCacheManager
-            )
+        )
         {
             _sessionContext = sessionContext;
             _trCallRecordRepository = trCallRecordRepository;
@@ -89,7 +89,7 @@ namespace Hotline.Application.CallCenter
         /// <returns></returns>
         public new Task<TrOnDutyResponseDto> GetTelStateAsync(CancellationToken cancellationToken) =>
             _trApplication.TelState(_sessionContext.RequiredUserId, cancellationToken);
-        
+
 
         public new async Task<TrCallRecord?> GetTianrunCallAsync(string callId, CancellationToken cancellationToken)
         {
@@ -117,11 +117,19 @@ namespace Hotline.Application.CallCenter
         /// <summary>
         /// 查询通话记录
         /// </summary>
-        public new async Task<List<TrCallRecord>> QueryTianrunCallsAsync(string phone, ECallDirection? direction, CancellationToken cancellationToken)
+        public new async Task<List<TrCallRecord>> QueryTianrunCallsAsync(
+            string? phone,
+            ECallDirection? direction,
+            DateTime? callStartTimeStart,
+            DateTime? callStartTimeEnd,
+            CancellationToken cancellationToken)
         {
             return await _trCallRecordRepository.Queryable()
-                .Where(x => x.CallDirection == 0 && x.CPN == phone)
+                .WhereIF(!string.IsNullOrEmpty(phone), d => d.CPN == phone || d.CDPN == phone)
+                .WhereIF(direction != null, d => d.CallDirection == direction)
+                .WhereIF(callStartTimeStart != null, d => d.BeginIvrTime >= callStartTimeStart)
+                .WhereIF(callStartTimeEnd != null, d => d.BeginIvrTime <= callStartTimeEnd)
                 .OrderBy(x => x.CreatedTime).ToListAsync(cancellationToken);
         }
     }
-}
+}

+ 12 - 2
src/Hotline.Share/Dtos/CallCenter/QueryCallsFixedDto.cs

@@ -24,12 +24,12 @@ namespace Hotline.Share.Dtos.CallCenter
         /// <summary>
         /// 呼入时间
         /// </summary>
-        public DateTime? CallStartTimeBT { get; set; }
+        public DateTime? CallStartTimeStart { get; set; }
 
         /// <summary>
         /// 呼入时间
         /// </summary>
-        public DateTime? CallStartTimeLT { get; set; }
+        public DateTime? CallStartTimeEnd { get; set; }
 
         /// <summary>
         /// 是否接通
@@ -40,5 +40,15 @@ namespace Hotline.Share.Dtos.CallCenter
         /// 通话方向
         /// </summary>
         public ECallDirection? Direction { get; set; }
+
+        /// <summary>
+        /// 等待时长
+        /// </summary>
+        public int? WaitDurationStart { get; set; }
+        
+        /// <summary>
+        /// 等待时长
+        /// </summary>
+        public int? WaitDurationEnd { get; set; }
     }
 }

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio