2022-04-11 18:00:34 +05:00
using AsbCloudApp.Data.SAUB ;
2021-11-24 16:16:17 +05:00
using AsbCloudApp.Services ;
using AsbCloudDb.Model ;
using AsbCloudInfrastructure.Services.Cache ;
using Mapster ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading ;
using System.Threading.Tasks ;
2022-04-11 18:00:34 +05:00
namespace AsbCloudInfrastructure.Services.SAUB
2021-11-24 16:16:17 +05:00
{
public class SetpointsService : ISetpointsService , IConverter < SetpointsRequestDto , SetpointsRequest >
{
2021-11-30 15:25:29 +05:00
// ## Инфо от АПГ от 26.11.2021, дополнения ШОВ от 26.11.2021
2022-04-11 18:00:34 +05:00
private static readonly Dictionary < string , SetpointInfoDto > SetpointInfos = new ( )
2021-11-24 17:58:53 +05:00
{
2021-12-16 16:10:58 +05:00
{ "bitLoad" , new SetpointInfoDto { Name = "bitLoad" , DisplayName = "Осевая нагрузка, т" } } ,
{ "dPressureMaxRotorSP" , new SetpointInfoDto { Name = "dPressureMaxRotorSP" , DisplayName = "Дифференциальное рабочее давление в роторе, атм" } } ,
{ "dPressureMaxSlideSP" , new SetpointInfoDto { Name = "dPressureMaxSlideSP" , DisplayName = "Дифференциальное рабочее давление в слайде, атм" } } ,
2022-03-09 16:49:02 +05:00
{ "torque" , new SetpointInfoDto { Name = "torque" , DisplayName = "Крутящий момент, кН*м" } } ,
2021-12-16 16:10:58 +05:00
{ "speedRotorSp" , new SetpointInfoDto { Name = "speedRotorSp" , DisplayName = "Скорость бурения в роторе, м/ч" } } ,
{ "speedSlideSp" , new SetpointInfoDto { Name = "speedSlideSp" , DisplayName = "Скорость бурения в слайде, м/ч" } } ,
{ "speedDevelopSp" , new SetpointInfoDto { Name = "speedDevelopSp" , DisplayName = "Скорость проработки, м/ч" } } ,
2022-04-13 17:50:09 +05:00
{ "torque_pid_out_limit" , new SetpointInfoDto { Name = "torque_pid_out_limit" , DisplayName = "Торк мастер. Допустимый процент отклонения от заданой скорости вращения" } } , // Такая же что и прямой
2021-12-16 16:10:58 +05:00
//{ "", new SetpointInfoDto { Name = "", DisplayName = "Обороты ВСП, о б /мин" } }, // Оно в ПЛК спинмастера, пока сделать нельзя, позднее можно.
//{ "", new SetpointInfoDto { Name = "", DisplayName = "Расход промывочной жидкости, л/с " } }, // Нет в контроллере
2021-11-24 17:58:53 +05:00
} ;
2021-11-24 16:16:17 +05:00
private readonly CacheTable < SetpointsRequest > cacheSetpoints ;
private readonly ITelemetryService telemetryService ;
public SetpointsService ( IAsbCloudDbContext db , CacheDb cacheDb , ITelemetryService telemetryService )
{
cacheSetpoints = cacheDb . GetCachedTable < SetpointsRequest > (
2022-04-11 18:00:34 +05:00
( AsbCloudDbContext ) db ,
2021-12-22 11:41:18 +05:00
nameof ( SetpointsRequest . Author ) ,
nameof ( SetpointsRequest . Well ) ) ;
2021-11-24 16:16:17 +05:00
this . telemetryService = telemetryService ;
}
public async Task < int > InsertAsync ( SetpointsRequestDto setpoints , CancellationToken token )
{
setpoints . IdState = 1 ;
2022-03-09 16:49:02 +05:00
setpoints . UploadDate = DateTime . UtcNow ;
var dto = Convert ( setpoints ) ;
var inserted = await cacheSetpoints . InsertAsync ( dto , token )
2021-11-24 16:16:17 +05:00
. ConfigureAwait ( false ) ;
return inserted ? . Id ? ? 0 ;
}
public async Task < IEnumerable < SetpointsRequestDto > > GetAsync ( int idWell , CancellationToken token )
{
var entities = await cacheSetpoints . WhereAsync ( s = > s . IdWell = = idWell , token )
. ConfigureAwait ( false ) ;
var dtos = entities . Select ( s = > Convert ( s ) ) ;
return dtos ;
}
public async Task < IEnumerable < SetpointsRequestDto > > GetForPanelAsync ( string uid , CancellationToken token )
{
2021-12-07 18:27:52 +05:00
var idWell = telemetryService . GetIdWellByTelemetryUid ( uid ) ? ? - 1 ;
2021-11-24 16:16:17 +05:00
if ( idWell < 0 )
return null ;
2021-11-24 17:58:53 +05:00
var entities = ( await cacheSetpoints . WhereAsync ( s = >
2021-11-24 16:16:17 +05:00
s . IdWell = = idWell & & s . IdState = = 1 & & s . UploadDate . AddSeconds ( s . ObsolescenceSec ) > DateTime . Now ,
token )
2021-11-24 17:58:53 +05:00
. ConfigureAwait ( false ) )
. ToList ( ) ; // без .ToList() работает не правильно.
2021-11-24 16:16:17 +05:00
if ( ! entities . Any ( ) )
return null ;
foreach ( var entity in entities )
entity . IdState = 2 ;
2022-04-11 18:00:34 +05:00
2021-11-24 16:16:17 +05:00
await cacheSetpoints . UpsertAsync ( entities , token )
. ConfigureAwait ( false ) ;
var dtos = entities . Select ( Convert ) ;
return dtos ;
}
public async Task < int > UpdateStateAsync ( string uid , int id , SetpointsRequestDto setpointsRequestDto , CancellationToken token )
{
2021-11-24 17:58:53 +05:00
if ( setpointsRequestDto . IdState ! = 3 & & setpointsRequestDto . IdState ! = 4 )
2021-11-24 16:16:17 +05:00
throw new ArgumentOutOfRangeException ( nameof ( setpointsRequestDto ) , $"{nameof(setpointsRequestDto.IdState)} = {setpointsRequestDto.IdState}. Mast be 3 or 4." ) ;
2021-12-07 18:27:52 +05:00
var idWell = telemetryService . GetIdWellByTelemetryUid ( uid ) ? ? - 1 ;
2021-11-24 16:16:17 +05:00
if ( idWell < 0 )
return 0 ;
bool Predicate ( SetpointsRequest s ) = > s . Id = = id & & s . IdWell = = idWell & & s . IdState = = 2 ;
var entity = await cacheSetpoints . FirstOrDefaultAsync ( Predicate , token )
. ConfigureAwait ( false ) ;
2021-11-24 17:58:53 +05:00
if ( entity is null )
return 0 ;
2021-11-24 16:16:17 +05:00
entity . IdState = setpointsRequestDto . IdState ;
await cacheSetpoints . UpsertAsync ( entity , token )
. ConfigureAwait ( false ) ;
return 1 ;
}
public async Task < int > TryDelete ( int idWell , int id , CancellationToken token )
{
bool Predicate ( SetpointsRequest s ) = > s . Id = = id & & s . IdWell = = idWell & & s . IdState = = 1 ;
var isExist = await cacheSetpoints . ContainsAsync ( Predicate , token )
. ConfigureAwait ( false ) ;
if ( ! isExist )
return 0 ;
await cacheSetpoints . RemoveAsync ( Predicate , token )
. ConfigureAwait ( false ) ;
return 1 ;
}
public SetpointsRequest Convert ( SetpointsRequestDto src )
{
var entity = src . Adapt < SetpointsRequest > ( ) ;
return entity ;
}
public SetpointsRequestDto Convert ( SetpointsRequest src )
{
var dto = src . Adapt < SetpointsRequestDto > ( ) ;
return dto ;
}
2021-11-24 17:58:53 +05:00
public IEnumerable < SetpointInfoDto > GetSetpointsNames ( int idWell )
= > SetpointInfos . Values ;
2021-11-24 16:16:17 +05:00
}
}