2023-10-12 15:21:41 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AsbCloudApp.Data.ProcessMaps;
|
2023-12-25 11:28:42 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
2023-10-12 15:21:41 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using AsbCloudDb.Model.ProcessMaps;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
|
|
|
|
|
public class ProcessMapPlanRepository<TDto, TEntity> : CrudWellRelatedRepositoryBase<TDto, TEntity>,
|
|
|
|
|
IProcessMapPlanRepository<TDto>
|
|
|
|
|
where TDto : ProcessMapPlanBaseDto
|
|
|
|
|
where TEntity : ProcessMapBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
|
|
|
|
|
public ProcessMapPlanRepository(IAsbCloudDbContext context, IWellService wellService)
|
2023-12-25 11:28:42 +05:00
|
|
|
|
: base(context, dbSet =>
|
|
|
|
|
dbSet
|
|
|
|
|
.Include(p => p.WellSectionType)
|
|
|
|
|
.Include(p => p.Well))
|
2023-10-12 15:21:41 +05:00
|
|
|
|
{
|
2023-12-25 11:28:42 +05:00
|
|
|
|
this.wellService = wellService;
|
2023-10-12 15:21:41 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<TDto>> GetAsync(IEnumerable<ProcessMapPlanRequest> requests, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2023-12-25 11:28:42 +05:00
|
|
|
|
var query = BuildQuery(requests);
|
2023-10-16 13:45:29 +05:00
|
|
|
|
|
2023-12-25 11:28:42 +05:00
|
|
|
|
var entities = await query.ToArrayAsync(cancellationToken);
|
2023-10-12 15:21:41 +05:00
|
|
|
|
|
2023-12-25 11:28:42 +05:00
|
|
|
|
return entities.Select(Convert);
|
2023-10-12 15:21:41 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 13:45:29 +05:00
|
|
|
|
public Task<int> RemoveByWellAsync(int idWell)
|
2023-10-12 15:21:41 +05:00
|
|
|
|
{
|
2023-12-25 11:28:42 +05:00
|
|
|
|
var query = GetQuery().Where(x => x.IdWell == idWell);
|
2023-10-12 15:21:41 +05:00
|
|
|
|
|
2023-12-25 11:28:42 +05:00
|
|
|
|
dbSet.RemoveRange(query);
|
2023-10-12 15:21:41 +05:00
|
|
|
|
|
2023-12-25 11:28:42 +05:00
|
|
|
|
return dbContext.SaveChangesAsync(CancellationToken.None);
|
2023-10-12 15:21:41 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IQueryable<TEntity> BuildQuery(IEnumerable<ProcessMapPlanRequest> requests)
|
|
|
|
|
{
|
2023-12-25 11:28:42 +05:00
|
|
|
|
var queries = requests
|
|
|
|
|
.Select(request => BuildQuery(request))
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var query = queries.FirstOrDefault()
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(requests), "Пустые запросы недопустимы");
|
2023-10-12 15:21:41 +05:00
|
|
|
|
|
2023-12-25 11:28:42 +05:00
|
|
|
|
for ( var i = 1; i < queries.Length; i++)
|
|
|
|
|
query = query.Union(queries[i]);
|
2023-10-12 15:21:41 +05:00
|
|
|
|
|
2023-12-25 11:28:42 +05:00
|
|
|
|
query = query
|
|
|
|
|
.Distinct()
|
|
|
|
|
.OrderBy(e => e.DepthStart)
|
|
|
|
|
.ThenBy(e => e.Id)
|
|
|
|
|
.AsNoTracking();
|
2023-10-12 15:21:41 +05:00
|
|
|
|
|
2023-12-25 11:28:42 +05:00
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IQueryable<TEntity> BuildQuery(ProcessMapPlanRequest request)
|
|
|
|
|
{
|
|
|
|
|
var query = GetQuery();
|
|
|
|
|
|
|
|
|
|
query = query.Where(p => p.IdWell == request.IdWell);
|
|
|
|
|
|
|
|
|
|
if (request.IdWellSectionType is not null)
|
|
|
|
|
query = query.Where(p => p.IdWellSectionType == request.IdWellSectionType);
|
2023-10-12 15:21:41 +05:00
|
|
|
|
|
2023-12-25 11:28:42 +05:00
|
|
|
|
if (request.UpdateFrom is not null)
|
|
|
|
|
{
|
|
|
|
|
var timezone = wellService.GetTimezone(request.IdWell);
|
|
|
|
|
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
|
2023-10-12 15:21:41 +05:00
|
|
|
|
|
2023-12-25 11:28:42 +05:00
|
|
|
|
query = query.Where(p => p.LastUpdate >= updateFromUtc);
|
|
|
|
|
}
|
|
|
|
|
return query;
|
2023-10-12 15:21:41 +05:00
|
|
|
|
}
|
|
|
|
|
}
|