using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using PARR.DAL.CacheServices; using PARR.DAL.Context; using PARR.DAL.Models; using PARR.DAL.Services.Abstracts; using PARR.DAL.Services.Interfaces; using PARR.DAL.Settings; namespace PARR.DAL.Services.Implementations { internal class WeekendDayService : BaseService, IWeekendDayService { private readonly DataContext dataContext; private readonly ILogger logger; private readonly IRedisCacheService redisCacheService; private readonly WeekendCacheSettings weekendCacheSettings; protected override DbSet EntitySet => dataContext.WeekendDays; protected override DataContext EntitiContext => dataContext; public WeekendDayService( DataContext dataContext, ILogger logger, IRedisCacheService redisCacheService, WeekendCacheSettings weekendCacheSettings ) : base(logger) { this.dataContext = dataContext; this.logger = logger; this.redisCacheService = redisCacheService; this.weekendCacheSettings = weekendCacheSettings; } public IQueryable GetWeekends(DateOnly start, DateOnly end) { return EntitySet.Where(t => t.Date >= start && t.Date <= end).Select(t => t.Date); } public async Task IsWorkDayAsync(DateOnly date, bool useCache = false) { if (useCache) { // смотрим, есть ли в кэше рабочий день var workdayInCache = await redisCacheService.GetCachedDataAsync(GetWorkDayKey(date)); if (workdayInCache != null) return true; // смотрим, есть ли в кэше выходной день var weekendInCache = await redisCacheService.GetCachedDataAsync(GetWeekendKey(date)); if (weekendInCache != null) return false; } var weekendInDb = await EntitySet.FirstOrDefaultAsync(t => t.Date == date); var isWorkday = weekendInDb == null; //сохраняем всегда в КЭШ значение выходного и рабочего дня var cacheKey = isWorkday ? GetWorkDayKey(date) : GetWeekendKey(date); //TODO:!!! Тут настройки вынести в DB settings await redisCacheService.SetCachedDataAsync(cacheKey, date, weekendCacheSettings.WeekendTtl); return isWorkday; } private string GetWeekendKey(DateOnly day) { return $"weekend_{day.ToString("yyyy-MM-dd")}"; } private string GetWorkDayKey(DateOnly day) { return $"workday_{day.ToString("yyyy-MM-dd")}"; } } }