feat(tempateMatcher,aihitLoader,dal): В UnitFilter добавлен Cache; в данные по ПТК добавлены аттрибуты "Холодный резерв" и "ДОПОЛНИТЕЛЬНАЯ_ИНФОРМАЦИЯ"; в синхронизаторах устанавливается неактуальная работа при деактивации

This commit is contained in:
Mikhail Kuznetsov
2025-12-25 21:16:20 +10:00
parent dbb7dee4b4
commit 2af8ed26d8
6 changed files with 154 additions and 32 deletions

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.DAL.CacheServices;
using PARR.DAL.Contracts;
using PARR.DAL.DomainServices.Interfaces;
using PARR.DAL.Models.Job;
@@ -15,6 +16,7 @@ namespace PARR.DAL.DomainServices.Implementations
private readonly IJobService jobService;
private readonly IUnitService unitService;
private readonly IUnitInUnitService unitInUnitService;
private readonly IRedisCacheService cacheService;
private readonly IUnitInValueService unitInValueService;
public UnitFilterService(
@@ -22,12 +24,15 @@ namespace PARR.DAL.DomainServices.Implementations
IJobService jobService,
IUnitService unitService,
IUnitInUnitService unitInUnitService,
IUnitInValueService unitInValueService)
IUnitInValueService unitInValueService,
IRedisCacheService cacheService
)
{
this.logger = logger;
this.jobService = jobService;
this.unitService = unitService;
this.unitInUnitService = unitInUnitService;
this.cacheService = cacheService;
this.unitInValueService = unitInValueService;
}
@@ -61,6 +66,15 @@ namespace PARR.DAL.DomainServices.Implementations
public List<RelatedUnitDto> Children { get; set; } = new();
}
public class CachedUnitIds
{
public List<Guid> UnitIds { get; set; } = new();
public DateTimeOffset Timestamp { get; set; }
public string? Source { get; set; } = "UnitFilterService";
public int Version { get; set; } = 1;
}
#endregion
public async Task<IEnumerable<Guid>?> GetUnitsIdByJobFilterAsync(Guid jobId, int? takeCount = null)
@@ -100,10 +114,7 @@ namespace PARR.DAL.DomainServices.Implementations
logger.LogDebug("Применяем фильтр #{Index} (Id={FilterId})", filterNumber, filter.Id);
// 1 Найти ID юнитов по UnitFilter (Name LIKE)
var initialUnitIds = await unitService.Get().AsNoTracking()
.Where(unit => EF.Functions.Like(unit.Name, filter.UnitFilter))
.Select(u => u.Id)
.ToListAsync();
var initialUnitIds = await GetUnitIdsFromCacheOrDbAsync(filter);
logger.LogDebug("Базовый фильтр по Name '{NameFilter}' дал {Count} юнитов", filter.UnitFilter, initialUnitIds.Count);
@@ -516,6 +527,46 @@ namespace PARR.DAL.DomainServices.Implementations
});
}
private async Task<List<Guid>> GetUnitIdsFromCacheOrDbAsync(JobUnitFilter filter)
{
var cacheKey = $"uf_ids_{ComputeHash(filter.UnitFilter)}";
var cachedData = await cacheService.GetCachedDataAsync<CachedUnitIds>(cacheKey);
if (cachedData != null)
{
logger.LogDebug("Кэш попал для UnitFilter '{Name}': {Count} юнитов", filter.UnitFilter, cachedData.UnitIds.Count);
return cachedData.UnitIds;
}
logger.LogDebug("Кэш промахнут для UnitFilter '{Name}'. Запрашиваем из БД.", filter.UnitFilter);
var initialUnitIds = await unitService.Get().AsNoTracking()
.Where(unit => EF.Functions.Like(unit.Name, filter.UnitFilter))
.Select(u => u.Id)
.ToListAsync();
logger.LogDebug("Загружено {Count} юнитов из БД для UnitFilter '{Name}'", initialUnitIds.Count, filter.UnitFilter);
var toCache = new CachedUnitIds
{
UnitIds = initialUnitIds,
Timestamp = DateTimeOffset.UtcNow,
Source = GetType().Name,
Version = 1
};
await cacheService.SetCachedDataAsync(cacheKey, toCache, TimeSpan.FromHours(1));
return initialUnitIds;
}
private static string ComputeHash(string input)
{
using var sha256 = System.Security.Cryptography.SHA256.Create();
var hashedBytes = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input));
return Convert.ToBase64String(hashedBytes).Replace('+', '-').Replace('/', '_').Substring(0, 16);
}
#endregion
}
}