DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DailyReport/DailyReportService.cs

177 lines
7.9 KiB
C#
Raw Normal View History

using System;
2022-05-05 10:06:15 +05:00
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Mapster;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using System.Collections.Generic;
using AsbCloudApp.Data.DailyReport;
using AsbCloudApp.Requests;
using AsbCloudInfrastructure.Services.DetectOperations;
2022-08-10 13:11:05 +05:00
using AsbCloudApp.Data.DetectedOperation;
namespace AsbCloudInfrastructure.Services.DailyReport
{
2022-05-05 10:06:15 +05:00
#nullable enable
public class DailyReportService : IDailyReportService
{
private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
private readonly IDetectedOperationService detectedOperationService;
2022-05-05 10:06:15 +05:00
private readonly DailyReportMakerExcel dailyReportMaker = new DailyReportMakerExcel();
public DailyReportService(IAsbCloudDbContext db, IWellService wellService, IDetectedOperationService detectedOperationService)
{
this.db = db;
this.wellService = wellService;
this.detectedOperationService = detectedOperationService;
}
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)
query = query.Where(d => d.StartDate >= begin.Value.Date);
2022-05-05 10:06:15 +05:00
if (end is not null)
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);
return entities.Select(r => Convert(r));
}
2022-05-05 10:06:15 +05:00
public async Task<DailyReportDto> GetOrGenerateAsync(int idWell, DateTime date, CancellationToken token)
{
2022-05-05 10:06:15 +05:00
var dailyReportDto = await GetAsync(idWell, date, token);
if (dailyReportDto is null)
dailyReportDto = await MakeDefaultDailyReportAsync(idWell, date, token);
return dailyReportDto;
}
2022-05-05 10:06:15 +05:00
public async Task<int> AddAsync(int idWell, DailyReportDto dto, CancellationToken token = default)
{
var info = Convert(dto);
var entity = new AsbCloudDb.Model.DailyReport.DailyReport
{
2022-05-05 10:06:15 +05:00
IdWell = idWell,
StartDate = info.Head.ReportDate,
Info = info
};
2022-05-05 10:06:15 +05:00
db.DailyReports.Add(entity);
var result = await db.SaveChangesAsync(token);
return result;
}
2022-05-05 10:06:15 +05:00
public async Task<int> UpdateAsync(int idWell, DateTime date, DailyReportDto dto, CancellationToken token)
{
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-05-05 10:06:15 +05:00
if (entity is null)
return 0;
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-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;
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)
{
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-05-05 15:14:29 +05:00
if (entity is null)
2022-05-05 10:06:15 +05:00
return null;
var dto = Convert(entity);
return dto;
}
private async Task<DailyReportDto> MakeDefaultDailyReportAsync(int idWell, DateTime date, CancellationToken token)
{
2022-08-03 11:13:23 +05:00
var well = await wellService.GetOrDefaultAsync(idWell, token);
var detectedOperations = await detectedOperationService.GetOperationsStatAsync(new DetectedOperationRequest
{
IdWell = idWell,
GtDate = date.Date,
LtDate = date.Date.AddDays(1)
}, token);
var dto = new DailyReportDto()
{
Head = new HeadDto()
{
ReportDate = date.Date,
WellName = well?.Caption ?? "",
ClusterName = well?.Cluster ?? "",
},
2022-08-10 13:11:05 +05:00
TimeBalance = detectedOperations is null ? new TimeBalanceDto() { } : GetTimeBalanceDto(detectedOperations),
};
return dto;
}
private TimeBalanceDto GetTimeBalanceDto(IEnumerable<DetectedOperationStatDto> stat)
{
var dto = stat is null ? new TimeBalanceDto() { } : new TimeBalanceDto()
{
Drilling = $"{FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.IdOperationRotor)) + FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.IdOperationSlide))}",
Flushing = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationFlushing)).ToString(),
Building = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.IdOperationSlipsTime)).ToString(),
Elaboration = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationElaboration)).ToString(),
ElaborationBeforeBuilding = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationElaborationBeforeBuilding)).ToString(),
TemplatingBeforeBuilding = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationTemplatingBeforeBuilding)).ToString(),
FlushingBeforeBuilding = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationFlushingBeforeBuilding)).ToString(),
StaticSurveying = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationStaticSurveying)).ToString(),
Gis = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationGis)).ToString(),
Ozc = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationOzc)).ToString(),
Cementing = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationCementing)).ToString(),
Npv = FormatOperationValue(stat.FirstOrDefault(x => x.IdCategory == DetectedOperationService.idOperationNpv)).ToString(),
};
return dto;
}
2022-08-10 13:11:05 +05:00
private double FormatOperationValue(DetectedOperationStatDto? operationStat)
{
return operationStat is not null ? Math.Round(operationStat.MinutesTotal / 60, 2) : 0;
}
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;
}
private static DailyReportInfo Convert(DailyReportDto dto)
{
2022-06-15 14:57:37 +05:00
var entity = dto.Adapt<DailyReportInfo>();
entity.Head.ReportDate = dto.Head.ReportDate.Date.Date;
2022-05-05 10:06:15 +05:00
return entity;
}
}
2022-05-05 10:06:15 +05:00
#nullable disable
}