2024-10-08 11:23:25 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using AsbCloudApp.Requests;
|
2024-02-07 13:28:02 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2024-02-05 10:47:00 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2024-10-08 11:23:25 +05:00
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-02-05 10:47:00 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
2024-10-08 11:23:25 +05:00
|
|
|
|
public class DataSaubStatRepository<TEntity, TDto> : IDataSaubStatRepository<TDto>
|
|
|
|
|
where TDto : AsbCloudApp.Data.IDataSaubStatDto
|
|
|
|
|
where TEntity : class, AsbCloudDb.Model.IDataSaubStat
|
2024-02-05 10:47:00 +05:00
|
|
|
|
{
|
2024-10-08 11:23:25 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
|
|
|
|
|
|
public DataSaubStatRepository(IAsbCloudDbContext dbContext, ITelemetryService telemetryService)
|
|
|
|
|
{
|
|
|
|
|
db = dbContext;
|
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<TDto>> GetLastsAsync(int[] idTelemetries, CancellationToken token)
|
2024-08-19 10:01:07 +05:00
|
|
|
|
{
|
2024-10-08 11:23:25 +05:00
|
|
|
|
var timeZoneOffsets = idTelemetries
|
|
|
|
|
.Distinct()
|
|
|
|
|
.ToDictionary(idTelemetry => idTelemetry, idTelemetry => TimeSpan.FromHours(telemetryService.GetTimezone(idTelemetry).Hours));
|
|
|
|
|
|
|
|
|
|
var stats = await db.Set<TEntity>()
|
|
|
|
|
.Where(s => idTelemetries.Contains(s.IdTelemetry))
|
|
|
|
|
.GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First())
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var result = stats.Select(s => ConvertToDto(s, timeZoneOffsets[s.IdTelemetry]));
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2024-02-07 13:28:02 +05:00
|
|
|
|
|
2024-10-16 18:02:38 +05:00
|
|
|
|
public async Task<IEnumerable<TDto>> GetAsync(IEnumerable<int> idsTelemetries, DateTimeOffset geDate, DateTimeOffset leDate, CancellationToken token)
|
2024-10-08 11:23:25 +05:00
|
|
|
|
{
|
2024-10-16 18:02:38 +05:00
|
|
|
|
//todo
|
|
|
|
|
var timeSpanDict = new Dictionary<int, TimeSpan>();
|
|
|
|
|
|
|
|
|
|
idsTelemetries = idsTelemetries.Distinct();
|
|
|
|
|
foreach (var idTelemetry in idsTelemetries)
|
|
|
|
|
{
|
|
|
|
|
var timeSpan = TimeSpan.FromHours(telemetryService.GetTimezone(idTelemetry).Hours);
|
|
|
|
|
timeSpanDict.Add(idTelemetry, timeSpan);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 11:23:25 +05:00
|
|
|
|
var geDateUtc = geDate.ToUniversalTime();
|
|
|
|
|
var leDateUtc = leDate.ToUniversalTime();
|
|
|
|
|
|
|
|
|
|
var stats = await db.Set<TEntity>()
|
2024-10-16 18:02:38 +05:00
|
|
|
|
.Where(s => idsTelemetries.Contains(s.IdTelemetry))
|
2024-10-08 11:23:25 +05:00
|
|
|
|
.Where(s => s.DateStart >= geDateUtc)
|
|
|
|
|
.Where(s => s.DateEnd <= leDateUtc)
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
2024-10-16 18:02:38 +05:00
|
|
|
|
var result = stats.Select(s => ConvertToDto(s, timeSpanDict[s.IdTelemetry]));
|
2024-10-08 11:23:25 +05:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> InsertRangeAsync(IEnumerable<TDto> dataSaubStats, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var entities = dataSaubStats.Select(data => ConvertToEntity(data));
|
|
|
|
|
db.Set<TEntity>().AddRange(entities);
|
|
|
|
|
return await db.SaveChangesAsync(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static TDto ConvertToDto(TEntity entity, TimeSpan timeSpan)
|
|
|
|
|
{
|
|
|
|
|
var dto = entity.Adapt<TDto>();
|
|
|
|
|
dto.DateStart = dto.DateStart.ToOffset(timeSpan);
|
|
|
|
|
dto.DateEnd = dto.DateEnd.ToOffset(timeSpan);
|
|
|
|
|
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static TEntity ConvertToEntity(TDto dto)
|
|
|
|
|
{
|
|
|
|
|
var entity = dto.Adapt<TEntity>();
|
|
|
|
|
entity.DateStart = dto.DateStart.ToUniversalTime();
|
|
|
|
|
entity.DateEnd = dto.DateEnd.ToUniversalTime();
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IQueryable<TEntity> BuildQuery(TelemetryPartDeleteRequest request)
|
|
|
|
|
{
|
|
|
|
|
var query = db.Set<TEntity>()
|
|
|
|
|
.Where(o => o.IdTelemetry == request.IdTelemetry);
|
|
|
|
|
|
|
|
|
|
if (request.LeDate is not null)
|
|
|
|
|
{
|
|
|
|
|
var leDate = request.LeDate.Value.ToUniversalTime();
|
|
|
|
|
query = query.Where(o => o.DateStart <= leDate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.GeDate is not null)
|
|
|
|
|
{
|
|
|
|
|
var geDate = request.GeDate.Value.ToUniversalTime();
|
|
|
|
|
query = query.Where(o => o.DateEnd >= geDate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = BuildQuery(request);
|
|
|
|
|
db.Set<TEntity>().RemoveRange(query);
|
|
|
|
|
return await db.SaveChangesAsync(token);
|
2024-02-05 10:47:00 +05:00
|
|
|
|
}
|
|
|
|
|
}
|