рефакторинг

This commit is contained in:
eugeniy_ivanov 2022-11-03 00:01:44 +05:00
parent 0880c3b75c
commit 494fd2a107
2 changed files with 56 additions and 37 deletions

View File

@ -8,9 +8,9 @@ namespace AsbCloudApp.Data.Subsystems
public class SubsystemActiveWellStatDto
{
/// <summary>
/// Идентификатор скважины
/// Активная скважина
/// </summary>
public int IdWell { get; set; }
public WellDto ActiveWell { get; set; }
/// <summary>
/// Наработки подсистем
/// </summary>

View File

@ -201,24 +201,40 @@ namespace AsbCloudInfrastructure.Services.Subsystems
public async Task<IEnumerable<SubsystemActiveWellStatDto>?> GetStatByActiveWell(int idCompany, DateTime? gtDate, DateTime? ltDate, CancellationToken token)
{
{
var activeWell = await GetActiveWellByCompany(idCompany, token);
var telemetryIds = activeWell.Select(w => w.IdTelemetry).Distinct();
var firstWell = activeWell.FirstOrDefault();
if (firstWell == null)
return null;
var _well = wellService.GetOrDefault(firstWell.Id);
if (_well is null || _well.Timezone is null)
return null;
DateTimeOffset ExtractDate(DateTime dateTime)
{
var dateTimeOffset = dateTime.ToUtcDateTimeOffset(_well!.Timezone.Hours);
var date = new DateTimeOffset(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, 0, 0, 0, TimeSpan.Zero);
return date;
}
var query = db.SubsystemOperationTimes
.Where(o => telemetryIds.Contains(o.IdTelemetry))
.AsNoTracking();
DateTime today = DateTime.Today;
if (!gtDate.HasValue)
.AsNoTracking();
if (gtDate is not null)
{
gtDate = today;
}
query = query.Where(o => o.DateStart >= gtDate.Value.ToUniversalTime());
var beginUTC = ExtractDate(gtDate.Value);
query = query.Where(d => d.DateStart >= beginUTC);
}
//query = query.Where(o => o.DateStart >= DateTime.Today.ToUtcDateTimeOffset(_well.Timezone.Hours));
if (!ltDate.HasValue)
if (ltDate is not null)
{
ltDate = today.AddDays(-1);
}
query = query.Where(o => o.DateEnd <= ltDate.Value.ToUniversalTime());
var endUTC = ExtractDate(ltDate.Value);
query = query.Where(d => d.DateEnd <= endUTC);
}
//query = query.Where(o => o.DateEnd <= DateTime.Today.AddDays(-1).ToUtcDateTimeOffset(_well.Timezone.Hours));
var result = new List<SubsystemActiveWellStatDto>();
if (query is null)
{
@ -229,33 +245,36 @@ namespace AsbCloudInfrastructure.Services.Subsystems
foreach (var group in groupingSubsystemsOperationTime)
{
var idWell = activeWell.Where(w => w.IdTelemetry == group.Key).Select(w => w.Id).FirstOrDefault();
var wellStat = new SubsystemActiveWellStatDto()
var well = activeWell.Where(w => w.IdTelemetry == group.Key).FirstOrDefault();
if (well != null)
{
IdWell = idWell,
listSubsystemStat = new List<SubsystemStatDto>()
};
var detectedOperationsRequest = new DetectedOperationRequest()
{
IdWell = idWell,
IdsCategories = new List<int>() {1,3},
LtDate = ltDate,
GtDate = gtDate,
};
var detectedOperations = await detectedOperationService.GetOperationsAsync(detectedOperationsRequest, token);
if (detectedOperations is not null && detectedOperations.Any())
{
var depthInterval = GetDepthInterval(detectedOperations);
var groupSubsystem = group.GroupBy(g => g.IdSubsystem);
foreach (var subsystem in groupSubsystem)
var wellStat = new SubsystemActiveWellStatDto()
{
var dto = subsystem.Select(s => s.Adapt<SubsystemOperationTimeDto>());
var subsystemStat = CalcStat(dto, depthInterval);
wellStat.listSubsystemStat.Concat(subsystemStat);
ActiveWell = well,
listSubsystemStat = new List<SubsystemStatDto>()
};
var detectedOperationsRequest = new DetectedOperationRequest()
{
IdWell = well.Id,
IdsCategories = new List<int>() { 1, 3 },
LtDate = ltDate,
GtDate = gtDate,
};
var detectedOperations = await detectedOperationService.GetOperationsAsync(detectedOperationsRequest, token);
if (detectedOperations is not null && detectedOperations.Any())
{
var depthInterval = GetDepthInterval(detectedOperations);
var groupSubsystem = group.GroupBy(g => g.IdSubsystem);
foreach (var subsystem in groupSubsystem)
{
var dto = subsystem.Select(s => s.Adapt<SubsystemOperationTimeDto>());
var subsystemStat = CalcStat(dto, depthInterval);
wellStat.listSubsystemStat.Concat(subsystemStat);
}
result.Add(wellStat);
}
result.Add(wellStat);
}
}
return result;