using DocumentFormat.OpenXml.Drawing;
using Hotline.CallCenter.Calls;
using Hotline.DI;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace Hotline.Application.Bigscreen;

[Injection(AppScopes = EAppScope.YiBin)]
public class YiBinSeatStateDataService : SeatStateDataServiceBase, ISeatStateDataService, IScopeDependency
{
    private readonly IRepository<TrCallRecord> _callRepository;
    public YiBinSeatStateDataService(IRepository<TrCallRecord> callRepository, IRepository<CallNative> repository) : base(repository)
    {
        _callRepository = callRepository;
    }

    public override async Task<object> GetCall24(CancellationToken stoppingToken)
    {
        List<string> timeList = new List<string>();
        for (int i = 0;i < 24;i++)
        {
            var time = i < 10 ? $"0{i}" : i.ToString();
            timeList.Add(time);
        }
        var now = DateTime.Now.Date;
        var call24 = await _callRepository.Queryable()
            .Where(x => x.CreatedTime.Date == now
                && x.CallOrderType == Share.Enums.CallCenter.ECallOrderType.Order && x.CallDirection == Share.Enums.CallCenter.ECallDirection.In
            )
            .Select(x => new { time = x.CreatedTime.ToString("hh"), x.CallDirection }).MergeTable()
            .GroupBy(x => x.time)
            .Select(x => new
            {
                Time = x.time.ToString(),
                In = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In, 1, 0)),
                //Out = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)),
            }).MergeTable().ToListAsync(stoppingToken);

        var callOut24 = await _callRepository.Queryable()
            .Where(x => x.CreatedTime.Date == now && x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out)
            .Select(x => new { time = x.CreatedTime.ToString("hh"), x.CallDirection }).MergeTable()
            .GroupBy(x => x.time)
            .Select(x => new
            {
                Time = x.time.ToString(),
                Out = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)),
            }).MergeTable().ToListAsync(stoppingToken);

        var call24List = (from t1 in timeList
                          join t2 in call24 on t1 equals t2.Time into t1_t2
                          join t3 in callOut24 on t1 equals t3.Time into t1_t3
                          from item in t1_t3.DefaultIfEmpty()
                          select new
                          {
                              Time = t1 + ":00",
                              In = t1_t2.Select(x => x.In).FirstOrDefault(),
                              Out = t1_t3.Select(x => x.Out).FirstOrDefault()
                          }).ToList();
        return call24List;
    }

    public override async Task<object> GetCallTop10(CancellationToken stoppingToken)
    {

        var callTop10 = await _callRepository.Queryable()
            .Where(x => x.CreatedTime.Date == DateTime.Now.Date
                && x.CallOrderType == Share.Enums.CallCenter.ECallOrderType.Order
            )
            .GroupBy(x => x.UserName)
            .Select(x => new
            {
                UserName = x.UserName,
                In = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In, 1, 0))
            }).MergeTable().OrderByDescending(x => x.In).Take(10).ToListAsync(stoppingToken);
        return callTop10;
    }

    public override async Task<object> GetCallList(CancellationToken stoppingToken)
    {
        var callList = await _callRepository.Queryable()
            .Where(x => x.CreatedTime.Date == DateTime.Now.Date
                && x.CallOrderType == Share.Enums.CallCenter.ECallOrderType.Order
            )
            .Select(x => new
            {
                InOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && x.OnState == Share.Enums.CallCenter.EOnState.On, 1, 0)),
                ValidOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && x.OnState == Share.Enums.CallCenter.EOnState.On && x.Duration > 5, 1, 0)),
                InNoOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && x.OnState == Share.Enums.CallCenter.EOnState.NoOn, 1, 0)),
                OutOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out && x.OnState == Share.Enums.CallCenter.EOnState.On, 1, 0)),
                InQueueNoOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && x.OnState == Share.Enums.CallCenter.EOnState.NoOn && x.QueueTims > 0, 1, 0)),
            }).MergeTable().ToListAsync(stoppingToken);
        return callList;
    }

    public override async Task<object> GetCallAverage(CancellationToken stoppingToken)
    {
        List<string> timeList = new List<string>();
        for (int i = 0;i < 24;i++)
        {
            var time = i < 10 ? $"0{i}" : i.ToString();
            timeList.Add(time);
        }
        var callAverage = await _callRepository.Queryable()
            .Where(x => x.CreatedTime.Date == DateTime.Now.Date
                && x.CallOrderType == Share.Enums.CallCenter.ECallOrderType.Order
            )
            .Select(x => new { time = x.CreatedTime.ToString("hh"), x.CallDirection }).MergeTable()
            .GroupBy(x => x.time)
            .Select(x => new
            {
                Time = x.time.ToString(),
                In = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In, 1, 0)),
                InAverag = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In, 1, 0)) / 60,
                Out = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)),
                OutAverag = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)) / 60,
            }).MergeTable().ToListAsync(stoppingToken);
        var callAverageList = (from t1 in timeList
                               join t2 in callAverage on t1 equals t2.Time into t1_t2
                               from item in t1_t2.DefaultIfEmpty()
                               select new
                               {
                                   Time = t1 + ":00",
                                   In = t1_t2.Select(x => x.In).FirstOrDefault(),
                                   InAverag = t1_t2.Select(x => x.InAverag).FirstOrDefault(),
                                   Out = t1_t2.Select(x => x.Out).FirstOrDefault(),
                                   OutAverag = t1_t2.Select(x => x.OutAverag).FirstOrDefault()
                               }).ToList();
        return callAverageList;
    }
}
