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 CacheTable cacheEvents; private readonly CacheTable cacheTUsers; public MessageService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService, MapperConfiguration mapperConfiguration) { this.db = db; this.telemetryService = telemetryService; mapper = mapperConfiguration.CreateMapper(); cacheEvents = cacheDb.GetCachedTable((AsbCloudDbContext)db); cacheTUsers = cacheDb.GetCachedTable((AsbCloudDbContext)db); } public PaginationContainer GetMessages(int wellId, IEnumerable categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32) { var telemetry = telemetryService.GetTelemetryByWellId(wellId); 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() { 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 wellId) { var telemetry = telemetryService.GetTelemetryByWellId(wellId); if (telemetry is null) return null; var (From, To) = db.GetDatesRange(telemetry.Id); 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 = mapper.Map(dto); entity.Id = 0; entity.IdTelemetry = telemetryId; db.Messages.Add(entity); } db.SaveChanges(); } } }