using Hotline.Caching.Interfaces;
using Hotline.Orders;
using Hotline.Repository.SqlSugar.Orders;
using Hotline.Settings;
using Hotline.Share.Enums.Order;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using XF.Domain.Repository;

namespace Hotline.Api.Controllers
{
    public class BsController : BaseController
    {
        private readonly IOrderRepository _orderRepository;
        private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;
        private readonly IRepository<SystemArea> _areaRepository;

        public BsController(
            IOrderRepository orderRepository,
            ISystemDicDataCacheManager systemDicDataCacheManager,
            IRepository<SystemArea> areaRepository)
        {
            _orderRepository = orderRepository;
            _systemDicDataCacheManager = systemDicDataCacheManager;
            _areaRepository = areaRepository;
        }

        [AllowAnonymous]
        [HttpGet("datashow")]
        public async Task<dynamic> InitDataShowAsync()
        {
            var now = DateTime.Now;
            var month = now.Month;
            var day = now.Day;

            //area2
            //var area2 = await _orderRepository.Queryable(workflowFilter: false)
            //    //.Where(d => d.CreationTime.Month == now.Month)
            //    .GroupBy(d => d.SourceChannel)
            //    .Select(d => new
            //    {
            //        Source = d.SourceChannel,
            //        Count = SqlFunc.AggregateCount(d.SourceChannel)
            //    })
            //    .ToListAsync(HttpContext.RequestAborted);
            var area2 = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.SourceChannel)
                .Select(d => new
                {
                    d.DicDataName,
                    Count = d.DicDataValue == "RGDH" ? Random.Shared.Next(2000, 3000) : Random.Shared.Next(10, 50)
                })
                .ToList();


            //area4
            var areas = await _areaRepository.Queryable()
                .Where(d => d.AreaId != "511500" && d.AreaId.StartsWith("5115"))
                .ToListAsync(HttpContext.RequestAborted);
            var area4 = areas.Select(d => new
            {
                d.AreaName,
                Total = Random.Shared.Next(500, 1500),
                Completed = Random.Shared.Next(100, 500)
            })
                .ToList();

            //var area4 = await _orderRepository.Queryable(workflowFilter: false)
            //    .Where(d => d.AreaCode != "511500" && d.AreaCode.StartsWith("5115"))
            //    .GroupBy(d => new { d.AreaCode, d.Address })
            //    .Select(d => new
            //    {
            //        d.Address,
            //        Count = SqlFunc.AggregateCount(d.AreaCode)
            //    })
            //    .ToListAsync(HttpContext.RequestAborted);



            //area5
            //var acceptTypes = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.AcceptType)
            //    .OrderBy(d => d.DicDataName).ToList();
            //var completed = await _orderRepository.Queryable(workflowFilter: false)
            //    .Where(d => d.Status == EOrderStatus.Filed)
            //    .GroupBy(d => d.AcceptType)
            //    .Select(d => new
            //    {
            //        d.AcceptType,
            //        Count = SqlFunc.AggregateCount(d.AcceptType)
            //    })
            //    .ToListAsync(HttpContext.RequestAborted);

            //var uncompleted = await _orderRepository.Queryable(workflowFilter: false)
            //    .Where(d => d.Status != EOrderStatus.Filed)
            //    .GroupBy(d => d.AcceptType)
            //    .Select(d => new
            //    {
            //        d.AcceptType,
            //        Count = SqlFunc.AggregateCount(d.AcceptType)
            //    })
            //    .ToListAsync(HttpContext.RequestAborted);

            var area5 = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.AcceptType)
                .Select(d => new
                {
                    AcceptType = d.DicDataName,
                    Completed = Random.Shared.Next(1, 200),
                    Uncompleted = Random.Shared.Next(1, 100)
                }).ToList();

            //area6
            //var area6 = await _orderRepository.Queryable(workflowFilter: false)
            //    .GroupBy(d => d.CreationTime.Date)
            //    .Select(d => new
            //    {
            //        d.CreationTime.Date,
            //        Count = SqlFunc.AggregateCount(d.CreationTime.Date)
            //    })
            //    .ToListAsync(HttpContext.RequestAborted);

            //var query6 = await _orderRepository.Queryable(workflowFilter: false)
            //    .Select(it => new
            //    {
            //        Id = it.Id,
            //        CreateTime = it.CreationTime.Date//只取日期
            //    })
            //    .MergeTable()//将查询结果转成一个表
            //    .GroupBy(it => it.CreateTime)
            //    .Select(it => new { Count = SqlFunc.AggregateCount(it.Id), it.CreateTime })
            //    .ToListAsync(HttpContext.RequestAborted);

            //var area6 = query6.OrderByDescending(d => d.CreateTime).Take(6).ToList();
            //var date6 = new List<DateTime>();
            //for (int i = 0; i < 6; i++)
            //{
            //    date6.Add(DateTime.Now.AddDays(-i).Date);
            //}

            //date6.Reverse();
            



            //area7
            var query7 = await _orderRepository.Queryable(canView: false)
                .GroupBy(d => d.HotspotName)
                .Select(d => new
                {
                    d.HotspotName,
                    Count = SqlFunc.AggregateCount(d.HotspotName)
                })
                .ToListAsync(HttpContext.RequestAborted);
            var area7 = query7.OrderByDescending(d => d.Count).Take(8).ToList();

            return new { area2, area4, area5, area7 };
        }
    }
}
