forked from ddrilling/AsbCloudServer
149 lines
4.9 KiB
C#
149 lines
4.9 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
{
|
|
public class MessageService : IMessageService
|
|
{
|
|
private readonly IAsbCloudDbContext db;
|
|
private readonly ITelemetryService telemetryService;
|
|
private readonly CacheTable<TelemetryEvent> cacheEvents;
|
|
private readonly CacheTable<TelemetryUser> cacheTUsers;
|
|
|
|
public MessageService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
|
|
{
|
|
this.db = db;
|
|
this.telemetryService = telemetryService;
|
|
cacheEvents = cacheDb.GetCachedTable<TelemetryEvent>((AsbCloudDbContext)db);
|
|
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)
|
|
{
|
|
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
|
|
if (telemetryId is null)
|
|
return null;
|
|
|
|
var events = cacheEvents.Where(e => e.IdTelemetry == telemetryId);
|
|
|
|
if (!events.Any())
|
|
return null;
|
|
|
|
var query = db.TelemetryMessages.Where(m => m.IdTelemetry == telemetryId)
|
|
.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.Date);
|
|
|
|
if (begin != default)
|
|
query = query.Where(m => m.Date >= begin);
|
|
|
|
if (end != default)
|
|
query = query.Where(m => m.Date <= end);
|
|
|
|
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 users = cacheTUsers.Where(u => u.IdTelemetry == telemetryId);
|
|
|
|
foreach (var message in messagesList)
|
|
{
|
|
var messageDto = new MessageDto
|
|
{
|
|
Date = message.Date,
|
|
Id = message.Id,
|
|
User = users.FirstOrDefault(u => u.IdUser == message.IdTelemetryUser).MakeDisplayName(),
|
|
};
|
|
|
|
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 async Task<DatesRangeDto> GetMessagesDatesRangeAsync(int idWell,
|
|
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);
|
|
|
|
return new DatesRangeDto { From = From, To = To };
|
|
}
|
|
|
|
public Task InsertAsync(string uid, IEnumerable<TelemetryMessageDto> dtos,
|
|
CancellationToken token = default)
|
|
{
|
|
if (!dtos.Any())
|
|
return null;
|
|
|
|
var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid);
|
|
|
|
foreach (var dto in dtos)
|
|
{
|
|
var entity = dto.Adapt<TelemetryMessage>();
|
|
entity.Id = 0;
|
|
entity.IdTelemetry = telemetryId;
|
|
db.TelemetryMessages.Add(entity);
|
|
}
|
|
|
|
return db.SaveChangesAsync(token);
|
|
}
|
|
}
|
|
}
|