DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/SAUB/MessageRepository.cs

202 lines
6.3 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
2022-04-08 13:10:06 +05:00
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
2021-04-23 10:21:25 +05:00
using AsbCloudApp.Services;
using AsbCloudDb;
2021-04-23 10:21:25 +05:00
using AsbCloudDb.Model;
using DocumentFormat.OpenXml.Bibliography;
2021-07-28 09:46:58 +05:00
using Mapster;
2021-04-23 10:21:25 +05:00
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
2021-04-23 10:21:25 +05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2021-04-23 10:21:25 +05:00
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure.Services.SAUB;
public class MessageRepository : IMessageRepository
2021-04-23 10:21:25 +05:00
{
2024-08-19 10:01:07 +05:00
private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
private readonly ITelemetryService telemetryService;
2021-04-23 10:21:25 +05:00
public MessageRepository(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService)
2024-08-19 10:01:07 +05:00
{
this.db = db;
this.memoryCache = memoryCache;
this.telemetryService = telemetryService;
}
2021-04-23 10:21:25 +05:00
public async Task<IEnumerable<TelemetryMessageDto>> GetMessagesAsync(MessageTelemetryRequest request, CancellationToken token)
{
var query = BuildQuery(request, token);
var entities = await query.ToArrayAsync(token);
var dtos = entities.Select(m => m.Adapt<TelemetryMessageDto>());
return dtos;
}
public async Task<PaginationContainer<MessageDto>> GetPaginatedMessagesAsync(MessageTelemetryRequest request, CancellationToken token)
2024-08-19 10:01:07 +05:00
{
var result = new PaginationContainer<MessageDto>
2021-04-23 10:21:25 +05:00
{
2024-08-19 10:01:07 +05:00
Skip = request.Skip ?? 0,
Take = request.Take ?? 32,
};
if (request.Telemetries.IsNullOrEmpty() || request.Events.IsNullOrEmpty())
2024-08-19 10:01:07 +05:00
return result;
2021-04-23 10:21:25 +05:00
var query = BuildQuery(request, token);
2021-04-23 10:21:25 +05:00
if (request.SortFields?.Any() == true)
2024-08-19 10:01:07 +05:00
{
query = query.SortBy(request.SortFields);
2024-08-19 10:01:07 +05:00
}
2021-04-23 10:21:25 +05:00
2024-08-19 10:01:07 +05:00
result.Count = query.Count();
2021-04-23 10:21:25 +05:00
var messagesList = await query
.Skip(result.Skip)
.Take(result.Take)
.AsNoTracking()
.ToArrayAsync(token);
2021-04-23 10:21:25 +05:00
if (messagesList.Count() == 0)
2024-08-19 10:01:07 +05:00
return result;
2021-04-23 10:21:25 +05:00
2024-08-19 10:01:07 +05:00
var allUsers = await memoryCache.GetOrCreateBasicAsync(db.Set<TelemetryUser>(), token);
var users = allUsers.Where(u => request.Telemetries!.Select(t => t.Id).Contains(u.IdTelemetry));
if (!request.Events.Any())
return result;
2021-04-23 10:21:25 +05:00
var eventsDict = request.Events.ToDictionary(x => x.Id);
2024-08-19 10:01:07 +05:00
var usersDict = users.ToDictionary(x => x.IdUser, x => x);
2024-08-19 10:01:07 +05:00
var messagesDtoList = new List<MessageDto>();
2024-08-19 10:01:07 +05:00
foreach (var message in messagesList)
{
var messageDto = new MessageDto
2021-04-23 10:21:25 +05:00
{
2024-08-19 10:01:07 +05:00
Id = message.Id,
WellDepth = message.WellDepth,
2024-08-19 10:01:07 +05:00
};
var telemetry = request.Telemetries.Where(t => t.Id == message.IdTelemetry).FirstOrDefault();
if(telemetry != null && telemetry.TimeZone != null)
messageDto.DateTime = message.DateTime.ToOffset(TimeSpan.FromHours(telemetry.TimeZone.Hours));
2024-08-19 10:01:07 +05:00
if (message.IdTelemetryUser is not null)
{
if (usersDict.TryGetValue((int)message.IdTelemetryUser, out TelemetryUser? user))
2021-04-23 10:21:25 +05:00
{
2024-08-19 10:01:07 +05:00
messageDto.User = user.MakeDisplayName();
2021-04-23 10:21:25 +05:00
}
2024-08-19 10:01:07 +05:00
else
messageDto.User = message.IdTelemetryUser.ToString();
2021-04-23 10:21:25 +05:00
}
2022-04-11 18:00:34 +05:00
if (eventsDict.TryGetValue(message.IdEvent, out TelemetryEventDto? e))
2024-08-19 10:01:07 +05:00
{
messageDto.CategoryId = e.IdCategory;
messageDto.Message = e.MakeMessageText([
message.Arg0,
message.Arg1,
message.Arg2,
message.Arg3
]);
2024-08-19 10:01:07 +05:00
}
messagesDtoList.Add(messageDto);
2021-04-23 10:21:25 +05:00
}
2024-08-19 10:01:07 +05:00
result.Items = result.Items.Concat(messagesDtoList);
2021-04-23 10:21:25 +05:00
2024-08-19 10:01:07 +05:00
return result;
}
public IQueryable<TelemetryMessage> BuildQuery(MessageTelemetryRequest request, CancellationToken token)
{
var idsTelemetries = request.Telemetries.Select(t => t.Id);
var eventIds = request.Events.Select(e => e.Id);
var query = db.TelemetryMessages
.Where(m => idsTelemetries.Contains(m.IdTelemetry))
.Where(m => eventIds.Contains(m.IdEvent));
if (request.Begin is not null)
{
var beginUtc = request.Begin.Value.ToUniversalTime();
query = query.Where(m => m.DateTime >= beginUtc);
}
if (request.End is not null)
{
var endUtc = request.End.Value.ToUniversalTime();
query = query.Where(m => m.DateTime <= endUtc);
}
query = query.OrderByDescending(m => m.DateTime);
return query;
}
2024-08-19 10:01:07 +05:00
public Task InsertAsync(string uid, IEnumerable<TelemetryMessageDto> dtos,
CancellationToken token = default)
2024-08-19 10:01:07 +05:00
{
if (!dtos.Any())
return Task.CompletedTask;
2021-04-23 10:21:25 +05:00
2024-08-19 10:01:07 +05:00
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
2024-08-19 10:01:07 +05:00
foreach (var dto in dtos)
{
2024-08-19 10:01:07 +05:00
var entity = dto.Adapt<TelemetryMessage>();
entity.Id = 0;
entity.IdTelemetry = telemetry.Id;
entity.DateTime = dto.Date.ToUniversalTime();
db.TelemetryMessages.Add(entity);
}
2024-08-19 10:01:07 +05:00
return db.SaveChangesAsync(token);
}
2024-08-19 10:01:07 +05:00
private IQueryable<TelemetryMessage> BuildQuery(TelemetryPartDeleteRequest request)
{
var query = db.Set<TelemetryMessage>()
.Where(o => o.IdTelemetry == request.IdTelemetry);
2024-08-19 10:01:07 +05:00
if (request.GeDate is not null)
{
var geDate = request.GeDate.Value.ToUniversalTime();
query = query.Where(o => o.DateTime <= geDate);
}
2024-08-19 10:01:07 +05:00
if (request.LeDate is not null)
{
2024-08-19 10:01:07 +05:00
var leDate = request.LeDate.Value.ToUniversalTime();
query = query.Where(o => o.DateTime >= leDate);
}
2024-08-19 10:01:07 +05:00
return query;
}
public async Task<int> DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token)
{
var query = BuildQuery(request);
db.Set<TelemetryMessage>().RemoveRange(query);
return await db.SaveChangesAsync(token);
2021-04-23 10:21:25 +05:00
}
}