forked from ddrilling/AsbCloudServer
fix WellOperationRepository.GetSectionsAsync данные из БД запрашиваются одним запросом
This commit is contained in:
parent
93b3c47053
commit
9dd530a9fb
@ -20,6 +20,7 @@ namespace AsbCloudInfrastructure.Repository;
|
|||||||
public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto, WellOperation>,
|
public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto, WellOperation>,
|
||||||
IWellOperationRepository
|
IWellOperationRepository
|
||||||
{
|
{
|
||||||
|
private const string keyCacheTemplate = "OperationsBySectionSummaries_{0}";
|
||||||
private const string cacheKeyWellOperations = "FirstAndLastFactWellsOperations";
|
private const string cacheKeyWellOperations = "FirstAndLastFactWellsOperations";
|
||||||
private readonly IMemoryCache memoryCache;
|
private readonly IMemoryCache memoryCache;
|
||||||
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
|
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
|
||||||
@ -109,61 +110,79 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto,
|
|||||||
|
|
||||||
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
|
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
|
||||||
{
|
{
|
||||||
const string keyCacheTemplate = "OperationsBySectionSummaries_{0}";
|
|
||||||
|
|
||||||
var result = new List<SectionByOperationsDto>();
|
var result = new List<SectionByOperationsDto>();
|
||||||
|
var notFoundIds = new List<int>();
|
||||||
|
|
||||||
foreach (var idWell in idsWells)
|
foreach (var idWell in idsWells)
|
||||||
{
|
{
|
||||||
var sections = await memoryCache.GetOrCreateAsync(string.Format(keyCacheTemplate, idWell), async (entry) =>
|
var cacheKey = string.Format(keyCacheTemplate, idWell);
|
||||||
|
if (memoryCache.TryGetValue<IEnumerable<SectionByOperationsDto>>(cacheKey, out var section))
|
||||||
{
|
{
|
||||||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
|
result.AddRange(section!);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
notFoundIds.Add(idWell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var query = dbContext.Set<WellOperation>()
|
if (notFoundIds.Count != 0)
|
||||||
.Where(x => x.IdWell == idWell)
|
{
|
||||||
.GroupBy(operation => new
|
var query = dbContext.Set<WellOperation>()
|
||||||
{
|
.Where(operation => notFoundIds.Contains( operation.IdWell))
|
||||||
operation.IdType, operation.IdWellSectionType, operation.WellSectionType.Caption,
|
.GroupBy(operation => new
|
||||||
})
|
{
|
||||||
.Select(group => new
|
operation.IdWell,
|
||||||
{
|
operation.IdType,
|
||||||
group.Key.IdType,
|
operation.IdWellSectionType,
|
||||||
group.Key.IdWellSectionType,
|
operation.WellSectionType.Caption,
|
||||||
group.Key.Caption,
|
})
|
||||||
First = group
|
.Select(group => new
|
||||||
|
{
|
||||||
|
group.Key.IdWell,
|
||||||
|
group.Key.IdType,
|
||||||
|
group.Key.IdWellSectionType,
|
||||||
|
group.Key.Caption,
|
||||||
|
First = group
|
||||||
.OrderBy(operation => operation.DateStart)
|
.OrderBy(operation => operation.DateStart)
|
||||||
.Select(operation => new { operation.DateStart, operation.DepthStart, })
|
.Select(operation => new { operation.DateStart, operation.DepthStart, })
|
||||||
.First(),
|
.First(),
|
||||||
Last = group
|
Last = group
|
||||||
.OrderByDescending(operation => operation.DateStart)
|
.OrderByDescending(operation => operation.DateStart)
|
||||||
.Select(operation => new
|
.Select(operation => new
|
||||||
{
|
{
|
||||||
operation.DateStart, operation.DurationHours, operation.DepthEnd,
|
operation.DateStart,
|
||||||
|
operation.DurationHours,
|
||||||
|
operation.DepthEnd,
|
||||||
})
|
})
|
||||||
.First(),
|
.First(),
|
||||||
});
|
});
|
||||||
|
|
||||||
var dbData = await query.ToArrayAsync(token);
|
var entities = await query.ToArrayAsync(token);
|
||||||
|
var dtos = entities.Select(
|
||||||
var sections = dbData.Select(
|
entity => new SectionByOperationsDto
|
||||||
item => new SectionByOperationsDto
|
|
||||||
{
|
{
|
||||||
IdWell = idWell,
|
IdWell = entity.IdWell,
|
||||||
IdType = item.IdType,
|
IdType = entity.IdType,
|
||||||
IdWellSectionType = item.IdWellSectionType,
|
IdWellSectionType = entity.IdWellSectionType,
|
||||||
Caption = item.Caption,
|
Caption = entity.Caption,
|
||||||
DateStart = item.First.DateStart,
|
DateStart = entity.First.DateStart,
|
||||||
DepthStart = item.First.DepthStart,
|
DepthStart = entity.First.DepthStart,
|
||||||
DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours),
|
DateEnd = entity.Last.DateStart.AddHours(entity.Last.DurationHours),
|
||||||
DepthEnd = item.Last.DepthEnd,
|
DepthEnd = entity.Last.DepthEnd,
|
||||||
});
|
})
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
result.AddRange(dtos);
|
||||||
|
|
||||||
entry.Value = sections;
|
var groupedByWellDtos = dtos
|
||||||
return sections;
|
.GroupBy(dto => dto.IdWell);
|
||||||
});
|
|
||||||
|
|
||||||
result.AddRange(sections!);
|
foreach (var group in groupedByWellDtos)
|
||||||
|
{
|
||||||
|
var cacheKey = string.Format(keyCacheTemplate, group.Key);
|
||||||
|
memoryCache.Set(cacheKey, group.AsEnumerable(), TimeSpan.FromMinutes(30));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
Loading…
Reference in New Issue
Block a user