DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DetectOperations/DetectedOperationService.cs

214 lines
8.2 KiB
C#
Raw Normal View History

2022-04-28 15:04:13 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.DetectOperations
{
2022-06-15 14:57:37 +05:00
public class DetectedOperationService : IDetectedOperationService
2022-04-28 15:04:13 +05:00
{
public const int IdOperationRotor = 1;
public const int IdOperationSlide = 3;
public const int IdOperationSlipsTime = 14;
2022-04-28 15:04:13 +05:00
private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
private readonly IOperationValueService operationValueService;
private readonly IScheduleService scheduleService;
2022-04-28 15:04:13 +05:00
2022-06-15 14:57:37 +05:00
public DetectedOperationService(IAsbCloudDbContext db, IWellService wellService,
IOperationValueService operationValueService, IScheduleService scheduleService)
2022-04-28 15:04:13 +05:00
{
this.db = db;
this.wellService = wellService;
this.operationValueService = operationValueService;
this.scheduleService = scheduleService;
2022-04-28 15:04:13 +05:00
}
public async Task<DetectedOperationListDto> GetAsync(DetectedOperationRequest request, CancellationToken token)
2022-04-28 15:04:13 +05:00
{
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
2022-04-28 15:04:13 +05:00
if (well?.IdTelemetry is null || well.Timezone is null)
return null;
var query = BuildQuery(well, request)
.AsNoTracking();
var data = await query.ToListAsync(token);
2022-06-15 14:57:37 +05:00
var operationValues = await operationValueService.GetByIdWellAsync(request.IdWell, token);
var schedules = await scheduleService.GetByIdWellAsync(request.IdWell, token);
var dtos = data.Select(o => Convert(o, well, operationValues, schedules));
var groups = dtos.GroupBy(o => o.Driller);
var stats = new List<DetectedOperationStatDto>(groups.Count());
foreach (var group in groups)
{
var itemsWithTarget = group.Where(i => i.OperationValue is not null);
var stat = new DetectedOperationStatDto
{
Driller = group.Key,
AverageValue = group.Sum(e => e.Value) / group.Count(),
Count = group.Count(),
};
if (itemsWithTarget.Any())
{
var itemsOutOfTarget = itemsWithTarget.Where(o => !IsTargetOk(o));
stat.AverageTargetValue = itemsWithTarget.Average(e => e.OperationValue.TargetValue);
stat.Efficiency = 100d * itemsOutOfTarget.Count() / itemsWithTarget.Count();
stat.Loss = itemsOutOfTarget.Sum(DeltaToTarget);
}
stats.Add(stat);
}
var result = new DetectedOperationListDto
{
Operations = dtos,
Stats = stats
};
return result;
2022-04-28 15:04:13 +05:00
}
public async Task<int> DeleteAsync(DetectedOperationRequest request, CancellationToken token)
2022-04-28 15:04:13 +05:00
{
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
2022-04-28 15:04:13 +05:00
if (well?.IdTelemetry is null || well.Timezone is null)
return 0;
2022-06-15 14:57:37 +05:00
var query = BuildQuery(well, request);
2022-04-28 15:04:13 +05:00
db.DetectedOperations.RemoveRange(query);
return await db.SaveChangesAsync(token);
}
private static bool IsTargetOk(DetectedOperationDto op)
{
return (op.IdCategory) switch
{
IdOperationRotor => op.Value > op.OperationValue.TargetValue,
IdOperationSlide => op.Value > op.OperationValue.TargetValue,
IdOperationSlipsTime => op.Value > op.OperationValue.TargetValue,
_ => op.Value > op.OperationValue.TargetValue,
};
}
private static double DeltaToTarget(DetectedOperationDto op)
{
return (op.IdCategory) switch
{
IdOperationRotor => 0,
IdOperationSlide => 0,
IdOperationSlipsTime => op.Value - op.OperationValue.TargetValue,
_ => 0,
};
}
2022-04-28 15:04:13 +05:00
private IQueryable<DetectedOperation> BuildQuery(WellDto well, DetectedOperationRequest request)
{
if (well?.IdTelemetry is null || well.Timezone is null)
return null;
var query = db.Set<DetectedOperation>()
.Include(o => o.OperationCategory)
.Where(o => o.IdTelemetry == well.IdTelemetry);
if (request is not null)
{
query = query.Where(o => request.IdCategory == o.IdCategory);
2022-04-28 15:04:13 +05:00
if (request.GtDate is not null)
query = query.Where(o => o.DateStart >= request.GtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours));
if (request.LtDate is not null)
2022-04-29 15:54:01 +05:00
query = query.Where(o => o.DateEnd <= request.LtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours));
2022-04-28 15:04:13 +05:00
if (request.GtDepth is not null)
query = query.Where(o => o.DepthStart >= request.GtDepth);
if (request.LtDepth is not null)
2022-04-29 15:54:01 +05:00
query = query.Where(o => o.DepthEnd <= request.LtDepth);
2022-04-28 15:04:13 +05:00
if (request.EqIdTelemetryUser is not null)
query = query.Where(o => o.IdUsersAtStart == request.EqIdTelemetryUser);
}
if (request?.SortFields?.Any() == true)
{
query = query.SortBy(request.SortFields);
}
else
query = query
.OrderBy(o => o.DateStart)
.ThenBy(o => o.DepthStart);
if (request?.Skip > 0)
query = query.Skip((int)request.Skip);
if (request?.Take > 0)
query = query.Take((int)request.Take);
return query;
}
private DetectedOperationDto Convert(DetectedOperation operation, WellDto well, IEnumerable<OperationValueDto> operationValues, IEnumerable<ScheduleDto> schedules)
2022-04-28 15:04:13 +05:00
{
var dto = operation.Adapt<DetectedOperationDto>();
dto.IdWell = well.Id;
var dateStart = operation.DateStart.ToRemoteDateTime(well.Timezone.Hours);
dto.DateStart = dateStart;
2022-04-28 15:04:13 +05:00
dto.DateEnd = operation.DateEnd.ToRemoteDateTime(well.Timezone.Hours);
dto.OperationValue = operationValues.FirstOrDefault(v => v.IdOperationCategory == dto.IdCategory
&& v.DepthStart <= dto.DepthStart
&& v.DepthEnd > dto.DepthStart);
var timeStart = new TimeDto(dateStart);
2022-06-15 14:57:37 +05:00
var driller = schedules.FirstOrDefault(s =>
s.DrillStart <= dateStart &&
s.DrillEnd > dateStart && (
2022-06-15 14:57:37 +05:00
s.ShiftStart > s.ShiftEnd
) ^ (s.ShiftStart <= timeStart &&
s.ShiftEnd > timeStart
))
?.Driller;
dto.Driller = driller;
2022-04-28 15:04:13 +05:00
return dto;
}
public async Task<IEnumerable<WellOperationCategoryDto>> GetCategoriesAsync(int? idWell, CancellationToken token)
2022-04-28 15:04:13 +05:00
{
IQueryable<WellOperationCategory> query = null;
if(idWell is null)
{
query = db.WellOperationCategories;
}
else
{
var well = await wellService.GetOrDefaultAsync((int )idWell, token);
if (well?.IdTelemetry is null)
return null;
var idTelemetry = (int)well.IdTelemetry;
query = db.DetectedOperations
.Include(o => o.OperationCategory)
.Where(o => o.IdTelemetry == idTelemetry)
.Select(o => o.OperationCategory)
.Distinct();
}
var result = await query
2022-04-28 15:04:13 +05:00
.Where(c => c.Id < 1000)
.AsNoTracking()
2022-04-28 15:04:13 +05:00
.Select(c => c.Adapt<WellOperationCategoryDto>())
.ToArrayAsync(token);
return result;
}
}
}