using System; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Mapster; using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using System.Collections.Generic; using AsbCloudApp.Data.DailyReport; namespace AsbCloudInfrastructure.Services.DailyReport { #nullable enable public class DailyReportService : IDailyReportService { private readonly IAsbCloudDbContext db; private readonly IWellService wellService; private readonly DailyReportMakerExcel dailyReportMaker = new DailyReportMakerExcel(); public DailyReportService(IAsbCloudDbContext db, IWellService wellService) { this.db = db; this.wellService = wellService; } public async Task> GetListAsync(int idWell, DateTime? begin, DateTime? end, CancellationToken token) { 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); } var entities = await query .ToListAsync(token); return entities.Select(r => Convert(r, offsetHours)); } public async Task GetOrGenerateAsync(int idWell, DateTime date, CancellationToken token) { var dailyReportDto = await GetAsync(idWell, date, token); if (dailyReportDto is null) return await MakeDefaultDailyReportAsync(idWell, date, token); else return dailyReportDto; } public async Task AddAsync(int idWell, DailyReportDto dto, CancellationToken token = default) { var offsetHours = wellService.GetTimezone(idWell).Hours; var reportDateOffset = dto.Head.ReportDate.ToUtcDateTimeOffset(offsetHours); var info = Convert(dto, offsetHours); var entity = new AsbCloudDb.Model.DailyReportDB.DailyReport { IdWell = idWell, StartDate = reportDateOffset, Info = info }; db.DailyReports.Add(entity); var result = await db.SaveChangesAsync(token); return result; } public async Task UpdateAsync(int idWell, DateTime date, DailyReportDto dto, CancellationToken token) { var offsetHours = wellService.GetTimezone(idWell).Hours; var dateOffset = date.ToUtcDateTimeOffset(offsetHours); var entity = await db.DailyReports .FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate.Year == dateOffset.Year && r.StartDate.DayOfYear == dateOffset.DayOfYear , token); if (entity is null) return 0; entity.Info = Convert(dto, offsetHours); db.DailyReports.Update(entity); var result = await db.SaveChangesAsync(token); return result; } public async Task 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); return memoryStream; } private async Task GetAsync(int idWell, DateTime date, CancellationToken token) { var offsetHours = wellService.GetTimezone(idWell).Hours; var dateOffset = date.ToUtcDateTimeOffset(offsetHours); var entity = await db.DailyReports .FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate.Year == dateOffset.Year && r.StartDate.DayOfYear == dateOffset.DayOfYear , token); if (entity is null) return null; else return Convert(entity, offsetHours); } private async Task MakeDefaultDailyReportAsync(int idWell, DateTime date, CancellationToken token) { var well = await wellService.GetAsync(idWell, token); var offsetHours = wellService.GetTimezone(idWell).Hours; var dto = new DailyReportDto() { Head=new DailyReportHeadDto() { ReportDate = DateTimeOffset.UtcNow.ToRemoteDateTime(offsetHours), WellName = well.Caption, ClusterName = well.Cluster } }; return dto; } private static DailyReportDto Convert(AsbCloudDb.Model.DailyReportDB.DailyReport entity, double offsetHours) { var dto = new DailyReportDto() { Bha = entity.Info.Bha.Adapt(), Head = entity.Info.Head.Adapt(), TimeBalance = entity.Info.TimeBalance.Adapt(), Dimensionless = entity.Info.Dimensionless.Adapt(), Saub = entity.Info.Saub.Adapt(), Sign = entity.Info.Sign.Adapt() }; dto.Head.ReportDate = entity.StartDate .ToRemoteDateTime(offsetHours); return dto; } private static DailyReportInfo Convert(DailyReportDto dto, double offsetHours) { var entity = dto.Adapt(); entity.Head.ReportDate = dto.Head.ReportDate .ToUtcDateTimeOffset(offsetHours) .Date; return entity; } } #nullable disable }