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

161 lines
5.6 KiB
C#
Raw Normal View History

2021-04-23 10:21:25 +05:00
using AsbCloudApp.Data;
2022-04-08 13:10:06 +05:00
using AsbCloudApp.Data.SAUB;
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;
2021-07-28 09:46:58 +05:00
using Mapster;
2021-04-23 10:21:25 +05:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
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
2022-04-11 18:00:34 +05:00
namespace AsbCloudInfrastructure.Services.SAUB
2021-04-23 10:21:25 +05:00
{
public class MessageService : IMessageService
{
private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
2021-04-23 10:21:25 +05:00
private readonly ITelemetryService telemetryService;
public MessageService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService)
2021-04-23 10:21:25 +05:00
{
this.db = db;
this.memoryCache = memoryCache;
2021-04-23 10:21:25 +05:00
this.telemetryService = telemetryService;
}
public async Task<PaginationContainer<MessageDto>> GetMessagesAsync(MessageRequest request, CancellationToken token)
2021-04-23 10:21:25 +05:00
{
var result = new PaginationContainer<MessageDto>
{
Skip = request.Skip ?? 0,
Take = request.Take ?? 32,
};
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(request.IdWell);
if (telemetry is null)
return result;
2021-04-23 10:21:25 +05:00
var allEvents = await memoryCache.GetOrCreateBasicAsync(db.Set<TelemetryEvent>(), token);
var events = allEvents.Where(e => e.IdTelemetry == telemetry.Id);
2021-04-23 10:21:25 +05:00
if (!events.Any())
return result;
2021-04-23 10:21:25 +05:00
var query = db.TelemetryMessages.Where(m => m.IdTelemetry == telemetry.Id)
2022-04-01 17:55:44 +05:00
.OrderBy(m => m.DateTime).AsNoTracking();
2021-04-23 10:21:25 +05:00
if (request.Categoryids?.Any() == true || !string.IsNullOrEmpty(request.SearchString))
2021-04-23 10:21:25 +05:00
{
if (!string.IsNullOrEmpty(request.SearchString))
events = events.Where(e => e.MessageTemplate.Contains(request.SearchString, StringComparison.OrdinalIgnoreCase));
if (request.Categoryids?.Any() == true)
events = events.Where(e => request.Categoryids.ToList().Contains(e.IdCategory));
var eventIds = events.Select(e => e.IdEvent);
2021-04-23 10:21:25 +05:00
2021-05-19 15:21:19 +05:00
if (!eventIds.Any())
return result;
2021-04-23 10:21:25 +05:00
2021-08-10 14:36:35 +05:00
query = query.Where(m => eventIds.Contains(m.IdEvent));
2021-04-23 10:21:25 +05:00
}
2022-04-01 17:55:44 +05:00
query = query.OrderByDescending(m => m.DateTime);
2021-04-23 10:21:25 +05:00
if (request.Begin is not null)
{
var beginUtc = request.Begin.Value.ToUniversalTime();
2022-04-01 17:55:44 +05:00
query = query.Where(m => m.DateTime >= beginUtc);
2022-04-11 18:00:34 +05:00
}
2021-04-23 10:21:25 +05:00
if (request.End is not null)
{
var endUtc = request.End.Value.ToUniversalTime();
2022-04-01 17:55:44 +05:00
query = query.Where(m => m.DateTime <= endUtc);
2022-04-11 18:00:34 +05:00
}
2021-04-23 10:21:25 +05:00
result.Count = query.Count();
2021-04-23 10:21:25 +05:00
if (request.SortFields?.Any() == true)
{
2023-02-02 15:58:07 +05:00
query = query.SortBy(request.SortFields);
}
var messagesList = await query.Skip(result.Skip)
.Take(result.Take).AsNoTracking()
2023-02-02 15:58:07 +05:00
.ToListAsync(token).ConfigureAwait(false);
2021-04-23 10:21:25 +05:00
if (messagesList.Count == 0)
return result;
var allUsers = await memoryCache.GetOrCreateBasicAsync(db.Set<TelemetryUser>(), token);
var users = allUsers.Where(u => u.IdTelemetry == telemetry.Id);
2021-04-23 10:21:25 +05:00
var eventsDict = events.ToDictionary(x=>x.IdEvent, x => x);
var usersDict = users.ToDictionary(x => x.IdUser, x => x);
var messagesDtoList = new List<MessageDto>();
var timezone = telemetryService.GetTimezone(telemetry.Id);
2021-04-23 10:21:25 +05:00
foreach (var message in messagesList)
{
var messageDto = new MessageDto
{
Id = message.Id,
WellDepth = message.WellDepth
2021-04-23 10:21:25 +05:00
};
messageDto.DateTime = message.DateTime.ToOffset(TimeSpan.FromHours(timezone.Hours));
if (message.IdTelemetryUser is not null)
{
if (usersDict.TryGetValue((int)message.IdTelemetryUser, out TelemetryUser? user))
{
messageDto.User = user.MakeDisplayName();
}
else
messageDto.User = message.IdTelemetryUser.ToString();
}
if (eventsDict.TryGetValue(message.IdEvent, out TelemetryEvent? e))
2021-04-23 10:21:25 +05:00
{
messageDto.CategoryId = e.IdCategory;
messageDto.Message = e.MakeMessageText(message);
}
messagesDtoList.Add(messageDto);
2021-04-23 10:21:25 +05:00
}
2022-04-11 18:00:34 +05:00
result.Items = result.Items.Concat(messagesDtoList);
2021-04-23 10:21:25 +05:00
return result;
}
public Task InsertAsync(string uid, IEnumerable<TelemetryMessageDto> dtos,
CancellationToken token = default)
2021-04-23 10:21:25 +05:00
{
if (!dtos.Any())
return Task.CompletedTask;
2021-04-23 10:21:25 +05:00
2023-06-20 14:41:19 +05:00
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
var timezone = telemetryService.GetTimezone(telemetry.Id);
2022-04-11 18:00:34 +05:00
2021-04-23 10:21:25 +05:00
foreach (var dto in dtos)
{
2022-04-11 18:00:34 +05:00
var entity = dto.Adapt<TelemetryMessage>();
entity.Id = 0;
2023-06-20 14:41:19 +05:00
entity.IdTelemetry = telemetry.Id;
entity.DateTime = dto.Date.ToOffset(TimeSpan.FromHours(timezone.Hours));
2021-08-10 16:37:13 +05:00
db.TelemetryMessages.Add(entity);
2021-04-23 10:21:25 +05:00
}
return db.SaveChangesAsync(token);
2021-04-23 10:21:25 +05:00
}
}
}