forked from ddrilling/AsbCloudServer
Доработки суточного рапорта
Сделана оптимизация получения фиктивного рапорта. Получение диапозона дат операций по скважине вынес в репозиторий. Избавился от получения всего списка фактический операций.
This commit is contained in:
parent
9a7649d765
commit
24232d4f36
@ -105,5 +105,14 @@ namespace AsbCloudApp.Repositories
|
|||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token);
|
Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token);
|
||||||
}
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получить диапазон дат выполнения операций
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="idWell"></param>
|
||||||
|
/// <param name="idType"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken);
|
||||||
|
}
|
||||||
}
|
}
|
@ -184,6 +184,20 @@ public class WellOperationRepository : IWellOperationRepository
|
|||||||
return sections;
|
return sections;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var query = db.WellOperations.Where(o => o.IdWell == idWell && o.IdType == idType);
|
||||||
|
|
||||||
|
if (!await query.AnyAsync(cancellationToken))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new DatesRangeDto
|
||||||
|
{
|
||||||
|
From = (await query.MinAsync(o => o.DateStart, cancellationToken)).Date,
|
||||||
|
To = (await query.MaxAsync(o => o.DateStart, cancellationToken)).Date
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public DateTimeOffset? FirstOperationDate(int idWell)
|
public DateTimeOffset? FirstOperationDate(int idWell)
|
||||||
{
|
{
|
||||||
|
@ -19,7 +19,6 @@ using AsbCloudApp.Services.ProcessMaps.WellDrilling;
|
|||||||
using AsbCloudApp.Services.Subsystems;
|
using AsbCloudApp.Services.Subsystems;
|
||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
using AsbCloudApp.Data.Trajectory;
|
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services.DailyReport;
|
namespace AsbCloudInfrastructure.Services.DailyReport;
|
||||||
|
|
||||||
@ -108,7 +107,15 @@ public class DailyReportService : IDailyReportService
|
|||||||
IdWell = well.Id
|
IdWell = well.Id
|
||||||
};
|
};
|
||||||
|
|
||||||
var factWellOperations = (await GetFactWellOperationsAsync(idWell, dailyReport.Date, cancellationToken))
|
var factOperationRequest = new WellOperationRequest
|
||||||
|
{
|
||||||
|
IdWell = idWell,
|
||||||
|
OperationType = WellOperation.IdOperationTypeFact,
|
||||||
|
GeDate = dateDailyReport,
|
||||||
|
LtDate = dateDailyReport.AddHours(24)
|
||||||
|
};
|
||||||
|
|
||||||
|
var factWellOperations = (await wellOperationRepository.GetAsync(factOperationRequest, cancellationToken))
|
||||||
.OrderBy(o => o.DateStart)
|
.OrderBy(o => o.DateStart)
|
||||||
.ThenBy(o => o.DepthStart);
|
.ThenBy(o => o.DepthStart);
|
||||||
|
|
||||||
@ -149,10 +156,7 @@ public class DailyReportService : IDailyReportService
|
|||||||
return result;
|
return result;
|
||||||
|
|
||||||
var dailyReports = new List<DailyReportDto>();
|
var dailyReports = new List<DailyReportDto>();
|
||||||
|
|
||||||
var existingDailyReports = await dailyReportRepository.GetAsync(idWell, request, cancellationToken);
|
|
||||||
var factWellOperations = await GetFactWellOperationsAsync(idWell, null, cancellationToken);
|
|
||||||
|
|
||||||
if (request.GeDate.HasValue)
|
if (request.GeDate.HasValue)
|
||||||
{
|
{
|
||||||
var startDate = new DateTime(request.GeDate.Value.Year, request.GeDate.Value.Month,
|
var startDate = new DateTime(request.GeDate.Value.Year, request.GeDate.Value.Month,
|
||||||
@ -173,8 +177,22 @@ public class DailyReportService : IDailyReportService
|
|||||||
|
|
||||||
if (datesRange.From.AddDays(result.Skip) <= datesRange.To)
|
if (datesRange.From.AddDays(result.Skip) <= datesRange.To)
|
||||||
result.Count = (int)(Math.Ceiling((datesRange.To - DateTime.UnixEpoch).TotalDays) -
|
result.Count = (int)(Math.Ceiling((datesRange.To - DateTime.UnixEpoch).TotalDays) -
|
||||||
Math.Floor((datesRange.From - DateTime.UnixEpoch).TotalDays));
|
Math.Floor((datesRange.From - DateTime.UnixEpoch).TotalDays)) + 1;
|
||||||
|
|
||||||
|
var existingDailyReports = await dailyReportRepository.GetAsync(idWell, request, cancellationToken);
|
||||||
|
|
||||||
|
var geDateFactWellOperation = datesRange.From.AddDays(result.Skip);
|
||||||
|
var ltDateFactWellOperation = geDateFactWellOperation.AddDays(result.Take);
|
||||||
|
|
||||||
|
var factWellOperationRequest = new WellOperationRequest
|
||||||
|
{
|
||||||
|
IdWell = idWell,
|
||||||
|
OperationType = WellOperation.IdOperationTypeFact,
|
||||||
|
GeDate = geDateFactWellOperation,
|
||||||
|
LtDate = ltDateFactWellOperation
|
||||||
|
};
|
||||||
|
|
||||||
|
var factWellOperations = await wellOperationRepository.GetAsync(factWellOperationRequest, cancellationToken);
|
||||||
|
|
||||||
if (request.SortFields?.Contains("DateStart desc") == true)
|
if (request.SortFields?.Contains("DateStart desc") == true)
|
||||||
{
|
{
|
||||||
@ -208,7 +226,9 @@ public class DailyReportService : IDailyReportService
|
|||||||
IdWell = idWell
|
IdWell = idWell
|
||||||
};
|
};
|
||||||
|
|
||||||
AddFactWellOperationBlock(dailyReport, factWellOperations.Where(o => o.DateStart >= date && o.DateStart <= date.AddDays(1)));
|
var factWellOperationPerDay = factWellOperations.Where(o => o.DateStart.Date >= date && o.DateStart.Date <= date.AddDays(1));
|
||||||
|
|
||||||
|
AddFactWellOperationBlock(dailyReport, factWellOperationPerDay);
|
||||||
|
|
||||||
dailyReports.Add(dailyReport);
|
dailyReports.Add(dailyReport);
|
||||||
}
|
}
|
||||||
@ -216,18 +236,16 @@ public class DailyReportService : IDailyReportService
|
|||||||
|
|
||||||
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, CancellationToken cancellationToken)
|
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var factOperations = await GetFactWellOperationsAsync(idWell, null, cancellationToken);
|
var factOperationDatesRange = await wellOperationRepository.GetDatesRangeAsync(idWell, WellOperation.IdOperationTypeFact,
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
if (!factOperations.Any())
|
if (factOperationDatesRange is null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var minDateStart = factOperations.Min(o => o.DateStart).Date;
|
|
||||||
var maxDateStart = factOperations.Max(o => o.DateStart).Date;
|
|
||||||
|
|
||||||
return new DatesRangeDto
|
return new DatesRangeDto
|
||||||
{
|
{
|
||||||
From = minDateStart.AddDays(1) <= DateTime.UtcNow ? minDateStart : DateTime.UtcNow.Date.AddDays(-1),
|
From = factOperationDatesRange.From.AddDays(1) <= DateTime.UtcNow ? factOperationDatesRange.From : DateTime.UtcNow.Date.AddDays(-1),
|
||||||
To = maxDateStart.AddDays(1) <= DateTime.UtcNow ? maxDateStart : DateTime.UtcNow.Date.AddDays(-1)
|
To = factOperationDatesRange.To.AddDays(1) <= DateTime.UtcNow ? factOperationDatesRange.To : DateTime.UtcNow.Date.AddDays(-1)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,15 +376,6 @@ public class DailyReportService : IDailyReportService
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<IEnumerable<WellOperationDto>> GetFactWellOperationsAsync(int idWell, DateTime? dateDailyReport, CancellationToken cancellationToken) =>
|
|
||||||
wellOperationRepository.GetAsync(new WellOperationRequest
|
|
||||||
{
|
|
||||||
IdWell = idWell,
|
|
||||||
OperationType = WellOperation.IdOperationTypeFact,
|
|
||||||
GeDate = dateDailyReport,
|
|
||||||
LtDate = dateDailyReport?.AddHours(24)
|
|
||||||
}, cancellationToken);
|
|
||||||
|
|
||||||
private async Task<bool> IsDateDailyReportInRangeAsync(int idWell, DateTime dateDailyReport, CancellationToken cancellationToken)
|
private async Task<bool> IsDateDailyReportInRangeAsync(int idWell, DateTime dateDailyReport, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var datesRange = await GetDatesRangeAsync(idWell, cancellationToken);
|
var datesRange = await GetDatesRangeAsync(idWell, cancellationToken);
|
||||||
|
@ -212,6 +212,7 @@ public class DailyReportServiceTest
|
|||||||
|
|
||||||
private readonly DailyReportDto fakeDailyReport;
|
private readonly DailyReportDto fakeDailyReport;
|
||||||
private readonly WellDto fakeWell;
|
private readonly WellDto fakeWell;
|
||||||
|
private readonly DatesRangeDto fakeDatesRange;
|
||||||
|
|
||||||
public DailyReportServiceTest()
|
public DailyReportServiceTest()
|
||||||
{
|
{
|
||||||
@ -233,6 +234,12 @@ public class DailyReportServiceTest
|
|||||||
Companies = new[] { fakeCustomer, fakeContractor }
|
Companies = new[] { fakeCustomer, fakeContractor }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fakeDatesRange = new DatesRangeDto
|
||||||
|
{
|
||||||
|
From = fakeFirstFactWellOperation.DateStart,
|
||||||
|
To = fakeLastFactWellOperation.DateStart
|
||||||
|
};
|
||||||
|
|
||||||
dailyReportService = new DailyReportService(wellServiceMock,
|
dailyReportService = new DailyReportService(wellServiceMock,
|
||||||
trajectoryFactNnbRepositoryMock,
|
trajectoryFactNnbRepositoryMock,
|
||||||
dailyReportRepositoryMock,
|
dailyReportRepositoryMock,
|
||||||
@ -260,6 +267,9 @@ public class DailyReportServiceTest
|
|||||||
wellOperationRepositoryMock.GetAsync(Arg.Any<WellOperationRequest>(), Arg.Any<CancellationToken>())
|
wellOperationRepositoryMock.GetAsync(Arg.Any<WellOperationRequest>(), Arg.Any<CancellationToken>())
|
||||||
.ReturnsForAnyArgs(new[] { fakeFirstFactWellOperation, fakeLastFactWellOperation });
|
.ReturnsForAnyArgs(new[] { fakeFirstFactWellOperation, fakeLastFactWellOperation });
|
||||||
|
|
||||||
|
wellOperationRepositoryMock.GetDatesRangeAsync(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
|
||||||
|
.ReturnsForAnyArgs(fakeDatesRange);
|
||||||
|
|
||||||
wellOperationRepositoryMock.GetSectionTypes()
|
wellOperationRepositoryMock.GetSectionTypes()
|
||||||
.ReturnsForAnyArgs(new[] { fakeSectionType });
|
.ReturnsForAnyArgs(new[] { fakeSectionType });
|
||||||
|
|
||||||
@ -490,20 +500,23 @@ public class DailyReportServiceTest
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task GetAsync_ShouldReturn_FictiveDailyReport()
|
public async Task GetAsync_ShouldReturn_FictiveDailyReport()
|
||||||
{
|
{
|
||||||
|
//arrange
|
||||||
|
var expectedCount = (fakeLastFactWellOperation.DateStart - fakeFirstFactWellOperation.DateStart).TotalDays + 1;
|
||||||
|
|
||||||
//act
|
//act
|
||||||
var result = await dailyReportService.GetAsync(idWell, new FileReportRequest(), CancellationToken.None);
|
var result = await dailyReportService.GetAsync(idWell, new FileReportRequest(), CancellationToken.None);
|
||||||
|
|
||||||
//assert
|
//assert
|
||||||
Assert.True((fakeLastFactWellOperation.DateStart - fakeFirstFactWellOperation.DateStart).Days == result.Count);
|
Assert.Equal(expectedCount, result.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[MemberData(nameof(FactWellOperationDates))]
|
[MemberData(nameof(FactWellOperationDatesRange))]
|
||||||
public async Task GetDatesRangeAsync_ShouldReturn_DateRangeByFactWellOperations(IEnumerable<DateTime> factWellOperationDates)
|
public async Task GetDatesRangeAsync_ShouldReturn_DateRangeByFactWellOperations(DatesRangeDto datesRange)
|
||||||
{
|
{
|
||||||
//arrange
|
//arrange
|
||||||
wellOperationRepositoryMock.GetAsync(Arg.Any<WellOperationRequest>(), CancellationToken.None)
|
wellOperationRepositoryMock.GetDatesRangeAsync(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
|
||||||
.ReturnsForAnyArgs(factWellOperationDates.Select(dateStart => new WellOperationDto { DateStart = dateStart }));
|
.Returns(datesRange);
|
||||||
|
|
||||||
//act
|
//act
|
||||||
var result = await dailyReportService.GetDatesRangeAsync(idWell, CancellationToken.None);
|
var result = await dailyReportService.GetDatesRangeAsync(idWell, CancellationToken.None);
|
||||||
@ -514,43 +527,41 @@ public class DailyReportServiceTest
|
|||||||
Assert.True(result.To < DateTime.UtcNow.Date);
|
Assert.True(result.To < DateTime.UtcNow.Date);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<object[]> FactWellOperationDates()
|
public static IEnumerable<object[]> FactWellOperationDatesRange()
|
||||||
{
|
{
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
new[]
|
new DatesRangeDto
|
||||||
{
|
{
|
||||||
new DateTime(2023, 11, 1),
|
From = new DateTime(2023, 11, 1),
|
||||||
new DateTime(2023, 11, 9),
|
To = new DateTime(2023, 11, 9)
|
||||||
DateTime.UtcNow
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
new[]
|
new DatesRangeDto
|
||||||
{
|
{
|
||||||
new DateTime(2023, 11, 1),
|
From = new DateTime(2023, 11, 1),
|
||||||
new DateTime(2023, 11, 1)
|
To = new DateTime(2023, 11, 1)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
new[]
|
new DatesRangeDto
|
||||||
{
|
{
|
||||||
DateTime.UtcNow,
|
From = DateTime.UtcNow,
|
||||||
DateTime.UtcNow
|
To = DateTime.UtcNow
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
yield return new object[]
|
yield return new object[]
|
||||||
{
|
{
|
||||||
new[]
|
new DatesRangeDto
|
||||||
{
|
{
|
||||||
new DateTime(2023, 11, 1),
|
From = new DateTime(2023, 11, 1),
|
||||||
new DateTime(2023, 11, 9),
|
To = new DateTime(2023, 11, 11)
|
||||||
new DateTime(2023, 11, 11)
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user