forked from ddrilling/AsbCloudServer
WellOperationController remove validation by dates gap between operations
This commit is contained in:
parent
f7caa7eb38
commit
97e01be7db
@ -118,21 +118,6 @@ namespace AsbCloudApp.Repositories
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken);
|
Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Валидация данных
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="wellOperations"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
IEnumerable<ValidationResult> Validate(IEnumerable<WellOperationDto> wellOperations);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Валидация данных (проверка с базой)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="wellOperations"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task<IEnumerable<ValidationResult>> ValidateWithDbAsync(IEnumerable<WellOperationDto> wellOperations, CancellationToken cancellationToken);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удаление полных дубликатов операций по всем скважинам
|
/// Удаление полных дубликатов операций по всем скважинам
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -4,10 +4,6 @@ using AsbCloudApp.Requests;
|
|||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using AsbCloudDb;
|
using AsbCloudDb;
|
||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using DocumentFormat.OpenXml.Spreadsheet;
|
|
||||||
using DocumentFormat.OpenXml.Vml;
|
|
||||||
using DocumentFormat.OpenXml.Wordprocessing;
|
|
||||||
using Irony.Parsing;
|
|
||||||
using Mapster;
|
using Mapster;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
@ -21,7 +17,6 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace AsbCloudInfrastructure.Repository;
|
namespace AsbCloudInfrastructure.Repository;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// репозиторий операций по скважине
|
/// репозиторий операций по скважине
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -326,59 +321,6 @@ public class WellOperationRepository : IWellOperationRepository
|
|||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<ValidationResult>> ValidateWithDbAsync(IEnumerable<WellOperationDto> wellOperationDtos, CancellationToken token)
|
|
||||||
{
|
|
||||||
var firstOperation = wellOperationDtos
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
if (firstOperation is null)
|
|
||||||
return Enumerable.Empty<ValidationResult>();
|
|
||||||
|
|
||||||
var request = new WellOperationRequest()
|
|
||||||
{
|
|
||||||
IdWell = firstOperation.IdWell,
|
|
||||||
OperationType = firstOperation.IdType,
|
|
||||||
};
|
|
||||||
|
|
||||||
var entities = await BuildQuery(request)
|
|
||||||
.AsNoTracking()
|
|
||||||
.ToArrayAsync(token);
|
|
||||||
|
|
||||||
var wellOperationsUnion = entities.Union(wellOperationDtos).OrderBy(o => o.DateStart);
|
|
||||||
|
|
||||||
var results = Validate(wellOperationsUnion);
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<ValidationResult> Validate(IEnumerable<WellOperationDto> wellOperationDtos)
|
|
||||||
{
|
|
||||||
var enumerator = wellOperationDtos.OrderBy(o => o.DateStart)
|
|
||||||
.GetEnumerator();
|
|
||||||
|
|
||||||
if (!enumerator.MoveNext())
|
|
||||||
yield break;
|
|
||||||
|
|
||||||
var previous = enumerator.Current;
|
|
||||||
|
|
||||||
while(enumerator.MoveNext())
|
|
||||||
{
|
|
||||||
var current = enumerator.Current;
|
|
||||||
var previousDateStart = previous.DateStart.ToUniversalTime();
|
|
||||||
var currentDateStart = current.DateStart.ToUniversalTime();
|
|
||||||
|
|
||||||
var previousDateEnd = previous.DateStart.AddHours(previous.DurationHours).ToUniversalTime();
|
|
||||||
|
|
||||||
if (previousDateStart.AddDays(Gap) < currentDateStart)
|
|
||||||
{
|
|
||||||
yield return new ValidationResult(
|
|
||||||
"Разница дат между операциями не должна превышать 90 дней",
|
|
||||||
new[] { nameof(wellOperationDtos) });
|
|
||||||
}
|
|
||||||
|
|
||||||
previous = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public async Task<int> InsertRangeAsync(
|
public async Task<int> InsertRangeAsync(
|
||||||
IEnumerable<WellOperationDto> wellOperationDtos,
|
IEnumerable<WellOperationDto> wellOperationDtos,
|
||||||
|
@ -36,56 +36,6 @@ public class WellOperationControllerTest : BaseIntegrationTest
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private readonly WellOperationDto[] dtosWithError = new WellOperationDto[]
|
|
||||||
{
|
|
||||||
new()
|
|
||||||
{
|
|
||||||
Id = 3,
|
|
||||||
IdWell = idWell,
|
|
||||||
IdType = 1,
|
|
||||||
DateStart = DateTimeOffset.Now,
|
|
||||||
CategoryInfo = "1",
|
|
||||||
CategoryName = "1",
|
|
||||||
Comment = "1",
|
|
||||||
Day = 1,
|
|
||||||
DepthEnd = 20,
|
|
||||||
DepthStart = 10,
|
|
||||||
DurationHours = 1,
|
|
||||||
IdCategory = 5000,
|
|
||||||
IdParentCategory = null,
|
|
||||||
IdPlan = null,
|
|
||||||
IdUser = 1,
|
|
||||||
IdWellSectionType = 1,
|
|
||||||
LastUpdateDate = DateTimeOffset.Now,
|
|
||||||
NptHours = 1,
|
|
||||||
WellSectionTypeName = null,
|
|
||||||
UserName = null
|
|
||||||
},
|
|
||||||
new()
|
|
||||||
{
|
|
||||||
Id = 4,
|
|
||||||
IdWell = idWell,
|
|
||||||
IdType = 1,
|
|
||||||
DateStart = DateTimeOffset.Now.AddDays(1000),
|
|
||||||
CategoryInfo = "1",
|
|
||||||
CategoryName = "1",
|
|
||||||
Comment = "1",
|
|
||||||
Day = 1,
|
|
||||||
DepthEnd = 20,
|
|
||||||
DepthStart = 10,
|
|
||||||
DurationHours = 1,
|
|
||||||
IdCategory = 5000,
|
|
||||||
IdParentCategory = null,
|
|
||||||
IdPlan = null,
|
|
||||||
IdUser = 1,
|
|
||||||
IdWellSectionType = 1,
|
|
||||||
LastUpdateDate = DateTimeOffset.Now,
|
|
||||||
NptHours = 1,
|
|
||||||
WellSectionTypeName = null,
|
|
||||||
UserName = null
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private IWellOperationClient client;
|
private IWellOperationClient client;
|
||||||
|
|
||||||
public WellOperationControllerTest(WebAppFactoryFixture factory)
|
public WellOperationControllerTest(WebAppFactoryFixture factory)
|
||||||
@ -109,21 +59,6 @@ public class WellOperationControllerTest : BaseIntegrationTest
|
|||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Неуспешное добавление операций (без предварительной очистки данных)
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
[Fact]
|
|
||||||
public async Task InsertRange_returns_error()
|
|
||||||
{
|
|
||||||
//act
|
|
||||||
var response = await client.InsertRangeAsync(idWell, 1, false, dtosWithError);
|
|
||||||
|
|
||||||
//assert
|
|
||||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Успешное добавление операций (с предварительной очисткой данных)
|
/// Успешное добавление операций (с предварительной очисткой данных)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -138,20 +73,6 @@ public class WellOperationControllerTest : BaseIntegrationTest
|
|||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Неуспешное добавление операций (с предварительной очисткой данных)
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
[Fact]
|
|
||||||
public async Task InsertRangeWithDeleteBefore_returns_error()
|
|
||||||
{
|
|
||||||
//act
|
|
||||||
var response = await client.InsertRangeAsync(idWell, 1, true, dtosWithError);
|
|
||||||
|
|
||||||
//assert
|
|
||||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Успешное обновление операции
|
/// Успешное обновление операции
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -167,21 +88,6 @@ public class WellOperationControllerTest : BaseIntegrationTest
|
|||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Неуспешное обновление операции
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
[Fact]
|
|
||||||
public async Task UpdateAsync_returns_error()
|
|
||||||
{
|
|
||||||
//act
|
|
||||||
var dto = dtosWithError.LastOrDefault()!;
|
|
||||||
var response = await client.UpdateAsync(idWell, 1, dto, CancellationToken.None);
|
|
||||||
|
|
||||||
//assert
|
|
||||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получение плановых операций
|
/// Получение плановых операций
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -236,10 +236,6 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
wellOperation.IdUser = User.GetUserId();
|
wellOperation.IdUser = User.GetUserId();
|
||||||
wellOperation.IdType = idType;
|
wellOperation.IdType = idType;
|
||||||
|
|
||||||
var validationResult = await operationRepository.ValidateWithDbAsync(new[] { wellOperation }, cancellationToken);
|
|
||||||
if (validationResult.Any())
|
|
||||||
return this.ValidationBadRequest(validationResult);
|
|
||||||
|
|
||||||
var result = await operationRepository.InsertRangeAsync(new[] { wellOperation }, cancellationToken);
|
var result = await operationRepository.InsertRangeAsync(new[] { wellOperation }, cancellationToken);
|
||||||
|
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
@ -290,32 +286,11 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
wellOperation.IdType = idType;
|
wellOperation.IdType = idType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var validationResult = await Validate(wellOperations, deleteBeforeInsert, cancellationToken);
|
|
||||||
if (validationResult.Any())
|
|
||||||
return this.ValidationBadRequest(validationResult);
|
|
||||||
|
|
||||||
var result = await operationRepository.InsertRangeAsync(wellOperations, cancellationToken);
|
var result = await operationRepository.InsertRangeAsync(wellOperations, cancellationToken);
|
||||||
|
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Валидация данных перед вставкой / обновлением / импортом
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="wellOperations"></param>
|
|
||||||
/// <param name="deleteBeforeInsert"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private async Task<IEnumerable<ValidationResult>> Validate(IEnumerable<WellOperationDto> wellOperations, bool deleteBeforeInsert, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
if (deleteBeforeInsert)
|
|
||||||
return operationRepository.Validate(wellOperations);
|
|
||||||
else
|
|
||||||
return await operationRepository.ValidateWithDbAsync(wellOperations, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Обновляет выбранную операцию на скважине
|
/// Обновляет выбранную операцию на скважине
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -341,10 +316,6 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
value.LastUpdateDate = DateTimeOffset.UtcNow;
|
value.LastUpdateDate = DateTimeOffset.UtcNow;
|
||||||
value.IdUser = User.GetUserId();
|
value.IdUser = User.GetUserId();
|
||||||
|
|
||||||
var validationResult = await operationRepository.ValidateWithDbAsync(new[] { value }, token);
|
|
||||||
if (validationResult.Any())
|
|
||||||
return this.ValidationBadRequest(validationResult);
|
|
||||||
|
|
||||||
var result = await operationRepository.UpdateAsync(value, token)
|
var result = await operationRepository.UpdateAsync(value, token)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
|
Loading…
Reference in New Issue
Block a user