2024-07-01 11:32:19 +10:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2026-01-15 15:01:56 +10:00
|
|
|
|
using PARR.DAL.Cache.Services.Base;
|
2024-07-01 11:32:19 +10:00
|
|
|
|
using PARR.DAL.Context;
|
2024-09-17 11:55:55 +10:00
|
|
|
|
using PARR.DAL.Contracts;
|
2024-07-01 11:32:19 +10:00
|
|
|
|
using PARR.DAL.Models;
|
|
|
|
|
|
using PARR.DAL.Services.Abstracts;
|
|
|
|
|
|
using PARR.DAL.Services.Interfaces;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PARR.DAL.Services.Implementations
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class WeekendDayService : BaseService<WeekendDay>, IWeekendDayService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly DataContext dataContext;
|
|
|
|
|
|
private readonly ILogger<WeekendDayService> logger;
|
2024-09-16 17:01:04 +10:00
|
|
|
|
private readonly IRedisCacheService redisCacheService;
|
2024-09-17 11:55:55 +10:00
|
|
|
|
private readonly SettingsFromDb settings;
|
2024-07-01 11:32:19 +10:00
|
|
|
|
|
|
|
|
|
|
protected override DbSet<WeekendDay> EntitySet => dataContext.WeekendDays;
|
|
|
|
|
|
|
|
|
|
|
|
protected override DataContext EntitiContext => dataContext;
|
|
|
|
|
|
|
2024-09-16 17:01:04 +10:00
|
|
|
|
public WeekendDayService(
|
|
|
|
|
|
DataContext dataContext,
|
|
|
|
|
|
ILogger<WeekendDayService> logger,
|
|
|
|
|
|
IRedisCacheService redisCacheService,
|
2024-09-17 11:55:55 +10:00
|
|
|
|
SettingsFromDb settings
|
2024-09-16 17:01:04 +10:00
|
|
|
|
) : base(logger)
|
2024-07-01 11:32:19 +10:00
|
|
|
|
{
|
|
|
|
|
|
this.dataContext = dataContext;
|
|
|
|
|
|
this.logger = logger;
|
2024-09-16 17:01:04 +10:00
|
|
|
|
this.redisCacheService = redisCacheService;
|
2024-09-17 11:55:55 +10:00
|
|
|
|
this.settings = settings;
|
2024-07-01 11:32:19 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IQueryable<DateOnly> GetWeekends(DateOnly start, DateOnly end)
|
|
|
|
|
|
{
|
2025-12-22 17:03:41 +10:00
|
|
|
|
return EntitySet.Where(t => t.Date >= start && t.Date <= end).AsNoTracking().Select(t => t.Date);
|
2024-07-01 11:32:19 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 17:01:04 +10:00
|
|
|
|
|
|
|
|
|
|
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);
|
2024-09-17 11:55:55 +10:00
|
|
|
|
await redisCacheService.SetCachedDataAsync(cacheKey, date, settings.WeekendCacheTtl);
|
2024-09-16 17:01:04 +10:00
|
|
|
|
|
|
|
|
|
|
return isWorkday;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-17 11:55:55 +10:00
|
|
|
|
public override async Task<bool> CreateAsync(WeekendDay obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
// При создании выходного дня, смотрим, был ли он в кэше, если был, удаляем
|
|
|
|
|
|
await redisCacheService.DeleteCachedDataAsync(GetWeekendKey(obj.Date));
|
|
|
|
|
|
await redisCacheService.DeleteCachedDataAsync(GetWorkDayKey(obj.Date));
|
|
|
|
|
|
|
|
|
|
|
|
return await base.CreateAsync(obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override bool Delete(WeekendDay obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
// При создании выходного дня, смотрим, был ли он в кэше, если был, удаляем
|
|
|
|
|
|
redisCacheService.DeleteCachedData(GetWeekendKey(obj.Date));
|
|
|
|
|
|
redisCacheService.DeleteCachedData(GetWorkDayKey(obj.Date));
|
|
|
|
|
|
|
|
|
|
|
|
return base.Delete(obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task<bool> DeleteAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var exist = await GetAsync(id);
|
|
|
|
|
|
if (exist == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError($"Ошибка при удалении из БД. Не найдена запись в БД с id: {id}");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Delete(exist);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-16 17:01:04 +10:00
|
|
|
|
private string GetWeekendKey(DateOnly day)
|
|
|
|
|
|
{
|
|
|
|
|
|
return $"weekend_{day.ToString("yyyy-MM-dd")}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string GetWorkDayKey(DateOnly day)
|
|
|
|
|
|
{
|
|
|
|
|
|
return $"workday_{day.ToString("yyyy-MM-dd")}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-01 11:32:19 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|