Репозиторий для суточного отчёта

This commit is contained in:
Степанов Дмитрий 2023-11-03 19:26:41 +05:00
parent 6c2feefff9
commit b1568820d3
2 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.DailyReport;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
namespace AsbCloudApp.Repositories;
/// <summary>
/// Суточные отчёты
/// </summary>
public interface IDailyReportRepository : ICrudRepository<DailyReportDto>
{
/// <summary>
/// Получить список суточный отчёт по скважине
/// </summary>
/// <param name="idWell"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IEnumerable<DailyReportDto>> GetAsync(int idWell, FileReportRequest request, CancellationToken cancellationToken);
/// <summary>
/// Получить суточный отчёт
/// </summary>
/// <param name="idWell"></param>
/// <param name="dateStart"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<DailyReportDto?> GetOrDefaultAsync(int idWell, DateTime dateStart, CancellationToken cancellationToken);
/// <summary>
/// Проверка существование суточного отчёта
/// </summary>
/// <param name="idWell"></param>
/// <param name="dateStart"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<bool> AnyAsync(int idWell, DateTime dateStart, CancellationToken cancellationToken);
}

View File

@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.DailyReport;
using AsbCloudApp.Data.DailyReport.Blocks.Sign;
using AsbCloudApp.Data.DailyReport.Blocks.Subsystems;
using AsbCloudApp.Data.DailyReport.Blocks.TimeBalance;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudDb;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Repository;
public class DailyReportRepository : CrudRepositoryBase<DailyReportDto, DailyReport>,
IDailyReportRepository
{
private class Blocks
{
public SubsystemBlockDto? Subsystem { get; set; }
public TimeBalanceBlockDto? TimeBalance { get; set; }
public SignBlockDto? Sign { get; set; }
}
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)
query = query.Where(d => d.DateStart <= request.GeDate.Value.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc));
if (request.LeDate.HasValue)
query = query.Where(d => d.DateStart >= request.LeDate.Value.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc));
if (request.SortFields?.Any() == true)
{
query = query.SortBy(request.SortFields);
}
var entities = await query
.AsNoTracking()
.ToArrayAsync(cancellationToken);
return entities.Skip(skip).Take(take).Select(Convert);
}
public async Task<DailyReportDto?> GetOrDefaultAsync(int idWell, DateTime dateStart, CancellationToken cancellationToken)
{
var entity = await GetQuery()
.Include(d => d.Well)
.SingleOrDefaultAsync(d => d.IdWell == idWell &&
d.DateStart == dateStart, cancellationToken);
return entity is null ? null : Convert(entity);
}
public Task<bool> AnyAsync(int idWell, DateTime dateStart, CancellationToken cancellationToken) =>
GetQuery().AnyAsync(d => d.IdWell == idWell && d.DateStart == dateStart, cancellationToken);
protected override DailyReport Convert(DailyReportDto src)
{
var entity = base.Convert(src);
entity.Blocks = JsonSerializer.Serialize(new Blocks
{
Subsystem = src.SubsystemBlock,
TimeBalance = src.TimeBalanceBlock,
Sign = src.SignBlock,
});
return entity;
}
protected override DailyReportDto Convert(DailyReport src)
{
var dto = base.Convert(src);
if (string.IsNullOrWhiteSpace(src.Blocks))
return dto;
var blocks = JsonSerializer.Deserialize<Blocks>(src.Blocks);
dto.SubsystemBlock = blocks?.Subsystem;
dto.TimeBalanceBlock = blocks?.TimeBalance;
dto.SignBlock = blocks?.Sign;
return dto;
}
}