195 lines
6.0 KiB
C#
195 lines
6.0 KiB
C#
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Persistence.Database.Model;
|
|
using Persistence.Models;
|
|
using Persistence.Repositories;
|
|
|
|
namespace Persistence.Repository.Repositories;
|
|
public class ChangeLogRepository : IChangeLogRepository
|
|
{
|
|
private DbContext db;
|
|
|
|
public ChangeLogRepository(DbContext db)
|
|
{
|
|
this.db = db;
|
|
}
|
|
|
|
public async Task<int> Clear(Guid idUser, Guid idDiscriminator, CancellationToken token)
|
|
{
|
|
var updateTime = DateTimeOffset.UtcNow;
|
|
|
|
var dbSet = db.Set<ChangeLog>();
|
|
var query = dbSet
|
|
.Where(s => s.IdDiscriminator == idDiscriminator)
|
|
.Where(e => e.Obsolete == null);
|
|
|
|
var entitiesToDelete = await query.ToArrayAsync(token);
|
|
|
|
foreach (var entity in entitiesToDelete)
|
|
{
|
|
entity.Obsolete = updateTime;
|
|
entity.IdEditor = idUser;
|
|
}
|
|
|
|
return await db.SaveChangesAsync(token);
|
|
}
|
|
|
|
public async Task<int> ClearAndInsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
|
|
{
|
|
var result = 0;
|
|
using var transaction = await db.Database.BeginTransactionAsync(token);
|
|
try
|
|
{
|
|
result += await Clear(idUser, idDiscriminator, token);
|
|
result += await InsertRange(idUser, idDiscriminator, dtos, token);
|
|
|
|
await transaction.CommitAsync(token);
|
|
return result;
|
|
}
|
|
catch
|
|
{
|
|
await transaction.RollbackAsync(token);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<ChangeLogDto>> GetChangeLogForDate(Guid idDiscriminator, DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
|
|
{
|
|
var dbSet = db.Set<ChangeLog>();
|
|
var query = dbSet.Where(s => s.IdDiscriminator == idDiscriminator);
|
|
|
|
var min = dateBegin;
|
|
var max = dateEnd;
|
|
|
|
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.ToListAsync(token);
|
|
|
|
var dtos = entities.Select(e => new ChangeLogDto
|
|
{
|
|
Creation = e.Creation,
|
|
IdAuthor = e.IdAuthor,
|
|
IdEditor = e.IdEditor,
|
|
IdNext = e.IdNext,
|
|
Obsolete = e.Obsolete,
|
|
Value = new DataWithWellDepthAndSectionDto()
|
|
{
|
|
Value = e.Value,
|
|
IdSection = e.IdSection,
|
|
Id = e.Id,
|
|
DepthEnd = e.DepthEnd,
|
|
DepthStart = e.DepthStart
|
|
}
|
|
});
|
|
|
|
return dtos;
|
|
}
|
|
|
|
public async Task<IEnumerable<DataWithWellDepthAndSectionDto>> GetByDate(Guid idDiscriminator, DateTimeOffset momentUtc, CancellationToken token)
|
|
{
|
|
var dbSet = db.Set<ChangeLog>();
|
|
var dbQuery = dbSet
|
|
.Where(s => s.IdDiscriminator == idDiscriminator)
|
|
.Where(e => e.Creation <= momentUtc)
|
|
.Where(e => e.Obsolete == null || e.Obsolete >= momentUtc);
|
|
|
|
var entities = await dbQuery.ToArrayAsync(token);
|
|
var dtos = entities.Select(e => e.Adapt<DataWithWellDepthAndSectionDto>());
|
|
|
|
return dtos;
|
|
}
|
|
|
|
public Task<IEnumerable<DateOnly>> GetDatesChange(CancellationToken token)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public Task<IEnumerable<IDictionary<string, object>>> GetGtDate(DateTimeOffset dateBegin, CancellationToken token)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public Task<int> InsertRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
|
|
{
|
|
foreach (var dto in dtos)
|
|
{
|
|
var entity = CreateEntityFromDto(idUser, idDiscriminator, dto);
|
|
db.Set<ChangeLog>().Add(entity);
|
|
}
|
|
|
|
var result = db.SaveChangesAsync(token);
|
|
|
|
return result;
|
|
}
|
|
|
|
private ChangeLog CreateEntityFromDto(Guid idUser, Guid idDiscriminator, DataWithWellDepthAndSectionDto dto)
|
|
{
|
|
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;
|
|
}
|
|
|
|
public async Task<int> MarkAsDeleted(Guid idUser, IEnumerable<Guid> ids, CancellationToken token)
|
|
{
|
|
var result = 0;
|
|
var dbSet = db.Set<ChangeLog>();
|
|
|
|
var updatedEntity = dbSet
|
|
.Where(s => ids.Contains(s.Id))
|
|
.FirstOrDefault();
|
|
if (updatedEntity != null)
|
|
{
|
|
updatedEntity.Obsolete = DateTimeOffset.UtcNow;
|
|
|
|
result = await db.SaveChangesAsync(token);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public Task<int> UpdateOrInsertRange(int idUser, IEnumerable<IDictionary<string, object>> dtos, CancellationToken token)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task<int> UpdateRange(Guid idUser, Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)
|
|
{
|
|
var dbSet = db.Set<ChangeLog>();
|
|
|
|
var updatedIds = dtos.Select(d => d.Id);
|
|
var updatedEntities = dbSet
|
|
.Where(s => updatedIds.Contains(s.Id))
|
|
.ToDictionary(s => s.Id);
|
|
//todo
|
|
foreach (var dto in dtos)
|
|
{
|
|
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;
|
|
|
|
}
|
|
|
|
var result = await db.SaveChangesAsync(token);
|
|
|
|
return result;
|
|
}
|
|
}
|