
//using Hotline.Ai.Visit;
//using Hotline.Orders;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Hosting;
//using Org.BouncyCastle.Utilities;
//using XF.Domain.Repository;

//namespace Hotline.Application.Visit
//{
//    public class AiOrderVisitService : BackgroundService
//    {
//        private readonly IServiceScopeFactory _serviceScopeFactory;

//        public AiOrderVisitService(IServiceScopeFactory serviceScopeFactory)
//        {
//            _serviceScopeFactory = serviceScopeFactory;
//        }

//        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
//        {
//            using var scope = _serviceScopeFactory.CreateScope();
//            var _aivisitService = scope.ServiceProvider.GetRequiredService<IAiVisitService>();
//            var _aivisitRepository = scope.ServiceProvider.GetRequiredService<IRepository<AiOrderVisit>>();
//            var _aiVisitDetailRepository = scope.ServiceProvider.GetRequiredService<IRepository<AiOrderVisitDetail>>();
//            var _orderVisitRepository = scope.ServiceProvider.GetRequiredService<IRepository<OrderVisit>>();

//            int times = 300000;
//            while (!stoppingToken.IsCancellationRequested)
//            {
//                try
//                {
//                    var aivisitList = await _aivisitRepository.Queryable()
//                    .Includes(x => x.AiOrderVisitDetails,d=>d.OrderVisit).Where(x=>x.TaskState == Share.Enums.Ai.EAiOrderVisitTaskState.InProgress).ToListAsync();
//                    if (aivisitList != null)
//                    {
//                        foreach (var aivisit in aivisitList)
//                        {
//                            foreach (var aivisitDetail in aivisit.AiOrderVisitDetails)
//                            {
//                                if (!string.IsNullOrEmpty(aivisit.BatchUid) && !string.IsNullOrEmpty(aivisitDetail.TaskUid))
//                                {
//                                    var res = await _aivisitService.QueryAiVisitTask(aivisit.BatchUid, aivisitDetail.TaskUid, stoppingToken);
//                                    if(res!=null  && res.Status == 4)
//                                    {
//                                        aivisitDetail.AiOrderVisitState = Share.Enums.Ai.EAiOrderVisitState.LoseEfficacy;
//                                        await _aiVisitDetailRepository.UpdateAsync(aivisitDetail,stoppingToken);
//                                        aivisitDetail.OrderVisit.VisitState = Share.Enums.Order.EVisitState.WaitForVisit;
//                                        aivisitDetail.OrderVisit.IsCanAiVisit = true;
//                                        await _orderVisitRepository.UpdateAsync(aivisitDetail.OrderVisit,stoppingToken);
//                                        aivisit.VisitedFailCount++;
//                                        if ((aivisit.VisitedFailCount + aivisit.VisitedCount) == aivisit.HasVisitCount)
//                                        {
//                                            aivisit.TaskState = Share.Enums.Ai.EAiOrderVisitTaskState.Ended;
//                                        }
//                                        await _aivisitRepository.UpdateAsync(aivisit,stoppingToken);
//                                    }
//                                }

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