2024-11-25 18:11:46 +05:00
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Persistence.Database.Model;
|
|
|
|
|
using Persistence.Models;
|
2024-11-28 16:29:00 +05:00
|
|
|
|
using Persistence.Models.Requests;
|
2024-11-25 18:11:46 +05:00
|
|
|
|
using Persistence.Repositories;
|
|
|
|
|
|
|
|
|
|
namespace Persistence.Repository.Repositories;
|
2024-11-26 11:47:42 +05:00
|
|
|
|
public class ChangeLogRepository : IChangeLogRepository
|
2024-11-25 18:11:46 +05:00
|
|
|
|
{
|
|
|
|
|
private DbContext db;
|
|
|
|
|
|
|
|
|
|
public ChangeLogRepository(DbContext db)
|
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
public Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
|
2024-11-25 18:11:46 +05:00
|
|
|
|
{
|
2024-11-28 16:29:00 +05:00
|
|
|
|
foreach (var dto in dtos)
|
|
|
|
|
{
|
|
|
|
|
var entity = CreateEntityFromDto(idUser, idDiscriminator, dto);
|
|
|
|
|
db.Set<ChangeLog>().Add(entity);
|
|
|
|
|
}
|
2024-11-27 17:59:37 +05:00
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var result = db.SaveChangesAsync(token);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> MarkAsDeleted(Guid idUser, IEnumerable<Guid> ids, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = db.Set<ChangeLog>().Where(s => ids.Contains(s.Id));
|
|
|
|
|
var entities = await query.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var result = await Clear(idUser, entities, token);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> MarkAsDeleted(Guid idUser, Guid idDiscriminator, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = db.Set<ChangeLog>()
|
2024-11-27 17:59:37 +05:00
|
|
|
|
.Where(s => s.IdDiscriminator == idDiscriminator)
|
|
|
|
|
.Where(e => e.Obsolete == null);
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var entities = await query.ToArrayAsync(token);
|
2024-11-27 17:59:37 +05:00
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var result = await Clear(idUser, entities, token);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<int> Clear(Guid idUser, IEnumerable<ChangeLog> entities, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var updateTime = DateTimeOffset.UtcNow;
|
2024-11-27 17:59:37 +05:00
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
foreach (var entity in entities)
|
2024-11-27 17:59:37 +05:00
|
|
|
|
{
|
|
|
|
|
entity.Obsolete = updateTime;
|
|
|
|
|
entity.IdEditor = idUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await db.SaveChangesAsync(token);
|
2024-11-25 18:11:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 17:59:37 +05:00
|
|
|
|
public async Task<int> ClearAndInsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
|
2024-11-25 18:11:46 +05:00
|
|
|
|
{
|
2024-11-27 17:59:37 +05:00
|
|
|
|
var result = 0;
|
|
|
|
|
using var transaction = await db.Database.BeginTransactionAsync(token);
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-11-28 16:29:00 +05:00
|
|
|
|
result += await MarkAsDeleted(idUser, idDiscriminator, token);
|
2024-11-27 17:59:37 +05:00
|
|
|
|
result += await InsertRange(idUser, idDiscriminator, dtos, token);
|
|
|
|
|
|
|
|
|
|
await transaction.CommitAsync(token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
await transaction.RollbackAsync(token);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-25 18:11:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
public async Task<int> UpdateRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
|
2024-11-25 18:11:46 +05:00
|
|
|
|
{
|
2024-11-27 17:59:37 +05:00
|
|
|
|
var dbSet = db.Set<ChangeLog>();
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var updatedIds = dtos.Select(d => d.Id);
|
|
|
|
|
var updatedEntities = dbSet
|
|
|
|
|
.Where(s => updatedIds.Contains(s.Id))
|
|
|
|
|
.ToDictionary(s => s.Id);
|
2024-11-27 17:59:37 +05:00
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var result = 0;
|
|
|
|
|
using var transaction = await db.Database.BeginTransactionAsync(token);
|
|
|
|
|
try
|
2024-11-27 17:59:37 +05:00
|
|
|
|
{
|
2024-11-28 16:29:00 +05:00
|
|
|
|
foreach (var dto in dtos)
|
2024-11-27 17:59:37 +05:00
|
|
|
|
{
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var newEntity = CreateEntityFromDto(idUser, idDiscriminator, dto);
|
|
|
|
|
dbSet.Add(newEntity);
|
|
|
|
|
|
|
|
|
|
var updatedEntity = updatedEntities.GetValueOrDefault(dto.Id)!;
|
|
|
|
|
updatedEntity.IdNext = newEntity.Id;
|
|
|
|
|
updatedEntity.Obsolete = DateTimeOffset.UtcNow;
|
|
|
|
|
updatedEntity.IdEditor = idUser;
|
2024-11-27 17:59:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
await db.SaveChangesAsync(token);
|
|
|
|
|
await transaction.CommitAsync(token);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
await transaction.RollbackAsync(token);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-25 18:11:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
public async Task<PaginationContainer<DataWithWellDepthAndSectionDto>> GetByDate(
|
|
|
|
|
Guid idDiscriminator,
|
|
|
|
|
DateTimeOffset momentUtc,
|
|
|
|
|
SectionPartRequest request,
|
|
|
|
|
CancellationToken token)
|
2024-11-25 18:11:46 +05:00
|
|
|
|
{
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var query = BuildQuery(idDiscriminator, momentUtc, request);
|
|
|
|
|
var result = await BuildPaginationContainer(query, request, token);
|
2024-11-27 17:59:37 +05:00
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
return result;
|
2024-11-25 18:11:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
private IQueryable<ChangeLog> BuildQuery(Guid idDiscriminator, DateTimeOffset momentUtc, SectionPartRequest request)
|
2024-11-25 18:11:46 +05:00
|
|
|
|
{
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var query = db.Set<ChangeLog>()
|
|
|
|
|
.Where(e => e.IdDiscriminator == idDiscriminator)
|
|
|
|
|
.Where(e => e.Creation <= momentUtc)
|
|
|
|
|
.Where(e => e.Obsolete == null || e.Obsolete >= momentUtc);
|
|
|
|
|
|
|
|
|
|
if (request.IdSection.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(e => e.IdSection == request.IdSection);
|
|
|
|
|
}
|
|
|
|
|
if (request.DepthStart.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(e => e.DepthStart >= request.DepthStart);
|
|
|
|
|
}
|
|
|
|
|
if (request.DepthEnd.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(e => e.DepthEnd <= request.DepthEnd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query;
|
2024-11-25 18:11:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
public async Task<IEnumerable<ChangeLogDto>> GetChangeLogForDate(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
|
2024-11-25 18:11:46 +05:00
|
|
|
|
{
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var query = db.Set<ChangeLog>().Where(s => s.IdDiscriminator == idDiscriminator);
|
|
|
|
|
|
|
|
|
|
var min = new DateTimeOffset(dateBegin.Year, dateBegin.Month, dateBegin.Day, 0, 0, 0, TimeSpan.Zero);
|
|
|
|
|
var max = new DateTimeOffset(dateEnd.Year, dateEnd.Month, dateEnd.Day, 0, 0, 0, TimeSpan.Zero);
|
|
|
|
|
|
|
|
|
|
var createdQuery = query.Where(e => e.Creation >= min && e.Creation <= max);
|
|
|
|
|
var editedQuery = query.Where(e => e.Obsolete != null && e.Obsolete >= min && e.Obsolete <= max);
|
|
|
|
|
|
|
|
|
|
query = createdQuery.Union(editedQuery);
|
|
|
|
|
var entities = await query.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var dtos = entities.Select(e => e.Adapt<ChangeLogDto>());
|
|
|
|
|
|
|
|
|
|
return dtos;
|
2024-11-25 18:11:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<PaginationContainer<DataWithWellDepthAndSectionDto>> BuildPaginationContainer(IQueryable<ChangeLog> query, SectionPartRequest request, CancellationToken token)
|
2024-11-25 18:11:46 +05:00
|
|
|
|
{
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var result = new PaginationContainer<DataWithWellDepthAndSectionDto>
|
2024-11-26 11:47:42 +05:00
|
|
|
|
{
|
2024-11-28 16:29:00 +05:00
|
|
|
|
Skip = request.Skip ?? 0,
|
|
|
|
|
Take = request.Take ?? 32,
|
|
|
|
|
Items = Enumerable.Empty<DataWithWellDepthAndSectionDto>(),
|
|
|
|
|
Count = await query.CountAsync(token)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(request.SortSettings))
|
|
|
|
|
{
|
|
|
|
|
query = query.SortBy(request.SortSettings);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
query = query
|
|
|
|
|
.OrderBy(e => e.IdSection)
|
|
|
|
|
.ThenBy(e => e.DepthStart)
|
|
|
|
|
.ThenBy(e => e.DepthEnd);
|
2024-11-26 11:47:42 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
var entities = await query
|
|
|
|
|
.Skip(result.Skip)
|
|
|
|
|
.Take(result.Take)
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var dtos = entities.Select(e => e.Adapt<DataWithWellDepthAndSectionDto>());
|
|
|
|
|
result.Items = dtos;
|
2024-11-26 11:47:42 +05:00
|
|
|
|
|
2024-11-26 10:32:44 +05:00
|
|
|
|
return result;
|
2024-11-25 18:11:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:29:00 +05:00
|
|
|
|
public async Task<IEnumerable<DateOnly>> GetDatesChange(Guid idDiscriminator, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = db.Set<ChangeLog>().Where(e => e.IdDiscriminator == idDiscriminator);
|
|
|
|
|
|
|
|
|
|
var datesCreateQuery = query
|
|
|
|
|
.Select(e => e.Creation)
|
|
|
|
|
.Distinct();
|
|
|
|
|
|
|
|
|
|
var datesCreate = await datesCreateQuery.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var datesUpdateQuery = query
|
|
|
|
|
.Where(e => e.Obsolete != null)
|
|
|
|
|
.Select(e => e.Obsolete!.Value)
|
|
|
|
|
.Distinct();
|
|
|
|
|
|
|
|
|
|
var datesUpdate = await datesUpdateQuery.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var dates = Enumerable.Concat(datesCreate, datesUpdate);
|
|
|
|
|
var datesOnly = dates
|
|
|
|
|
.Select(d => new DateOnly(d.Year, d.Month, d.Day))
|
|
|
|
|
.Distinct()
|
|
|
|
|
.OrderBy(d => d);
|
|
|
|
|
|
|
|
|
|
return datesOnly;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<IEnumerable<IDictionary<string, object>>> GetGtDate(DateTimeOffset dateBegin, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 17:59:37 +05:00
|
|
|
|
private ChangeLog CreateEntityFromDto(Guid idUser, Guid idDiscriminator, DataWithWellDepthAndSectionDto dto)
|
2024-11-25 18:11:46 +05:00
|
|
|
|
{
|
2024-11-27 17:59:37 +05:00
|
|
|
|
var entity = new ChangeLog()
|
|
|
|
|
{
|
|
|
|
|
Id = default,
|
|
|
|
|
Creation = DateTimeOffset.UtcNow,
|
|
|
|
|
IdAuthor = idUser,
|
|
|
|
|
IdDiscriminator = idDiscriminator,
|
|
|
|
|
IdEditor = idUser,
|
|
|
|
|
|
|
|
|
|
Value = dto.Value,
|
|
|
|
|
IdSection = dto.IdSection,
|
|
|
|
|
DepthStart = dto.DepthStart,
|
|
|
|
|
DepthEnd = dto.DepthEnd,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 18:11:46 +05:00
|
|
|
|
}
|