2024-11-25 13:49:07 +05:00
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-11-28 08:55:50 +05:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2024-11-25 13:49:07 +05:00
|
|
|
|
using Persistence.Database.Entity;
|
|
|
|
|
using Persistence.Models;
|
2024-12-05 11:30:07 +05:00
|
|
|
|
using Persistence.Models.Requests;
|
2024-11-25 13:49:07 +05:00
|
|
|
|
using Persistence.Repositories;
|
2024-11-26 12:27:52 +05:00
|
|
|
|
using Persistence.Repository.Extensions;
|
2024-11-25 13:49:07 +05:00
|
|
|
|
|
|
|
|
|
namespace Persistence.Repository.Repositories
|
|
|
|
|
{
|
|
|
|
|
public class TechMessagesRepository : ITechMessagesRepository
|
|
|
|
|
{
|
2024-12-02 15:14:00 +05:00
|
|
|
|
private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DrillingSystem).FullName}CacheKey";
|
|
|
|
|
private const int CacheExpirationInMinutes = 60;
|
2024-11-28 08:55:50 +05:00
|
|
|
|
private readonly IMemoryCache memoryCache;
|
2024-11-25 13:49:07 +05:00
|
|
|
|
private DbContext db;
|
2024-11-28 08:55:50 +05:00
|
|
|
|
|
|
|
|
|
public TechMessagesRepository(DbContext db, IMemoryCache memoryCache)
|
2024-11-25 13:49:07 +05:00
|
|
|
|
{
|
2024-11-28 08:55:50 +05:00
|
|
|
|
this.memoryCache = memoryCache;
|
2024-11-25 13:49:07 +05:00
|
|
|
|
this.db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 13:13:07 +05:00
|
|
|
|
protected virtual IQueryable<TechMessage> GetQueryReadOnly() => db.Set<TechMessage>()
|
|
|
|
|
.Include(e => e.System);
|
2024-11-25 13:49:07 +05:00
|
|
|
|
|
2024-12-05 11:30:07 +05:00
|
|
|
|
public async Task<PaginationContainer<TechMessageDto>> GetPage(PaginationRequest request, CancellationToken token)
|
2024-11-26 10:23:48 +05:00
|
|
|
|
{
|
|
|
|
|
var query = GetQueryReadOnly();
|
2024-12-02 15:14:00 +05:00
|
|
|
|
var count = await query.CountAsync(token);
|
|
|
|
|
|
|
|
|
|
var sort = request.SortSettings != string.Empty
|
|
|
|
|
? request.SortSettings
|
|
|
|
|
: nameof(TechMessage.Timestamp);
|
2024-11-26 10:23:48 +05:00
|
|
|
|
var entities = await query
|
|
|
|
|
.SortBy(request.SortSettings)
|
|
|
|
|
.Skip(request.Skip)
|
|
|
|
|
.Take(request.Take)
|
2024-12-02 15:14:00 +05:00
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
2024-11-26 10:23:48 +05:00
|
|
|
|
var dto = new PaginationContainer<TechMessageDto>()
|
|
|
|
|
{
|
|
|
|
|
Skip = request.Skip,
|
|
|
|
|
Take = request.Take,
|
2024-12-02 15:14:00 +05:00
|
|
|
|
Count = count,
|
2024-11-26 10:23:48 +05:00
|
|
|
|
Items = entities.Select(e => e.Adapt<TechMessageDto>())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
|
public async Task<IEnumerable<MessagesStatisticDto>> GetStatistics(IEnumerable<string> autoDrillingSystem, IEnumerable<int> categoryIds, CancellationToken token)
|
2024-11-25 13:49:07 +05:00
|
|
|
|
{
|
|
|
|
|
var query = GetQueryReadOnly();
|
2024-12-02 15:14:00 +05:00
|
|
|
|
var systems = autoDrillingSystem.Select(s => s.ToLower().Trim());
|
|
|
|
|
var result = await query
|
|
|
|
|
.Where(e => systems.Count() == 0 || systems.Contains(e.System.Name.ToLower().Trim()))
|
|
|
|
|
.GroupBy(e => e.System.Name, (key, group) => new
|
|
|
|
|
{
|
|
|
|
|
System = key,
|
|
|
|
|
Categories = group
|
|
|
|
|
.Where(g => categoryIds.Count() == 0 || categoryIds.Contains(g.CategoryId))
|
|
|
|
|
})
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var entities = new List<MessagesStatisticDto>();
|
|
|
|
|
foreach (var e in result)
|
|
|
|
|
{
|
|
|
|
|
var categories = e.Categories
|
|
|
|
|
.GroupBy(g => g.CategoryId)
|
|
|
|
|
.ToDictionary(c => c.Key, v => v.Count());
|
|
|
|
|
var entity = new MessagesStatisticDto()
|
|
|
|
|
{
|
|
|
|
|
System = e.System,
|
|
|
|
|
Categories = categories
|
|
|
|
|
};
|
|
|
|
|
entities.Add(entity);
|
|
|
|
|
}
|
2024-11-25 13:49:07 +05:00
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
|
return entities;
|
2024-11-25 13:49:07 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 13:13:07 +05:00
|
|
|
|
public async Task<IEnumerable<string>> GetSystems(CancellationToken token)
|
2024-11-25 13:49:07 +05:00
|
|
|
|
{
|
2024-12-02 15:14:00 +05:00
|
|
|
|
var entities = await GetDrillingSystems(token);
|
|
|
|
|
var result = entities.Select(e => e.Name);
|
2024-11-28 08:55:50 +05:00
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
|
return result;
|
2024-11-25 13:49:07 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 16:29:15 +05:00
|
|
|
|
public async Task<int> AddRange(IEnumerable<TechMessageDto> dtos, Guid userId, CancellationToken token)
|
2024-11-25 13:49:07 +05:00
|
|
|
|
{
|
2024-11-28 08:55:50 +05:00
|
|
|
|
|
2024-11-28 13:13:07 +05:00
|
|
|
|
var entities = new List<TechMessage>();
|
|
|
|
|
foreach (var dto in dtos)
|
|
|
|
|
{
|
|
|
|
|
var entity = dto.Adapt<TechMessage>();
|
2024-12-02 15:14:00 +05:00
|
|
|
|
var systems = await GetDrillingSystems(token);
|
|
|
|
|
var systemId = systems.FirstOrDefault(e => e.Name.ToLower().Trim() == dto.System.ToLower().Trim())?.SystemId
|
|
|
|
|
?? await CreateDrillingSystem(dto.System, token);
|
2024-11-28 08:55:50 +05:00
|
|
|
|
|
2024-11-28 13:13:07 +05:00
|
|
|
|
entity.SystemId = systemId;
|
2024-12-04 16:29:15 +05:00
|
|
|
|
entity.UserId = userId;
|
2024-11-28 08:55:50 +05:00
|
|
|
|
|
2024-11-28 13:13:07 +05:00
|
|
|
|
entities.Add(entity);
|
|
|
|
|
}
|
2024-11-25 13:49:07 +05:00
|
|
|
|
|
|
|
|
|
await db.Set<TechMessage>().AddRangeAsync(entities, token);
|
|
|
|
|
var result = await db.SaveChangesAsync(token);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2024-11-28 08:55:50 +05:00
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
|
public async Task<IEnumerable<TechMessageDto>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = GetQueryReadOnly();
|
|
|
|
|
var entities = await query
|
2024-12-04 14:44:33 +05:00
|
|
|
|
.Where(e => e.Timestamp >= dateBegin)
|
2024-12-02 15:14:00 +05:00
|
|
|
|
.Take(take)
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
var dtos = entities
|
|
|
|
|
.Select(e => e.Adapt<TechMessageDto>());
|
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<DatesRangeDto> GetDatesRangeAsync(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = GetQueryReadOnly()
|
|
|
|
|
.GroupBy(e => 1)
|
|
|
|
|
.Select(group => new
|
|
|
|
|
{
|
|
|
|
|
Min = group.Min(e => e.Timestamp),
|
|
|
|
|
Max = group.Max(e => e.Timestamp),
|
|
|
|
|
});
|
|
|
|
|
var values = await query.FirstOrDefaultAsync(token);
|
|
|
|
|
var result = new DatesRangeDto()
|
|
|
|
|
{
|
|
|
|
|
From = values?.Min ?? DateTimeOffset.MinValue,
|
|
|
|
|
To = values?.Max ?? DateTimeOffset.MaxValue
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<IEnumerable<Models.DrillingSystemDto>> GetDrillingSystems(CancellationToken token)
|
2024-11-28 13:13:07 +05:00
|
|
|
|
{
|
|
|
|
|
var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f =>
|
|
|
|
|
{
|
2024-12-02 15:14:00 +05:00
|
|
|
|
f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes);
|
2024-11-28 13:13:07 +05:00
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
|
var query = db.Set<Database.Entity.DrillingSystem>();
|
|
|
|
|
var entities = await query.ToListAsync(token);
|
|
|
|
|
var dtos = entities.Select(e => e.Adapt<Models.DrillingSystemDto>());
|
2024-11-28 13:13:07 +05:00
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
|
return systems!;
|
2024-11-28 13:13:07 +05:00
|
|
|
|
}
|
2024-12-02 15:14:00 +05:00
|
|
|
|
private async Task<Guid> CreateDrillingSystem(string name, CancellationToken token)
|
2024-11-28 08:55:50 +05:00
|
|
|
|
{
|
|
|
|
|
memoryCache.Remove(SystemCacheKey);
|
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
|
var entity = new Database.Entity.DrillingSystem()
|
2024-11-28 08:55:50 +05:00
|
|
|
|
{
|
2024-12-02 15:14:00 +05:00
|
|
|
|
SystemId = default,
|
|
|
|
|
Name = name.ToLower().Trim()
|
2024-11-28 08:55:50 +05:00
|
|
|
|
};
|
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
|
await db.Set<Database.Entity.DrillingSystem>().AddAsync(entity);
|
|
|
|
|
await db.SaveChangesAsync(token);
|
2024-11-28 08:55:50 +05:00
|
|
|
|
|
2024-12-02 15:14:00 +05:00
|
|
|
|
return entity.SystemId;
|
2024-11-28 08:55:50 +05:00
|
|
|
|
}
|
2024-11-25 13:49:07 +05:00
|
|
|
|
}
|
|
|
|
|
}
|