using DataSharing.Province.SendTask.ProvinceOther;
using DataSharing.Province.SendTask.SubmitCaseRecord;
using DataSharing.SendTask;
using DataSharing.Share.Enums;
using Microsoft.Extensions.Logging;
using Quartz;
using XF.Domain.Repository;

namespace DataSharing.Province.SendTask
{
    public class TaskSendCallJob : IJob, IDisposable
    {
        private readonly ILogger<TaskSendCallJob> _logger;
        private readonly IRepository<DsSendTaskSubmitCaseRecord> _sendTaskSubmitCaseRecordRepository;
        private readonly IRepository<DsSendCall> _dsSendCallRepository;
        private readonly IInitPushDataService _initPushDataService;
        private readonly IChannelConfigurationManager _channelConfigurationManager;

        public TaskSendCallJob(ILogger<TaskSendCallJob> logger,
                IRepository<DsSendTaskSubmitCaseRecord> sendTaskSubmitCaseRecordRepository,
            IRepository<DsSendCall> dsSendCallRepository,
             IInitPushDataService initPushDataService,
             IChannelConfigurationManager channelConfigurationManager)
        {
            _logger = logger;
            _sendTaskSubmitCaseRecordRepository = sendTaskSubmitCaseRecordRepository;
            _dsSendCallRepository = dsSendCallRepository;
            _initPushDataService = initPushDataService;
            _channelConfigurationManager = channelConfigurationManager;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            if (_channelConfigurationManager.GetConfigurationIsOpenPush())
            {
                Random random = new Random();
                int randomNumber = random.Next(0, 2); // 生成0到99之间的整数
                //读取数据
                var dataSendList = await _dsSendCallRepository.Queryable()
                        .Where(d => d.State == EWaitSendTaskState.WaitPush && d.SendTimes <= 6)
                        .OrderByIF(randomNumber == 0, d => d.Id, SqlSugar.OrderByType.Asc)
                        .OrderByIF(randomNumber == 1, d => d.Id, SqlSugar.OrderByType.Desc)
                        .Take(30)
                        .ToListAsync(context.CancellationToken);
                _logger.LogWarning("再次推送读取条数--------------------------------------" + dataSendList.Count);
                //数据不为空
                if (dataSendList != null && dataSendList.Any())
                {
                    List<DsSendCall> sendList = new List<DsSendCall>();
                    foreach (var dataSend in dataSendList)
                    {
                        //锁定数据
                        dataSend.State = EWaitSendTaskState.Pushing;
                        if (await _dsSendCallRepository.Updateable(dataSend).ExecuteCommandWithOptLockAsync() > 0)
                        {
                            sendList.Add(dataSend);
                        }

                    }
                    if (sendList != null && sendList.Any())
                    {
                        _logger.LogWarning("再次推送添加条数--------------------------------------" + sendList.Count);
                        //添加待推送表数据
                        foreach (var item in sendList)
                        {
                            var data = await _sendTaskSubmitCaseRecordRepository.GetAsync(item.OldDataId, context.CancellationToken);
                            if (data != null)
                            {
                                var id = await _initPushDataService.InitDsSendTaskSubmitCaseRecord(data.ProvinceNo, data.CallId, data.CallLogId, data.Request, data.RequestData, data.GenerationTime, context.CancellationToken);
                                if (!string.IsNullOrEmpty(id))
                                    item.State = EWaitSendTaskState.StopPush;
                                else
                                    item.State = EWaitSendTaskState.WaitPush;
                                item.SendTimes = item.SendTimes + 1;
                                await _dsSendCallRepository.UpdateAsync(item, context.CancellationToken);
                            }
                        }
                    }
                }
            }
        }


        public void Dispose()
        {
        }
    }
}
