//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using Hotline.Caching.Interfaces;
//using Hotline.CallCenter.Calls;
//using Hotline.Orders;
//using Hotline.Realtimes;
//using Hotline.Repository.SqlSugar.Orders;
//using Microsoft.AspNetCore.Http;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Hosting;
//using Microsoft.Extensions.Logging;
//using SqlSugar;
//using XF.Domain.Constants;
//using XF.Domain.Repository;

//namespace Hotline.Application.Bigscreen
//{
//    public class BigscreenDataShowRefreshService : BackgroundService
//    {
//        private readonly IServiceScopeFactory _serviceScopeFactory;
//        private DateTime _lastModifyTime;

//        public BigscreenDataShowRefreshService(IServiceScopeFactory serviceScopeFactory)
//        {
//            _serviceScopeFactory = serviceScopeFactory;
//            _lastModifyTime = DateTime.Now;
//        }

//        /// <summary>
//        /// This method is called when the <see cref="T:Microsoft.Extensions.Hosting.IHostedService" /> starts. The implementation should return a task that represents
//        /// the lifetime of the long running operation(s) being performed.
//        /// </summary>
//        /// <param name="stoppingToken">Triggered when <see cref="M:Microsoft.Extensions.Hosting.IHostedService.StopAsync(System.Threading.CancellationToken)" /> is called.</param>
//        /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> that represents the long running operations.</returns>
//        /// <remarks>See <see href="https://docs.microsoft.com/dotnet/core/extensions/workers">Worker Services in .NET</see> for implementation guidelines.</remarks>
//        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
//        {
//            using var scope = _serviceScopeFactory.CreateScope();
//            var realtimeService = scope.ServiceProvider.GetRequiredService<IRealtimeService>();
//            var orderRepository = scope.ServiceProvider.GetRequiredService<IOrderRepository>();
//            var callRepository = scope.ServiceProvider.GetRequiredService<IRepository<TrCallRecord>>();
//            var logger = scope.ServiceProvider.GetRequiredService<ILogger<BigscreenDataShowRefreshService>>();
//            var systemSettingCacheManager = scope.ServiceProvider.GetRequiredService<ISystemSettingCacheManager>();
//            int times = int.Parse(systemSettingCacheManager.GetSetting(SettingConstants.BsDataShowChangedTimes)?.SettingValue[0]);

//            while (!stoppingToken.IsCancellationRequested)
//            {
//                try
//                {

//                    var now = DateTime.Now;
//                    var today = now.Date;
//                    var month = now.Month;
//                    var secRange = (now - _lastModifyTime).TotalSeconds;


//                    if (secRange >= 10)
//                    {
//                        //area1
//                        var todayTotal = await orderRepository.CountAsync(d => d.CreationTime.Date == today, stoppingToken);
//                        var todayCompleted =
//                            await orderRepository.CountAsync(d => d.FiledTime.HasValue && d.FiledTime.Value.Date == today,
//                                stoppingToken);
//                        var monthTotal = await orderRepository.CountAsync(d => d.CreationTime.Month == month, stoppingToken);
//                        var monthCompleted = await orderRepository.CountAsync(d => d.FiledTime.HasValue && d.FiledTime.Value.Month == month, stoppingToken);
//                        await realtimeService.BsDataShowChanged1Async(new { todayTotal, todayCompleted, monthTotal, monthCompleted }, stoppingToken);

//                        //area3
//                        var calls = await callRepository.Queryable()
//                            .OrderByDescending(d => d.CreationTime)
//                            .Take(20)
//                            .Select(d => new { creationTime = d.CreatedTime, from = d.CPNName, to = d.CDPN, d.Duration })
//                            .ToListAsync(stoppingToken);
//                        await realtimeService.BsDataShowChanged3Async(calls, stoppingToken);

//                        //area8
//                        var orders = await orderRepository.Queryable(workflowFilter: false)
//                            .OrderByDescending(d => d.CreationTime)
//                            .Take(20)
//                            .Select(d => new { d.No, d.Title, d.Address, d.CreationTime, d.AcceptType })
//                            .ToListAsync(stoppingToken);
//                        await realtimeService.BsDataShowChanged8Async(orders, stoppingToken);

//                        logger.LogInformation($"send msg: {orders.First().Address}");
//                        _lastModifyTime = DateTime.Now;
//                    }

//                }
//                catch (Exception)
//                {
                    
//                }
//                await Task.Delay(times, stoppingToken);
//            }
//        }
//    }
//}
