84 lines
2.9 KiB
C#
84 lines
2.9 KiB
C#
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<WeekendDay>, IWeekendDayService
|
|
{
|
|
private readonly DataContext dataContext;
|
|
private readonly ILogger<WeekendDayService> logger;
|
|
private readonly IRedisCacheService redisCacheService;
|
|
private readonly WeekendCacheSettings weekendCacheSettings;
|
|
|
|
protected override DbSet<WeekendDay> EntitySet => dataContext.WeekendDays;
|
|
|
|
protected override DataContext EntitiContext => dataContext;
|
|
|
|
public WeekendDayService(
|
|
DataContext dataContext,
|
|
ILogger<WeekendDayService> logger,
|
|
IRedisCacheService redisCacheService,
|
|
WeekendCacheSettings weekendCacheSettings
|
|
) : base(logger)
|
|
{
|
|
this.dataContext = dataContext;
|
|
this.logger = logger;
|
|
this.redisCacheService = redisCacheService;
|
|
this.weekendCacheSettings = weekendCacheSettings;
|
|
}
|
|
|
|
|
|
public IQueryable<DateOnly> GetWeekends(DateOnly start, DateOnly end)
|
|
{
|
|
return EntitySet.Where(t => t.Date >= start && t.Date <= end).Select(t => t.Date);
|
|
}
|
|
|
|
|
|
|
|
public async Task<bool> IsWorkDayAsync(DateOnly date, bool useCache = false)
|
|
{
|
|
if (useCache)
|
|
{
|
|
// смотрим, есть ли в кэше рабочий день
|
|
var workdayInCache = await redisCacheService.GetCachedDataAsync<DateOnly?>(GetWorkDayKey(date));
|
|
if (workdayInCache != null)
|
|
return true;
|
|
|
|
// смотрим, есть ли в кэше выходной день
|
|
var weekendInCache = await redisCacheService.GetCachedDataAsync<DateOnly?>(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")}";
|
|
}
|
|
|
|
}
|
|
}
|