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

207 lines
8.6 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;
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
{
2021-12-21 11:52:53 +05:00
public class WellService : CrudCacheServiceBase<WellDto, Well>, IWellService
2021-12-22 11:41:18 +05:00
{
private static readonly TypeAdapterConfig typeAdapterConfig = TypeAdapterConfig<WellDto, Well>
.NewConfig()
.Ignore(dst => dst.Cluster,
dst => dst.RelationCompaniesWells,
dst => dst.Telemetry,
dst => dst.WellComposites,
dst => dst.WellCompositeSrcs,
dst => dst.WellOperations,
dst => dst.WellType)
.Config;
private readonly ITelemetryService telemetryService;
2021-07-21 17:23:57 +05:00
private readonly CacheTable<RelationCompanyWell> cacheRelationCompaniesWells;
2021-12-22 11:41:18 +05:00
private readonly CacheTable<CompanyType> cacheCompanyWellTypes;
2021-12-21 11:52:53 +05:00
public WellService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
:base(db, cacheDb)
{
this.telemetryService = telemetryService;
2021-12-21 11:52:53 +05:00
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db, nameof(RelationCompanyWell.Company), nameof(RelationCompanyWell.Well));
2021-12-22 11:41:18 +05:00
cacheCompanyWellTypes = cacheDb.GetCachedTable<CompanyType>((AsbCloudDbContext)db);
Includes.Add($"{nameof(Well.Cluster)}.{nameof(Cluster.Deposit)}");
Includes.Add(nameof(Well.Telemetry));
Includes.Add($"{nameof(Well.RelationCompaniesWells)}.{nameof(RelationCompanyWell.Company)}");
Includes.Add(nameof(Well.WellType));
}
public DateTime GetLastTelemetryDate(int idWell)
{
2021-12-21 11:52:53 +05:00
var well = Cache.FirstOrDefault(w => w.Id == idWell);
if (well?.IdTelemetry is null)
return DateTime.MinValue;
var lastTelemetryDate = telemetryService.GetLastTelemetryDate((int)well.IdTelemetry);
return lastTelemetryDate;
}
2021-12-21 11:52:53 +05:00
public async Task<IEnumerable<WellDto>> GetWellsByCompanyAsync(int idCompany, CancellationToken token)
{
2021-12-21 11:52:53 +05:00
var relations = await cacheRelationCompaniesWells
.WhereAsync(r => r.IdCompany == idCompany, token);
2021-12-22 11:41:18 +05:00
var wellsIds = relations.Select(r => r.IdWell);
var wells = await Cache.WhereAsync(w => wellsIds.Contains(w.Id));
var dtos = wells.Select(Convert);
2021-12-21 11:52:53 +05:00
return dtos;
}
2021-12-22 11:41:18 +05:00
public override async Task<int> InsertAsync(WellDto dto, CancellationToken token = default)
{
2021-12-22 11:41:18 +05:00
if (dto.IdWellType is < 1 or > 2)
throw new ArgumentException("Тип скважины указан неправильно.", nameof(dto));
if (dto.IdState is < 0 or > 2)
throw new ArgumentException("Текущее состояние работы скважины указано неправильно.", nameof(dto));
if (dto.Id != 0 && await Cache.ContainsAsync(w => w.Id == dto.Id, token))
throw new ArgumentException($"Нельзя повторно добавить скважину с id: {dto.Id}", nameof(dto));
var entity = Convert(dto);
var result = await Cache.InsertAsync(entity, token);
if (dto.Companies.Any())
{
var newRelations = dto.Companies.Select(c => new RelationCompanyWell { IdWell = result.Id, IdCompany = c.Id });
await cacheRelationCompaniesWells.InsertAsync(newRelations, token);
}
return result.Id;
}
2021-12-21 11:52:53 +05:00
public override Task<int> InsertRangeAsync(IEnumerable<WellDto> dtos, CancellationToken token)
{
throw new NotImplementedException();
}
public override async Task<int> UpdateAsync(int idWell, WellDto dto,
CancellationToken token = default)
{
if (dto.IdWellType is < 1 or > 2)
2021-10-27 17:48:19 +05:00
throw new ArgumentException("Тип скважины указан неправильно.", nameof(dto));
if (dto.IdState is < 0 or > 2)
2021-10-27 17:48:19 +05:00
throw new ArgumentException("Текущее состояние работы скважины указано неправильно.", nameof(dto));
2021-12-21 11:52:53 +05:00
if(dto.Id != idWell)
throw new ArgumentException($"Нельзя поменять id для скважины: {idWell} => {dto.Id}.", nameof(dto));
2021-10-27 17:48:19 +05:00
2021-12-21 11:52:53 +05:00
var entity = Convert(dto);
2021-12-22 11:41:18 +05:00
var oldRelations = await cacheRelationCompaniesWells
.WhereAsync(r => r.IdWell == idWell, token);
if(dto.Companies.Count() != oldRelations.Count() ||
dto.Companies.Any(c => !oldRelations.Any(oldC => oldC.IdCompany == c.Id)))
{
await cacheRelationCompaniesWells.RemoveAsync(r => r.IdWell == idWell, token);
var newRelations = dto.Companies.Select(c=> new RelationCompanyWell {IdWell = idWell, IdCompany = c.Id });
await cacheRelationCompaniesWells.InsertAsync(newRelations, token);
}
2021-12-21 11:52:53 +05:00
var result = await Cache.UpsertAsync(entity, token);
return result;
}
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-25 11:13:56 +05:00
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
{
2021-12-21 11:52:53 +05:00
var entity = await Cache.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)
{
2021-12-21 11:52:53 +05:00
var relations = await cacheRelationCompaniesWells.WhereAsync(r => r.IdWell == idWell, token);
var dtos = relations.Select(r => Convert(r.Company));
return dtos;
}
private IEnumerable<CompanyDto> GetCompanies(int idWell)
{
var relations = cacheRelationCompaniesWells.Where(r => r.IdWell == idWell);
var dtos = relations.Select(r => Convert(r.Company));
return dtos;
2021-08-25 11:13:56 +05:00
}
public string GetStateText(int state)
{
return state switch
{
1 => "В работе",
2 => "Завершена",
_ => "Незвестно",
};
}
public async Task<IEnumerable<int>> GetClusterWellsIdsAsync(int idWell, CancellationToken token)
{
2021-12-21 11:52:53 +05:00
var well = await Cache.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
if (well is null)
return null;
2021-12-21 11:52:53 +05:00
var clusterWells = await Cache.WhereAsync(w => w.IdCluster == well.IdCluster, token)
.ConfigureAwait(false);
return clusterWells.Select(w => w.Id);
}
2021-12-22 11:41:18 +05:00
protected override Well Convert(WellDto dto)
{
var entity = dto.Adapt<Well>(typeAdapterConfig);
//dto.WellType = entity.WellType?.Caption;
//dto.Cluster = entity.Cluster?.Caption;
//dto.Deposit = entity.Cluster?.Deposit?.Caption;
//dto.LastTelemetryDate = GetLastTelemetryDate(entity.Id);
//dto.Companies = GetCompanies(entity.Id);
return entity;
}
2021-12-21 11:52:53 +05:00
protected override WellDto Convert(Well entity)
{
2021-12-21 11:52:53 +05:00
var dto = base.Convert(entity);
2021-12-22 11:41:18 +05:00
dto.WellType = entity.WellType?.Caption;
2021-12-21 11:52:53 +05:00
dto.Cluster = entity.Cluster?.Caption;
dto.Deposit = entity.Cluster?.Deposit?.Caption;
dto.LastTelemetryDate = GetLastTelemetryDate(entity.Id);
dto.Companies = GetCompanies(entity.Id);
return dto;
}
private CompanyDto Convert(Company entity)
{
var dto = entity.Adapt<CompanyDto>();
2021-12-22 11:41:18 +05:00
dto.CompanyTypeCaption = entity.CompanyType?.Caption
?? cacheCompanyWellTypes.FirstOrDefault(c => c.Id == entity.IdCompanyType).Caption;
2021-12-21 11:52:53 +05:00
return dto;
}
}
}