using DataSharing.CityStateDataExchange;
using DataSharing.LuZhou.LZCityPortal;
using DataSharing.SendTask.OtherPlatforms;
using DataSharing.Share.Enums;
using DataSharing.TianQue;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using Quartz;
using XF.Domain.Repository;

namespace DataSharing.SendTask
{
    /// <summary>
    /// 
    /// </summary>
    public class TaskOtherPlatformsJob : IJob, IDisposable
    {
        private readonly IMapper _mapper;
        private readonly ILogger<TaskOtherPlatformsJob> _logger;
        private readonly IRepository<DsWaitSendTaskOtherPlatforms> _waitSendTaskOtherPlatformsRepository;
        private readonly ISendTaskDataService _sendTaskDataService;
        private readonly ICityStateDataExchangePusherProviderService _dataExchangePusherProviderService;
        private readonly ITianQuePusherProviderService _tianQuePusherProviderService;
        private readonly ILZCityPortalSitePusherProviderService _lzCityPortalSitePusherProviderService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="waitSendTaskOtherPlatformsRepository"></param>
        /// <param name="sendTaskDataService"></param>
        /// <param name="dataExchangePusherProviderService"></param>
        /// <param name="tianQuePusherProviderService"></param>
        public TaskOtherPlatformsJob(IMapper mapper,
            ILogger<TaskOtherPlatformsJob> logger,
            IRepository<DsWaitSendTaskOtherPlatforms> waitSendTaskOtherPlatformsRepository,
            ISendTaskDataService sendTaskDataService,
            ICityStateDataExchangePusherProviderService dataExchangePusherProviderService,
            ITianQuePusherProviderService tianQuePusherProviderService,
            ILZCityPortalSitePusherProviderService lzCityPortalSitePusherProviderService)
        {
            _mapper = mapper;
            _logger = logger;
            _waitSendTaskOtherPlatformsRepository = waitSendTaskOtherPlatformsRepository;
            _sendTaskDataService = sendTaskDataService;
            _dataExchangePusherProviderService = dataExchangePusherProviderService;
            _tianQuePusherProviderService = tianQuePusherProviderService;
            _lzCityPortalSitePusherProviderService = lzCityPortalSitePusherProviderService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Execute(IJobExecutionContext context)
        {
            //加载数据
            var tasks = await _waitSendTaskOtherPlatformsRepository.Queryable()
              .Where(d => d.State == EWaitSendTaskState.WaitPush && d.SendTimes <= 6)
               .OrderBy(d => d.CreationTime)
               .Take(10)
               .ToListAsync(context.CancellationToken);
            _logger.LogInformation("其他平台读取数据条数--------------------------------------" + tasks.Count);
            if (tasks.Count != 0)
            {
                //处理推送数据
                foreach (var sendTask in tasks)
                {
                    //状态更改为推送中
                    sendTask.State = EWaitSendTaskState.Pushing;
                    //使用乐观锁，控制数据
                    if (await _waitSendTaskOtherPlatformsRepository.Updateable(sendTask).ExecuteCommandWithOptLockAsync() > 0)
                    {
                        //如果是市州互转直接调用
                        if (sendTask.PlatformSource == Share.Enums.EPlatformSource.CityDataExchange)
                        {
                            await _dataExchangePusherProviderService.SendDataExchangeDataPusher(sendTask, context.CancellationToken);
                        } //如果是推随手拍天阙直接调用
                        //if (sendTask.PlatformSource == Share.Enums.EPlatformSource.TianQue)
                        //{
                        //    await _tianQuePusherProviderService.SendDataPusher(sendTask, context.CancellationToken);
                        //}
                        // 泸州市政府门户网站
                        else if (sendTask.PlatformSource == Share.Enums.EPlatformSource.LZCityPortalSite)
                        {
                            await _lzCityPortalSitePusherProviderService.SendDataOpinionDataPusher(sendTask, context.CancellationToken);
                        }
                        else
                            await _sendTaskDataService.SendTask(sendTask, context.CancellationToken);//其他推送

                    }
                }
            }
        }

        public void Dispose()
        {

        }
    }
}
