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

163 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;
2021-04-23 10:21:25 +05:00
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
2021-07-28 09:46:58 +05:00
using Mapster;
2021-04-23 10:21:25 +05:00
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2021-04-23 10:21:25 +05:00
namespace AsbCloudInfrastructure.Services
{
public class MessageService : IMessageService
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
2021-08-10 16:37:13 +05:00
private readonly CacheTable<TelemetryEvent> cacheEvents;
private readonly CacheTable<TelemetryUser> cacheTUsers;
2021-04-23 10:21:25 +05:00
2021-07-28 09:46:58 +05:00
public MessageService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
2021-04-23 10:21:25 +05:00
{
this.db = db;
this.telemetryService = telemetryService;
2021-08-10 16:37:13 +05:00
cacheEvents = cacheDb.GetCachedTable<TelemetryEvent>((AsbCloudDbContext)db);
2021-04-23 10:21:25 +05:00
cacheTUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
}
public async Task<PaginationContainer<MessageDto>> GetMessagesAsync(
int idWell,
IEnumerable<int> categoryids = default,
DateTime begin = default,
DateTime end = default,
string searchString = default,
int skip = 0,
int take = 32,
CancellationToken token = default)
2021-04-23 10:21:25 +05:00
{
2021-11-22 11:30:08 +05:00
var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell);
if (idTelemetry is null)
2021-04-23 10:21:25 +05:00
return null;
2021-11-22 11:30:08 +05:00
var events = cacheEvents.Where(e => e.IdTelemetry == idTelemetry);
2021-04-23 10:21:25 +05:00
if (!events.Any())
2021-04-23 10:21:25 +05:00
return null;
2021-11-22 11:30:08 +05:00
var query = db.TelemetryMessages.Where(m => m.IdTelemetry == idTelemetry)
2022-04-01 17:55:44 +05:00
.OrderBy(m => m.DateTime).AsNoTracking();
2021-04-23 10:21:25 +05:00
if ((categoryids?.Any() == true) || !string.IsNullOrEmpty(searchString))
2021-04-23 10:21:25 +05:00
{
if (!string.IsNullOrEmpty(searchString))
events = events.Where(e => e.MessageTemplate.Contains(searchString, StringComparison.OrdinalIgnoreCase));
if (categoryids?.Any() == true)
events = events.Where(e => 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())
2021-04-23 10:21:25 +05:00
return null;
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
var timezone = telemetryService.GetTimezone(idTelemetry??default);
2021-11-22 11:30:08 +05:00
2021-04-23 10:21:25 +05:00
if (begin != default)
{
var beginUtc = begin.ToUtcDateTimeOffset(timezone.Hours);
2022-04-01 17:55:44 +05:00
query = query.Where(m => m.DateTime >= beginUtc);
}
2021-04-23 10:21:25 +05:00
if (end != default)
{
var endUtc = end.ToUtcDateTimeOffset(timezone.Hours);
2022-04-01 17:55:44 +05:00
query = query.Where(m => m.DateTime <= endUtc);
}
2021-04-23 10:21:25 +05:00
var result = new PaginationContainer<MessageDto>
{
Skip = skip,
Take = take,
2021-08-10 14:36:35 +05:00
Count = query.Count()
};
2021-04-23 10:21:25 +05:00
if (skip > 0)
2021-08-10 14:36:35 +05:00
query = query.Skip(skip);
2021-04-23 10:21:25 +05:00
var messagesList = await query.Take(take).AsNoTracking()
.ToListAsync(token).ConfigureAwait(false);
2021-04-23 10:21:25 +05:00
if (messagesList.Count == 0)
return result;
2021-11-22 11:30:08 +05:00
var users = cacheTUsers.Where(u => u.IdTelemetry == idTelemetry);
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
};
2022-04-01 17:55:44 +05:00
messageDto.Date = message.DateTime.ToRemoteDateTime(timezone.Hours);
if (message.IdTelemetryUser is not null)
{
if (users.Any())
{
var user = users.FirstOrDefault(u => u.IdUser == message.IdTelemetryUser);
messageDto.User = user.MakeDisplayName();
}
else
messageDto.User = message.IdTelemetryUser.ToString();
}
2021-04-23 10:21:25 +05:00
var e = events.FirstOrDefault(e => e.IdEvent == message.IdEvent);
if (e != null)
{
messageDto.CategoryId = e.IdCategory;
messageDto.Message = e.MakeMessageText(message);
}
result.Items.Add(messageDto);
}
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 null;
2021-04-23 10:21:25 +05:00
var telemetryId = telemetryService.GetOrCreateTelemetryIdByUid(uid);
var timezone = telemetryService.GetTimezone(telemetryId);
var maxDateDto = dtos.Max(m => m.Date);
var maxDateUtc = maxDateDto.ToUtcDateTimeOffset(timezone.Hours);
telemetryService.SaveRequestDate(uid, maxDateDto);
2021-04-23 10:21:25 +05:00
foreach (var dto in dtos)
{
var entity = dto.Adapt<TelemetryMessage>();
entity.Id = 0;
2021-04-23 10:21:25 +05:00
entity.IdTelemetry = telemetryId;
2022-04-01 17:55:44 +05:00
entity.DateTime = dto.Date.ToUtcDateTimeOffset(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
}
}
}