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

189 lines
6.4 KiB
C#
Raw Normal View History

2021-04-23 10:21:25 +05:00
using AsbCloudApp.Data;
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;
private readonly CacheTable<Telemetry> cacheTelemetry;
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;
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
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,
bool isUtc = true,
CancellationToken token = default)
2021-04-23 10:21:25 +05:00
{
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
if (telemetryId is null)
2021-04-23 10:21:25 +05:00
return null;
var events = cacheEvents.Where(e => e.IdTelemetry == telemetryId);
2021-04-23 10:21:25 +05:00
if (!events.Any())
2021-04-23 10:21:25 +05:00
return null;
var query = db.TelemetryMessages.Where(m => m.IdTelemetry == telemetryId)
.OrderBy(m => m.Date).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
}
2021-08-10 14:36:35 +05:00
query = query.OrderByDescending(m => m.Date);
2021-04-23 10:21:25 +05:00
if (begin != default)
2021-08-10 14:36:35 +05:00
query = query.Where(m => m.Date >= begin);
2021-04-23 10:21:25 +05:00
if (end != default)
2021-08-10 14:36:35 +05:00
query = query.Where(m => m.Date <= end);
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;
var users = cacheTUsers.Where(u => u.IdTelemetry == telemetryId);
2021-04-23 10:21:25 +05:00
foreach (var message in messagesList)
{
var messageDto = new MessageDto
{
Date = message.Date,
Id = message.Id,
WellDepth = message.WellDepth
2021-04-23 10:21:25 +05:00
};
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);
}
if (isUtc)
return result;
var telemetry = await cacheTelemetry.FirstOrDefaultAsync(t => t.Id == telemetryId,
token).ConfigureAwait(false);
if (telemetry?.TelemetryTimeZone is null)
return result;
result.Items = result.Items.Select(m =>
{
m.Date = m.Date.AddHours(telemetry.TelemetryTimeZone.Hours);
return m;
}).ToList();
2021-04-23 10:21:25 +05:00
return result;
}
public async Task<DatesRangeDto> GetMessagesDatesRangeAsync(int idWell, bool isUtc,
CancellationToken token = default)
{
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
if (telemetryId is null)
return null;
var (From, To) = await db.GetDatesRangeAsync<TelemetryMessage>((int)telemetryId, token)
.ConfigureAwait(false);
var result = new DatesRangeDto { From = From, To = To };
if (isUtc)
return result;
result = await telemetryService.FixDatesRangeByTimeZoneAsync((int)telemetryId, result, token)
.ConfigureAwait(false);
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.GetOrCreateTemetryIdByUid(uid);
var maxDateDto = dtos.Max(m => m.Date);
telemetryService.SaveRequestDate(uid, maxDateDto);
2021-04-23 10:21:25 +05:00
foreach (var dto in dtos)
{
2021-08-10 16:37:13 +05:00
var entity = dto.Adapt<TelemetryMessage>();
entity.Id = 0;
2021-04-23 10:21:25 +05:00
entity.IdTelemetry = telemetryId;
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
}
}
}