finally refact

This commit is contained in:
eugeniy_ivanov 2023-01-11 14:59:54 +05:00
parent 5c00e996f5
commit 0b3825a531
9 changed files with 65 additions and 69 deletions

View File

@ -19,7 +19,7 @@ namespace AsbCloudApp.Services
/// Получить имя файла (исходя из названия скважины)
/// </summary>
/// <returns></returns>
string GetFileName(int idWell, CancellationToken token);
string GetFileNameAsync(int idWell, CancellationToken token);
/// <summary>
/// загрузить текущую плановую траекторию в .xlsx
@ -35,8 +35,8 @@ namespace AsbCloudApp.Services
/// <param name="idUser"></param>
/// <param name="stream"></param>
/// <param name="token"></param>
/// <param name="deletePlannedTrajectoryBeforeImport">Очистить старые координаты перед импортом (если файл проходит валидацию)</param>
Task<int> ImportAsync(int idWell, int idUser, Stream stream, CancellationToken token, bool deletePlannedTrajectoryBeforeImport = false);
/// <param name="deleteBeforeImport">Очистить старые координаты перед импортом (если файл проходит валидацию)</param>
Task<int> ImportAsync(int idWell, int idUser, Stream stream, bool deleteBeforeImport, CancellationToken token);
}
#nullable disable
}

View File

@ -38,12 +38,11 @@ namespace AsbCloudApp.Services
/// <summary>
/// Обновить строку с координатами
/// </summary>
/// <param name="idRow"></param>
/// </summary>
/// <param name="row"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> UpdateAsync(int idRow, PlannedTrajectoryDto row,
Task<int> UpdateAsync(PlannedTrajectoryDto row,
CancellationToken token);
/// <summary>

View File

@ -13,7 +13,7 @@ namespace AsbCloudDb.Model
public virtual DbSet<DailyReport.DailyReport> DailyReports => Set <DailyReport.DailyReport >();
public virtual DbSet<Deposit> Deposits => Set<Deposit>();
public virtual DbSet<DetectedOperation> DetectedOperations => Set<DetectedOperation>();
public virtual DbSet<PlannedTrajectory> PlannedTrajectorys => Set<PlannedTrajectory>();
public virtual DbSet<PlannedTrajectory> PlannedTrajectories => Set<PlannedTrajectory>();
public virtual DbSet<ProcessMap> ProcessMap => Set<ProcessMap>();
public virtual DbSet<DrillingProgramPart> DrillingProgramParts => Set<DrillingProgramPart>();
public virtual DbSet<FileCategory> FileCategories => Set<FileCategory>();

View File

@ -15,7 +15,7 @@ namespace AsbCloudDb.Model
DbSet<DailyReport.DailyReport> DailyReports { get; }
DbSet<Deposit> Deposits { get; }
DbSet<DetectedOperation> DetectedOperations { get; }
DbSet<PlannedTrajectory> PlannedTrajectorys { get; }
DbSet<PlannedTrajectory> PlannedTrajectories { get; }
DbSet<ProcessMap> ProcessMap { get; }
DbSet<DrillingProgramPart> DrillingProgramParts { get; }
DbSet<FileCategory> FileCategories { get; }

View File

@ -12,7 +12,7 @@
<None Remove="CommonLibs\logo_720x404.png" />
<None Remove="CommonLibs\Readme.md" />
<None Remove="Services\DailyReport\DailyReportTemplate.xlsx" />
<None Remove="Services\PlannedTrajectoryService\PlannedTrajectoryTemplate.xlsx" />
<None Remove="Services\PlannedTrajectory\PlannedTrajectoryTemplate.xlsx" />
<None Remove="Services\WellOperationService\ScheduleReportTemplate.xlsx" />
<None Remove="Services\WellOperationService\WellOperationImportTemplate.xlsx" />
<None Remove="Services\DailyReport\DailyReportBlocks\" />
@ -29,7 +29,7 @@
<ItemGroup>
<EmbeddedResource Include="Services\DailyReport\DailyReportTemplate.xlsx" />
<EmbeddedResource Include="Services\PlannedTrajectoryService\PlannedTrajectoryTemplate.xlsx" />
<EmbeddedResource Include="Services\PlannedTrajectory\PlannedTrajectoryTemplate.xlsx" />
<EmbeddedResource Include="Services\WellOperationService\ScheduleReportTemplate.xlsx" />
<EmbeddedResource Include="Services\WellOperationService\WellOperationImportTemplate.xlsx" />
</ItemGroup>

View File

@ -56,7 +56,7 @@ namespace AsbCloudInfrastructure.Services.PlannedTrajectory
return stream;
}
public string GetFileName (int idWell, CancellationToken token)
public string GetFileNameAsync (int idWell, CancellationToken token)
{
var fileName = wellService.GetWellCaptionByIdAsync(idWell, token) + "_plannedTrajectory.xlsx";
return fileName;
@ -118,18 +118,18 @@ namespace AsbCloudInfrastructure.Services.PlannedTrajectory
row.Cell(ColumnComment).Value = trajectory.Comment;
}
public async Task<int> ImportAsync(int idWell, int idUser, Stream stream, CancellationToken token, bool deletePrevRows = false)
public async Task<int> ImportAsync(int idWell, int idUser, Stream stream, bool deletePrevRows, CancellationToken token)
{
using var workbook = new XLWorkbook(stream, XLEventTracking.Disabled);
var trajectoryRows = ParseFileStream(stream);
foreach (var row in trajectoryRows)
row.IdWell = idWell;
var rowsCount = await SavePlannedTrajectoryAsync(idWell,trajectoryRows, token, deletePrevRows);
var rowsCount = await SavePlannedTrajectoryAsync(idWell,trajectoryRows, deletePrevRows, token);
return rowsCount;
}
private async Task<int> SavePlannedTrajectoryAsync(int idWell, IEnumerable<PlannedTrajectoryDto> newRows, CancellationToken token, bool deletePrevRow = false)
private async Task<int> SavePlannedTrajectoryAsync(int idWell, IEnumerable<PlannedTrajectoryDto> newRows, bool deletePrevRow, CancellationToken token)
{
if (deletePrevRow)
{

View File

@ -22,96 +22,93 @@ namespace AsbCloudInfrastructure.Services.PlannedTrajectory
this.db = db;
this.wellService = wellService;
}
/// <inheritdoc/>
public async Task<int> AddRangeAsync(IEnumerable<PlannedTrajectoryDto> plannedTrajectoryRows, CancellationToken token)
{
{
var idWell = plannedTrajectoryRows.First().IdWell;
var offsetHours = wellService.GetTimezone(idWell).Hours;
var entitys = plannedTrajectoryRows
.Select(e => Convert(e));
db.PlannedTrajectorys.AddRange(entitys);
.Select(e => Convert(e, offsetHours));
foreach(var item in entitys)
{
item.Id = 0;
}
db.PlannedTrajectories.AddRange(entitys);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<int> AddAsync(PlannedTrajectoryDto plannedTrajectoryRow, CancellationToken token)
{
var entity = Convert(plannedTrajectoryRow);
db.PlannedTrajectorys.Add(entity);
{
var offsetHours = wellService.GetTimezone(plannedTrajectoryRow.IdWell).Hours;
var entity = Convert(plannedTrajectoryRow, offsetHours);
entity.Id = 0;
db.PlannedTrajectories.Add(entity);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token)
{
var query = db.PlannedTrajectorys
var query = db.PlannedTrajectories
.Where(e => ids.Contains(e.Id));
db.PlannedTrajectorys.RemoveRange(query);
db.PlannedTrajectories.RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<int> DeleteByIdWellAsync(int idWell, CancellationToken token)
{
var query = db.PlannedTrajectorys
var query = db.PlannedTrajectories
.Where(e => e.IdWell == idWell);
db.PlannedTrajectorys.RemoveRange(query);
db.PlannedTrajectories.RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<IEnumerable<PlannedTrajectoryDto>> GetAsync(int idWell, CancellationToken token)
{
var well = wellService.GetOrDefault(idWell);
var timezone = wellService.GetTimezone(idWell);
var offsetHours = wellService.GetTimezone(idWell).Hours;
if (well is null || well.Timezone is null)
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
var query = db.PlannedTrajectorys
var query = db.PlannedTrajectories
.AsNoTracking()
.Where(x => x.IdWell == idWell);
var entities = await query
.OrderBy(e => e.UpdateDate)
.ToListAsync(token);
var result = entities
.Select(r => Convert(r));
.Select(r => Convert(r, offsetHours));
return result;
}
public async Task<int> UpdateAsync(int idRow, PlannedTrajectoryDto row, CancellationToken token)
/// <inheritdoc/>
public async Task<int> UpdateAsync(PlannedTrajectoryDto row, CancellationToken token)
{
var entity = Convert(row, idRow);
db.PlannedTrajectorys.Update(entity);
var entity = Convert(row, row.Id);
db.PlannedTrajectories.Update(entity);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
private PlannedTrajectoryDto Convert(AsbCloudDb.Model.PlannedTrajectory entity)
{
var timezone = wellService.GetTimezone(entity.IdWell);
private PlannedTrajectoryDto Convert(AsbCloudDb.Model.PlannedTrajectory entity, double offsetHours)
{
var dto = entity.Adapt<PlannedTrajectoryDto>();
dto.UpdateDate = entity.UpdateDate.ToRemoteDateTime(timezone.Hours);
dto.UpdateDate = entity.UpdateDate.ToRemoteDateTime(offsetHours);
return dto;
}
private AsbCloudDb.Model.PlannedTrajectory Convert(PlannedTrajectoryDto dto)
{
var timezone = wellService.GetTimezone(dto.IdWell);
private AsbCloudDb.Model.PlannedTrajectory Convert(PlannedTrajectoryDto dto, double offsetHours)
{
var entity = dto.Adapt<AsbCloudDb.Model.PlannedTrajectory>();
entity.IdWell = dto.IdWell;
entity.Id = 0;
entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(timezone.Hours);
entity.IdUser = dto.IdUser;
entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(offsetHours);
return entity;
}
private AsbCloudDb.Model.PlannedTrajectory Convert(PlannedTrajectoryDto dto, int idRow)
{
var timezone = wellService.GetTimezone(dto.IdWell);
var entity = dto.Adapt<AsbCloudDb.Model.PlannedTrajectory>();
entity.IdWell = dto.IdWell;
entity.Id = idRow;
entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(timezone.Hours);
entity.IdUser = dto.IdUser;
return entity;
}
}
}
#nullable disable
}

View File

@ -61,7 +61,7 @@ namespace AsbCloudWebApi.Controllers
token).ConfigureAwait(false))
return Forbid();
var stream = await plannedTrajectoryImportService.ExportAsync(idWell, token);
var fileName = plannedTrajectoryImportService.GetFileName(idWell, token);
var fileName = plannedTrajectoryImportService.GetFileNameAsync(idWell, token);
return File(stream, "application/octet-stream", fileName);
}
@ -76,6 +76,7 @@ namespace AsbCloudWebApi.Controllers
[HttpPost]
[Permission]
[Route("import/{options}")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> ImportAsync(int idWell,
[FromForm] IFormFileCollection files,
int options = 0,
@ -96,7 +97,7 @@ namespace AsbCloudWebApi.Controllers
try
{
var result = plannedTrajectoryImportService.ImportAsync(idWell, idUser.Value, stream, token, (options & 1) > 0);
var result = plannedTrajectoryImportService.ImportAsync(idWell, idUser.Value, stream, (options & 1) > 0, token);
return Ok(result);
}
catch (FileFormatException ex)
@ -110,12 +111,12 @@ namespace AsbCloudWebApi.Controllers
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Запрашиваемый файл</returns>
/// <returns>Список добавленных координат плановой траектории</returns>
[HttpGet]
[Route("allRows")]
[Route("getRows")]
[Permission]
[ProducesResponseType(typeof(IEnumerable<PlannedTrajectoryDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetRows([FromRoute] int idWell, CancellationToken token = default)
public async Task<IActionResult> GetAsync([FromRoute] int idWell, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell,
token).ConfigureAwait(false))
@ -134,13 +135,13 @@ namespace AsbCloudWebApi.Controllers
[HttpPost]
[Route("addRow")]
[Permission]
[ProducesResponseType(typeof(PlannedTrajectoryDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddRowAsync(int idWell, [FromBody] PlannedTrajectoryDto row,
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddAsync(int idWell, [FromBody] PlannedTrajectoryDto row,
CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
int? idUser = User.GetUserId();
var idUser = User.GetUserId();
if (!idUser.HasValue)
return Forbid();
row.IdUser = idUser.Value;
@ -160,8 +161,8 @@ namespace AsbCloudWebApi.Controllers
[HttpPost]
[Route("addRangeRows")]
[Permission]
[ProducesResponseType(typeof(IEnumerable<PlannedTrajectoryDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddRangeRowsAsync(int idWell, [FromBody] IEnumerable<PlannedTrajectoryDto> rows,
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddRangeAsync(int idWell, [FromBody] IEnumerable<PlannedTrajectoryDto> rows,
CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
@ -183,14 +184,13 @@ namespace AsbCloudWebApi.Controllers
/// Изменить выбранную строку с координатами
/// </summary>
/// <param name="idWell"></param>
/// <param name="idRow"></param>
/// <param name="row"></param>
/// <param name="token"></param>
/// <returns>количество успешно обновленных строк в БД</returns>
[HttpPut("{idRow}")]
[Permission]
[ProducesResponseType(typeof(PlannedTrajectoryDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateRowAsync(int idWell, int idRow,
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateAsync(int idWell,
[FromBody] PlannedTrajectoryDto row, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
@ -200,7 +200,7 @@ namespace AsbCloudWebApi.Controllers
return Forbid();
row.IdUser = idUser.Value;
row.IdWell = idWell;
var result = await plannedTrajectoryService.UpdateAsync(idRow, row, token)
var result = await plannedTrajectoryService.UpdateAsync(row, token)
.ConfigureAwait(false);
return Ok(result);
}
@ -215,7 +215,7 @@ namespace AsbCloudWebApi.Controllers
[HttpDelete("{idRow}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> DeleteRowAsync(int idWell, int idRow, CancellationToken token = default)
public async Task<IActionResult> DeleteAsync(int idWell, int idRow, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell,
token).ConfigureAwait(false))