доработка фонового сервиса

This commit is contained in:
eugeniy_ivanov 2022-08-18 15:48:20 +05:00
parent babd1076a4
commit 118ed4e701
2 changed files with 51 additions and 26 deletions

View File

@ -35,6 +35,7 @@
<PackageReference Include="Mapster" Version="7.3.0" /> <PackageReference Include="Mapster" Version="7.3.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.18.0" /> <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.18.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.18.0" /> <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.18.0" />
</ItemGroup> </ItemGroup>

View File

@ -6,6 +6,7 @@ using Microsoft.Extensions.Hosting;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.SqlClient;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -103,42 +104,61 @@ namespace AsbCloudInfrastructure.Services.Subsystems
} }
private static async Task<IEnumerable<SubsystemOperationTime>?> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token) private static async Task<IEnumerable<SubsystemOperationTime>?> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
{ {
var dateStart = db.TelemetryDataSaub var operationTimeSaub =
.AsNoTracking() $"select tt.date, tt.mode, tt.well_depth" +
.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" +
$" from (select date, mode, well_depth, lag(mode,1) over (order by date) as mode_prev" + $" 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" + $" 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;"; $" where tt.mode != tt.mode_prev and tt.date >=@begin order by tt.date;";
using var command = db.Database.GetDbConnection().CreateCommand(); using var command = db.Database.GetDbConnection().CreateCommand();
command.CommandText = operationTimeSpinWithDepth; command.CommandText = operationTimeSaub;
command. SqlParameter telemetry = new SqlParameter("@idTelemetry", idTelemetry);
command.Parameters.Add(telemetry);
SqlParameter dateStart = new SqlParameter("@begin", begin);
command.Parameters.Add(dateStart);
db.Database.OpenConnection(); db.Database.OpenConnection();
using var result = await command.ExecuteReaderAsync(token); using var result = await command.ExecuteReaderAsync(token);
var resultSubsystemOperationTime = new List<SubsystemOperationTime>(); var telemetryOpearationSaubSubsystems = new List<SubsystemsSpinWithDepth>();
if (result.HasRows) if (result.HasRows)
{ {
while (result.Read()) while (result.Read())
{ {
var itemEntity = new SubsystemOperationTime() var itemEntity = new SubsystemsSpinWithDepth()
{
Date = result.GetFieldValue<DateTimeOffset>(0),
IdSubsystem = result.GetFieldValue<short>(1)+1,
Depth = result.GetFieldValue<float>(2)
};
telemetryOpearationSaubSubsystems.Add(itemEntity);
}
}
var resultSubsystemOperationTime = new List<SubsystemOperationTime>();
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, IdTelemetry = idTelemetry,
DateStart = dateStart, DateStart = dateBegin,
DateEnd = result.GetFieldValue<DateTimeOffset>(0), IdSubsystem = telemetryOpearationSaubSubsystems[i - 1].IdSubsystem,
DateEnd = telemetryOpearationSaubSubsystems[i].Date,
DepthStart = depthStart, DepthStart = depthStart,
DepthEnd = result.GetFieldValue<float>(3), DepthEnd = telemetryOpearationSaubSubsystems[i].Depth
IdSubsystem = result.GetFieldValue<short>(2) + 1
}; };
dateStart = itemEntity.DateEnd; dateBegin = telemetryOpearationSaubSubsystems[i].Date;
depthStart = itemEntity.DepthStart; depthStart = telemetryOpearationSaubSubsystems[i].Depth;
resultSubsystemOperationTime.Add(itemEntity); idSubsystem = telemetryOpearationSaubSubsystems[i].IdSubsystem;
if (telemetryOpearationSaubSubsystems[i - 1].IdSubsystem != 0)
{
resultSubsystemOperationTime.Add(operationTimeItem);
}
} }
} }
return resultSubsystemOperationTime; return resultSubsystemOperationTime;
@ -157,6 +177,10 @@ namespace AsbCloudInfrastructure.Services.Subsystems
$"where mode!=mode_pre or state != state_pre order by \"date\";"; $"where mode!=mode_pre or state != state_pre order by \"date\";";
using var command = db.Database.GetDbConnection().CreateCommand(); using var command = db.Database.GetDbConnection().CreateCommand();
command.CommandText = operationTimeSpinWithDepth; 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(); db.Database.OpenConnection();
using var result = await command.ExecuteReaderAsync(token); using var result = await command.ExecuteReaderAsync(token);
var telemetryOpearationSpinSubsystems = new List<SubsystemsSpinWithDepth>(); var telemetryOpearationSpinSubsystems = new List<SubsystemsSpinWithDepth>();