using DataSharing.FwDataExchange;
using DataSharing.SendTask;
using DataSharing.Share.Consts;
using DataSharing.Share.Dtos.HotlineSetting;
using DataSharing.Share.Dtos.HotlineWeb;
using DataSharing.Share.Dtos.Province.HuiJu;
using DataSharing.Share.Enums;
using Microsoft.Extensions.Logging;
using Quartz;
using System.Text.Json;
using XF.Domain.Cache;

namespace DataSharing.Province.SendTask
{
    /// <summary>
    /// 
    /// </summary>
    public class TaskSubmitCaseTotalJob : IJob, IDisposable
    {
        private readonly ILogger<TaskSubmitCaseTotalJob> _logger;
        private readonly FwClient _fwClient;
        private readonly ITypedCache<ConfigurationInformationDto> _configurationInformationCache;
        private readonly IChannelConfigurationManager _channelConfigurationManager;
        private readonly ISharingConfigurationManager _sharingConfigurationManager;
        private readonly IInitPushDataService _initPushDataService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="fwClient"></param>
        /// <param name="configurationInformationCache"></param>
        /// <param name="channelConfigurationManager"></param>
        /// <param name="dsSendTaskRepository"></param>
        /// <param name="sharingConfigurationManager"></param>
        public TaskSubmitCaseTotalJob(ILogger<TaskSubmitCaseTotalJob> logger, FwClient fwClient,
              ITypedCache<ConfigurationInformationDto> configurationInformationCache,
              IChannelConfigurationManager channelConfigurationManager,
              ISharingConfigurationManager sharingConfigurationManager,
              IInitPushDataService initPushDataService)
        {
            _logger = logger;
            _fwClient = fwClient;
            _configurationInformationCache = configurationInformationCache;
            _channelConfigurationManager = channelConfigurationManager;
            _sharingConfigurationManager = sharingConfigurationManager;
            _initPushDataService = initPushDataService;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            _logger.LogInformation($"数据汇聚，进来了时间是{DateTime.Now}");

            var data = await _fwClient.RequestNoTokenAsync<SubmitCaseTotalnfoResponse>("api/v1/Order/get-orderurge-count", "Get", null);
            SubmitCaseTotalInfo submitCaseTotal = new()
            {
                AreaCode = _sharingConfigurationManager.GetCityCode(),
                SubmitDate = Convert.ToDateTime(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00"))
            };
            if (data != null && data.code == 0)
            {
                submitCaseTotal.CBZL = data.result.Totle;
            }
            var configurationInformation = await _configurationInformationCache.GetAsync(ConstantSettings.ConfigurationInformationCacheKey, context.CancellationToken);
            if (configurationInformation != null)
            {
                submitCaseTotal.SeatNum = configurationInformation.SeatNum;
                submitCaseTotal.SeatHwyNum = configurationInformation.SeatHwyNum;
            }
            #region 处理请求参数
            var configProvince = _channelConfigurationManager.GetConfigurationProvince();
            var request = new ProvinceListRequest<SubmitCaseTotalInfo>(configProvince.ClientId, configProvince.ClientSecret);
            request.SetData(new List<SubmitCaseTotalInfo>() { submitCaseTotal });
            var jsonData = request.ToString();

            #endregion

            //组装参数
            await _initPushDataService.InitDsSendTaskProvinceOther("SubmitCaseTotal", "submit_case_total", "", jsonData,
                JsonSerializer.Serialize(submitCaseTotal, ProvinceJsonSerializerOptions.UnStandardDatetimeJsonOptions),
                pathType: EPathType.HuiJu, cancellationToken: context.CancellationToken);
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {

        }
    }
}
