2021-08-24 10:59:10 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
|
|
|
using Mapster;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-08-18 16:57:20 +05:00
|
|
|
|
using System;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
2021-10-09 20:16:22 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.WellOperationService
|
2021-08-13 17:25:06 +05:00
|
|
|
|
{
|
|
|
|
|
public class WellOperationService : IWellOperationService
|
|
|
|
|
{
|
2021-12-22 17:09:26 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-12-30 17:05:44 +05:00
|
|
|
|
private readonly IWellService wellService;
|
2021-08-17 09:20:31 +05:00
|
|
|
|
private readonly CacheTable<WellOperationCategory> cachedOperationCategories;
|
2021-12-22 17:09:26 +05:00
|
|
|
|
private readonly CacheTable<WellSectionType> cachedSectionTypes;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
2022-03-11 16:53:31 +05:00
|
|
|
|
public const int idOperationBhaAssembly = 1025;
|
|
|
|
|
public const int idOperationBhaDisassembly = 1026;
|
|
|
|
|
public const int idOperationNonProductiveTime = 1043;
|
|
|
|
|
public const int idOperationDrilling = 1001;
|
|
|
|
|
public const int idOperationBhaDown = 1046;
|
|
|
|
|
public const int idOperationBhaUp = 1047;
|
|
|
|
|
public const int idOperationCasingDown = 1048;
|
|
|
|
|
public const int idOperationTypePlan = 0;
|
|
|
|
|
public const int idOperationTypeFact = 1;
|
|
|
|
|
|
2021-12-30 17:05:44 +05:00
|
|
|
|
public WellOperationService(IAsbCloudDbContext db, CacheDb cache, IWellService wellService)
|
2021-08-13 17:25:06 +05:00
|
|
|
|
{
|
2021-12-22 17:09:26 +05:00
|
|
|
|
this.db = db;
|
2021-12-30 17:05:44 +05:00
|
|
|
|
this.wellService = wellService;
|
2021-12-22 17:09:26 +05:00
|
|
|
|
cachedOperationCategories = cache.GetCachedTable<WellOperationCategory>((DbContext)db);
|
|
|
|
|
cachedSectionTypes = cache.GetCachedTable<WellSectionType>((DbContext)db);
|
2021-08-16 14:19:43 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-22 17:09:26 +05:00
|
|
|
|
public IDictionary<int, string> GetSectionTypes()
|
|
|
|
|
=> cachedSectionTypes.ToDictionary(s => s.Id, s => s.Caption);
|
|
|
|
|
|
2021-08-17 09:20:31 +05:00
|
|
|
|
public IEnumerable<WellOperationCategoryDto> GetCategories()
|
2021-08-16 14:19:43 +05:00
|
|
|
|
{
|
2021-10-08 11:30:06 +05:00
|
|
|
|
var operationTypes = cachedOperationCategories
|
2021-09-23 17:34:27 +05:00
|
|
|
|
.Distinct().OrderBy(o => o.Name);
|
2021-08-17 09:20:31 +05:00
|
|
|
|
var result = operationTypes.Adapt<WellOperationCategoryDto>();
|
2021-08-16 14:19:43 +05:00
|
|
|
|
|
|
|
|
|
return result;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 16:57:20 +05:00
|
|
|
|
public async Task<PaginationContainer<WellOperationDto>> GetOperationsAsync(
|
|
|
|
|
int idWell,
|
2021-12-07 18:27:52 +05:00
|
|
|
|
int? operationType = default,
|
2021-08-18 16:57:20 +05:00
|
|
|
|
IEnumerable<int> sectionTypeIds = default,
|
|
|
|
|
IEnumerable<int> operationCategoryIds = default,
|
|
|
|
|
DateTime begin = default,
|
|
|
|
|
DateTime end = default,
|
2021-08-23 16:59:26 +05:00
|
|
|
|
double minDepth = double.MinValue,
|
|
|
|
|
double maxDepth = double.MaxValue,
|
2021-08-18 16:57:20 +05:00
|
|
|
|
int skip = 0,
|
|
|
|
|
int take = 32,
|
|
|
|
|
CancellationToken token = default)
|
2021-08-13 17:25:06 +05:00
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var timezone = wellService.GetTimezone(idWell);
|
|
|
|
|
|
2021-12-22 17:09:26 +05:00
|
|
|
|
var query = db.WellOperations
|
2021-08-16 10:21:46 +05:00
|
|
|
|
.Include(s => s.WellSectionType)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.Include(s => s.OperationCategory)
|
2021-08-17 09:20:31 +05:00
|
|
|
|
.Where(s => s.IdWell == idWell);
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
2022-03-11 16:53:31 +05:00
|
|
|
|
var dateStart = query.Min(o => o.DateStart);
|
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
if (operationType != default)
|
|
|
|
|
query = query.Where(e => e.IdType == (int)operationType);
|
2021-08-18 16:57:20 +05:00
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
if (sectionTypeIds != default && sectionTypeIds.Any())
|
2021-08-18 16:57:20 +05:00
|
|
|
|
query = query.Where(e => sectionTypeIds.Contains(e.IdWellSectionType));
|
|
|
|
|
|
|
|
|
|
if (operationCategoryIds != default && operationCategoryIds.Any())
|
|
|
|
|
query = query.Where(e => operationCategoryIds.Contains(e.IdCategory));
|
|
|
|
|
|
2021-08-23 16:59:26 +05:00
|
|
|
|
if (minDepth != double.MinValue)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
query = query.Where(e => e.DepthEnd >= minDepth);
|
2021-08-23 16:59:26 +05:00
|
|
|
|
|
|
|
|
|
if (maxDepth != double.MaxValue)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
query = query.Where(e => e.DepthEnd <= maxDepth);
|
2021-08-23 16:59:26 +05:00
|
|
|
|
|
2021-08-18 16:57:20 +05:00
|
|
|
|
if (begin != default)
|
2021-12-30 17:05:44 +05:00
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var beginOffset = begin.ToUtcDateTimeOffset(timezone.Hours);
|
|
|
|
|
query = query.Where(e => e.DateStart >= beginOffset);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
}
|
2021-08-18 16:57:20 +05:00
|
|
|
|
|
|
|
|
|
if (end != default)
|
2021-12-30 17:05:44 +05:00
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var endOffset = end.ToUtcDateTimeOffset(timezone.Hours);
|
|
|
|
|
query = query.Where(e => e.DateStart <= endOffset);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
}
|
2021-08-18 16:57:20 +05:00
|
|
|
|
|
2021-08-13 17:25:06 +05:00
|
|
|
|
var result = new PaginationContainer<WellOperationDto>
|
|
|
|
|
{
|
|
|
|
|
Skip = skip,
|
|
|
|
|
Take = take,
|
|
|
|
|
Count = await query.CountAsync(token).ConfigureAwait(false),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
query = query
|
2021-10-08 11:30:06 +05:00
|
|
|
|
.OrderBy(e => e.DateStart)
|
|
|
|
|
.ThenBy(e => e.DepthEnd)
|
2021-08-18 16:57:20 +05:00
|
|
|
|
.ThenBy(e => e.Id);
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
|
|
|
|
if (skip > 0)
|
|
|
|
|
query = query.Skip(skip);
|
|
|
|
|
|
2021-08-17 09:20:31 +05:00
|
|
|
|
var entities = await query.Take(take).AsNoTracking()
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.ToListAsync(token).ConfigureAwait(false);
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
2022-03-11 16:53:31 +05:00
|
|
|
|
var nptHours = 0d;
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
foreach (var entity in entities)
|
2021-08-13 17:25:06 +05:00
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var dto = entity.Adapt<WellOperationDto>();
|
2022-03-11 16:53:31 +05:00
|
|
|
|
dto.Day = (entity.DateStart - dateStart).TotalDays;
|
2022-01-05 17:50:45 +05:00
|
|
|
|
dto.WellSectionTypeName = entity.WellSectionType.Caption;
|
|
|
|
|
dto.DateStart = entity.DateStart.ToRemoteDateTime(timezone.Hours);
|
|
|
|
|
dto.CategoryName = entity.OperationCategory.Name;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
if (entity.IdType == idOperationTypeFact)
|
2022-03-11 16:53:31 +05:00
|
|
|
|
{
|
2022-04-11 18:00:34 +05:00
|
|
|
|
if (entity.IdCategory == idOperationNonProductiveTime)
|
2022-03-11 16:53:31 +05:00
|
|
|
|
nptHours += entity.DurationHours;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
dto.NptHours = nptHours;
|
2022-03-11 16:53:31 +05:00
|
|
|
|
}
|
2021-08-13 17:25:06 +05:00
|
|
|
|
result.Items.Add(dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-08-16 14:19:43 +05:00
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<WellOperationDto> GetAsync(int id,
|
2021-08-16 14:19:43 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
2021-12-22 17:09:26 +05:00
|
|
|
|
var entity = await db.WellOperations
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.Include(s => s.WellSectionType)
|
|
|
|
|
.Include(s => s.OperationCategory)
|
|
|
|
|
.FirstOrDefaultAsync(e => e.Id == id, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (entity is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var timezone = wellService.GetTimezone(entity.IdWell);
|
|
|
|
|
|
2021-08-16 14:19:43 +05:00
|
|
|
|
var dto = entity.Adapt<WellOperationDto>();
|
|
|
|
|
dto.WellSectionTypeName = entity.WellSectionType.Caption;
|
2022-01-05 17:50:45 +05:00
|
|
|
|
dto.DateStart = entity.DateStart.ToRemoteDateTime(timezone.Hours);
|
2021-08-16 14:19:43 +05:00
|
|
|
|
dto.CategoryName = entity.OperationCategory.Name;
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<int> InsertRangeAsync(int idWell,
|
|
|
|
|
IEnumerable<WellOperationDto> wellOperationDtos,
|
2021-08-16 14:19:43 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var timezone = wellService.GetTimezone(idWell);
|
|
|
|
|
foreach (var dto in wellOperationDtos)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var entity = dto.Adapt<WellOperation>();
|
2021-09-30 18:02:50 +05:00
|
|
|
|
entity.Id = default;
|
2022-01-05 17:50:45 +05:00
|
|
|
|
entity.DateStart = dto.DateStart.ToUtcDateTimeOffset(timezone.Hours);
|
2021-08-18 16:57:20 +05:00
|
|
|
|
entity.IdWell = idWell;
|
2021-12-22 17:09:26 +05:00
|
|
|
|
db.WellOperations.Add(entity);
|
2021-08-16 14:19:43 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-22 17:09:26 +05:00
|
|
|
|
return await db.SaveChangesAsync(token)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<int> UpdateAsync(int idWell, int idOperation,
|
2022-01-05 17:50:45 +05:00
|
|
|
|
WellOperationDto dto, CancellationToken token = default)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var timezone = wellService.GetTimezone(idWell);
|
|
|
|
|
var entity = dto.Adapt<WellOperation>();
|
2021-08-18 16:57:20 +05:00
|
|
|
|
entity.Id = idOperation;
|
2022-01-05 17:50:45 +05:00
|
|
|
|
entity.DateStart = dto.DateStart.ToUtcDateTimeOffset(timezone.Hours);
|
2021-12-22 17:09:26 +05:00
|
|
|
|
db.WellOperations.Update(entity);
|
|
|
|
|
return await db.SaveChangesAsync(token)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> DeleteAsync(IEnumerable<int> ids,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2021-12-22 17:09:26 +05:00
|
|
|
|
var query = db.WellOperations.Where(e => ids.Contains(e.Id));
|
|
|
|
|
db.WellOperations.RemoveRange(query);
|
|
|
|
|
return await db.SaveChangesAsync(token)
|
2021-08-16 14:19:43 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
2021-08-13 17:25:06 +05:00
|
|
|
|
}
|
|
|
|
|
}
|