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

128 lines
4.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;
using AutoMapper;
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 IMapper mapper;
private readonly ICacheTable<Event> cacheEvents;
private readonly ICacheTable<TelemetryUser> cacheTUsers;
private readonly ICacheTable<Telemetry> cacheTelemetry;
private readonly ICacheTable<Well> cacheWells;
public MessageService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService, MapperConfiguration mapperConfiguration)
{
this.db = db;
this.telemetryService = telemetryService;
mapper = mapperConfiguration.CreateMapper();
cacheEvents = cacheDb.GetCachedTable<Event>((AsbCloudDbContext)db);
cacheTUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
public PaginationContainer<MessageDto> GetMessages(int wellId, IEnumerable<int> categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32)
{
var well = cacheWells.FirstOrDefault(w => w.Id == wellId);
if (well is null)
return null;
var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == well.Id);
if (telemetry is null)
return null;
var events = cacheEvents.Select(e => e.IdTelemetry == telemetry.Id);
if (events.Count() == 0)
return null;
var messages = from m in db.Messages
where m.IdTelemetry == telemetry.Id
select m;
if ((categoryids != default) && (categoryids.Count() > 0))
{
var eventIds = from e in events
where categoryids.ToList().Contains(e.IdCategory)
select e.IdEvent;
if (eventIds.Count() == 0)
return null;
messages = messages.Where(m => eventIds.Contains(m.IdEvent));
}
var result = new PaginationContainer<MessageDto>() { Skip = skip, Take = take };
2021-04-23 10:21:25 +05:00
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 void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos)
{
if (dtos.Count() == 0)
return;
var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid);
foreach (var dto in dtos)
{
var entity = mapper.Map<Message>(dto);
entity.Id = 0;
2021-04-23 10:21:25 +05:00
entity.IdTelemetry = telemetryId;
db.Messages.Add(entity);
}
db.SaveChanges();
}
}
}