using AsbCloudApp.Data; using AsbCloudApp.Services; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services.Cache; using AsbCloudApp.Exceptions; using Mapster; using Microsoft.EntityFrameworkCore; using System.Collections; using System.IO; using ClosedXML.Excel; namespace AsbCloudInfrastructure.Services.DailyReport { public class DailyReportService : IDailyReportService { private readonly IAsbCloudDbContext db; private readonly IWellService wellService; private readonly IOperationsStatService operationsStatService; private DailyReportMakerExcel DailyReportExcel = new DailyReportMakerExcel(); public DailyReportService(IAsbCloudDbContext db, IWellService wellService) { this.db = db; this.wellService = wellService; } private DateTimeOffset Convert(DateTime date, int idWell) { var hours = wellService.GetTimezone(idWell).Hours; var dateOffset = date.ToUtcDateTimeOffset(hours);// DateTimeExtentions.ToUtcDateTimeOffset((DateTime)date, hours); return dateOffset; } private AsbCloudDb.Model.DailyReport ConvertDto(DailyReportDto dto, int idWell) { AsbCloudDb.Model.DailyReport entity = new AsbCloudDb.Model.DailyReport(); entity.Info = dto.Adapt(); entity.StartDate = Convert(dto.ReportDate, idWell); entity.IdWell = idWell; return entity; } public DailyReportDto GetDefaultDailyReportDto() { var dto = new DailyReportDto() { ReportDate = DateTime.Now, }; DailyReportDto result = dto; return result; } public async Task GetListAsync(int idWell, DateTime? begin, DateTime? end, CancellationToken token) { var query = db.DailyReport.Where(r => r.IdWell == idWell); var beginOffset = Convert((DateTime)begin, idWell); var endOffset = Convert((DateTime)end, idWell); if (begin is not null) query = query.Where(d => d.StartDate >= beginOffset); if (end is not null) query = query.Where(d => d.StartDate <= endOffset); var data = await query.FirstOrDefaultAsync(token); return data.Adapt(); } public async Task GetOrGenerateAsync(int idWell, DateTime date, CancellationToken token) { var query = db.DailyReport .Where(r => r.IdWell == idWell) .Where(d => d.StartDate == Convert(date, idWell)) ; var data = await query.FirstOrDefaultAsync(token); if (data == null) return GetDefaultDailyReportDto(); else return data.Adapt(); } public async Task AddAsync(int idWell, DailyReportDto dto, CancellationToken token = default) { var entity = ConvertDto(dto, idWell); db.DailyReport.Add(entity); var result = await db.SaveChangesAsync(token); return result; } public async Task UpdateAsync(int idWell, DateTime date, DailyReportDto dto, CancellationToken token) { var entity = ConvertDto(dto, idWell); db.DailyReport.Update(entity); var result = await db.SaveChangesAsync(token); return result; } public async Task MakeReportAsync(int idWell, DateTime date, CancellationToken token = default) { var well = await wellService.GetAsync(idWell, token); var query = db.DailyReport.Where(r => r.IdWell == idWell); query = query.Where(d => d.StartDate == Convert(date, idWell)); var data = await query.FirstOrDefaultAsync(token); if (data == null) return null; var dailyReportDto = data.Adapt(); var memoryStream = DailyReportExcel.MakeReportAsync(dailyReportDto, well); return memoryStream; } } }