2022-06-17 13:20:48 +05:00
|
|
|
|
using System;
|
2022-05-05 10:06:15 +05:00
|
|
|
|
using System.IO;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-06-17 13:20:48 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using System.Collections.Generic;
|
2022-06-29 13:04:30 +05:00
|
|
|
|
using AsbCloudApp.Data.DailyReport;
|
2022-08-10 12:01:29 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
using AsbCloudInfrastructure.Services.DetectOperations;
|
2022-08-10 13:11:05 +05:00
|
|
|
|
using AsbCloudApp.Data.DetectedOperation;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.DailyReport
|
|
|
|
|
{
|
2022-05-05 10:06:15 +05:00
|
|
|
|
#nullable enable
|
2022-04-29 15:39:12 +05:00
|
|
|
|
public class DailyReportService : IDailyReportService
|
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly IWellService wellService;
|
2022-08-10 12:01:29 +05:00
|
|
|
|
private readonly IDetectedOperationService detectedOperationService;
|
2022-05-05 10:06:15 +05:00
|
|
|
|
private readonly DailyReportMakerExcel dailyReportMaker = new DailyReportMakerExcel();
|
2022-04-29 15:39:12 +05:00
|
|
|
|
|
2022-08-10 12:01:29 +05:00
|
|
|
|
public DailyReportService(IAsbCloudDbContext db, IWellService wellService, IDetectedOperationService detectedOperationService)
|
2022-04-29 15:39:12 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
|
|
|
|
this.wellService = wellService;
|
2022-08-10 12:01:29 +05:00
|
|
|
|
this.detectedOperationService = detectedOperationService;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
public async Task<IEnumerable<DailyReportDto>> GetListAsync(int idWell, DateTime? begin, DateTime? end, CancellationToken token)
|
2022-06-15 14:57:37 +05:00
|
|
|
|
{
|
2022-05-05 10:06:15 +05:00
|
|
|
|
var query = db.DailyReports.Where(r => r.IdWell == idWell);
|
|
|
|
|
|
|
|
|
|
if (begin is not null)
|
2022-07-25 18:02:39 +05:00
|
|
|
|
query = query.Where(d => d.StartDate >= begin.Value.Date);
|
2022-05-05 10:06:15 +05:00
|
|
|
|
|
|
|
|
|
if (end is not null)
|
2022-07-25 18:02:39 +05:00
|
|
|
|
query = query.Where(d => d.StartDate <= end.Value.Date);
|
2022-06-15 14:57:37 +05:00
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
var entities = await query
|
|
|
|
|
.ToListAsync(token);
|
|
|
|
|
|
2022-07-25 18:02:39 +05:00
|
|
|
|
return entities.Select(r => Convert(r));
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
public async Task<DailyReportDto> GetOrGenerateAsync(int idWell, DateTime date, CancellationToken token)
|
2022-04-29 15:39:12 +05:00
|
|
|
|
{
|
2022-10-04 09:09:35 +05:00
|
|
|
|
var dateOnly = DateTime.SpecifyKind(date.Date, DateTimeKind.Unspecified);
|
|
|
|
|
var dailyReportDto = await GetAsync(idWell, dateOnly, token);
|
2022-05-05 10:06:15 +05:00
|
|
|
|
if (dailyReportDto is null)
|
2022-10-04 09:09:35 +05:00
|
|
|
|
dailyReportDto = await MakeDefaultDailyReportAsync(idWell, dateOnly, token);
|
2022-07-25 18:02:39 +05:00
|
|
|
|
return dailyReportDto;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
public async Task<int> AddAsync(int idWell, DailyReportDto dto, CancellationToken token = default)
|
2022-04-29 15:39:12 +05:00
|
|
|
|
{
|
2022-07-25 18:02:39 +05:00
|
|
|
|
var info = Convert(dto);
|
|
|
|
|
var entity = new AsbCloudDb.Model.DailyReport.DailyReport
|
2022-04-29 15:39:12 +05:00
|
|
|
|
{
|
2022-05-05 10:06:15 +05:00
|
|
|
|
IdWell = idWell,
|
2022-07-25 18:02:39 +05:00
|
|
|
|
StartDate = info.Head.ReportDate,
|
2022-07-05 11:30:25 +05:00
|
|
|
|
Info = info
|
2022-04-29 15:39:12 +05:00
|
|
|
|
};
|
2022-05-05 10:06:15 +05:00
|
|
|
|
db.DailyReports.Add(entity);
|
|
|
|
|
var result = await db.SaveChangesAsync(token);
|
2022-04-29 15:39:12 +05:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
public async Task<int> UpdateAsync(int idWell, DateTime date, DailyReportDto dto, CancellationToken token)
|
|
|
|
|
{
|
2022-07-25 18:02:39 +05:00
|
|
|
|
var dateOffset = date.Date;
|
2022-05-05 10:06:15 +05:00
|
|
|
|
var entity = await db.DailyReports
|
2022-05-05 15:14:29 +05:00
|
|
|
|
.FirstOrDefaultAsync(r => r.IdWell == idWell &&
|
|
|
|
|
r.StartDate.Year == dateOffset.Year &&
|
|
|
|
|
r.StartDate.DayOfYear == dateOffset.DayOfYear
|
|
|
|
|
, token);
|
2022-04-29 15:39:12 +05:00
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
if (entity is null)
|
|
|
|
|
return 0;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
|
2022-07-25 18:02:39 +05:00
|
|
|
|
entity.Info = Convert(dto);
|
2022-05-05 10:06:15 +05:00
|
|
|
|
db.DailyReports.Update(entity);
|
|
|
|
|
var result = await db.SaveChangesAsync(token);
|
|
|
|
|
return result;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
public async Task<Stream?> MakeReportAsync(int idWell, DateTime date, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
var dailyReportDto = await GetAsync(idWell, date, token);
|
|
|
|
|
if (dailyReportDto is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2022-06-30 16:01:46 +05:00
|
|
|
|
var memoryStream = dailyReportMaker.MakeReportFromBlocks(dailyReportDto);
|
2022-05-05 10:06:15 +05:00
|
|
|
|
return memoryStream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<DailyReportDto?> GetAsync(int idWell, DateTime date, CancellationToken token)
|
|
|
|
|
{
|
2022-07-25 18:02:39 +05:00
|
|
|
|
var dateOffset = date.Date;
|
2022-05-05 15:14:29 +05:00
|
|
|
|
var entity = await db.DailyReports
|
|
|
|
|
.FirstOrDefaultAsync(r => r.IdWell == idWell &&
|
|
|
|
|
r.StartDate.Year == dateOffset.Year &&
|
|
|
|
|
r.StartDate.DayOfYear == dateOffset.DayOfYear
|
|
|
|
|
, token);
|
2022-04-29 15:39:12 +05:00
|
|
|
|
|
2022-05-05 15:14:29 +05:00
|
|
|
|
if (entity is null)
|
2022-05-05 10:06:15 +05:00
|
|
|
|
return null;
|
2022-07-25 18:02:39 +05:00
|
|
|
|
var dto = Convert(entity);
|
|
|
|
|
return dto;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 17:02:53 +05:00
|
|
|
|
private async Task<DailyReportDto> MakeDefaultDailyReportAsync(int idWell, DateTime date, CancellationToken token)
|
2022-04-29 15:39:12 +05:00
|
|
|
|
{
|
2022-08-03 11:13:23 +05:00
|
|
|
|
var well = await wellService.GetOrDefaultAsync(idWell, token);
|
2022-08-10 13:44:35 +05:00
|
|
|
|
|
2022-06-30 16:01:46 +05:00
|
|
|
|
var dto = new DailyReportDto()
|
|
|
|
|
{
|
2022-07-25 18:02:39 +05:00
|
|
|
|
Head = new HeadDto()
|
2022-07-05 11:30:25 +05:00
|
|
|
|
{
|
2022-07-25 18:02:39 +05:00
|
|
|
|
ReportDate = date.Date,
|
|
|
|
|
WellName = well?.Caption ?? "",
|
2022-08-10 12:01:29 +05:00
|
|
|
|
ClusterName = well?.Cluster ?? "",
|
|
|
|
|
},
|
2022-08-10 13:44:35 +05:00
|
|
|
|
TimeBalance = await MakeTimeBalanceAsync(idWell, date, token),
|
2022-09-21 12:56:18 +05:00
|
|
|
|
Bha = await GetPrevOrNewBhaAsync(idWell, date, token)
|
2022-08-10 13:11:05 +05:00
|
|
|
|
};
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 12:56:18 +05:00
|
|
|
|
private async Task<BhaDto> GetPrevOrNewBhaAsync(int idWell, DateTime date, CancellationToken token)
|
2022-09-21 09:41:01 +05:00
|
|
|
|
{
|
|
|
|
|
var dateOffset = date.Date;
|
|
|
|
|
var entity = await db.DailyReports
|
2022-09-21 12:56:18 +05:00
|
|
|
|
.Where(x => x.IdWell == idWell)
|
2022-09-21 09:41:01 +05:00
|
|
|
|
.OrderByDescending(x => x.StartDate)
|
2022-09-21 12:56:18 +05:00
|
|
|
|
.FirstOrDefaultAsync(r => r.StartDate <= dateOffset, token);
|
2022-09-21 09:41:01 +05:00
|
|
|
|
|
|
|
|
|
if (entity is null)
|
2022-09-21 12:56:18 +05:00
|
|
|
|
return new BhaDto();
|
|
|
|
|
|
2022-09-21 09:41:01 +05:00
|
|
|
|
var dto = Convert(entity);
|
2022-09-21 12:56:18 +05:00
|
|
|
|
return dto.Bha;
|
2022-09-21 09:41:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 13:44:35 +05:00
|
|
|
|
private async Task<TimeBalanceDto> MakeTimeBalanceAsync(int idWell, DateTime date, CancellationToken token)
|
2022-08-10 13:11:05 +05:00
|
|
|
|
{
|
2022-08-10 13:44:35 +05:00
|
|
|
|
var stat = await detectedOperationService.GetOperationsStatAsync(new DetectedOperationRequest
|
|
|
|
|
{
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
GtDate = date.Date,
|
|
|
|
|
LtDate = date.Date.AddDays(1)
|
|
|
|
|
}, token);
|
|
|
|
|
|
|
|
|
|
if (stat is null)
|
|
|
|
|
return new TimeBalanceDto();
|
|
|
|
|
|
|
|
|
|
var dto = new TimeBalanceDto()
|
2022-08-10 13:11:05 +05:00
|
|
|
|
{
|
2022-08-10 13:44:35 +05:00
|
|
|
|
Drilling = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationRotor, DetectedOperationService.IdOperationSlide):#0.00}",
|
|
|
|
|
Flushing = $"{GetHoursFromStat(stat, DetectedOperationService.idOperationFlushing):#0.00}",
|
|
|
|
|
Building = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationSlipsTime):#0.00}",
|
|
|
|
|
Elaboration = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationElaboration):#0.00}",
|
|
|
|
|
ElaborationBeforeBuilding = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationElaborationBeforeBuilding):#0.00}",
|
|
|
|
|
TemplatingBeforeBuilding = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationTemplatingBeforeBuilding):#0.00}",
|
|
|
|
|
FlushingBeforeBuilding = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationFlushingBeforeBuilding):#0.00}",
|
|
|
|
|
StaticSurveying = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationStaticSurveying):#0.00}",
|
|
|
|
|
Gis = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationGis):#0.00}",
|
|
|
|
|
Ozc = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationOzc):#0.00}",
|
|
|
|
|
Cementing = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationCementing):#0.00}",
|
|
|
|
|
Npv = $"{GetHoursFromStat(stat, DetectedOperationService.IdOperationNpv):#0.00}",
|
2022-06-30 16:01:46 +05:00
|
|
|
|
};
|
2022-07-05 11:30:25 +05:00
|
|
|
|
return dto;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 13:44:35 +05:00
|
|
|
|
private static double GetHoursFromStat(IEnumerable<DetectedOperationStatDto> stat, params int[] idCategories)
|
2022-08-10 13:11:05 +05:00
|
|
|
|
{
|
2022-08-10 13:44:35 +05:00
|
|
|
|
var valueMinutes = stat.Where(o => idCategories.Contains(o.IdCategory))
|
|
|
|
|
.Sum(o => o.MinutesTotal);
|
|
|
|
|
return valueMinutes / 60d;
|
2022-08-10 13:11:05 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 18:02:39 +05:00
|
|
|
|
private static DailyReportDto Convert(AsbCloudDb.Model.DailyReport.DailyReport entity)
|
|
|
|
|
{
|
|
|
|
|
var dto = entity.Info.Adapt<DailyReportDto>();
|
|
|
|
|
dto.Head.ReportDate = entity.StartDate.Date;
|
2022-05-05 10:06:15 +05:00
|
|
|
|
return dto;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 18:02:39 +05:00
|
|
|
|
private static DailyReportInfo Convert(DailyReportDto dto)
|
2022-04-29 15:39:12 +05:00
|
|
|
|
{
|
2022-06-15 14:57:37 +05:00
|
|
|
|
var entity = dto.Adapt<DailyReportInfo>();
|
2022-07-25 18:02:39 +05:00
|
|
|
|
entity.Head.ReportDate = dto.Head.ReportDate.Date.Date;
|
2022-05-05 10:06:15 +05:00
|
|
|
|
return entity;
|
2022-06-30 16:01:46 +05:00
|
|
|
|
}
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|
2022-05-05 10:06:15 +05:00
|
|
|
|
#nullable disable
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|