using EasyCaching.Core;
using IRaCIS.Core.Infra.EFCore;
using Microsoft.Extensions.Logging;

namespace IRaCIS.Application.Services.BackGroundJob
{

    public interface ICacheTrialStatusJob
    {
        Task MemoryCacheTrialStatus();
    }
    public class CacheTrialStatusHangfireJob: ICacheTrialStatusJob
    {
        private readonly IRepository<Trial> _trialRepository;
        private readonly IEasyCachingProvider _provider;
        private readonly ILogger<CacheTrialStatusHangfireJob> _logger;

        public CacheTrialStatusHangfireJob(IRepository<Trial> trialRepository, IEasyCachingProvider provider,ILogger<CacheTrialStatusHangfireJob> logger)
        {
            _trialRepository = trialRepository;
            _provider = provider;
            _logger = logger;
        }
        public Task MemoryCacheTrialStatus()
        {
            _logger.LogInformation("hangfire 定时任务开始~");
            try
            {
                var list = _trialRepository.Select(t => new { TrialId = t.Id, TrialStatusStr = t.TrialStatusStr })
                    .ToList();
                //_provider.GetCount("");

                list.ForEach(t => _provider.Set(t.TrialId.ToString(), t.TrialStatusStr, TimeSpan.FromDays(7)));
            }
            catch (Exception e)
            {
                _logger.LogError("hangfire 定时任务执行失败"+e.Message);

            }
           
            _logger.LogInformation("hangfire 定时任务执行结束");

            return Task.CompletedTask;
        }
    }
}