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 cacheEvents; private readonly CacheTable cacheTUsers; public MessageService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService) { this.db = db; this.telemetryService = telemetryService; cacheEvents = cacheDb.GetCachedTable((AsbCloudDbContext)db); cacheTUsers = cacheDb.GetCachedTable((AsbCloudDbContext)db); } public PaginationContainer GetMessages( int idWell, IEnumerable categoryids = default, DateTime begin = default, DateTime end = default, string searchString = default, int skip = 0, int take = 32) { var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell); if (telemetryId is null) return null; var events = cacheEvents.Select(e => e.IdTelemetry == telemetryId); if (!events.Any()) return null; var messages = db.Messages.Where(m => m.IdTelemetry == telemetryId); 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; messages = messages.Where(m => eventIds.Contains(m.IdEvent)); } 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); var result = new PaginationContainer { Skip = skip, Take = take, 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); 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 telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell); if (telemetryId is null) return null; var (From, To) = db.GetDatesRange((int)telemetryId); return new DatesRangeDto { From = From, To = To }; } public void Insert(string uid, IEnumerable dtos) { if (!dtos.Any()) return; var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid); foreach (var dto in dtos) { var entity = dto.Adapt(); entity.Id = 0; entity.IdTelemetry = telemetryId; db.Messages.Add(entity); } db.SaveChanges(); } } }