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

129 lines
4.2 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;
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;
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;
cacheEvents = cacheDb.GetCachedTable<Event>((AsbCloudDbContext)db);
cacheTUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
}
2021-07-27 14:43:30 +05:00
public PaginationContainer<MessageDto> GetMessages(int idWell, IEnumerable<int> categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32)
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.Select(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 messages = from m in db.Messages
where m.IdTelemetry == telemetryId
2021-04-23 10:21:25 +05:00
select m;
2021-05-19 15:21:19 +05:00
if ((categoryids != default) && (categoryids.Any()))
2021-04-23 10:21:25 +05:00
{
var eventIds = from e in events
where categoryids.ToList().Contains(e.IdCategory)
select e.IdEvent;
2021-05-19 15:21:19 +05:00
if (!eventIds.Any())
2021-04-23 10:21:25 +05:00
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 == telemetryId);
2021-04-23 10:21:25 +05:00
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;
}
2021-07-27 14:43:30 +05:00
public DatesRangeDto GetMessagesDatesRange(int idWell)
{
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
if (telemetryId is null)
return null;
var (From, To) = db.GetDatesRange<Message>((int)telemetryId);
2021-05-19 15:21:19 +05:00
return new DatesRangeDto { From = From, To = To };
}
2021-04-23 10:21:25 +05:00
public void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos)
{
if (!dtos.Any())
2021-04-23 10:21:25 +05:00
return;
var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid);
foreach (var dto in dtos)
{
2021-07-28 09:46:58 +05:00
var entity = dto.Adapt<Message>();
entity.Id = 0;
2021-04-23 10:21:25 +05:00
entity.IdTelemetry = telemetryId;
db.Messages.Add(entity);
}
db.SaveChanges();
}
}
}