DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/WellService.cs
Фролов eab95cb7a1 Refactor GetLastTelemetryDate(..) in services (single resp).
Add State and LastTelemetryDate into StatWellDto.
2021-10-20 12:52:31 +05:00

135 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class WellService : IWellService
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
private readonly CacheTable<RelationCompanyWell> cacheRelationCompaniesWells;
private readonly CacheTable<Well> cacheWells;
public WellService(IAsbCloudDbContext db, ITelemetryService telemetryService, CacheDb cacheDb)
{
this.db = db;
this.telemetryService = telemetryService;
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
public DateTime GetLastTelemetryDate(int idWell)
{
var lastTelemetryDate = DateTime.MinValue;
var well = cacheWells.FirstOrDefault(w => w.Id == idWell);
if (well is null || well.IdTelemetry is null)
return lastTelemetryDate;
lastTelemetryDate = telemetryService.GetLastTelemetryDate(well.IdTelemetry??0);
return lastTelemetryDate;
}
public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany,
CancellationToken token) =>
await telemetryService.GetTransmittingWellsAsync(idCompany, token);
public async Task<IEnumerable<WellDto>> GetWellsByCompanyAsync(int idCompany, CancellationToken token)
{
var wells = await db.GetWellsForCompany(idCompany).ToListAsync(token);
return wells.Select(w => new WellDto
{
Id = w.Id,
Caption = w.Caption,
Cluster = w.Cluster.Caption,
Deposit = w.Cluster.Deposit.Caption,
});
}
public async Task<int?> UpdateWellAsync(int idWell, WellParamsDto dto,
CancellationToken token = default)
{
if (dto.IdWellType is < 1 or > 2)
throw new ArgumentException("Тип секции указан неправильно.", nameof(dto));
if (dto.IdState is < 0 or > 2)
throw new ArgumentException("Текущее состояние работы скважины" +
"указано неправильно.", nameof(dto));
var entity = await db.Wells
.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
if (entity is null)
throw new ArgumentException("Тип секции указан неправильно.", nameof(idWell));
entity.Caption = dto.Caption;
entity.Latitude = dto.Latitude;
entity.Longitude = dto.Longitude;
entity.IdWellType = dto.IdWellType;
entity.IdState = dto.IdState;
db.Wells.Update(entity);
return await db.SaveChangesAsync(token);
}
public bool IsCompanyInvolvedInWell(int idCompany, int idWell)
=> cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany);
public async Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token)
=> await cacheRelationCompaniesWells.ContainsAsync(r => r.IdWell == idWell &&
r.IdCompany == idCompany, token).ConfigureAwait(false);
public async Task<WellDto> GetAsync(int idWell, CancellationToken token)
{
var entity = await db.Wells
.Include(w => w.Cluster)
.ThenInclude(c => c.Deposit)
.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
var dto = entity.Adapt<WellDto>();
dto.Cluster = entity.Cluster?.Caption;
dto.Deposit = entity.Cluster?.Deposit?.Caption;
return dto;
}
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
{
var entity = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token).ConfigureAwait(false);
var dto = entity.Adapt<WellDto>();
return dto.Caption;
}
public async Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token)
{
var well = await db.Wells
.Include(w => w.RelationCompaniesWells)
.ThenInclude(r => r.Company)
.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
var companies = well.RelationCompaniesWells.Select(r => r.Company);
return companies.Adapt<CompanyDto>();
}
public string GetStateText(int state)
{
return state switch
{
1 => "В работе",
2 => "Завершена",
_ => "Незвестно",
};
}
}
}