2023-11-03 19:26:41 +05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
|
|
|
public class DailyReportRepository : CrudRepositoryBase<DailyReportDto, DailyReport>,
|
|
|
|
IDailyReportRepository
|
|
|
|
{
|
|
|
|
public DailyReportRepository(IAsbCloudDbContext dbContext)
|
|
|
|
: base(dbContext)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (request.GeDate.HasValue)
|
2023-11-14 11:01:34 +05:00
|
|
|
{
|
|
|
|
var geDate = request.GeDate.Value.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc);
|
|
|
|
query = query.Where(d => d.Date >= geDate);
|
|
|
|
}
|
2023-11-03 19:26:41 +05:00
|
|
|
|
|
|
|
if (request.LeDate.HasValue)
|
2023-11-14 11:01:34 +05:00
|
|
|
{
|
|
|
|
var leDate = request.LeDate.Value.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc);
|
|
|
|
query = query.Where(d => d.Date <= leDate);
|
|
|
|
}
|
2023-11-03 19:26:41 +05:00
|
|
|
|
|
|
|
if (request.SortFields?.Any() == true)
|
|
|
|
{
|
|
|
|
query = query.SortBy(request.SortFields);
|
|
|
|
}
|
2023-11-16 10:45:44 +05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
query = query.OrderBy(d => d.Date);
|
|
|
|
}
|
2023-11-03 19:26:41 +05:00
|
|
|
|
|
|
|
var entities = await query
|
2023-11-09 15:01:29 +05:00
|
|
|
.Skip(skip)
|
|
|
|
.Take(take)
|
2023-11-03 19:26:41 +05:00
|
|
|
.AsNoTracking()
|
|
|
|
.ToArrayAsync(cancellationToken);
|
|
|
|
|
2023-11-09 15:01:29 +05:00
|
|
|
var dtos = entities.Select(Convert);
|
|
|
|
|
|
|
|
return dtos;
|
2023-11-03 19:26:41 +05:00
|
|
|
}
|
|
|
|
|
2023-11-14 10:44:06 +05:00
|
|
|
public async Task<DailyReportDto?> GetOrDefaultAsync(int idWell, DateTime date, CancellationToken cancellationToken)
|
2023-11-03 19:26:41 +05:00
|
|
|
{
|
|
|
|
var entity = await GetQuery()
|
2023-11-14 10:44:06 +05:00
|
|
|
.AsNoTracking()
|
2023-11-14 16:43:39 +05:00
|
|
|
.SingleOrDefaultAsync(d => d.IdWell == idWell && d.Date == date, cancellationToken);
|
2023-11-03 19:26:41 +05:00
|
|
|
|
|
|
|
return entity is null ? null : Convert(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override DailyReportDto Convert(DailyReport src)
|
|
|
|
{
|
2023-11-14 16:43:39 +05:00
|
|
|
var dto = new DailyReportDto
|
2023-11-14 10:44:06 +05:00
|
|
|
{
|
2023-11-14 16:43:39 +05:00
|
|
|
Id = src.Id,
|
|
|
|
IdWell = src.IdWell,
|
|
|
|
DateLastUpdate = src.DateLastUpdate,
|
|
|
|
Date = src.Date,
|
|
|
|
SignBlock = src.SignBlock?.Adapt<SignBlockDto>(),
|
|
|
|
TimeBalanceBlock = src.TimeBalanceBlock?.Adapt<TimeBalanceBlockDto>(),
|
|
|
|
SubsystemBlock = src.SubsystemBlock?.Adapt<SubsystemBlockDto>()
|
2023-11-14 10:44:06 +05:00
|
|
|
};
|
2023-11-14 16:43:39 +05:00
|
|
|
|
2023-11-03 19:26:41 +05:00
|
|
|
return dto;
|
|
|
|
}
|
|
|
|
}
|