//using Hotline.Caching.Interfaces;
//using Hotline.CallCenter.Calls;
//using Hotline.CallCenter.Tels;
//using Hotline.Settings;
//using Hotline.Share.Dtos.CallCenter;
//using Hotline.Share.Enums.CallCenter;
//using Hotline.Share.Enums.User;
//using Hotline.Share.Requests;
//using Hotline.Users;
//using Microsoft.AspNetCore.Http;
//using SqlSugar;
//using XF.Domain.Dependency;
//using XF.Domain.Exceptions;
//using XF.Domain.Repository;

//namespace Hotline.Application.StatisticalReport
//{
    //public class CallReportApplication : ICallReportApplication, IScopeDependency
    //{
    //    private readonly IRepository<TrCallRecord> _trCallRecordRepository;
    //    private readonly ISystemSettingCacheManager _systemSettingCacheManager;
    //    private readonly IRepository<User> _userRepository;
    //    private readonly IRepository<Work> _workRepository;
    //    private readonly IRepository<TelRest> _telRestRepository;

    //    public CallReportApplication(
    //        IRepository<TrCallRecord> trCallRecordRepository,
    //        ISystemSettingCacheManager systemSettingCacheManager,
    //        IRepository<User> userRepository,
    //        IRepository<Work> workRepository,
    //        IRepository<TelRest> telRestRepository)
    //    {
    //        _trCallRecordRepository = trCallRecordRepository;
    //        _systemSettingCacheManager = systemSettingCacheManager;
    //        _userRepository = userRepository;
    //        _workRepository = workRepository;
    //        _telRestRepository = telRestRepository;
    //    }

    //    /// <summary>
    //    /// 话务日期明细
    //    /// </summary>
    //    /// <param name="dto"></param>
    //    /// <returns></returns>
    //    public async Task<List<QueryCallsDetailDto>> QueryCallsDetailAsync(BiQueryCallsDto dto)
    //    {
    //        //超时接通量
    //        int CallInOverConnRingTime = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.CallInOverConnRingTime)?.SettingValue[0]);
    //        //坐席超时挂断时间
    //        int SeatChaoTime = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.SeatChaoTime)?.SettingValue[0]);

    //        //未接秒挂时间
    //        int noConnectByeTimes = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.NoConnectByeTimes)?.SettingValue[0]);
    //        //呼入有效时间
    //        int effectiveTimes = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.EffectiveTimes)?.SettingValue[0]);
    //        //接通秒挂时间
    //        int connectByeTimes = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.ConnectByeTimes)?.SettingValue[0]);

    //        var callData = await _trCallRecordRepository.Queryable()
    //                .Where(p => p.CreatedTime >= dto.StartTime && p.CreatedTime <= dto.EndTime)
    //                 // .Where(p => p.Gateway != "82826886" && SqlFunc.Length(p.Gateway) != 4)
    //                 .WhereIF(!string.IsNullOrEmpty(dto.Keyword), p => p.Gateway == dto.Keyword)
    //                 .GroupBy(p => p.CreatedTime.ToString("yyyy-MM-dd"))
    //                .Select(p => new QueryCallsDetailDto
    //                {
    //                    Date = p.CreatedTime.ToString("yyyy-MM-dd"),
    //                    InTotal = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In, 1, 0)),//呼入总量
    //                    InConnectionQuantity = SqlFunc.AggregateSum(SqlFunc.IIF(p.OnState == EOnState.On && p.CallDirection == ECallDirection.In && p.AnsweredTime != null, 1, 0)),//呼入接通量
    //                    NotAcceptedHang = SqlFunc.AggregateSum(SqlFunc.IIF(p.Duration == 0 && p.RingTimes <= noConnectByeTimes && p.RingTimes > 0 && p.CallDirection == ECallDirection.In, 1, 0)), //未接通秒挂
    //                    TotalDurationIncomingCalls = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.AnsweredTime != null && p.OnState == EOnState.On, p.Duration, 0)), //呼入总时长
    //                    InAvailableAnswer = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.AnsweredTime != null && p.Duration >= effectiveTimes, 1, 0)),//有效接通量
    //                    InHangupImmediateWhenAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.Duration > 0 && p.Duration <= connectByeTimes, 1, 0)), //呼入接通秒挂
    //                    TimeoutConnection = SqlFunc.AggregateSum(SqlFunc.IIF(p.OnState == EOnState.On && p.CallDirection == ECallDirection.In && p.AnsweredTime != null && p.RingTimes >= CallInOverConnRingTime, 1, 0)),//超时接通量
    //                    TimeoutSuspension = SqlFunc.AggregateSum(SqlFunc.IIF(p.OnState == EOnState.On && p.CallDirection == ECallDirection.In && p.AnsweredTime != null && p.Duration >= SeatChaoTime, 1, 0)),//超时挂断量
    //                    QueueByeCount = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.QueueTims > 0 && p.RingTimes == 0 && p.OnState == EOnState.NoOn, 1, 0)), //队列挂断
    //                    IvrByeCount = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.In && p.BeginIvrTime.HasValue && !p.BeginQueueTime.HasValue && !p.BeginRingTime.HasValue && p.OnState == EOnState.NoOn, 1, 0)), //IVR挂断
    //                    OutTotal = SqlFunc.AggregateSum(SqlFunc.IIF(p.CallDirection == ECallDirection.Out, 1, 0)),//呼出总量
    //                    OutConnectionQuantity = SqlFunc.AggregateSum(SqlFunc.IIF(p.OnState == EOnState.On && p.CallDirection == ECallDirection.Out && p.AnsweredTime != null, 1, 0))
    //                })
    //                .OrderBy(p => p.Date)
    //                .ToListAsync();

    //        return callData;
    //    }

    //    /// <summary>
    //    /// 话务日期明细-呼入总量/接通总量
    //    /// </summary>
    //    /// <param name="dto"></param>
    //    /// <returns></returns>
    //    public ISugarQueryable<TrCallRecord> QueryCallsDetailInTotalAsync(BiQueryCallsDto dto)
    //    {
    //        if (!dto.StartTime.HasValue || !dto.EndTime.HasValue)
    //            throw UserFriendlyException.SameMessage("请选择时间!");

    //        return _trCallRecordRepository.Queryable()
    //                 .Includes(p => p.Order)
    //                .Where(p => p.CreatedTime >= dto.StartTime && p.CreatedTime <= dto.EndTime && p.CallDirection == ECallDirection.In)
    //                .WhereIF(dto.TypeCode == "2", p => p.OnState == EOnState.On && p.AnsweredTime != null)
    //                 // .Where(p => p.Gateway != "82826886" && SqlFunc.Length(p.Gateway) != 4)
    //                 .WhereIF(!string.IsNullOrEmpty(dto.Keyword), p => p.Gateway == dto.Keyword)
    //                 .OrderByDescending(p => p.CreatedTime)
    //                .MergeTable();
    //    }

    //    /// <summary>
    //    /// 坐席话务统计分析
    //    /// </summary>
    //    /// <param name="dto"></param>
    //    /// <returns></returns>
    //    public async Task<IList<BiSeatCallsDto>> QuerySeatCallAsync(ReportPagedRequest dto)
    //    {
    //        int noConnectByeTimes = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.NoConnectByeTimes)?.SettingValue[0]);
    //        int effectiveTimes = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.EffectiveTimes)?.SettingValue[0]);
    //        int connectByeTimes = int.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.ConnectByeTimes)?.SettingValue[0]);

    //        var list = await _userRepository.Queryable()
    //              .LeftJoin<TrCallRecord>((u, c) => u.Id == c.UserId)
    //              .Where(u => !u.IsDeleted && u.UserType == EUserType.Seat)
    //              .WhereIF(dto.StartTime.HasValue, (u, c) => c.CreatedTime >= dto.StartTime.Value)
    //              .WhereIF(dto.EndTime.HasValue, (u, c) => c.CreatedTime <= dto.EndTime.Value)
    //              .GroupBy((u, c) => new { c.UserName, c.UserId })
    //              .Select((u, c) => new BiSeatCallsDto
    //              {
    //                  Name = c.UserName,
    //                  UserId = c.UserId,
    //                  InTotal = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In, 1, 0)),
    //                  OutTotal = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.Out, 1, 0)),
    //                  InAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime != null, 1, 0)),
    //                  OutAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.Out && c.AnsweredTime != null, 1, 0)),
    //                  InHangupImmediate = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime == null && c.RingTimes < noConnectByeTimes, 1, 0)),
    //                  InHanguped = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime == null, 1, 0)),
    //                  InDurationAvg = SqlFunc.Ceil(SqlFunc.AggregateAvg(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime != null, c.Duration, 0))),
    //                  OutDurationAvg = SqlFunc.Ceil(SqlFunc.AggregateAvg(SqlFunc.IIF(c.CallDirection == ECallDirection.Out && c.AnsweredTime != null, c.Duration, 0))),
    //                  InAvailableAnswer = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime != null && c.Duration >= effectiveTimes, 1, 0)),
    //                  InHangupImmediateWhenAnswered = SqlFunc.AggregateSum(SqlFunc.IIF(c.CallDirection == ECallDirection.In && c.AnsweredTime != null && c.Duration < connectByeTimes, 1, 0)),
    //              })
    //              .MergeTable()
    //              .ToListAsync();


    //        list.ForEach(d =>
    //        {
    //            d.LoginDuration = _workRepository.Queryable().Where(q => q.UserId == d.UserId && q.CreationTime >= dto.StartTime && q.CreationTime <= dto.EndTime).Sum(q => q.WorkingDuration);
    //            if (d.LoginDuration != null)
    //            {
    //                d.LoginDuration = Math.Round(d.LoginDuration.Value, digits: 2);
    //            }
    //            d.RestDuration = _telRestRepository.Queryable().Where(q => q.UserId == d.UserId && q.CreationTime >= dto.StartTime && q.CreationTime <= dto.EndTime).Sum(q => q.RestDuration);
    //            d.RestDuration = Math.Round(d.RestDuration, digits: 2);
    //        });

    //        return list;
    //    }
    //}
//}
