From 616cb242d6a4512dd866e293792558027f8a3da1 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 19 Mar 2024 17:21:05 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BD=D0=B8=D1=84=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=B4=D0=B0=D1=82=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?DailyReport=20+=20=D0=B2=D1=81=D1=91,=20=D1=87=D1=82=D0=BE=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D1=88=D0=BB=D0=BE=D1=81=D1=8C=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D1=82=D1=8C,=20=D1=87=D1=82?= =?UTF-8?q?=D0=BE=D0=B1=D1=8B=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B1=D1=80=D0=B0=D0=BB=D1=81=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Data/DailyReport/DailyReportDto.cs | 2 +- AsbCloudApp/Data/DatesRangeDto.cs | 4 +- AsbCloudApp/Data/StatWellDto.cs | 2 +- AsbCloudApp/Data/WellDto.cs | 2 +- AsbCloudApp/Services/IWellService.cs | 2 +- .../AsbCloudDbContextModelSnapshot.cs | 2 +- AsbCloudDb/Model/DailyReports/DailyReport.cs | 2 +- .../Repository/DailyReportRepository.cs | 111 +++++++++--------- .../Repository/DepositRepository.cs | 4 +- .../Repository/WellOperationRepository.cs | 18 +-- .../DailyReport/DailyReportService.cs | 46 ++++---- .../Services/SAUB/TelemetryDataBaseService.cs | 2 +- .../OperationsStatService.cs | 9 +- .../Services/WellService.cs | 6 +- .../Services/WellboreServiceTest.cs | 4 +- 15 files changed, 110 insertions(+), 106 deletions(-) diff --git a/AsbCloudApp/Data/DailyReport/DailyReportDto.cs b/AsbCloudApp/Data/DailyReport/DailyReportDto.cs index d8d9d4c8..c31dd73c 100644 --- a/AsbCloudApp/Data/DailyReport/DailyReportDto.cs +++ b/AsbCloudApp/Data/DailyReport/DailyReportDto.cs @@ -74,7 +74,7 @@ public class DailyReportDto : IId, /// /// Дата последнего обновления /// - public DateTime? DateLastUpdate { get; set; } + public DateTimeOffset? DateLastUpdate { get; set; } /// /// Блок фактической траектории diff --git a/AsbCloudApp/Data/DatesRangeDto.cs b/AsbCloudApp/Data/DatesRangeDto.cs index 3f12acb7..a61034b6 100644 --- a/AsbCloudApp/Data/DatesRangeDto.cs +++ b/AsbCloudApp/Data/DatesRangeDto.cs @@ -12,12 +12,12 @@ namespace AsbCloudApp.Data /// Дата начала диапазона /// [Required] - public DateTime From { get; set; } + public DateTimeOffset From { get; set; } /// /// Дата окончания диапазона /// [Required] - public DateTime To { get; set; } + public DateTimeOffset To { get; set; } } } diff --git a/AsbCloudApp/Data/StatWellDto.cs b/AsbCloudApp/Data/StatWellDto.cs index ece9756c..9cf27fe4 100644 --- a/AsbCloudApp/Data/StatWellDto.cs +++ b/AsbCloudApp/Data/StatWellDto.cs @@ -42,7 +42,7 @@ namespace AsbCloudApp.Data /// дата прихода последней телеметрии /// [Required] - public DateTime LastTelemetryDate { get; set; } + public DateTimeOffset LastTelemetryDate { get; set; } /// /// Статистика по секциям diff --git a/AsbCloudApp/Data/WellDto.cs b/AsbCloudApp/Data/WellDto.cs index 30f524c2..d9a67431 100644 --- a/AsbCloudApp/Data/WellDto.cs +++ b/AsbCloudApp/Data/WellDto.cs @@ -72,7 +72,7 @@ namespace AsbCloudApp.Data /// /// Дата/время кода приходили данные последний раз /// - public DateTime LastTelemetryDate { get; set; } + public DateTimeOffset LastTelemetryDate { get; set; } /// /// ID телеметрии diff --git a/AsbCloudApp/Services/IWellService.cs b/AsbCloudApp/Services/IWellService.cs index edea98ae..4279f587 100644 --- a/AsbCloudApp/Services/IWellService.cs +++ b/AsbCloudApp/Services/IWellService.cs @@ -72,7 +72,7 @@ namespace AsbCloudApp.Services /// /// /// - DateTime GetLastTelemetryDate(int idWell); + DateTimeOffset GetLastTelemetryDate(int idWell); //TODO: выяснить и удалить отсюда /// diff --git a/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs b/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs index 9957e235..8f326f19 100644 --- a/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs +++ b/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs @@ -282,7 +282,7 @@ namespace AsbCloudDb.Migrations .HasColumnName("date") .HasComment("Дата формирования отчёта"); - b.Property("DateLastUpdate") + b.Property("DateLastUpdate") .HasColumnType("timestamp with time zone") .HasColumnName("date_last_update") .HasComment("Дата последнего обновления"); diff --git a/AsbCloudDb/Model/DailyReports/DailyReport.cs b/AsbCloudDb/Model/DailyReports/DailyReport.cs index c521ca54..8bc99470 100644 --- a/AsbCloudDb/Model/DailyReports/DailyReport.cs +++ b/AsbCloudDb/Model/DailyReports/DailyReport.cs @@ -18,7 +18,7 @@ public class DailyReport : IId public int IdWell { get; set; } [Column("date_last_update", TypeName = "timestamp with time zone"), Comment("Дата последнего обновления")] - public DateTime? DateLastUpdate { get; set; } + public DateTimeOffset? DateLastUpdate { get; set; } [Column("date", TypeName = "date"), Comment("Дата формирования отчёта")] public DateOnly Date { get; set; } diff --git a/AsbCloudInfrastructure/Repository/DailyReportRepository.cs b/AsbCloudInfrastructure/Repository/DailyReportRepository.cs index b82b0ce1..f7f582fa 100644 --- a/AsbCloudInfrastructure/Repository/DailyReportRepository.cs +++ b/AsbCloudInfrastructure/Repository/DailyReportRepository.cs @@ -1,80 +1,85 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; using AsbCloudApp.Data.DailyReport; using AsbCloudApp.Data.DailyReport.Blocks.Sign; using AsbCloudApp.Data.DailyReport.Blocks.Subsystems; using AsbCloudApp.Data.DailyReport.Blocks.TimeBalance; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; +using AsbCloudApp.Services; using AsbCloudDb; using AsbCloudDb.Model; using AsbCloudDb.Model.DailyReports; using Mapster; using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace AsbCloudInfrastructure.Repository; public class DailyReportRepository : CrudRepositoryBase, - IDailyReportRepository + IDailyReportRepository { - public DailyReportRepository(IAsbCloudDbContext dbContext) - : base(dbContext) - { - } + private IWellService wellService; - public async Task> GetAsync(int idWell, FileReportRequest request, CancellationToken cancellationToken) - { - var skip = request.Skip ?? 0; - var take = request.Take ?? 10; + public DailyReportRepository(IAsbCloudDbContext dbContext, IWellService wellService) + : base(dbContext) + { + this.wellService = wellService; + } - var query = GetQuery().Where(d => d.IdWell == idWell); + public async Task> GetAsync(int idWell, FileReportRequest request, CancellationToken cancellationToken) + { + var skip = request.Skip ?? 0; + var take = request.Take ?? 10; - if (request.GeDate.HasValue) - query = query.Where(d => d.Date >= request.GeDate.Value); - - if (request.LeDate.HasValue) - query = query.Where(d => d.Date <= request.LeDate.Value); + var query = GetQuery().Where(d => d.IdWell == idWell); - query = request.SortFields?.Any() == true ? - query.SortBy(request.SortFields) : - query.OrderBy(d => d.Date); + if (request.GeDate.HasValue) + query = query.Where(d => d.Date >= request.GeDate.Value); - var entities = await query - .Skip(skip) - .Take(take) - .AsNoTracking() - .ToArrayAsync(cancellationToken); + if (request.LeDate.HasValue) + query = query.Where(d => d.Date <= request.LeDate.Value); - var dtos = entities.Select(Convert); + query = request.SortFields?.Any() == true ? + query.SortBy(request.SortFields) : + query.OrderBy(d => d.Date); - return dtos; - } + var entities = await query + .Skip(skip) + .Take(take) + .AsNoTracking() + .ToArrayAsync(cancellationToken); - public async Task GetOrDefaultAsync(int idWell, DateOnly date, CancellationToken cancellationToken) - { - var entity = await GetQuery() - .AsNoTracking() - .SingleOrDefaultAsync(d => d.IdWell == idWell && d.Date == date, cancellationToken); + var timezoneOffset = wellService.GetTimezone(idWell).Offset; + var dtos = entities.Select(entity => Convert(entity, timezoneOffset)); - return entity is null ? null : Convert(entity); - } + return dtos; + } - protected override DailyReportDto Convert(DailyReport src) - { - var dto = new DailyReportDto - { - Id = src.Id, - IdWell = src.IdWell, - DateLastUpdate = src.DateLastUpdate, - Date = src.Date, - SignBlock = src.SignBlock?.Adapt(), - TimeBalanceBlock = src.TimeBalanceBlock?.Adapt(), - SubsystemBlock = src.SubsystemBlock?.Adapt() - }; - - return dto; - } + public async Task GetOrDefaultAsync(int idWell, DateOnly date, CancellationToken cancellationToken) + { + var entity = await GetQuery() + .AsNoTracking() + .SingleOrDefaultAsync(d => d.IdWell == idWell && d.Date == date, cancellationToken); + + return entity is null ? null : Convert(entity); + } + + protected DailyReportDto Convert(DailyReport src, TimeSpan timezoneOffset) + { + var dto = new DailyReportDto + { + Id = src.Id, + IdWell = src.IdWell, + DateLastUpdate = src.DateLastUpdate?.ToOffset(timezoneOffset), + Date = src.Date, + SignBlock = src.SignBlock?.Adapt(), + TimeBalanceBlock = src.TimeBalanceBlock?.Adapt(), + SubsystemBlock = src.SubsystemBlock?.Adapt() + }; + + return dto; + } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/DepositRepository.cs b/AsbCloudInfrastructure/Repository/DepositRepository.cs index 7f19f1d7..804d130e 100644 --- a/AsbCloudInfrastructure/Repository/DepositRepository.cs +++ b/AsbCloudInfrastructure/Repository/DepositRepository.cs @@ -4,6 +4,7 @@ using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -111,7 +112,8 @@ namespace AsbCloudInfrastructure.Repository { var dto = well.Adapt(); dto.WellType = well.WellType.Caption; - dto.LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id); + dto.LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id) + .ToOffset(TimeSpan.FromHours(well.Timezone.Hours)); dto.Cluster = gCluster.Key.Caption; dto.Deposit = gDeposit.Key.Caption; return dto; diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index c75ab4fd..1c3f6fdc 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -177,8 +177,8 @@ public class WellOperationRepository : IWellOperationRepository return new DatesRangeDto { - From = minDate.ToRemoteDateTime(timezone.Hours), - To = maxDate.ToRemoteDateTime(timezone.Hours) + From = minDate.ToOffset(timezone.Offset), + To = maxDate.ToOffset(timezone.Offset) }; } @@ -197,12 +197,12 @@ public class WellOperationRepository : IWellOperationRepository WellOperationRequest request, CancellationToken token) { - var query = BuildQuery(request) + var query = BuildQuery(request) .AsNoTracking(); - var dtos = await query.ToArrayAsync(token); + var dtos = await query.ToArrayAsync(token); - return dtos.Select(Convert); + return dtos.Select(Convert); } public async Task> GetAsync( @@ -413,14 +413,14 @@ public class WellOperationRepository : IWellOperationRepository if (request.GeDate.HasValue) { - //var geDateOffset = request.GeDate.Value.ToUtcDateTimeOffset(timeZoneOffset); - query = query.Where(e => e.DateStart >= request.GeDate); + var geDateOffset = request.GeDate.Value.ToUniversalTime(); + query = query.Where(e => e.DateStart >= geDateOffset); } if (request.LeDate.HasValue) { - //var leDateOffset = request.LeDate.Value.ToUtcDateTimeOffset(timeZoneOffset); - query = query.Where(e => e.DateStart <= request.LeDate); + var leDateOffset = request.LeDate.Value.ToUniversalTime(); + query = query.Where(e => e.DateStart <= leDateOffset); } var currentWellOperations = db.WellOperations diff --git a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs index 6a4cfba5..fba35c59 100644 --- a/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs +++ b/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs @@ -83,7 +83,7 @@ public class DailyReportService : IDailyReportService editableBlock.IdUser = idUser; editableBlock.LastUpdateDate = DateTime.UtcNow; - dailyReport.DateLastUpdate = DateTime.UtcNow; + dailyReport.DateLastUpdate = DateTimeOffset.UtcNow; if (dailyReport.Id == 0) return await dailyReportRepository.InsertAsync(dailyReport, cancellationToken); @@ -158,28 +158,27 @@ public class DailyReportService : IDailyReportService return result; var dailyReports = new List(); - - if (request.GeDate.HasValue) - { - var startDate = new DateTime(request.GeDate.Value.Year, request.GeDate.Value.Month, - request.GeDate.Value.Day); + TimeSpan offset = wellService.GetTimezone(idWell).Offset; - if (startDate.Date >= datesRange.From.Date) + if (request.GeDate.HasValue) + { + var startDate = new DateTimeOffset(request.GeDate.Value, TimeOnly.MinValue, offset); + + if (startDate >= datesRange.From) datesRange.From = startDate; } if (request.LeDate.HasValue) { - var finishDate = new DateTime(request.LeDate.Value.Year, request.LeDate.Value.Month, - request.LeDate.Value.Day); + var finishDate = new DateTimeOffset(request.LeDate.Value, TimeOnly.MinValue, offset); - if (finishDate.Date <= datesRange.To.Date) + if (finishDate <= datesRange.To) datesRange.To = finishDate; } if (datesRange.From.AddDays(result.Skip) <= datesRange.To) - result.Count = (int)(Math.Ceiling((datesRange.To - DateTime.UnixEpoch).TotalDays) - - Math.Floor((datesRange.From - DateTime.UnixEpoch).TotalDays)) + 1; + result.Count = (int)(Math.Ceiling((datesRange.To - DateTimeOffset.UnixEpoch).TotalDays) - + Math.Floor((datesRange.From - DateTimeOffset.UnixEpoch).TotalDays)) + 1; var existingDailyReports = await dailyReportRepository.GetAsync(idWell, request, cancellationToken); @@ -200,16 +199,16 @@ public class DailyReportService : IDailyReportService { for (var day = result.Skip; day - result.Skip < result.Take && datesRange.To.AddDays(-day) >= datesRange.From; day++) { - var dateDailyReport = DateOnly.FromDateTime(datesRange.To.AddDays(-day)); + var dateDailyReport = DateOnly.FromDateTime(datesRange.To.AddDays(-day).DateTime); - AddDailyReport(dateDailyReport); + AddDailyReport(dateDailyReport); } } else { for (var day = result.Skip; day - result.Skip < result.Take && datesRange.From.AddDays(day) <= datesRange.To; day++) { - var dateDailyReport = DateOnly.FromDateTime(datesRange.From.AddDays(day)); + var dateDailyReport = DateOnly.FromDateTime(datesRange.From.AddDays(day).DateTime); AddDailyReport(dateDailyReport); } @@ -243,7 +242,7 @@ public class DailyReportService : IDailyReportService public async Task GetDatesRangeAsync(int idWell, CancellationToken cancellationToken) { var timezone = wellService.GetTimezone(idWell); - var currentDate = DateTimeOffset.UtcNow.ToRemoteDateTime(timezone.Hours); + var currentDate = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(timezone.Hours)); var factOperationDatesRange = await wellOperationRepository.GetDatesRangeAsync(idWell, WellOperation.IdOperationTypeFact, cancellationToken); @@ -251,15 +250,13 @@ public class DailyReportService : IDailyReportService if (factOperationDatesRange is null) return null; - var from = (factOperationDatesRange.From.AddDays(1) <= DateTime.UtcNow ? + var from = (factOperationDatesRange.From.AddDays(1) <= DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(timezone.Hours)) ? factOperationDatesRange.From : - currentDate.AddDays(-1)) - .Date; + currentDate.AddDays(-1)); - var to = (factOperationDatesRange.To.AddDays(1) <= DateTime.UtcNow ? + var to = (factOperationDatesRange.To.AddDays(1) <= DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(timezone.Hours)) ? factOperationDatesRange.To : - currentDate.AddDays(-1)) - .Date; + currentDate.AddDays(-1)); return new DatesRangeDto { @@ -417,9 +414,8 @@ public class DailyReportService : IDailyReportService if (datesRange is null) return false; - - var from = DateOnly.FromDateTime(datesRange.From); - var to = DateOnly.FromDateTime(datesRange.To); + var from = DateOnly.FromDateTime(datesRange.From.ToUniversalTime().DateTime); + var to = DateOnly.FromDateTime(datesRange.To.ToUniversalTime().DateTime); return dateDailyReport >= from && dateDailyReport <= to; } diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs index f9cc64ee..52966fad 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs @@ -102,7 +102,7 @@ namespace AsbCloudInfrastructure.Services.SAUB if (dateBegin == default) { var dateRange = telemetryDataCache.GetOrDefaultDataDateRange(telemetry.Id); - dateBeginUtc = (dateRange?.To.ToUtcDateTimeOffset(timezone.Hours) ?? DateTime.UtcNow) + dateBeginUtc = (dateRange?.To.ToOffset(timezone.Offset) ?? DateTimeOffset.UtcNow) .AddSeconds(-intervalSec); } else diff --git a/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs index c9018051..4bf326e7 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs @@ -145,6 +145,8 @@ public class OperationsStatService : IOperationsStatService private async Task CalcWellStatAsync(Well well, CancellationToken token) { + var timezone = wellService.GetTimezone(well.Id); + var wellType = (await memoryCache .GetOrCreateBasicAsync(db.Set(), token)) .FirstOrDefault(t => t.Id == well.IdWellType); @@ -155,7 +157,7 @@ public class OperationsStatService : IOperationsStatService WellType = wellType?.Caption ?? "", IdState = well.IdState, State = wellService.GetStateText(well.IdState), - LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id), + LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id).ToOffset(timezone.Offset), Companies = await wellService.GetCompaniesAsync(well.Id, token) }; @@ -169,9 +171,8 @@ public class OperationsStatService : IOperationsStatService if (!wellOperations.Any()) return statWellDto; - var timezoneOffsetH = wellService.GetTimezone(well.Id).Hours; - statWellDto.Sections = CalcSectionsStats(wellOperations, timezoneOffsetH); - statWellDto.Total = GetStatTotal(wellOperations, well.IdState, timezoneOffsetH); + statWellDto.Sections = CalcSectionsStats(wellOperations, timezone.Hours); + statWellDto.Total = GetStatTotal(wellOperations, well.IdState, timezone.Hours); statWellDto.TvdLagDays = CalcTvdLagDays(wellOperations); statWellDto.TvdDrillingDays = CalcDrillingDays(wellOperations); diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 4d6ec10a..2c579140 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -63,12 +63,12 @@ namespace AsbCloudInfrastructure.Services private void DropCacheRelationCompanyWell() => memoryCache.DropBasic(); - public DateTime GetLastTelemetryDate(int idWell) + public DateTimeOffset GetLastTelemetryDate(int idWell) { var well = GetOrDefault(idWell); if (well?.IdTelemetry is null) - return DateTime.MinValue; + return DateTimeOffset.MinValue; var datesRange = telemetryService.GetDatesRange(well.IdTelemetry.Value); return datesRange.To; @@ -278,7 +278,7 @@ namespace AsbCloudInfrastructure.Services dto.Cluster = entity.Cluster.Caption; dto.Deposit = entity.Cluster.Deposit.Caption; if (entity.IdTelemetry is not null) - dto.LastTelemetryDate = telemetryService.GetDatesRange(entity.IdTelemetry.Value).To; + dto.LastTelemetryDate = telemetryService.GetDatesRange(entity.IdTelemetry.Value).To.ToOffset(dto.Timezone.Offset); dto.Companies = entity.RelationCompaniesWells .Select(r => Convert(r.Company)) .ToList(); diff --git a/AsbCloudWebApi.Tests/Services/WellboreServiceTest.cs b/AsbCloudWebApi.Tests/Services/WellboreServiceTest.cs index 7f647158..f256c384 100644 --- a/AsbCloudWebApi.Tests/Services/WellboreServiceTest.cs +++ b/AsbCloudWebApi.Tests/Services/WellboreServiceTest.cs @@ -21,7 +21,7 @@ public class WellboreServiceTest Id = 1, IdState = 1, IdTelemetry = 1, - LastTelemetryDate = DateTime.Now, + LastTelemetryDate = DateTimeOffset.Now, Caption = "well 1" }; @@ -30,7 +30,7 @@ public class WellboreServiceTest Id = 2, IdState = 1, IdTelemetry = 100, - LastTelemetryDate = DateTime.Now, + LastTelemetryDate = DateTimeOffset.Now, Caption = "well 2" };