forked from ddrilling/AsbCloudServer
157 lines
5.2 KiB
C#
157 lines
5.2 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Data.SAUB;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.EfCache;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Services.SAUB
|
|
{
|
|
public class MessageService : IMessageService
|
|
{
|
|
private readonly IAsbCloudDbContext db;
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
public MessageService(IAsbCloudDbContext db, ITelemetryService telemetryService)
|
|
{
|
|
this.db = db;
|
|
this.telemetryService = telemetryService;
|
|
}
|
|
|
|
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)
|
|
{
|
|
var idTelemetry = telemetryService.GetOrDefaultIdTelemetryByIdWell(idWell);
|
|
if (idTelemetry is null)
|
|
return null;
|
|
|
|
var allEvents = await db.TelemetryEvents.FromCacheAsync(token);
|
|
var events = allEvents.Where(e => e.IdTelemetry == idTelemetry);
|
|
|
|
if (!events.Any())
|
|
return null;
|
|
|
|
var query = db.TelemetryMessages.Where(m => m.IdTelemetry == idTelemetry)
|
|
.OrderBy(m => m.DateTime).AsNoTracking();
|
|
|
|
if (categoryids?.Any() == true || !string.IsNullOrEmpty(searchString))
|
|
{
|
|
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);
|
|
|
|
if (!eventIds.Any())
|
|
return null;
|
|
|
|
query = query.Where(m => eventIds.Contains(m.IdEvent));
|
|
}
|
|
|
|
query = query.OrderByDescending(m => m.DateTime);
|
|
|
|
var timezone = telemetryService.GetTimezone(idTelemetry ?? default);
|
|
|
|
if (begin != default)
|
|
{
|
|
var beginUtc = begin.ToUtcDateTimeOffset(timezone.Hours);
|
|
query = query.Where(m => m.DateTime >= beginUtc);
|
|
}
|
|
|
|
if (end != default)
|
|
{
|
|
var endUtc = end.ToUtcDateTimeOffset(timezone.Hours);
|
|
query = query.Where(m => m.DateTime <= endUtc);
|
|
}
|
|
|
|
var result = new PaginationContainer<MessageDto>
|
|
{
|
|
Skip = skip,
|
|
Take = take,
|
|
Count = query.Count()
|
|
};
|
|
|
|
if (skip > 0)
|
|
query = query.Skip(skip);
|
|
|
|
var messagesList = await query.Take(take).AsNoTracking()
|
|
.ToListAsync(token).ConfigureAwait(false);
|
|
|
|
if (messagesList.Count == 0)
|
|
return result;
|
|
|
|
var allUsers = await db.TelemetryUsers.FromCacheAsync(token);
|
|
var users = allUsers.Where(u => u.IdTelemetry == idTelemetry);
|
|
|
|
foreach (var message in messagesList)
|
|
{
|
|
var messageDto = new MessageDto
|
|
{
|
|
Id = message.Id,
|
|
WellDepth = message.WellDepth
|
|
};
|
|
|
|
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();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public Task InsertAsync(string uid, IEnumerable<TelemetryMessageDto> dtos,
|
|
CancellationToken token = default)
|
|
{
|
|
if (!dtos.Any())
|
|
return null;
|
|
|
|
var telemetryId = telemetryService.GetOrCreateTelemetryIdByUid(uid);
|
|
var timezone = telemetryService.GetTimezone(telemetryId);
|
|
|
|
foreach (var dto in dtos)
|
|
{
|
|
var entity = dto.Adapt<TelemetryMessage>();
|
|
entity.Id = 0;
|
|
entity.IdTelemetry = telemetryId;
|
|
entity.DateTime = dto.Date.ToUtcDateTimeOffset(timezone.Hours);
|
|
db.TelemetryMessages.Add(entity);
|
|
}
|
|
|
|
return db.SaveChangesAsync(token);
|
|
}
|
|
}
|
|
}
|