2024-05-03 14:37:46 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Exceptions;
|
2024-01-29 12:25:58 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
|
2024-06-05 12:08:38 +05:00
|
|
|
|
public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : IChangeLogRepository<TDto, TRequest>
|
2024-05-28 00:05:21 +05:00
|
|
|
|
where TDto : AsbCloudApp.Data.IId
|
2024-01-29 12:25:58 +05:00
|
|
|
|
where TEntity : ChangeLogAbstract
|
|
|
|
|
{
|
2024-02-06 16:49:29 +05:00
|
|
|
|
protected readonly IAsbCloudDbContext db;
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
2024-02-06 16:49:29 +05:00
|
|
|
|
public ChangeLogRepositoryAbstract(IAsbCloudDbContext db)
|
2024-01-29 12:25:58 +05:00
|
|
|
|
{
|
2024-02-06 16:49:29 +05:00
|
|
|
|
this.db = db;
|
2024-01-29 12:25:58 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 16:55:44 +05:00
|
|
|
|
private class ChangeLogRepositoryBuilder<TEntityChangeLog, TDtoChangeLog, TRequestChangeLog> : IChangeLogRepositoryBuilder<TDto, TRequest>
|
|
|
|
|
{
|
|
|
|
|
private readonly ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> Repository;
|
|
|
|
|
private IQueryable<TEntity> Query;
|
|
|
|
|
|
|
|
|
|
public ChangeLogRepositoryBuilder(
|
|
|
|
|
ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> repository,
|
|
|
|
|
ChangeLogRequest request)
|
|
|
|
|
{
|
|
|
|
|
this.Repository = repository;
|
2024-06-05 12:08:38 +05:00
|
|
|
|
this.Query = repository.db.Set<TEntity>()
|
|
|
|
|
.Include(e => e.Author)
|
|
|
|
|
.Include(e => e.Editor);
|
2024-06-04 16:55:44 +05:00
|
|
|
|
|
|
|
|
|
if (request.Moment.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var momentUtc = request.Moment.Value.ToUniversalTime();
|
|
|
|
|
|
2024-06-05 12:08:38 +05:00
|
|
|
|
this.Query = this.Query
|
2024-06-04 16:55:44 +05:00
|
|
|
|
.Where(e => e.Creation <= momentUtc)
|
|
|
|
|
.Where(e => e.Obsolete == null || e.Obsolete >= momentUtc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IChangeLogRepositoryBuilder<TDto, TRequest> ApplyRequest(TRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
this.Query = Repository.BuildQuery(request, Query);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<TDto>> GetData(TimeSpan offset, CancellationToken token)
|
|
|
|
|
{
|
2024-06-05 12:08:38 +05:00
|
|
|
|
|
2024-06-04 16:55:44 +05:00
|
|
|
|
var dtos = await this.Query.Select(e => Repository.Convert(e, offset))
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogData(TimeSpan offset, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var dtos = await this.Query.Select(e => Repository.ConvertChangeLogDto(e, offset))
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IChangeLogRepositoryBuilder<TDto, TRequest> GetRequestBuilder(ChangeLogRequest request)
|
|
|
|
|
{
|
|
|
|
|
var builder = new ChangeLogRepositoryBuilder<TEntity, TDto, TRequest>(this, request);
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 12:25:58 +05:00
|
|
|
|
public async Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = 0;
|
|
|
|
|
if (dtos.Any())
|
|
|
|
|
{
|
|
|
|
|
var entities = dtos.Select(Convert);
|
|
|
|
|
var creation = DateTimeOffset.UtcNow;
|
2024-02-06 16:49:29 +05:00
|
|
|
|
var dbSet = db.Set<TEntity>();
|
2024-01-29 12:25:58 +05:00
|
|
|
|
foreach (var entity in entities)
|
|
|
|
|
{
|
|
|
|
|
entity.Id = default;
|
|
|
|
|
entity.IdAuthor = idUser;
|
|
|
|
|
entity.Creation = creation;
|
|
|
|
|
entity.IdState = ChangeLogAbstract.IdStateActual;
|
|
|
|
|
entity.IdEditor = null;
|
|
|
|
|
entity.IdPrevious = null;
|
|
|
|
|
entity.Obsolete = null;
|
|
|
|
|
dbSet.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result += await SaveChangesWithExceptionHandling(token);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> UpdateRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (dtos.Any(d => d.Id == 0))
|
|
|
|
|
throw new ArgumentInvalidException(nameof(dtos), "Отредактированные значения должны иметь id не 0");
|
|
|
|
|
|
|
|
|
|
if (!dtos.Any())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
var updateTime = DateTimeOffset.UtcNow;
|
|
|
|
|
|
|
|
|
|
var ids = dtos.Select(d => d.Id);
|
|
|
|
|
|
|
|
|
|
var result = 0;
|
2024-02-06 16:49:29 +05:00
|
|
|
|
var dbSet = db
|
2024-01-29 12:25:58 +05:00
|
|
|
|
.Set<TEntity>();
|
|
|
|
|
|
|
|
|
|
var entitiesToDelete = await dbSet
|
|
|
|
|
.Where(e => e.Obsolete == null)
|
|
|
|
|
.Where(e => ids.Contains(e.Id))
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var entitiesNotFound = dtos.Where(d => !entitiesToDelete.Any(e => e.Id == d.Id));
|
|
|
|
|
if (entitiesNotFound.Any())
|
|
|
|
|
{
|
|
|
|
|
var notFoundIds = entitiesNotFound.Select(e => e.Id);
|
|
|
|
|
var stringnotFoundIds = string.Join(", ", notFoundIds);
|
|
|
|
|
throw new ArgumentInvalidException(nameof(dtos), $"записи с id:[{stringnotFoundIds}] не найдены, или не актуальны.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 16:49:29 +05:00
|
|
|
|
using var transaction = db.Database.BeginTransaction();
|
2024-02-16 15:38:27 +05:00
|
|
|
|
try
|
2024-01-29 12:25:58 +05:00
|
|
|
|
{
|
2024-02-16 15:38:27 +05:00
|
|
|
|
foreach (var entity in entitiesToDelete)
|
|
|
|
|
{
|
|
|
|
|
entity.IdState = ChangeLogAbstract.IdStateReplaced;
|
|
|
|
|
entity.Obsolete = updateTime;
|
|
|
|
|
entity.IdEditor = idUser;
|
|
|
|
|
}
|
|
|
|
|
result += await db.SaveChangesAsync(token);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
2024-02-16 15:38:27 +05:00
|
|
|
|
var entitiesNew = dtos.Select(Convert);
|
|
|
|
|
foreach (var entity in entitiesNew)
|
|
|
|
|
{
|
|
|
|
|
entity.IdPrevious = entity.Id;
|
|
|
|
|
entity.Id = default;
|
|
|
|
|
entity.Creation = updateTime;
|
|
|
|
|
entity.IdAuthor = idUser;
|
|
|
|
|
entity.Obsolete = null;
|
|
|
|
|
entity.IdEditor = null;
|
|
|
|
|
entity.IdState = ChangeLogAbstract.IdStateActual;
|
|
|
|
|
dbSet.Add(entity);
|
|
|
|
|
}
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
2024-02-16 15:38:27 +05:00
|
|
|
|
result += await SaveChangesWithExceptionHandling(token);
|
2024-02-16 14:00:46 +05:00
|
|
|
|
|
|
|
|
|
await transaction.CommitAsync(token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
await transaction.RollbackAsync(token);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-01-29 12:25:58 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> UpdateOrInsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var itemsToInsert = dtos.Where(e => e.Id == 0);
|
|
|
|
|
var itemsToUpdate = dtos.Where(e => e.Id != 0);
|
|
|
|
|
|
|
|
|
|
var result = 0;
|
|
|
|
|
if (itemsToInsert.Any())
|
|
|
|
|
result += await InsertRange(idUser, itemsToInsert, token);
|
|
|
|
|
|
|
|
|
|
if (itemsToUpdate.Any())
|
|
|
|
|
result += await UpdateRange(idUser, itemsToUpdate, token);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> Clear(int idUser, TRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var updateTime = DateTimeOffset.UtcNow;
|
2024-06-04 16:55:44 +05:00
|
|
|
|
|
2024-01-29 12:25:58 +05:00
|
|
|
|
var query = BuildQuery(request);
|
|
|
|
|
query = query.Where(e => e.Obsolete == null);
|
|
|
|
|
|
|
|
|
|
var entitiesToDelete = await query.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
foreach (var entity in entitiesToDelete)
|
|
|
|
|
{
|
|
|
|
|
entity.IdState = ChangeLogAbstract.IdCleared;
|
|
|
|
|
entity.Obsolete = updateTime;
|
|
|
|
|
entity.IdEditor = idUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await SaveChangesWithExceptionHandling(token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> ClearAndInsertRange(int idUser, TRequest request, IEnumerable<TDto> dtos, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = 0;
|
2024-02-12 13:58:02 +05:00
|
|
|
|
using var transaction = await db.Database.BeginTransactionAsync(token);
|
2024-02-16 14:00:46 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-02-16 15:38:27 +05:00
|
|
|
|
result += await Clear(idUser, request, token);
|
|
|
|
|
result += await InsertRange(idUser, dtos, token);
|
|
|
|
|
|
2024-02-16 14:00:46 +05:00
|
|
|
|
await transaction.CommitAsync(token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
await transaction.RollbackAsync(token);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-01-29 12:25:58 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-31 13:37:48 +05:00
|
|
|
|
public async Task<int> MarkAsDeleted(int idUser, IEnumerable<int> ids, CancellationToken token)
|
2024-01-29 12:25:58 +05:00
|
|
|
|
{
|
|
|
|
|
var updateTime = DateTimeOffset.UtcNow;
|
2024-02-06 16:49:29 +05:00
|
|
|
|
var query = db.Set<TEntity>()
|
2024-01-29 12:25:58 +05:00
|
|
|
|
.Where(e => ids.Contains(e.Id))
|
|
|
|
|
.Where(e => e.Obsolete == null);
|
|
|
|
|
|
|
|
|
|
var entitiesToDelete = await query.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
foreach (var entity in entitiesToDelete)
|
|
|
|
|
{
|
|
|
|
|
entity.IdState = ChangeLogAbstract.IdStateDeleted;
|
|
|
|
|
entity.Obsolete = updateTime;
|
|
|
|
|
entity.IdEditor = idUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await SaveChangesWithExceptionHandling(token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<DateOnly>> GetDatesChange(TRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = BuildQuery(request);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
TimeSpan offset = GetTimezoneOffset(request);
|
|
|
|
|
|
|
|
|
|
var dates = Enumerable.Concat(datesCreate, datesUpdate);
|
|
|
|
|
dates = dates.Select(date => date.ToOffset(offset));
|
|
|
|
|
var datesOnly = dates
|
|
|
|
|
.Select(d => new DateOnly(d.Year, d.Month, d.Day))
|
2024-01-31 12:03:35 +05:00
|
|
|
|
.Distinct()
|
|
|
|
|
.OrderBy(d => d);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
|
|
|
|
return datesOnly;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-31 15:58:28 +05:00
|
|
|
|
public async Task<IEnumerable<ChangeLogDto<TDto>>> GetChangeLogForDate(TRequest request, DateOnly? date, CancellationToken token)
|
2024-01-29 12:25:58 +05:00
|
|
|
|
{
|
|
|
|
|
var query = BuildQuery(request);
|
|
|
|
|
TimeSpan offset = GetTimezoneOffset(request);
|
|
|
|
|
|
|
|
|
|
if (date.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var min = new DateTimeOffset(date.Value.Year, date.Value.Month, date.Value.Day, 0, 0, 0, offset).ToUniversalTime();
|
|
|
|
|
var max = min.AddDays(1);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-31 11:53:58 +05:00
|
|
|
|
var dtos = await CreateChangeLogDto(query, offset, token);
|
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 16:55:44 +05:00
|
|
|
|
public async Task<IEnumerable<ChangeLogDto<TDto>>> CreateChangeLogDto(IQueryable<TEntity> query, TimeSpan offset, CancellationToken token)
|
2024-05-31 11:53:58 +05:00
|
|
|
|
{
|
2024-01-31 12:03:35 +05:00
|
|
|
|
var entities = await query
|
|
|
|
|
.OrderBy(e => e.Creation)
|
|
|
|
|
.ThenBy(e => e.Obsolete)
|
|
|
|
|
.ThenBy(e => e.Id)
|
|
|
|
|
.ToListAsync(token);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
2024-05-31 11:53:58 +05:00
|
|
|
|
var dtos = entities.Select(e => ConvertChangeLogDto(e, offset));
|
2024-01-29 12:25:58 +05:00
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-31 11:53:58 +05:00
|
|
|
|
public async Task<IEnumerable<TDto>> GetCurrent(TRequest request, CancellationToken token)
|
|
|
|
|
{
|
2024-05-31 15:58:28 +05:00
|
|
|
|
var changeLogRequest = new ChangeLogRequest()
|
|
|
|
|
{
|
|
|
|
|
Moment = new DateTimeOffset(3000, 1, 1, 0, 0, 0, TimeSpan.Zero)
|
|
|
|
|
};
|
2024-06-04 16:55:44 +05:00
|
|
|
|
|
|
|
|
|
var builder = new ChangeLogRepositoryBuilder<TEntity, TDto, TRequest>(this, changeLogRequest);
|
|
|
|
|
builder.ApplyRequest(request, token);
|
|
|
|
|
|
|
|
|
|
var offset = GetTimezoneOffset(request);
|
|
|
|
|
var dtos = await builder.GetData(offset, token);
|
|
|
|
|
|
|
|
|
|
return dtos;
|
2024-05-31 11:53:58 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 12:25:58 +05:00
|
|
|
|
protected abstract TimeSpan GetTimezoneOffset(TRequest request);
|
|
|
|
|
|
2024-06-05 12:08:38 +05:00
|
|
|
|
private IQueryable<TEntity> BuildQuery(TRequest request) =>
|
|
|
|
|
BuildQuery(request, db.Set<TEntity>()
|
|
|
|
|
.Include(e => e.Author)
|
|
|
|
|
.Include(e => e.Editor));
|
|
|
|
|
|
|
|
|
|
protected abstract IQueryable<TEntity> BuildQuery(TRequest request, IQueryable<TEntity> query);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
|
|
|
|
protected virtual TEntity Convert(TDto dto)
|
|
|
|
|
{
|
|
|
|
|
var entity = dto.Adapt<TEntity>();
|
|
|
|
|
entity.Creation = entity.Creation.ToUniversalTime();
|
|
|
|
|
|
2024-05-31 11:53:58 +05:00
|
|
|
|
if (entity.Obsolete.HasValue)
|
2024-01-29 12:25:58 +05:00
|
|
|
|
entity.Obsolete = entity.Obsolete.Value.ToUniversalTime();
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 16:34:42 +05:00
|
|
|
|
protected virtual ChangeLogDto<TDto> ConvertChangeLogDto(TEntity entity, TimeSpan offset)
|
2024-01-29 12:25:58 +05:00
|
|
|
|
{
|
2024-05-28 16:34:42 +05:00
|
|
|
|
var changeLogDto = entity.Adapt<ChangeLogDto<TDto>>();
|
|
|
|
|
changeLogDto.Creation = entity.Creation.ToOffset(offset);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
|
|
|
|
if (entity.Obsolete.HasValue)
|
2024-05-28 16:34:42 +05:00
|
|
|
|
changeLogDto.Obsolete = entity.Obsolete.Value.ToOffset(offset);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
|
2024-05-28 16:34:42 +05:00
|
|
|
|
changeLogDto.Item = Convert(entity, offset);
|
|
|
|
|
return changeLogDto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual TDto Convert(TEntity entity, TimeSpan offset)
|
|
|
|
|
{
|
|
|
|
|
var dto = entity.Adapt<TDto>();
|
2024-01-29 12:25:58 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<int> SaveChangesWithExceptionHandling(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-02-06 16:49:29 +05:00
|
|
|
|
var result = await db.SaveChangesAsync(token);
|
2024-01-29 12:25:58 +05:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateException ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex.InnerException is PostgresException pgException)
|
|
|
|
|
TryConvertPostgresExceptionToValidateException(pgException);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void TryConvertPostgresExceptionToValidateException(PostgresException pgException)
|
|
|
|
|
{
|
|
|
|
|
if (pgException.SqlState == PostgresErrorCodes.ForeignKeyViolation)
|
|
|
|
|
throw new ArgumentInvalidException("dtos", pgException.Message + "\r\n" + pgException.Detail);
|
|
|
|
|
}
|
2024-06-04 16:55:44 +05:00
|
|
|
|
|
2024-01-29 12:25:58 +05:00
|
|
|
|
}
|