start refactoring// fire alarm

This commit is contained in:
Фролов 2021-12-21 11:52:53 +05:00
parent a0208f412e
commit 6924a6bac7
17 changed files with 96 additions and 150 deletions

View File

@ -8,7 +8,8 @@ namespace AsbCloudApp.Data
public string Caption { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public int? IdDeposit { get; set; }
public DepositBaseDto Deposit { get; set; }
public IEnumerable<WellDto> Wells { get; set; }
}
}

View File

@ -2,13 +2,16 @@
namespace AsbCloudApp.Data
{
public class DepositDto : IMapPoint, IId
public class DepositBaseDto : IMapPoint, IId
{
public int Id { get; set; }
public string Caption { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
}
public class DepositDto : DepositBaseDto
{
public IEnumerable<ClusterDto> Clusters { get; set; }
}
}

View File

@ -1,10 +1,13 @@
namespace AsbCloudApp.Data
using System.Text.Json.Serialization;
namespace AsbCloudApp.Data
{
public class TelemetryDto : IId
{
public int Id { get; set; }
public string RemoteUid { get; set; }
public TelemetryInfoDto Info { get; set; }
public WellDto Well { get; set; }
public int? IdWell { get; set; }
public WellInfoDto Well { get; set; }
}
}

View File

@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
namespace AsbCloudApp.Data
{
public class WellDto : WellInfoDto, IMapPoint, IId
{
public int Id { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public string WellType { get; set; }
public int IdWellType { get; set; }
public int IdWellType { get; set; }
public int? IdCluster { get; set; }
/// <summary>
@ -22,5 +21,6 @@ namespace AsbCloudApp.Data
public DateTime LastTelemetryDate { get; set; }
public int? IdTelemetry { get; set; }
public TelemetryDto Telemetry { get; set; }
public IEnumerable<CompanyDto> Companies { get; set; }
}
}

View File

@ -1,11 +0,0 @@
namespace AsbCloudApp.Data
{
public class WellParamsDto
{
public string Caption { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public int IdWellType { get; set; }
public int IdState { get; set; }
}
}

View File

@ -1,8 +0,0 @@
using AsbCloudApp.Data;
namespace AsbCloudApp.Services
{
public interface ICachedCrudService<Tdto> where Tdto : IId
{
}
}

View File

@ -6,15 +6,13 @@ using System.Threading.Tasks;
namespace AsbCloudApp.Services
{
public interface IWellService
public interface IWellService: ICrudService<WellDto>
{
Task<IEnumerable<WellDto>> GetWellsByCompanyAsync(int idCompany, CancellationToken token);
Task<int?> UpdateWellAsync(int idWell, WellParamsDto dto, CancellationToken token = default);
Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, CancellationToken token);
Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token);
Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token);
Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token);
Task<WellDto> GetAsync(int idWell, CancellationToken token);
//TODO: remove that
Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token);
bool IsCompanyInvolvedInWell(int idCompany, int idWell);
string GetStateText(int state);
DateTime GetLastTelemetryDate(int idWell);

View File

@ -50,7 +50,6 @@ namespace AsbCloudDb.Model
DbSet<TEntity> Set<TEntity>() where TEntity : class;
IQueryable<Well> GetWellsForCompany(int idCompany);
Task<(DateTime From, DateTime To)> GetDatesRangeAsync<T>(int idTelemetry, CancellationToken token) where T : class, ITelemetryData;
Task<IEnumerable<(double? MinDepth, double? MaxDepth, DateTime BeginPeriodDate)>> GetDepthToIntervalAsync(int telemetryId,
int intervalHoursTimestamp, int workStartTimestamp, double timezoneOffset, CancellationToken token);

View File

@ -63,7 +63,6 @@ namespace AsbCloudInfrastructure
services.AddTransient<IWellOperationService, WellOperationService>();
// admin crud services:
services.AddTransient<ICrudService<WellDto>, CrudServiceBase<WellDto, Well>>();
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>();
services.AddTransient<ICrudService<DrillParamsDto>, DrillParamsService>();
services.AddTransient<ICrudService<DepositDto>, CrudServiceBase<DepositDto, Deposit>>();

View File

@ -11,6 +11,11 @@ namespace AsbCloudInfrastructure.Services.Cache
private readonly ConcurrentDictionary<string, CacheTableDataStore> cache =
new ConcurrentDictionary<string, CacheTableDataStore>();
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context, params string[] includes)
where TEntity : class
=> GetCachedTable<TEntity>(context, includes);
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context, IEnumerable<string> includes = null)
where TEntity : class
{

View File

@ -20,7 +20,7 @@ namespace AsbCloudInfrastructure.Services
public List<string> Includes { get; } = new ();
private CacheTable<TModel> Cache {
protected CacheTable<TModel> Cache {
get {
if(cache is null)
cache = cacheDb.GetCachedTable<TModel>((AsbCloudDbContext)db, Includes);
@ -103,13 +103,13 @@ namespace AsbCloudInfrastructure.Services
return affected;
}
private TModel Convert(TDto src)
protected virtual TModel Convert(TDto src)
{
var entity = src.Adapt<TModel>();
return entity;
}
private TDto Convert(TModel src)
protected virtual TDto Convert(TModel src)
{
var dto = src.Adapt<TDto>();
return dto;

View File

@ -68,7 +68,7 @@ namespace AsbCloudInfrastructure.Services
var entities = await query
.OrderBy(e => e.Id)
.ToListAsync(token).ConfigureAwait(false);
var dto = entities.Select(entity => Convert(entity));
var dto = entities.Select(Convert).ToList();
return dto;
}

View File

@ -104,7 +104,13 @@ namespace AsbCloudInfrastructure.Services
public IEnumerable<PermissionDto> GetNestedPermissions(int idUser)
{
var roles = GetRolesByIdUser(idUser);
return roles.SelectMany(r => r.Permissions);
if(roles?.Any() != true)
return null;
var permissions = roles
.Where(r => r.Permissions is not null)
.SelectMany(r => r.Permissions);
return permissions;
}
private async Task UpdateRolesCacheForUserAsync(int idUser, IEnumerable<string> newRoleNames, CancellationToken token)

View File

@ -12,24 +12,21 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class WellService : IWellService
{
private readonly IAsbCloudDbContext db;
public class WellService : CrudCacheServiceBase<WellDto, Well>, IWellService
{
private readonly ITelemetryService telemetryService;
private readonly CacheTable<RelationCompanyWell> cacheRelationCompaniesWells;
private readonly CacheTable<Well> cacheWells;
public WellService(IAsbCloudDbContext db, ITelemetryService telemetryService, CacheDb cacheDb)
public WellService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
:base(db, cacheDb)
{
this.db = db;
this.telemetryService = telemetryService;
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db, nameof(RelationCompanyWell.Company), nameof(RelationCompanyWell.Well));
}
public DateTime GetLastTelemetryDate(int idWell)
{
var well = cacheWells.FirstOrDefault(w => w.Id == idWell);
var well = Cache.FirstOrDefault(w => w.Id == idWell);
if (well?.IdTelemetry is null)
return DateTime.MinValue;
@ -38,30 +35,27 @@ namespace AsbCloudInfrastructure.Services
return lastTelemetryDate;
}
public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany,
CancellationToken token)
{
var activeTelemetryIds = telemetryService.GetTransmittingTelemetries()
.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);
var relations = await cacheRelationCompaniesWells
.WhereAsync(r => r.IdCompany == idCompany, token);
var dtos = relations.Select(r => Convert(r.Well));
return dtos;
}
public async Task<int?> UpdateWellAsync(int idWell, WellParamsDto dto,
public override Task<int> InsertAsync(WellDto newItem, CancellationToken token = default)
{
throw new NotImplementedException();
implement this
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
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)
@ -69,23 +63,16 @@ namespace AsbCloudInfrastructure.Services
if (dto.IdState is < 0 or > 2)
throw new ArgumentException("Текущее состояние работы скважины указано неправильно.", nameof(dto));
if(dto.Id != idWell)
throw new ArgumentException($"Нельзя поменять id для скважины: {idWell} => {dto.Id}.", nameof(dto));
var entity = await db.Wells
.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
var entity = Convert(dto);
if (entity is null)
throw new ArgumentException("Тип секции указан неправильно.", nameof(idWell));
var oldRelations = await cacheRelationCompaniesWells.FirstOrDefaultAsync(w => w.Id == idWell, token);
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);
var result = await Cache.UpsertAsync(entity, token);
return result;
}
public bool IsCompanyInvolvedInWell(int idCompany, int idWell)
@ -95,38 +82,25 @@ namespace AsbCloudInfrastructure.Services
=> 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);
if (entity is null)
return null;
var dto = Convert(entity);
return dto;
}
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
{
var entity = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token).ConfigureAwait(false);
var entity = await Cache.FirstOrDefaultAsync(w => w.Id == idWell, token).ConfigureAwait(false);
var dto = Convert(entity);
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>();
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;
}
public string GetStateText(int state)
@ -141,33 +115,32 @@ namespace AsbCloudInfrastructure.Services
public async Task<IEnumerable<int>> GetClusterWellsIdsAsync(int idWell, CancellationToken token)
{
var well = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token)
var well = await Cache.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
if (well is null)
return null;
var clusterWells = await cacheWells.WhereAsync(w => w.IdCluster == well.IdCluster, token)
var clusterWells = await Cache.WhereAsync(w => w.IdCluster == well.IdCluster, token)
.ConfigureAwait(false);
return clusterWells.Select(w => w.Id);
}
private WellDto Convert(Well well)
protected override WellDto Convert(Well entity)
{
return new WellDto
{
Id = well.Id,
Caption = well.Caption,
IdCluster = well.IdCluster,
Cluster = well.Cluster?.Caption,
Deposit = well.Cluster?.Deposit?.Caption,
LastTelemetryDate = GetLastTelemetryDate(well.Id),
IdWellType = well.IdWellType ?? default,
IdState = well.IdState,
Latitude = well.Latitude,
Longitude = well.Longitude,
};
var dto = base.Convert(entity);
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>();
return dto;
}
}
}

View File

@ -13,7 +13,8 @@ namespace AsbCloudWebApi.Controllers
public AdminClusterController(ICrudService<ClusterDto> service)
:base(service)
{
service.Includes.Add("Wells");
service.Includes.Add(nameof(ClusterDto.Wells));
service.Includes.Add(nameof(ClusterDto.Deposit));
}
}
}

View File

@ -10,10 +10,11 @@ namespace AsbCloudWebApi.Controllers
[Authorize]
public class AdminWellController : CrudController<WellDto, ICrudService<WellDto>>
{
public AdminWellController(ICrudService<WellDto> service)
public AdminWellController(IWellService service)
:base(service)
{
service.Includes.Add("Telemetry");
}
}
}

View File

@ -67,30 +67,6 @@ namespace AsbCloudWebApi.Controllers
return Ok(well);
}
/// <summary>
/// Возвращает список скважин, передающих телеметрию в данный момент
/// </summary>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Список скважин</returns>
[HttpGet("transmittingWells")]
[ProducesResponseType(typeof(IEnumerable<WellDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetTransmittingWellsAsync(CancellationToken token = default)
{
var idCompany = User.GetCompanyId();
if (idCompany is null)
return NoContent();
var transmittingWells = await wellService.GetTransmittingWellsAsync((int)idCompany,
token).ConfigureAwait(false);
if (transmittingWells is null || !transmittingWells.Any())
return NoContent();
return Ok(transmittingWells);
}
/// <summary>
/// Редактирует указанные поля скважины
/// </summary>
@ -102,7 +78,7 @@ namespace AsbCloudWebApi.Controllers
/// <returns></returns>
[HttpPut]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateWellAsync(int idWell, WellParamsDto dto,
public async Task<IActionResult> UpdateWellAsync(int idWell, WellDto dto,
CancellationToken token = default)
{
var idCompany = User.GetCompanyId();
@ -111,7 +87,7 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellService.UpdateWellAsync(idWell, dto, token)
var result = await wellService.UpdateAsync(idWell, dto, token)
.ConfigureAwait(false);
return Ok(result);