2023-11-03 19:26:41 +05:00
|
|
|
using AsbCloudApp.Data.DailyReport;
|
2023-11-14 16:43:39 +05:00
|
|
|
using AsbCloudApp.Data.DailyReport.Blocks.Sign;
|
|
|
|
using AsbCloudApp.Data.DailyReport.Blocks.Subsystems;
|
2023-11-03 19:26:41 +05:00
|
|
|
using AsbCloudApp.Data.DailyReport.Blocks.TimeBalance;
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
using AsbCloudApp.Requests;
|
2024-03-19 17:21:05 +05:00
|
|
|
using AsbCloudApp.Services;
|
2023-11-03 19:26:41 +05:00
|
|
|
using AsbCloudDb;
|
|
|
|
using AsbCloudDb.Model;
|
2023-11-14 10:44:06 +05:00
|
|
|
using AsbCloudDb.Model.DailyReports;
|
|
|
|
using Mapster;
|
2023-11-03 19:26:41 +05:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-03-19 17:21:05 +05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2023-11-03 19:26:41 +05:00
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
|
|
|
public class DailyReportRepository : CrudRepositoryBase<DailyReportDto, DailyReport>,
|
2024-03-19 17:21:05 +05:00
|
|
|
IDailyReportRepository
|
2023-11-03 19:26:41 +05:00
|
|
|
{
|
2024-03-19 17:21:05 +05:00
|
|
|
private IWellService wellService;
|
|
|
|
|
|
|
|
public DailyReportRepository(IAsbCloudDbContext dbContext, IWellService wellService)
|
|
|
|
: base(dbContext)
|
|
|
|
{
|
|
|
|
this.wellService = wellService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IEnumerable<DailyReportDto>> GetAsync(int idWell, FileReportRequest request, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var skip = request.Skip ?? 0;
|
|
|
|
var take = request.Take ?? 10;
|
|
|
|
|
|
|
|
var query = GetQuery().Where(d => d.IdWell == idWell);
|
2023-11-03 19:26:41 +05:00
|
|
|
|
2024-03-19 17:21:05 +05:00
|
|
|
if (request.GeDate.HasValue)
|
|
|
|
query = query.Where(d => d.Date >= request.GeDate.Value);
|
2023-11-03 19:26:41 +05:00
|
|
|
|
2024-03-19 17:21:05 +05:00
|
|
|
if (request.LeDate.HasValue)
|
|
|
|
query = query.Where(d => d.Date <= request.LeDate.Value);
|
2023-11-03 19:26:41 +05:00
|
|
|
|
2024-03-19 17:21:05 +05:00
|
|
|
query = request.SortFields?.Any() == true ?
|
|
|
|
query.SortBy(request.SortFields) :
|
|
|
|
query.OrderBy(d => d.Date);
|
2023-11-03 19:26:41 +05:00
|
|
|
|
2024-03-19 17:21:05 +05:00
|
|
|
var entities = await query
|
|
|
|
.Skip(skip)
|
|
|
|
.Take(take)
|
|
|
|
.AsNoTracking()
|
|
|
|
.ToArrayAsync(cancellationToken);
|
2023-11-03 19:26:41 +05:00
|
|
|
|
2024-03-19 17:21:05 +05:00
|
|
|
var timezoneOffset = wellService.GetTimezone(idWell).Offset;
|
|
|
|
var dtos = entities.Select(entity => Convert(entity, timezoneOffset));
|
2023-11-03 19:26:41 +05:00
|
|
|
|
2024-03-19 17:21:05 +05:00
|
|
|
return dtos;
|
|
|
|
}
|
2023-11-09 15:01:29 +05:00
|
|
|
|
2024-03-19 17:21:05 +05:00
|
|
|
public async Task<DailyReportDto?> GetOrDefaultAsync(int idWell, DateOnly date, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var entity = await GetQuery()
|
|
|
|
.AsNoTracking()
|
|
|
|
.SingleOrDefaultAsync(d => d.IdWell == idWell && d.Date == date, cancellationToken);
|
2023-11-03 19:26:41 +05:00
|
|
|
|
2024-04-11 10:12:03 +05:00
|
|
|
var timezoneOffset = wellService.GetTimezone(idWell).Offset;
|
|
|
|
|
|
|
|
return entity is null ? null : Convert(entity, timezoneOffset);
|
2024-03-19 17:21:05 +05:00
|
|
|
}
|
2023-11-03 19:26:41 +05:00
|
|
|
|
2024-04-11 10:12:03 +05:00
|
|
|
private static DailyReportDto Convert(DailyReport src, TimeSpan timezoneOffset)
|
2024-03-19 17:21:05 +05:00
|
|
|
{
|
|
|
|
var dto = new DailyReportDto
|
|
|
|
{
|
|
|
|
Id = src.Id,
|
|
|
|
IdWell = src.IdWell,
|
|
|
|
DateLastUpdate = src.DateLastUpdate?.ToOffset(timezoneOffset),
|
|
|
|
Date = src.Date,
|
|
|
|
SignBlock = src.SignBlock?.Adapt<SignBlockDto>(),
|
|
|
|
TimeBalanceBlock = src.TimeBalanceBlock?.Adapt<TimeBalanceBlockDto>(),
|
|
|
|
SubsystemBlock = src.SubsystemBlock?.Adapt<SubsystemBlockDto>()
|
|
|
|
};
|
2023-11-03 19:26:41 +05:00
|
|
|
|
2024-03-19 17:21:05 +05:00
|
|
|
return dto;
|
|
|
|
}
|
2023-11-03 19:26:41 +05:00
|
|
|
}
|