Реализация репозиториев

This commit is contained in:
Степанов Дмитрий 2023-10-09 17:00:46 +05:00
parent 5d71ff7393
commit 17a4d7c7fd
7 changed files with 208 additions and 223 deletions

View File

@ -32,6 +32,20 @@ namespace AsbCloudInfrastructure.Repository
return dtos;
}
public async Task<int> RemoveByWellAsync(int idWell, CancellationToken cancellationToken)
{
var query = GetQuery().Where(x => x.IdWell == idWell);
dbSet.RemoveRange(query);
var result = await dbContext.SaveChangesAsync(cancellationToken);
if(result > 0)
DropCache();
return result;
}
public async Task<IEnumerable<TDto>> GetByIdWellAsync(IEnumerable<int> idsWells, CancellationToken token)
{
if (!idsWells.Any())

View File

@ -29,6 +29,15 @@ namespace AsbCloudInfrastructure.Repository
return dtos;
}
public Task<int> RemoveByWellAsync(int idWell, CancellationToken cancellationToken)
{
var query = GetQuery().Where(x => x.IdWell == idWell);
dbSet.RemoveRange(query);
return dbContext.SaveChangesAsync(cancellationToken);
}
public async Task<IEnumerable<TDto>> GetByIdWellAsync(IEnumerable<int> idsWells, CancellationToken token)
{
if (!idsWells.Any())

View File

@ -1,169 +0,0 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
public class ProcessMapRepository : CrudWellRelatedRepositoryBase<ProcessMapPlanDto, ProcessMap>,
IProcessMapPlanRepository
{
private readonly IWellService wellService;
public ProcessMapRepository(IAsbCloudDbContext context, IWellService wellService)
: base(context, dbSet =>
dbSet.Include(x => x.Well)
.Include(x => x.WellSectionType)
)
{
this.wellService = wellService;
}
public async Task<IEnumerable<ProcessMapPlanDto>> GetAllAsync(int idWell,
DateTime? updateFrom, CancellationToken token)
{
var requests = new[]
{
new ProcessMapRequest {
IdWell = idWell,
UpdateFrom = updateFrom
}
};
var entities = await BuildQuery(requests)
.OrderBy(e => e.DepthStart)
.ThenBy(e => e.Id)
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Select(Convert);
return dtos;
}
public async Task<IEnumerable<ProcessMapPlanDto>> GetProcessMapAsync(IEnumerable<ProcessMapRequest> requests, CancellationToken token)
{
var entities = await BuildQuery(requests)
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Select(Convert).ToList();
return dtos;
}
public Task<int> RemoveByWellAsync(int idWell, CancellationToken cancellationToken)
{
var query = dbContext.ProcessMap.Where(x => x.IdWell == idWell);
dbContext.ProcessMap.RemoveRange(query);
return dbContext.SaveChangesAsync(cancellationToken);
}
public override async Task<int> InsertAsync(ProcessMapPlanDto dto,
CancellationToken token)
{
dto.LastUpdate = DateTime.UtcNow;
var result = await base.InsertAsync(dto, token);
return result;
}
public override async Task<int> UpdateAsync(ProcessMapPlanDto dto,
CancellationToken token)
{
dto.LastUpdate = DateTime.UtcNow;
var result = await base.UpdateAsync(dto, token);
return result;
}
private IQueryable<ProcessMap> BuildQuery(IEnumerable<ProcessMapRequest> requests)
{
var query = GetQuery();
if (requests.Any())
{
Expression<Func<ProcessMap, bool>> predicate = map => false;
foreach (var request in requests)
{
Expression<Func<ProcessMap, bool>> predicate2 = map => map.IdWell == request.IdWell;
if (request.IdWellSectionType is not null)
predicate2 = predicate2.And(map => map.IdWellSectionType == request.IdWellSectionType);
if (request.UpdateFrom is not null)
{
var timezone = wellService.GetTimezone(request.IdWell);
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
predicate2 = predicate2.And(map => map.LastUpdate >= updateFromUtc);
}
predicate = predicate.Or(predicate2);
}
query = query.Where(predicate);
}
return query;
}
protected override ProcessMapPlanDto Convert(ProcessMap entity)
{
var dto = entity.Adapt<ProcessMapPlanDto>();
dto.LastUpdate = entity.LastUpdate.ToRemoteDateTime(entity.Well.Timezone.Hours);
dto.AxialLoad = new PlanLimitDto
{
LimitMax = entity.AxialLoadLimitMax,
Plan = entity.AxialLoadPlan
};
dto.Flow = new PlanLimitDto
{
LimitMax = entity.FlowLimitMax,
Plan = entity.FlowPlan
};
dto.Pressure = new PlanLimitDto
{
LimitMax = entity.PressureLimitMax,
Plan = entity.PressurePlan
};
dto.TopDriveSpeed = new PlanLimitDto
{
LimitMax = entity.TopDriveSpeedLimitMax,
Plan = entity.TopDriveSpeedPlan
};
dto.TopDriveTorque = new PlanLimitDto
{
LimitMax = entity.TopDriveTorqueLimitMax,
Plan = entity.TopDriveTorquePlan
};
return dto;
}
protected override ProcessMap Convert(ProcessMapPlanDto dto)
{
var entity = dto.Adapt<ProcessMap>();
entity.AxialLoadPlan = dto.AxialLoad.Plan;
entity.AxialLoadLimitMax = dto.AxialLoad.LimitMax;
entity.FlowPlan = dto.Flow.Plan;
entity.FlowLimitMax = dto.Flow.LimitMax;
entity.PressurePlan = dto.Pressure.Plan;
entity.PressureLimitMax = dto.Pressure.LimitMax;
entity.TopDriveSpeedPlan = dto.TopDriveSpeed.Plan;
entity.TopDriveSpeedLimitMax = dto.TopDriveSpeed.LimitMax;
entity.TopDriveTorquePlan = dto.TopDriveTorque.Plan;
entity.TopDriveTorqueLimitMax = dto.TopDriveTorque.LimitMax;
return entity;
}
}
}

View File

@ -1,46 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Repository;
public class ProcessMapWellboreDevelopmentRepository :
CrudWellRelatedRepositoryBase<ProcessMapWellboreDevelopmentDto, ProcessMapWellboreDevelopment>,
IProcessMapWellboreDevelopmentRepository
{
private readonly IWellService wellService;
public ProcessMapWellboreDevelopmentRepository(IAsbCloudDbContext context, IWellService wellService) : base(context)
{
this.wellService = wellService;
}
public async Task<IEnumerable<ProcessMapWellboreDevelopmentDto>> GetAllAsync(int idWell, DateTime? updateFrom,
CancellationToken cancellationToken)
{
var query = dbContext.ProcessMapWellboreDevelopments
.Where(p => p.IdWell == idWell);
if (updateFrom.HasValue)
{
var timezone = wellService.GetTimezone(idWell);
var updateFromUtc = updateFrom.Value.ToUtcDateTimeOffset(timezone.Hours);
query = query.Where(p => p.LastUpdate >= updateFromUtc);
}
var entities = await query
.OrderBy(p => p.DepthStart)
.ThenBy(p => p.Id)
.AsNoTracking()
.ToArrayAsync(cancellationToken);
return entities.Select(Convert);
}
}

View File

@ -1,5 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudDb.Model;
@ -17,12 +17,12 @@ namespace AsbCloudInfrastructure.Repository
public class WellCompositeRepository : IWellCompositeRepository
{
private readonly IAsbCloudDbContext db;
private readonly IProcessMapPlanRepository processMapRepository;
private readonly IWellDrillingProcessMapRepository wellDrillingProcessMapRepository;
public WellCompositeRepository(IAsbCloudDbContext db, IProcessMapPlanRepository processMapRepository)
public WellCompositeRepository(IAsbCloudDbContext db, IWellDrillingProcessMapRepository wellDrillingProcessMapRepository)
{
this.db = db;
this.processMapRepository = processMapRepository;
this.wellDrillingProcessMapRepository = wellDrillingProcessMapRepository;
}
/// <inheritdoc/>
@ -50,16 +50,16 @@ namespace AsbCloudInfrastructure.Repository
}
/// <inheritdoc/>
public async Task<IEnumerable<ProcessMapPlanDto>> GetCompositeProcessMap(int idWell, CancellationToken token)
public async Task<IEnumerable<WellDrillingProcessMapDto>> GetCompositeProcessMap(int idWell, CancellationToken token)
{
var dtos = await GetAsync(idWell, token);
var requests = dtos.Select(x => new ProcessMapRequest {
var requests = dtos.Select(x => new WellDrillingProcessMapRequest {
IdWell = x.IdWellSrc,
IdWellSectionType = x.IdWellSectionType
});
var result = await processMapRepository.GetProcessMapAsync(requests, token);
var result = await wellDrillingProcessMapRepository.GetAsync(requests, token);
return result;
}
@ -75,5 +75,5 @@ namespace AsbCloudInfrastructure.Repository
return dto;
}
}
}

View File

@ -0,0 +1,129 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudDb.Model.ProcessMaps;
using Mapster;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Repository;
public class WellDrillingProcessMapRepository : CrudWellRelatedRepositoryBase<WellDrillingProcessMapDto, WellDrillingProcessMap>,
IWellDrillingProcessMapRepository
{
private readonly IWellService wellService;
public WellDrillingProcessMapRepository(IAsbCloudDbContext context, IWellService wellService)
: base(context, dbSet =>
dbSet
.Include(p => p.WellSectionType)
.Include(p => p.Well))
{
this.wellService = wellService;
}
public async Task<IEnumerable<WellDrillingProcessMapDto>> GetAsync(WellDrillingProcessMapRequest request,
CancellationToken cancellationToken)
{
var requests = new[] { request };
var entities = await BuildQuery(requests)
.ToArrayAsync(cancellationToken);
return entities.Select(Convert);
}
public async Task<IEnumerable<WellDrillingProcessMapDto>> GetAsync(
IEnumerable<WellDrillingProcessMapRequest> requests,
CancellationToken cancellationToken)
{
var entities = await BuildQuery(requests)
.ToArrayAsync(cancellationToken);
return entities.Select(Convert);
}
private IQueryable<WellDrillingProcessMap> BuildQuery(IEnumerable<WellDrillingProcessMapRequest> requests)
{
var query = GetQuery();
foreach (var request in requests)
{
query = query.Where(p => p.IdWell == request.IdWell);
if (request.IdWellSectionType is not null)
query = query.Where(p => p.IdWellSectionType == request.IdWellSectionType);
if (request.UpdateFrom is not null)
{
var timezone = wellService.GetTimezone(request.IdWell);
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
query = query.Where(p => p.LastUpdate >= updateFromUtc);
}
}
return query.OrderBy(e => e.DepthStart)
.ThenBy(e => e.Id)
.AsNoTracking();
}
protected override WellDrillingProcessMapDto Convert(WellDrillingProcessMap entity)
{
var dto = entity.Adapt<WellDrillingProcessMapDto>();
dto.LastUpdate = entity.LastUpdate.ToRemoteDateTime(entity.Well.Timezone.Hours);
dto.AxialLoad = new PlanLimitDto
{
LimitMax = entity.AxialLoadLimitMax,
Plan = entity.AxialLoadPlan
};
dto.Flow = new PlanLimitDto
{
LimitMax = entity.FlowLimitMax,
Plan = entity.FlowPlan
};
dto.Pressure = new PlanLimitDto
{
LimitMax = entity.PressureLimitMax,
Plan = entity.PressurePlan
};
dto.TopDriveSpeed = new PlanLimitDto
{
LimitMax = entity.TopDriveSpeedLimitMax,
Plan = entity.TopDriveSpeedPlan
};
dto.TopDriveTorque = new PlanLimitDto
{
LimitMax = entity.TopDriveTorqueLimitMax,
Plan = entity.TopDriveTorquePlan
};
return dto;
}
protected override WellDrillingProcessMap Convert(WellDrillingProcessMapDto dto)
{
var entity = dto.Adapt<WellDrillingProcessMap>();
entity.AxialLoadPlan = dto.AxialLoad.Plan;
entity.AxialLoadLimitMax = dto.AxialLoad.LimitMax;
entity.FlowPlan = dto.Flow.Plan;
entity.FlowLimitMax = dto.Flow.LimitMax;
entity.PressurePlan = dto.Pressure.Plan;
entity.PressureLimitMax = dto.Pressure.LimitMax;
entity.TopDriveSpeedPlan = dto.TopDriveSpeed.Plan;
entity.TopDriveSpeedLimitMax = dto.TopDriveSpeed.LimitMax;
entity.TopDriveTorquePlan = dto.TopDriveTorque.Plan;
entity.TopDriveTorqueLimitMax = dto.TopDriveTorque.LimitMax;
return entity;
}
}

View File

@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudDb.Model.ProcessMaps;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Repository;
internal class WellReamProcessMapRepository : CrudWellRelatedRepositoryBase<WellReamProcessMapDto, WellReamProcessMap>,
IWellReamProcessMapRepository
{
private readonly IWellService wellService;
public WellReamProcessMapRepository(IAsbCloudDbContext context, IWellService wellService)
: base(context, dbSet =>
dbSet.Include(p => p.Well))
{
this.wellService = wellService;
}
public async Task<IEnumerable<WellReamProcessMapDto>> GetAsync(WellReamProcessMapRequest request,
CancellationToken cancellationToken)
{
var query = GetQuery()
.Where(p => p.IdWell == request.IdWell);
if (request.UpdateFrom is not null)
{
var timezone = wellService.GetTimezone(request.IdWell);
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
query = query.Where(p => p.LastUpdate >= updateFromUtc);
}
var entities = await query.OrderBy(e => e.DepthStart)
.ThenBy(e => e.Id)
.AsNoTracking()
.ToArrayAsync(cancellationToken);
return entities.Select(Convert);
}
}