forked from ddrilling/AsbCloudServer
121 lines
4.2 KiB
C#
121 lines
4.2 KiB
C#
|
using AsbCloudApp.Data;
|
|||
|
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;
|
|||
|
|
|||
|
namespace AsbCloudInfrastructure.Services
|
|||
|
{
|
|||
|
public class SetpointsService : ISetpointsService, IConverter<SetpointsRequestDto, SetpointsRequest>
|
|||
|
{
|
|||
|
private readonly CacheTable<SetpointsRequest> cacheSetpoints;
|
|||
|
private readonly ITelemetryService telemetryService;
|
|||
|
|
|||
|
public SetpointsService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
|
|||
|
{
|
|||
|
cacheSetpoints = cacheDb.GetCachedTable<SetpointsRequest>(
|
|||
|
(AsbCloudDbContext)db,
|
|||
|
new List<string> {
|
|||
|
nameof(SetpointsRequest.Author),
|
|||
|
nameof(SetpointsRequest.Well),
|
|||
|
});
|
|||
|
this.telemetryService = telemetryService;
|
|||
|
}
|
|||
|
|
|||
|
public async Task<int> InsertAsync(SetpointsRequestDto setpoints, CancellationToken token)
|
|||
|
{
|
|||
|
setpoints.IdState = 1;
|
|||
|
var inserted = await cacheSetpoints.InsertAsync(Convert(setpoints), token)
|
|||
|
.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)
|
|||
|
{
|
|||
|
var idWell = telemetryService.GetidWellByTelemetryUid(uid) ?? -1;
|
|||
|
|
|||
|
if (idWell < 0)
|
|||
|
return null;
|
|||
|
|
|||
|
var entities = await cacheSetpoints.WhereAsync(s =>
|
|||
|
s.IdWell == idWell && s.IdState == 1 && s.UploadDate.AddSeconds(s.ObsolescenceSec) > DateTime.Now,
|
|||
|
token)
|
|||
|
.ConfigureAwait(false);
|
|||
|
|
|||
|
if (!entities.Any())
|
|||
|
return null;
|
|||
|
|
|||
|
foreach (var entity in entities)
|
|||
|
entity.IdState = 2;
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
if (setpointsRequestDto.IdState != 3 || setpointsRequestDto.IdState != 4)
|
|||
|
throw new ArgumentOutOfRangeException(nameof(setpointsRequestDto), $"{nameof(setpointsRequestDto.IdState)} = {setpointsRequestDto.IdState}. Mast be 3 or 4.");
|
|||
|
|
|||
|
var idWell = telemetryService.GetidWellByTelemetryUid(uid) ?? -1;
|
|||
|
|
|||
|
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);
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|