DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Subsystems/SubsystemService.cs

55 lines
2.0 KiB
C#

using AsbCloudApp.Data.Subsystems;
using AsbCloudApp.Services;
using AsbCloudApp.Services.Subsystems;
using AsbCloudDb.Model;
using AsbCloudDb.Model.Subsystems;
using AsbCloudInfrastructure.Repository;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.Subsystems
{
#nullable enable
internal class SubsystemService : CrudCacheRepositoryBase<SubsystemDto, Subsystem>, ISubsystemService
{
private readonly IWellService wellService;
public SubsystemService(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, IWellService wellService) : base(dbContext, memoryCache)
{
this.wellService = wellService;
}
private async Task<IEnumerable<SubsystemDto>?> GetSubsystemByIdWellAsync(int idWell, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(idWell, token);
if (well?.IdTelemetry is null || well.Timezone is null)
return null;
var entities = await dbContext.SubsystemOperationTimes
.Include(e => e.Subsystem)
.AsNoTracking()
.Where(o => o.IdTelemetry == well.IdTelemetry)
.Select(o => o.Subsystem)
.Distinct()
.ToArrayAsync(token);
var dtos = entities.Select(e => e.Adapt<SubsystemDto>());
return dtos;
}
public async Task<IEnumerable<SubsystemDto>?> GetSubsystemAsync(int? idWell, CancellationToken token)
{
if (idWell.HasValue)
{
var subsystemWell = await GetSubsystemByIdWellAsync(idWell.Value, token);
return subsystemWell;
}
var subsystem = await GetAllAsync(token);
return subsystem;
}
}
#nullable disable
}