Получение секций и категорий через Lazy + копирующий конструктор внутри WellOperationRequestBase

This commit is contained in:
Olga Nemt 2024-04-22 15:31:04 +05:00
parent 0ba1b5c5fc
commit 9926d3fcf2
2 changed files with 55 additions and 41 deletions

View File

@ -47,6 +47,34 @@ public class WellOperationRequestBase : RequestBase
/// Идентификаторы конструкций секции /// Идентификаторы конструкций секции
/// </summary> /// </summary>
public IEnumerable<int>? SectionTypeIds { get; set; } public IEnumerable<int>? SectionTypeIds { get; set; }
/// <summary>
///
/// </summary>
public WellOperationRequestBase()
{
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
public WellOperationRequestBase(WellOperationRequestBase request)
{
GeDepth = request.GeDepth;
LeDepth = request.LeDepth;
GeDate = request.GeDate;
LeDate = request.LeDate;
OperationCategoryIds = request.OperationCategoryIds;
OperationType = request.OperationType;
SectionTypeIds = request.SectionTypeIds;
Skip = request.Skip;
Take = request.Take;
SortFields = request.SortFields;
}
} }
/// <summary> /// <summary>
@ -62,20 +90,9 @@ public class WellOperationRequest : WellOperationRequestBase
/// <inheritdoc /> /// <inheritdoc />
public WellOperationRequest(WellOperationRequestBase request, IEnumerable<int> idsWell) public WellOperationRequest(WellOperationRequestBase request, IEnumerable<int> idsWell)
: this(idsWell) : base(request)
{ {
GeDepth = request.GeDepth; IdsWell = idsWell;
LeDepth = request.LeDepth;
GeDate = request.GeDate;
LeDate = request.LeDate;
OperationCategoryIds = request.OperationCategoryIds;
OperationType = request.OperationType;
SectionTypeIds = request.SectionTypeIds;
Skip = request.Skip;
Take = request.Take;
SortFields = request.SortFields;
} }
/// <summary> /// <summary>

View File

@ -23,6 +23,8 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
private readonly IMemoryCache memoryCache; private readonly IMemoryCache memoryCache;
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository; private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
private readonly IWellService wellService; private readonly IWellService wellService;
private Lazy<IDictionary<int, WellOperationCategoryDto>> LazyWellCategories { get; }
private Lazy<IDictionary<int, WellSectionTypeDto>> LazyWellSectionTypes { get; }
public WellOperationRepository(IAsbCloudDbContext context, public WellOperationRepository(IAsbCloudDbContext context,
IMemoryCache memoryCache, IMemoryCache memoryCache,
@ -33,6 +35,9 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
this.memoryCache = memoryCache; this.memoryCache = memoryCache;
this.wellOperationCategoryRepository = wellOperationCategoryRepository; this.wellOperationCategoryRepository = wellOperationCategoryRepository;
this.wellService = wellService; this.wellService = wellService;
LazyWellCategories = new(() => wellOperationCategoryRepository.Get(true, false).ToDictionary(c => c.Id));
LazyWellSectionTypes = new(() => GetSectionTypes().ToDictionary(c => c.Id));
} }
public IEnumerable<WellSectionTypeDto> GetSectionTypes() => public IEnumerable<WellSectionTypeDto> GetSectionTypes() =>
@ -43,7 +48,7 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
public async Task<IEnumerable<WellOperationDto>> GetAsync(WellOperationRequest request, CancellationToken token) public async Task<IEnumerable<WellOperationDto>> GetAsync(WellOperationRequest request, CancellationToken token)
{ {
var (items, _) = await GetAsyncWithDaysAndNpv(request, token); var (items, _) = await GetWithDaysAndNpvAsync(request, token);
return items; return items;
} }
@ -52,7 +57,7 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
var skip = request.Skip ?? 0; var skip = request.Skip ?? 0;
var take = request.Take ?? 32; var take = request.Take ?? 32;
var (items, count) = await GetAsyncWithDaysAndNpv(request, token); var (items, count) = await GetWithDaysAndNpvAsync(request, token);
var paginationContainer = new PaginationContainer<WellOperationDto> var paginationContainer = new PaginationContainer<WellOperationDto>
{ {
@ -165,27 +170,24 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
private async Task<IEnumerable<WellOperation>> GetByIdsWells(IEnumerable<int> idsWells, CancellationToken token) private async Task<IEnumerable<WellOperation>> GetByIdsWells(IEnumerable<int> idsWells, CancellationToken token)
{ {
var query = GetQuery() var query = GetQuery()
.Include(e => e.WellSectionType)
.Include(e => e.OperationCategory)
.Where(e => idsWells.Contains(e.IdWell)) .Where(e => idsWells.Contains(e.IdWell))
.OrderBy(e => e.DateStart); .OrderBy(e => e.DateStart);
var entities = await query.ToArrayAsync(token); var entities = await query.ToArrayAsync(token);
return entities; return entities;
} }
private async Task<(IEnumerable<WellOperationDto> items, int count)> GetAsyncWithDaysAndNpv(WellOperationRequest request, CancellationToken token) private async Task<(IEnumerable<WellOperationDto> items, int count)> GetWithDaysAndNpvAsync(WellOperationRequest request, CancellationToken token)
{ {
var skip = request.Skip ?? 0; var skip = request.Skip ?? 0;
var take = request.Take ?? 32; var take = request.Take ?? 32;
var entities = await GetByIdsWells(request.IdsWell, token); var entities = await GetByIdsWells(request.IdsWell, token);
var entitiesByWellAndType = entities var groupedByWellAndType = entities
.GroupBy(e => new { e.IdWell, e.IdType }) .GroupBy(e => new { e.IdWell, e.IdType });
.Select(grp => grp.ToArray());
var result = new List<WellOperationDto>(); var result = new List<WellOperationDto>();
var count = 0; var count = 0;
foreach (var wellOperationsWithType in entitiesByWellAndType) foreach (var wellOperationsWithType in groupedByWellAndType)
{ {
var firstWellOperation = wellOperationsWithType var firstWellOperation = wellOperationsWithType
.OrderBy(e => e.DateStart) .OrderBy(e => e.DateStart)
@ -201,8 +203,16 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
.Take(take); .Take(take);
var dtos = filteredWellOperationsPart var dtos = filteredWellOperationsPart
.Select(o => ConvertWithDrillingDaysAndNpvHours(o, firstWellOperation, operationsWithNpt)); .Select(entity =>
{
var dto = Convert(entity);
dto.Day = (entity.DateStart - firstWellOperation.DateStart).TotalDays;
dto.NptHours = operationsWithNpt
.Where(o => o.DateStart <= entity.DateStart)
.Sum(e => e.DurationHours);
return dto;
});
result.AddRange(dtos); result.AddRange(dtos);
count += filteredWellOperations.Count(); count += filteredWellOperations.Count();
} }
@ -336,20 +346,6 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
}; };
} }
private WellOperationDto ConvertWithDrillingDaysAndNpvHours(
WellOperation entity,
WellOperation firstOperation,
IEnumerable<WellOperation> wellOperationsWithNtp)
{
var dto = Convert(entity);
dto.Day = (entity.DateStart - firstOperation.DateStart).TotalDays;
dto.NptHours = wellOperationsWithNtp
.Where(o => o.DateStart <= entity.DateStart)
.Sum(e => e.DurationHours);
return dto;
}
protected override WellOperation Convert(WellOperationDto src) protected override WellOperation Convert(WellOperationDto src)
{ {
var entity = src.Adapt<WellOperation>(); var entity = src.Adapt<WellOperation>();
@ -361,13 +357,14 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
{ {
//TODO: пока такое получение TimeZone скважины, нужно исправить на Lazy //TODO: пока такое получение TimeZone скважины, нужно исправить на Lazy
//Хоть мы и тянем данные из кэша, но от получения TimeZone в этом методе нужно избавиться, пока так //Хоть мы и тянем данные из кэша, но от получения TimeZone в этом методе нужно избавиться, пока так
//.Include(e => e.WellSectionType)
// .Include(e => e.OperationCategory)
var timeZoneOffset = wellService.GetTimezone(src.IdWell).Offset; var timeZoneOffset = wellService.GetTimezone(src.IdWell).Offset;
var dto = src.Adapt<WellOperationDto>(); var dto = src.Adapt<WellOperationDto>();
dto.DateStart = src.DateStart.ToOffset(timeZoneOffset); dto.DateStart = src.DateStart.ToOffset(timeZoneOffset);
dto.LastUpdateDate = src.LastUpdateDate.ToOffset(timeZoneOffset); dto.LastUpdateDate = src.LastUpdateDate.ToOffset(timeZoneOffset);
dto.OperationCategoryName = LazyWellCategories.Value.TryGetValue(src.IdCategory, out WellOperationCategoryDto? category) ? category.Name : string.Empty;
dto.WellSectionTypeCaption = LazyWellSectionTypes.Value.TryGetValue(src.IdWellSectionType, out WellSectionTypeDto? sectionType) ? sectionType.Caption : string.Empty;
return dto; return dto;
} }
} }