forked from ddrilling/AsbCloudServer
129 lines
4.2 KiB
C#
129 lines
4.2 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;
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
{
|
|
public class MessageService : IMessageService
|
|
{
|
|
private readonly IAsbCloudDbContext db;
|
|
private readonly ITelemetryService telemetryService;
|
|
private readonly CacheTable<Event> cacheEvents;
|
|
private readonly CacheTable<TelemetryUser> cacheTUsers;
|
|
|
|
public MessageService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
|
|
{
|
|
this.db = db;
|
|
this.telemetryService = telemetryService;
|
|
cacheEvents = cacheDb.GetCachedTable<Event>((AsbCloudDbContext)db);
|
|
cacheTUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
|
|
}
|
|
|
|
public PaginationContainer<MessageDto> GetMessages(int idWell, IEnumerable<int> categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32)
|
|
{
|
|
var telemetry = telemetryService.GetTelemetryByidWell(idWell);
|
|
if (telemetry is null)
|
|
return null;
|
|
|
|
var events = cacheEvents.Select(e => e.IdTelemetry == telemetry.Id);
|
|
|
|
if (!events.Any())
|
|
return null;
|
|
|
|
var messages = from m in db.Messages
|
|
where m.IdTelemetry == telemetry.Id
|
|
select m;
|
|
|
|
if ((categoryids != default) && (categoryids.Any()))
|
|
{
|
|
var eventIds = from e in events
|
|
where categoryids.ToList().Contains(e.IdCategory)
|
|
select e.IdEvent;
|
|
|
|
if (!eventIds.Any())
|
|
return null;
|
|
|
|
messages = messages.Where(m => eventIds.Contains(m.IdEvent));
|
|
}
|
|
|
|
var result = new PaginationContainer<MessageDto>() { Skip = skip, Take = take };
|
|
|
|
messages = messages.OrderByDescending(m => m.Date);
|
|
|
|
if (begin != default)
|
|
messages = messages.Where(m => m.Date >= begin);
|
|
|
|
if (end != default)
|
|
messages = messages.Where(m => m.Date <= end);
|
|
|
|
result.Count = messages.Count();
|
|
|
|
if (skip > 0)
|
|
messages = messages.Skip(skip);
|
|
|
|
var messagesList = messages.Take(take).ToList();
|
|
|
|
if (messagesList.Count == 0)
|
|
return result;
|
|
|
|
var users = cacheTUsers.Select(u => u.IdTelemetry == telemetry.Id);
|
|
|
|
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 DatesRangeDto GetMessagesDatesRange(int idWell)
|
|
{
|
|
var telemetry = telemetryService.GetTelemetryByidWell(idWell);
|
|
if (telemetry is null)
|
|
return null;
|
|
|
|
var (From, To) = db.GetDatesRange<Message>(telemetry.Id);
|
|
|
|
return new DatesRangeDto { From = From, To = To };
|
|
}
|
|
|
|
public void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos)
|
|
{
|
|
if (!dtos.Any())
|
|
return;
|
|
|
|
var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid);
|
|
|
|
foreach (var dto in dtos)
|
|
{
|
|
var entity = dto.Adapt<Message>();
|
|
entity.Id = 0;
|
|
entity.IdTelemetry = telemetryId;
|
|
db.Messages.Add(entity);
|
|
}
|
|
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
}
|