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 string Caption { get; set; }
public double? Latitude { get; set; } public double? Latitude { get; set; }
public double? Longitude { get; set; } public double? Longitude { get; set; }
public int? IdDeposit { get; set; }
public DepositBaseDto Deposit { get; set; }
public IEnumerable<WellDto> Wells { get; set; } public IEnumerable<WellDto> Wells { get; set; }
} }
} }

View File

@ -2,13 +2,16 @@
namespace AsbCloudApp.Data namespace AsbCloudApp.Data
{ {
public class DepositDto : IMapPoint, IId public class DepositBaseDto : IMapPoint, IId
{ {
public int Id { get; set; } public int Id { get; set; }
public string Caption { get; set; } public string Caption { get; set; }
public double? Latitude { get; set; } public double? Latitude { get; set; }
public double? Longitude { get; set; } public double? Longitude { get; set; }
}
public class DepositDto : DepositBaseDto
{
public IEnumerable<ClusterDto> Clusters { get; set; } 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 class TelemetryDto : IId
{ {
public int Id { get; set; } public int Id { get; set; }
public string RemoteUid { get; set; } public string RemoteUid { get; set; }
public TelemetryInfoDto Info { 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;
using System.Collections.Generic;
namespace AsbCloudApp.Data namespace AsbCloudApp.Data
{ {
public class WellDto : WellInfoDto, IMapPoint, IId public class WellDto : WellInfoDto, IMapPoint, IId
{ {
public int Id { get; set; } public int Id { get; set; }
public double? Latitude { get; set; } public double? Latitude { get; set; }
public double? Longitude { get; set; } public double? Longitude { get; set; }
public string WellType { get; set; } public string WellType { get; set; }
public int IdWellType { get; set; } public int IdWellType { get; set; }
public int? IdCluster { get; set; } public int? IdCluster { get; set; }
/// <summary> /// <summary>
@ -22,5 +21,6 @@ namespace AsbCloudApp.Data
public DateTime LastTelemetryDate { get; set; } public DateTime LastTelemetryDate { get; set; }
public int? IdTelemetry { get; set; } public int? IdTelemetry { get; set; }
public TelemetryDto Telemetry { 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 namespace AsbCloudApp.Services
{ {
public interface IWellService public interface IWellService: ICrudService<WellDto>
{ {
Task<IEnumerable<WellDto>> GetWellsByCompanyAsync(int idCompany, CancellationToken token); 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<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token);
Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token); Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token);
Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token); //TODO: remove that
Task<WellDto> GetAsync(int idWell, CancellationToken token); Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token);
bool IsCompanyInvolvedInWell(int idCompany, int idWell); bool IsCompanyInvolvedInWell(int idCompany, int idWell);
string GetStateText(int state); string GetStateText(int state);
DateTime GetLastTelemetryDate(int idWell); DateTime GetLastTelemetryDate(int idWell);

View File

@ -50,7 +50,6 @@ namespace AsbCloudDb.Model
DbSet<TEntity> Set<TEntity>() where TEntity : class; 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<(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, Task<IEnumerable<(double? MinDepth, double? MaxDepth, DateTime BeginPeriodDate)>> GetDepthToIntervalAsync(int telemetryId,
int intervalHoursTimestamp, int workStartTimestamp, double timezoneOffset, CancellationToken token); int intervalHoursTimestamp, int workStartTimestamp, double timezoneOffset, CancellationToken token);

View File

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

View File

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

View File

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

View File

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

View File

@ -104,7 +104,13 @@ namespace AsbCloudInfrastructure.Services
public IEnumerable<PermissionDto> GetNestedPermissions(int idUser) public IEnumerable<PermissionDto> GetNestedPermissions(int idUser)
{ {
var roles = GetRolesByIdUser(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) private async Task UpdateRolesCacheForUserAsync(int idUser, IEnumerable<string> newRoleNames, CancellationToken token)

View File

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

View File

@ -13,7 +13,8 @@ namespace AsbCloudWebApi.Controllers
public AdminClusterController(ICrudService<ClusterDto> service) public AdminClusterController(ICrudService<ClusterDto> service)
:base(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] [Authorize]
public class AdminWellController : CrudController<WellDto, ICrudService<WellDto>> public class AdminWellController : CrudController<WellDto, ICrudService<WellDto>>
{ {
public AdminWellController(ICrudService<WellDto> service) public AdminWellController(IWellService service)
:base(service) :base(service)
{ {
service.Includes.Add("Telemetry"); service.Includes.Add("Telemetry");
} }
} }
} }

View File

@ -67,30 +67,6 @@ namespace AsbCloudWebApi.Controllers
return Ok(well); 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>
/// Редактирует указанные поля скважины /// Редактирует указанные поля скважины
/// </summary> /// </summary>
@ -102,7 +78,7 @@ namespace AsbCloudWebApi.Controllers
/// <returns></returns> /// <returns></returns>
[HttpPut] [HttpPut]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [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) CancellationToken token = default)
{ {
var idCompany = User.GetCompanyId(); var idCompany = User.GetCompanyId();
@ -111,7 +87,7 @@ namespace AsbCloudWebApi.Controllers
idWell, token).ConfigureAwait(false)) idWell, token).ConfigureAwait(false))
return Forbid(); return Forbid();
var result = await wellService.UpdateWellAsync(idWell, dto, token) var result = await wellService.UpdateAsync(idWell, dto, token)
.ConfigureAwait(false); .ConfigureAwait(false);
return Ok(result); return Ok(result);