DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/SAUB/SetpointsService.cs

115 lines
5.7 KiB
C#
Raw Normal View History

2022-04-11 18:00:34 +05:00
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Repository;
2022-06-15 14:57:37 +05:00
using Microsoft.EntityFrameworkCore;
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
{
2022-06-15 14:57:37 +05:00
public class SetpointsService : ISetpointsService
{
// ## Инфо от АПГ от 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 = "Дифференциальное рабочее давление в слайде, атм" } },
{ "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-06-15 14:57:37 +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
};
private readonly SetpointsRequestRepository setpointsRepository;
private readonly ITelemetryService telemetryService;
public SetpointsService(IAsbCloudDbContext db, ITelemetryService telemetryService, IWellService wellService)
{
setpointsRepository = new SetpointsRequestRepository(db, wellService);
this.telemetryService = telemetryService;
}
2022-06-15 14:57:37 +05:00
public async Task<int> InsertAsync(SetpointsRequestDto setpointsRequest, CancellationToken token)
{
2022-06-15 14:57:37 +05:00
setpointsRequest.IdState = 1;
setpointsRequest.UploadDate = DateTime.UtcNow;
var result = await setpointsRepository.InsertAsync(setpointsRequest, token);
return result;
}
public async Task<IEnumerable<SetpointsRequestDto>> GetAsync(int idWell, CancellationToken token)
{
2022-06-15 14:57:37 +05:00
var all = await setpointsRepository.GetAllAsync(token);
var dtos = all.Where(s => s.IdWell == idWell);
return dtos;
}
public async Task<IEnumerable<SetpointsRequestDto>> GetForPanelAsync(string uid, CancellationToken token)
{
var idWell = telemetryService.GetIdWellByTelemetryUid(uid) ?? -1;
if (idWell < 0)
return null;
2022-06-15 14:57:37 +05:00
var all = await setpointsRepository.GetAllAsync(token);
var filtered = all.Where(s =>
s.IdWell == idWell &&
s.IdState == 1 &&
s.UploadDate.AddSeconds(s.ObsolescenceSec) > DateTime.UtcNow)
.ToList();
2022-06-15 14:57:37 +05:00
if (!filtered.Any())
return null;
foreach (var item in filtered)
{
item.IdState = 2;
item.UploadDate = DateTime.SpecifyKind(item.UploadDate, DateTimeKind.Utc);
}
2022-04-11 18:00:34 +05:00
2022-06-15 14:57:37 +05:00
await setpointsRepository.UpdateRangeAsync(filtered, token);
2022-06-15 14:57:37 +05:00
return filtered;
}
public async Task<int> UpdateStateAsync(SetpointsRequestDto setpointsRequestDto, CancellationToken token)
{
2021-11-24 17:58:53 +05:00
if (setpointsRequestDto.IdState != 3 && setpointsRequestDto.IdState != 4)
throw new ArgumentOutOfRangeException(nameof(setpointsRequestDto), $"{nameof(setpointsRequestDto.IdState)} = {setpointsRequestDto.IdState}. Mast be 3 or 4.");
if (setpointsRequestDto.Id <= 0)
throw new ArgumentOutOfRangeException(nameof(setpointsRequestDto), $"{nameof(setpointsRequestDto.Id)} = {setpointsRequestDto.Id}. Mast be > 0");
if (setpointsRequestDto.IdWell <= 0)
throw new ArgumentOutOfRangeException(nameof(setpointsRequestDto), $"{nameof(setpointsRequestDto.IdWell)} = {setpointsRequestDto.IdWell}. Mast be > 0");
var entity = await setpointsRepository.GetOrDefaultAsync(setpointsRequestDto.Id, token);
if (entity.IdWell != setpointsRequestDto.IdWell)
return 0;
2021-11-24 17:58:53 +05:00
if (entity is null)
return 0;
entity.IdState = setpointsRequestDto.IdState;
2022-06-15 14:57:37 +05:00
var affected = await setpointsRepository.UpdateAsync(entity, token);
return affected;
}
2022-06-15 14:57:37 +05:00
public async Task<int> TryDelete(int id, CancellationToken token)
{
2022-06-15 14:57:37 +05:00
var affected = await setpointsRepository.DeleteAsync(id, token);
return affected;
}
2021-11-24 17:58:53 +05:00
2022-06-15 14:57:37 +05:00
public IEnumerable<SetpointInfoDto> GetSetpointsNames()
=> SetpointInfos.Values;
}
}