forked from ddrilling/AsbCloudServer
103 lines
2.8 KiB
C#
103 lines
2.8 KiB
C#
|
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;
|
||
|
}
|
||
|
}
|