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.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using System.Collections.Generic;
|
2022-07-04 13:15:26 +05:00
|
|
|
|
using AsbCloudDb.Model.DailyReportDB;
|
2022-06-29 13:04:30 +05:00
|
|
|
|
using AsbCloudApp.Data.DailyReport;
|
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-05-05 10:06:15 +05:00
|
|
|
|
private readonly DailyReportMakerExcel dailyReportMaker = new DailyReportMakerExcel();
|
2022-04-29 15:39:12 +05:00
|
|
|
|
|
|
|
|
|
public DailyReportService(IAsbCloudDbContext db, IWellService wellService)
|
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
var offsetHours = wellService.GetTimezone(idWell).Hours;
|
|
|
|
|
|
|
|
|
|
if (begin is not null)
|
|
|
|
|
{
|
|
|
|
|
var beginOffset = ((DateTime)begin).ToUtcDateTimeOffset(offsetHours);
|
|
|
|
|
query = query.Where(d => d.StartDate >= beginOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (end is not null)
|
|
|
|
|
{
|
|
|
|
|
var endOffset = ((DateTime)end).ToUtcDateTimeOffset(offsetHours);
|
|
|
|
|
query = query.Where(d => d.StartDate <= endOffset);
|
|
|
|
|
}
|
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, offsetHours));
|
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-05-05 10:06:15 +05:00
|
|
|
|
var dailyReportDto = await GetAsync(idWell, date, token);
|
|
|
|
|
if (dailyReportDto is null)
|
2022-05-06 17:02:53 +05:00
|
|
|
|
return await MakeDefaultDailyReportAsync(idWell, date, token);
|
2022-05-05 10:06:15 +05:00
|
|
|
|
else
|
|
|
|
|
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-05-05 10:06:15 +05:00
|
|
|
|
var offsetHours = wellService.GetTimezone(idWell).Hours;
|
2022-06-30 16:01:46 +05:00
|
|
|
|
var reportDateOffset = dto.HeadDto.ReportDate.ToUtcDateTimeOffset(offsetHours);
|
2022-05-05 10:06:15 +05:00
|
|
|
|
var info = Convert(dto, offsetHours);
|
2022-07-04 13:15:26 +05:00
|
|
|
|
var entity = new AsbCloudDb.Model.DailyReportDB.DailyReport
|
2022-04-29 15:39:12 +05:00
|
|
|
|
{
|
2022-05-05 10:06:15 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
StartDate = reportDateOffset,
|
2022-07-04 13:15:26 +05:00
|
|
|
|
BlockHead = info.HeadInfo,
|
|
|
|
|
BlockBha = info.BhaDto,
|
|
|
|
|
BlockDimensionless = info.DimensionlessInfo,
|
|
|
|
|
BlockTimeBalance = info.TimeBalanceInfo,
|
|
|
|
|
BlockSaub = info.SaubInfo,
|
|
|
|
|
BlockSign = info.SignInfo
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
var offsetHours = wellService.GetTimezone(idWell).Hours;
|
2022-05-05 15:14:29 +05:00
|
|
|
|
var dateOffset = date.ToUtcDateTimeOffset(offsetHours);
|
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-04 13:15:26 +05:00
|
|
|
|
var convertEntity = Convert(dto, offsetHours);
|
|
|
|
|
entity.BlockHead = convertEntity.HeadInfo;
|
2022-05-05 10:06:15 +05:00
|
|
|
|
db.DailyReports.Update(entity);
|
2022-04-29 15:39:12 +05:00
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 13:20:48 +05:00
|
|
|
|
|
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
private async Task<DailyReportDto?> GetAsync(int idWell, DateTime date, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var offsetHours = wellService.GetTimezone(idWell).Hours;
|
|
|
|
|
var dateOffset = date.ToUtcDateTimeOffset(offsetHours);
|
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-04-29 15:39:12 +05:00
|
|
|
|
else
|
2022-05-05 15:14:29 +05:00
|
|
|
|
return Convert(entity, offsetHours);
|
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-05-05 10:06:15 +05:00
|
|
|
|
var well = await wellService.GetAsync(idWell, token);
|
|
|
|
|
var offsetHours = wellService.GetTimezone(idWell).Hours;
|
2022-06-30 16:01:46 +05:00
|
|
|
|
var headDto = new DailyReportHeadDto()
|
2022-05-05 10:06:15 +05:00
|
|
|
|
{
|
2022-05-06 17:02:53 +05:00
|
|
|
|
ReportDate = date,
|
2022-05-05 10:06:15 +05:00
|
|
|
|
WellName = well.Caption,
|
2022-06-15 14:57:37 +05:00
|
|
|
|
ClusterName = well.Cluster,
|
2022-05-05 10:06:15 +05:00
|
|
|
|
};
|
2022-06-30 16:01:46 +05:00
|
|
|
|
var dto = new DailyReportDto()
|
|
|
|
|
{
|
|
|
|
|
HeadDto = headDto
|
|
|
|
|
};
|
2022-05-05 10:06:15 +05:00
|
|
|
|
DailyReportDto result = dto;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 13:15:26 +05:00
|
|
|
|
private static DailyReportDto Convert(AsbCloudDb.Model.DailyReportDB.DailyReport entity, double offsetHours)
|
2022-05-05 10:06:15 +05:00
|
|
|
|
{
|
2022-07-04 13:15:26 +05:00
|
|
|
|
var dto = new DailyReportDto()
|
|
|
|
|
{
|
|
|
|
|
BhaDto = entity.BlockBha.Adapt<DailyReportBhaDto>(),
|
|
|
|
|
HeadDto = entity.BlockHead.Adapt<DailyReportHeadDto>(),
|
|
|
|
|
TimeBalanceDto = entity.BlockTimeBalance.Adapt<DailyReportTimeBalanceDto>(),
|
|
|
|
|
DimensionlessDto = entity.BlockDimensionless.Adapt<DailyReportDimensionlessDto>(),
|
|
|
|
|
SaubDto = entity.BlockSaub.Adapt<DailyReportSaubDto>(),
|
|
|
|
|
SignDto = entity.BlockSign.Adapt<DailyReportSignDto>()
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-30 16:01:46 +05:00
|
|
|
|
dto.HeadDto.ReportDate = entity.StartDate
|
2022-05-05 15:14:29 +05:00
|
|
|
|
.ToRemoteDateTime(offsetHours);
|
2022-05-05 10:06:15 +05:00
|
|
|
|
return dto;
|
2022-04-29 15:39:12 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 10:06:15 +05:00
|
|
|
|
private static DailyReportInfo Convert(DailyReportDto dto, double offsetHours)
|
2022-04-29 15:39:12 +05:00
|
|
|
|
{
|
2022-06-15 14:57:37 +05:00
|
|
|
|
var entity = dto.Adapt<DailyReportInfo>();
|
2022-06-30 16:01:46 +05:00
|
|
|
|
entity.HeadInfo.ReportDate = dto.HeadDto.ReportDate
|
2022-05-05 15:14:29 +05:00
|
|
|
|
.ToUtcDateTimeOffset(offsetHours)
|
|
|
|
|
.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
|
|
|
|
}
|
2022-06-17 13:20:48 +05:00
|
|
|
|
|