Merge pull request 'Исправления получения диапазона дат для суточных отчётов.' (#96) from fix/autogenerated_daily_report into dev

Reviewed-on: http://test.digitaldrilling.ru:8080/DDrilling/AsbCloudServer/pulls/96
This commit is contained in:
Никита Фролов 2023-08-15 14:52:20 +05:00
commit b3969cbe47
3 changed files with 50 additions and 15 deletions

View File

@ -31,6 +31,14 @@ public interface IAutoGeneratedDailyReportService
/// <param name="reportDate"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<(string fileName, Stream stream)> GenerateReportAsync(int idWell, DateOnly reportDate,
Task<(string fileName, Stream stream)> GenerateAsync(int idWell, DateOnly reportDate,
CancellationToken cancellationToken);
/// <summary>
/// Получение диапазона дат
/// </summary>
/// <param name="idWell"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, CancellationToken cancellationToken);
}

View File

@ -6,7 +6,6 @@ using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Data.AutogeneratedDailyReport;
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Data.Subsystems;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
@ -15,7 +14,6 @@ using AsbCloudApp.Services;
using AsbCloudApp.Services.AutoGeneratedDailyReports;
using AsbCloudApp.Services.Subsystems;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.SAUB;
namespace AsbCloudInfrastructure.Services.AutoGeneratedDailyReports;
@ -25,7 +23,6 @@ public class AutoGeneratedDailyReportService : IAutoGeneratedDailyReportService
private readonly IWellService wellService;
private readonly IWellOperationRepository wellOperationRepository;
private readonly TelemetryDataCache<TelemetryDataSaubDto> telemetryDataCache;
private readonly ISubsystemOperationTimeService subsystemOperationTimeService;
private readonly ICrudRepository<SubsystemDto> subsystemRepository;
private readonly ILimitingParameterService limitingParameterService;
@ -33,7 +30,6 @@ public class AutoGeneratedDailyReportService : IAutoGeneratedDailyReportService
public AutoGeneratedDailyReportService(IWellService wellService,
IWellOperationRepository wellOperationRepository,
TelemetryDataCache<TelemetryDataSaubDto> telemetryDataCache,
ISubsystemOperationTimeService subsystemOperationTimeService,
ICrudRepository<SubsystemDto> subsystemRepository,
ILimitingParameterService limitingParameterService,
@ -41,7 +37,6 @@ public class AutoGeneratedDailyReportService : IAutoGeneratedDailyReportService
{
this.wellOperationRepository = wellOperationRepository;
this.wellService = wellService;
this.telemetryDataCache = telemetryDataCache;
this.subsystemOperationTimeService = subsystemOperationTimeService;
this.subsystemRepository = subsystemRepository;
this.limitingParameterService = limitingParameterService;
@ -67,9 +62,9 @@ public class AutoGeneratedDailyReportService : IAutoGeneratedDailyReportService
if (!well.IdTelemetry.HasValue)
throw new ArgumentInvalidException("Телеметрия для скважины отсутствует", nameof(idWell));
var datesRange = telemetryDataCache.GetOrDefaultDataDateRange(well.IdTelemetry.Value);
if (datesRange is null)
var datesRange = await GetDatesRangeAsync(idWell, cancellationToken);
if (datesRange is null)
return result;
result.Count = (int)(Math.Ceiling((datesRange.To - DateTime.UnixEpoch).TotalDays) - Math.Floor((datesRange.From - DateTime.UnixEpoch).TotalDays));
@ -109,7 +104,7 @@ public class AutoGeneratedDailyReportService : IAutoGeneratedDailyReportService
return result;
}
public async Task<(string fileName, Stream stream)> GenerateReportAsync(int idWell, DateOnly reportDate,
public async Task<(string fileName, Stream stream)> GenerateAsync(int idWell, DateOnly reportDate,
CancellationToken cancellationToken)
{
var startDate = new DateTime(reportDate.Year, reportDate.Month, reportDate.Day);
@ -144,6 +139,21 @@ public class AutoGeneratedDailyReportService : IAutoGeneratedDailyReportService
return (report.FileName, stream);
}
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, CancellationToken cancellationToken)
{
var factOperations = await GetFactOperationsAsync(idWell, null, null,
cancellationToken);
if (!factOperations.Any())
return null;
return new DatesRangeDto
{
From = factOperations.Min(o => o.DateStart),
To = factOperations.Max(o => o.DateStart)
};
}
private HeadBlockDto CreateHeadBlock(WellDto well, IEnumerable<WellOperationDto> factOperations)
{
var customer = well.Companies.FirstOrDefault(company => company.IdCompanyType == 1);
@ -199,8 +209,8 @@ public class AutoGeneratedDailyReportService : IAutoGeneratedDailyReportService
});
}
private Task<IEnumerable<WellOperationDto>> GetFactOperationsAsync(int idWell, DateTime startDate,
DateTime finishDate, CancellationToken cancellationToken)
private async Task<IOrderedEnumerable<WellOperationDto>> GetFactOperationsAsync(int idWell, DateTime? startDate,
DateTime? finishDate, CancellationToken cancellationToken)
{
var request = new WellOperationRequest
{
@ -211,7 +221,8 @@ public class AutoGeneratedDailyReportService : IAutoGeneratedDailyReportService
SortFields = new[] { "DateStart asc" },
};
return wellOperationRepository.GetAsync(request, cancellationToken);
return (await wellOperationRepository.GetAsync(request, cancellationToken))
.OrderBy(w => w.DateStart);
}
private Task<IEnumerable<SubsystemStatDto>?> GetSubsystemStatsAsync(int idWell, DateTime startDate,

View File

@ -49,7 +49,7 @@ public class AutoGeneratedDailyReportController : ControllerBase
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
var reportFile = await autoGeneratedDailyReportService.GenerateReportAsync(idWell,
var reportFile = await autoGeneratedDailyReportService.GenerateAsync(idWell,
reportDate,
cancellationToken);
@ -78,7 +78,23 @@ public class AutoGeneratedDailyReportController : ControllerBase
return Ok(reports);
}
/// <summary>
/// Диапазон дат для формирования суточных отчётов
/// </summary>
/// <param name="idWell"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet("datesRange")]
[ProducesResponseType(typeof(DatesRangeDto), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetDatesRangeAsync(int idWell, CancellationToken cancellationToken)
{
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
return Ok(await autoGeneratedDailyReportService.GetDatesRangeAsync(idWell, cancellationToken));
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken cancellationToken)
{
int? idCompany = User.GetCompanyId();