DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/WellService.cs

154 lines
5.8 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
2021-07-28 09:46:58 +05:00
using Mapster;
using Microsoft.EntityFrameworkCore;
2021-07-21 15:29:19 +05:00
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;
2021-07-21 17:23:57 +05:00
private readonly CacheTable<RelationCompanyWell> cacheRelationCompaniesWells;
2021-08-25 11:13:56 +05:00
private readonly CacheTable<Well> cacheWells;
public WellService(IAsbCloudDbContext db, ITelemetryService telemetryService, CacheDb cacheDb)
{
this.db = db;
this.telemetryService = telemetryService;
2021-07-21 17:23:57 +05:00
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db);
2021-08-25 11:13:56 +05:00
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
public DateTime GetLastTelemetryDate(int idWell)
{
var well = cacheWells.FirstOrDefault(w => w.Id == idWell);
if (well?.IdTelemetry is null)
return DateTime.MinValue;
var lastTelemetryDate = telemetryService.GetLastTelemetryDate((int)well.IdTelemetry);
return lastTelemetryDate;
}
public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany,
CancellationToken token)
{
var activeTelemetryIds = telemetryService.GetTransmittingTelemetriesAsync(idCompany)
.Select(t => t.Id);
var wells = await (from w in db.GetWellsForCompany(idCompany)
where w.IdTelemetry != null &&
activeTelemetryIds.Contains((int)w.IdTelemetry)
select w)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
return wells.Select(Convert);
}
public async Task<IEnumerable<WellDto>> GetWellsByCompanyAsync(int idCompany, CancellationToken token)
{
var wells = await db.GetWellsForCompany(idCompany).ToListAsync(token);
return wells.Select(Convert);
}
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);
2021-08-29 17:25:16 +05:00
public async Task<WellDto> GetAsync(int idWell, CancellationToken token)
{
var entity = await db.Wells
2021-09-10 11:28:57 +05:00
.Include(w => w.Cluster)
.ThenInclude(c => c.Deposit)
2021-08-29 17:25:16 +05:00
.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
if (entity is null)
return null;
var dto = Convert(entity);
2021-08-29 17:25:16 +05:00
return dto;
}
2021-08-25 11:13:56 +05:00
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
{
var entity = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token).ConfigureAwait(false);
var dto = Convert(entity);
2021-08-25 11:13:56 +05:00
return dto.Caption;
}
public async Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token)
{
var well = await db.Wells
2021-09-10 11:28:57 +05:00
.Include(w => w.RelationCompaniesWells)
.ThenInclude(r => r.Company)
2021-08-25 11:13:56 +05:00
.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 => "Завершена",
_ => "Незвестно",
};
}
private WellDto Convert(Well well)
{
return new WellDto
{
Id = well.Id,
Caption = well.Caption,
Cluster = well.Cluster.Caption,
Deposit = well.Cluster.Deposit.Caption,
LastTelemetryDate = GetLastTelemetryDate(well.Id)
};
}
}
}