diff --git a/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj b/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj index 36035215..189f6383 100644 --- a/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj +++ b/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj @@ -35,6 +35,7 @@ + diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index e67a969b..e8e879c1 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Data; +using System.Data.SqlClient; using System.Diagnostics; using System.Linq; using System.Threading; @@ -15,7 +16,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems { #nullable enable internal class SubsystemOperationTimeBackgroundService : BackgroundService - { + { private readonly string connectionString; private readonly TimeSpan period = TimeSpan.FromHours(1); private const int idSubsytemTorqueMaster = 65537; @@ -102,43 +103,62 @@ namespace AsbCloudInfrastructure.Services.Subsystems return affected; } private static async Task?> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token) - { - var dateStart = db.TelemetryDataSaub - .AsNoTracking() - .Where(d => (d.IdTelemetry == idTelemetry) & (d.DateTime >= begin)) - .Min(d => d.DateTime); - var depthStart = db.TelemetryDataSaub - .AsNoTracking() - .Where(d => (d.IdTelemetry == idTelemetry) & (d.DateTime >= begin)) - .Min(d => d.WellDepth); - - var operationTimeSpinWithDepth = - $"select tt.date, tt.mode, tt.mode_prev, tt.well_depth" + + { + var operationTimeSaub = + $"select tt.date, tt.mode, tt.well_depth" + $" from (select date, mode, well_depth, lag(mode,1) over (order by date) as mode_prev" + - $" from t_telemetry_data_saub where id_telemetry = {idTelemetry}) as tt" + - $" where tt.mode != tt.mode_prev and tt.date >='{begin}' order by tt.date;"; + $" from t_telemetry_data_saub where id_telemetry = @idTelemetry) as tt" + + $" where tt.mode != tt.mode_prev and tt.date >=@begin order by tt.date;"; using var command = db.Database.GetDbConnection().CreateCommand(); - command.CommandText = operationTimeSpinWithDepth; - command. + command.CommandText = operationTimeSaub; + SqlParameter telemetry = new SqlParameter("@idTelemetry", idTelemetry); + command.Parameters.Add(telemetry); + SqlParameter dateStart = new SqlParameter("@begin", begin); + command.Parameters.Add(dateStart); db.Database.OpenConnection(); using var result = await command.ExecuteReaderAsync(token); - var resultSubsystemOperationTime = new List(); + var telemetryOpearationSaubSubsystems = new List(); if (result.HasRows) { while (result.Read()) { - var itemEntity = new SubsystemOperationTime() + var itemEntity = new SubsystemsSpinWithDepth() + { + Date = result.GetFieldValue(0), + IdSubsystem = result.GetFieldValue(1)+1, + Depth = result.GetFieldValue(2) + }; + telemetryOpearationSaubSubsystems.Add(itemEntity); + } + } + var resultSubsystemOperationTime = new List(); + var firstItem = telemetryOpearationSaubSubsystems.FirstOrDefault(); + if (firstItem is null) + return null; + int idSubsystem = firstItem.IdSubsystem; + DateTimeOffset dateBegin = firstItem.Date; + float? depthStart = firstItem.Depth; + for (int i = 1; i < telemetryOpearationSaubSubsystems.Count; i++) + { + if (telemetryOpearationSaubSubsystems[i].IdSubsystem != idSubsystem) + { + var operationTimeItem = new SubsystemOperationTime() { IdTelemetry = idTelemetry, - DateStart = dateStart, - DateEnd = result.GetFieldValue(0), + DateStart = dateBegin, + IdSubsystem = telemetryOpearationSaubSubsystems[i - 1].IdSubsystem, + DateEnd = telemetryOpearationSaubSubsystems[i].Date, DepthStart = depthStart, - DepthEnd = result.GetFieldValue(3), - IdSubsystem = result.GetFieldValue(2) + 1 + DepthEnd = telemetryOpearationSaubSubsystems[i].Depth + }; - dateStart = itemEntity.DateEnd; - depthStart = itemEntity.DepthStart; - resultSubsystemOperationTime.Add(itemEntity); + dateBegin = telemetryOpearationSaubSubsystems[i].Date; + depthStart = telemetryOpearationSaubSubsystems[i].Depth; + idSubsystem = telemetryOpearationSaubSubsystems[i].IdSubsystem; + if (telemetryOpearationSaubSubsystems[i - 1].IdSubsystem != 0) + { + resultSubsystemOperationTime.Add(operationTimeItem); + } } } return resultSubsystemOperationTime; @@ -157,6 +177,10 @@ namespace AsbCloudInfrastructure.Services.Subsystems $"where mode!=mode_pre or state != state_pre order by \"date\";"; using var command = db.Database.GetDbConnection().CreateCommand(); command.CommandText = operationTimeSpinWithDepth; + SqlParameter telemetry = new SqlParameter("@idTelemetry", idTelemetry); + command.Parameters.Add(telemetry); + SqlParameter dateStart = new SqlParameter("@begin", begin); + command.Parameters.Add(dateStart); db.Database.OpenConnection(); using var result = await command.ExecuteReaderAsync(token); var telemetryOpearationSpinSubsystems = new List();