diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index 26daec7d..c18056e2 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -62,13 +62,11 @@ public interface IWellOperationRepository Task GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken); /// - /// Возвращает первую и последнюю операцию - /// - /// - /// - /// - /// - Task<(WellOperationBaseDto First, WellOperationBaseDto Last)?> GetFirstAndLastAsync(int idWell, int idType, CancellationToken token); + /// Возвращает первую и последнюю фактическую операцию + /// + /// + /// + (WellOperationBaseDto First, WellOperationBaseDto Last)? GetFirstAndLastFact(int idWell); /// /// Получить список операций по запросу diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 25033c6d..2fa7b639 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -195,34 +195,36 @@ public class WellOperationRepository : CrudRepositoryBase GetFirstAndLastAsync(int idWell, int idType, CancellationToken token) + public (WellOperationBaseDto First, WellOperationBaseDto Last)? GetFirstAndLastFact(int idWell) { - var cachedDictionary = await memoryCache.GetOrCreateAsync(cacheKeyWellOperations, async (entry) => - { - entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5); - var query = dbContext.Set() - .Where(o => o.IdType == idType) - .GroupBy(o => o.IdWell) - .Select(group => new - { - IdWell = group.Key, - FirstFact = group.OrderBy(o => o.DateStart).First(), - LastFact = group.OrderBy(o => o.DateStart).Last(), - }); + var cachedDictionary = memoryCache.GetOrCreate(cacheKeyWellOperations, (entry) => + { + entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5); + var query = dbContext.Set() + .Where(o => o.IdType == WellOperation.IdOperationTypeFact) + .GroupBy(o => o.IdWell) + .Select(group => new + { + IdWell = group.Key, + FirstFact = group.OrderBy(o => o.DateStart).First(), + LastFact = group.OrderBy(o => o.DateStart).Last(), + }); - var entities = await query.ToArrayAsync(token); + var entities = query.ToArray(); - var dictionary = entities.ToDictionary(s => s.IdWell, s => (Convert(s.FirstFact), Convert(s.LastFact))); - entry.Value = dictionary; + var dictionary = entities.ToDictionary(s => s.IdWell, s => (Convert(s.FirstFact), Convert(s.LastFact))); + entry.Value = dictionary; - return dictionary; - })!; + return dictionary; + + })!; + + var firstAndLast = cachedDictionary.GetValueOrDefault(idWell); + return firstAndLast; - var firstAndLast = cachedDictionary?.GetValueOrDefault(idWell); - return firstAndLast; } - public override async Task DeleteAsync(int id, CancellationToken token) + public override async Task DeleteAsync(int id, CancellationToken token) { var result = await base.DeleteAsync(id, token); if (result > 0) diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index fd7d1dd5..ba29dc06 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -274,7 +274,7 @@ public class WellService : CrudCacheRepositoryBase, IWellService dto.Timezone = GetTimezone(entity.Id); dto.StartDate = wellOperationRepository - .GetFirstAndLast(entity.Id)?.First?.DateStart; + .GetFirstAndLastFact(entity.Id)?.First?.DateStart; dto.WellType = entity.WellType.Caption; dto.Cluster = entity.Cluster.Caption; dto.Deposit = entity.Cluster.Deposit.Caption; diff --git a/AsbCloudInfrastructure/XLExtentions.cs b/AsbCloudInfrastructure/XLExtentions.cs index 7c85c0a6..62992ec2 100644 --- a/AsbCloudInfrastructure/XLExtentions.cs +++ b/AsbCloudInfrastructure/XLExtentions.cs @@ -15,22 +15,25 @@ public static class XLExtentions workbook.Worksheets.FirstOrDefault(ws => string.Equals(ws.Name.Trim(), sheetName.Trim(), StringComparison.CurrentCultureIgnoreCase)) ?? throw new FileFormatException(string.Format(NotFoundSheetTemplate, sheetName)); - public static IXLCell SetCellValue(this IXLCell cell, T value, string? format = null) + public static IXLCell SetCellValue(this IXLCell cell, T? value, string? format = null) { - if (value is DateTime || value is DateTimeOffset) - { - cell.Style.DateFormat.Format = format ?? "DD.MM.YYYY HH:MM:SS"; + if (value == null) + return cell; - if (value is DateTimeOffset dateTimeOffset) - { - cell.Value = XLCellValue.FromObject(dateTimeOffset.DateTime); - return cell; - } - } + if (value is DateTime || value is DateTimeOffset) + { + cell.Style.DateFormat.Format = format ?? "DD.MM.YYYY HH:MM:SS"; - cell.Value = XLCellValue.FromObject(value); + if (value is DateTimeOffset dateTimeOffset) + { + cell.Value = XLCellValue.FromObject(dateTimeOffset.DateTime); + return cell; + } + } - return cell; + cell.Value = XLCellValue.FromObject(value); + + return cell; } public static IXLCell SetHyperlink(this IXLCell cell, string link)