Редактирование автоопределенных операций

This commit is contained in:
Степанов Дмитрий 2024-04-01 13:32:48 +03:00
parent 69ec6cc765
commit 14a07fd5d5
10 changed files with 10092 additions and 515 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
namespace AsbCloudApp.Data.DetectedOperation;
@ -28,8 +29,7 @@ public class DetectedOperationDto: IId
/// <summary>
/// Id пользователя панели на момент начала операции
/// </summary>
[Required]
public int IdUserAtStart { get; set; }
public int? IdUserAtStart { get; set; }
/// <summary>
/// Пользователь панели оператора
@ -75,8 +75,8 @@ public class DetectedOperationDto: IId
/// <summary>
/// название/описание операции
/// </summary>
[Required]
public WellOperationCategoryDto OperationCategory { get; set; } = null!;
[JsonIgnore]
public WellOperationCategoryDto? OperationCategory { get; set; }
/// <summary>
/// Ключевой параметр операции

View File

@ -11,16 +11,16 @@ namespace AsbCloudApp.Repositories;
/// <summary>
/// Таблица автоматически определенных операций
/// </summary>
public interface IDetectedOperationRepository : ICrudRepository<DetectedOperationDto>
public interface IDetectedOperationRepository
{
/// <summary>
/// Добавление записей
/// </summary>
/// <param name="idUser"></param>
/// <param name="idEditor"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> Insert(int? idUser, IEnumerable<DetectedOperationDto> dtos, CancellationToken token);
Task<int> InsertRange(int? idEditor, IEnumerable<DetectedOperationDto> dtos, CancellationToken token);
/// <summary>
/// Получить автоматически определенные операции по телеметрии
@ -33,38 +33,27 @@ public interface IDetectedOperationRepository : ICrudRepository<DetectedOperatio
/// <summary>
/// Редактирование записей
/// </summary>
/// <param name="idUser"></param>
/// <param name="idEditor"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> Update(int idUser, IEnumerable<DetectedOperationDto> dtos, CancellationToken token);
/// <summary>
/// Добавляет Dto у которых id == 0, изменяет dto у которых id != 0
/// </summary>
/// <param name="idUser"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> UpdateOrInsert(int idUser, IEnumerable<DetectedOperationDto> dtos, CancellationToken token);
Task<int> UpdateRange(int idEditor, IEnumerable<DetectedOperationDto> dtos, CancellationToken token);
/// <summary>
/// Удалить операции
/// </summary>
/// <param name="idUser"></param>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> Delete(int idUser, DetectedOperationByTelemetryRequest request, CancellationToken token);
Task<int> Delete(DetectedOperationByTelemetryRequest request, CancellationToken token);
/// <summary>
/// Удаление записей
/// </summary>
/// <param name="idUser"></param>
/// <param name="ids"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> DeleteRange(int idUser, IEnumerable<int> ids, CancellationToken token);
Task<int> DeleteRange(IEnumerable<int> ids, CancellationToken token);
/// <summary>
/// Получение дат последних определённых операций

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,64 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AsbCloudDb.Migrations
{
/// <inheritdoc />
public partial class Update_DetectedOperation : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "id_user",
table: "t_detected_operation",
type: "integer",
nullable: true,
comment: "Id пользователя по телеметрии на момент начала операции",
oldClrType: typeof(int),
oldType: "integer",
oldComment: "Id пользователя по телеметрии на момент начала операции");
migrationBuilder.AddColumn<DateTimeOffset>(
name: "creation",
table: "t_detected_operation",
type: "timestamp with time zone",
nullable: false,
defaultValue: new DateTimeOffset(new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)),
comment: "дата создания");
migrationBuilder.AddColumn<int>(
name: "id_editor",
table: "t_detected_operation",
type: "integer",
nullable: true,
comment: "Редактор");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "creation",
table: "t_detected_operation");
migrationBuilder.DropColumn(
name: "id_editor",
table: "t_detected_operation");
migrationBuilder.AlterColumn<int>(
name: "id_user",
table: "t_detected_operation",
type: "integer",
nullable: false,
defaultValue: 0,
comment: "Id пользователя по телеметрии на момент начала операции",
oldClrType: typeof(int),
oldType: "integer",
oldNullable: true,
oldComment: "Id пользователя по телеметрии на момент начала операции");
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -20,6 +20,12 @@ namespace AsbCloudDb.Model
[Column("id_category"), Comment("Id категории операции")]
public int IdCategory { get; set; }
[Column("id_editor"), Comment("Редактор")]
public int? IdEditor { get; set; }
[Column("creation"), Comment("дата создания")]
public DateTimeOffset Creation { get; set; }
[Column("date_start", TypeName = "timestamp with time zone"), Comment("Дата начала операции")]
public DateTimeOffset DateStart { get; set; }
@ -27,7 +33,7 @@ namespace AsbCloudDb.Model
public DateTimeOffset DateEnd { get; set; }
[Column("id_user"), Comment("Id пользователя по телеметрии на момент начала операции")]
public int IdUsersAtStart { get; set; }
public int? IdUsersAtStart { get; set; }
[NotMapped]
public double DurationMinutes => (DateEnd - DateStart).TotalMinutes;

View File

@ -12,29 +12,30 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
namespace AsbCloudInfrastructure.Repository;
public class DetectedOperationRepository : CrudRepositoryBase<DetectedOperationDto, DetectedOperation>,
IDetectedOperationRepository
public class DetectedOperationRepository : IDetectedOperationRepository
{
private readonly IAsbCloudDbContext dbContext;
private readonly ITelemetryService telemetryService;
public DetectedOperationRepository(IAsbCloudDbContext context,
public DetectedOperationRepository(IAsbCloudDbContext dbContext,
ITelemetryService telemetryService)
: base(context)
{
this.dbContext = dbContext;
this.telemetryService = telemetryService;
}
public async Task<int> Delete(int idUser, DetectedOperationByTelemetryRequest request, CancellationToken token)
public async Task<int> Delete(DetectedOperationByTelemetryRequest request, CancellationToken token)
{
var query = BuildQuery(request);
dbContext.Set<DetectedOperation>().RemoveRange(query);
return await dbContext.SaveChangesAsync(token);
}
public async Task<int> DeleteRange(int idUser, IEnumerable<int> ids, CancellationToken token)
public async Task<int> DeleteRange(IEnumerable<int> ids, CancellationToken token)
{
var query = dbContext.Set<DetectedOperation>()
.Where(e => ids.Contains( e.Id));
@ -66,7 +67,7 @@ public class DetectedOperationRepository : CrudRepositoryBase<DetectedOperationD
return dtos;
}
public async Task<int> Insert(int? idUser, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
public async Task<int> InsertRange(int? idEditor, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
{
if(!dtos.Any())
return 0;
@ -75,6 +76,10 @@ public class DetectedOperationRepository : CrudRepositoryBase<DetectedOperationD
var dbset = dbContext.Set<DetectedOperation>();
foreach(var entity in entities)
{
if (idEditor.HasValue)
entity.IdEditor = idEditor.Value;
entity.Creation = DateTimeOffset.UtcNow;
entity.Id = default;
dbset.Add(entity);
}
@ -82,7 +87,7 @@ public class DetectedOperationRepository : CrudRepositoryBase<DetectedOperationD
return await dbContext.SaveChangesWithExceptionHandling(token);
}
public async Task<int> Update(int idUser, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
public async Task<int> UpdateRange(int idEditor, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
{
if (!dtos.Any())
return 0;
@ -108,32 +113,22 @@ public class DetectedOperationRepository : CrudRepositoryBase<DetectedOperationD
throw new ArgumentInvalidException(nameof(dtos), "Все записи должны существовать в БД");
var entities = dtos
.Select(Convert)
.Select(dto =>
{
var entity = Convert(dto);
entity.IdEditor = idEditor;
return entity;
})
.ToArray();
var entries = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<DetectedOperation>[entities.Length];
var entries = new EntityEntry<DetectedOperation>[entities.Length];
for(var i = 0; i < entities.Length; i++)
entries[i] = dbSet.Update(entities[i]);
var result = await dbContext.SaveChangesWithExceptionHandling(token);
for (var i = 0; i < entries.Length; i++)
entries[i].State = EntityState.Detached;
return result;
}
public async Task<int> UpdateOrInsert(int idUser, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
{
var result = 0;
var itemsToInsert = dtos.Where(e => e.Id == 0);
if (itemsToInsert.Any())
result += await Insert(idUser, itemsToInsert, token);
var itemsToUpdate = dtos.Where(e => e.Id != 0);
if (itemsToUpdate.Any())
result += await Update(idUser, itemsToUpdate, token);
foreach (var entry in entries)
entry.State = EntityState.Detached;
return result;
}
@ -182,7 +177,7 @@ public class DetectedOperationRepository : CrudRepositoryBase<DetectedOperationD
return query;
}
protected virtual DetectedOperationDto Convert(DetectedOperation src, TimeSpan offset)
private static DetectedOperationDto Convert(DetectedOperation src, TimeSpan offset)
{
var dto = src.Adapt<DetectedOperationDto>();
dto.DateStart = src.DateStart.ToOffset(offset);
@ -190,7 +185,7 @@ public class DetectedOperationRepository : CrudRepositoryBase<DetectedOperationD
return dto;
}
protected override DetectedOperation Convert(DetectedOperationDto src)
private static DetectedOperation Convert(DetectedOperationDto src)
{
var entity = src.Adapt<DetectedOperation>();
entity.DateStart = src.DateStart.ToUniversalTime();

View File

@ -209,7 +209,7 @@ public class DetectedOperationService : IDetectedOperationService
return 0;
var requestByTelemetry = new DetectedOperationByTelemetryRequest(well.IdTelemetry.Value, request);
var result = await operationRepository.Delete(-1, requestByTelemetry, token);
var result = await operationRepository.Delete(requestByTelemetry, token);
return result;
}

View File

@ -48,7 +48,7 @@ public class WorkOperationDetection: Work
var detectedOperations = await detectedOperationService.DetectOperationsAsync(telemetryId, beginDate, token);
if (detectedOperations.Any())
await detectedOperationRepository.InsertRangeAsync(detectedOperations, token);
await detectedOperationRepository.InsertRange(null, detectedOperations, token);
}
}
}

View File

@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudInfrastructure.Services.DetectOperations;
using Microsoft.AspNetCore.Http;
@ -15,21 +17,79 @@ namespace AsbCloudWebApi.Controllers.SAUB
/// <summary>
/// Операции определенные по телеметрии САУБ
/// </summary>
[Route("api/[controller]")]
[Route("api/well/{idWell}/[controller]")]
[ApiController]
[Authorize]
public class DetectedOperationController : ControllerBase
{
private readonly IDetectedOperationRepository detectedOperationRepository;
private readonly IDetectedOperationService detectedOperationService;
private readonly IWellService wellService;
private readonly DetectedOperationExportService detectedOperationExportService;
public DetectedOperationController(IDetectedOperationService detectedOperationService, IWellService wellService,
DetectedOperationExportService detectedOperationExportService)
public DetectedOperationController(IDetectedOperationService detectedOperationService,
IWellService wellService,
DetectedOperationExportService detectedOperationExportService,
IDetectedOperationRepository detectedOperationRepository)
{
this.detectedOperationService = detectedOperationService;
this.wellService = wellService;
this.detectedOperationExportService = detectedOperationExportService;
this.detectedOperationRepository = detectedOperationRepository;
}
/// <summary>
/// Добавить операции
/// </summary>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
public async Task<IActionResult> InsertRangeAsync(int idWell, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
{
var idUser = await AssertUserHasAccessToWellAsync(idWell, token);
var result = await detectedOperationRepository.InsertRange(idUser, dtos, token);
return Ok(result);
}
/// <summary>
/// Обновить операции
/// </summary>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPut]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
public async Task<IActionResult> UpdateRangeAsync(int idWell, IEnumerable<DetectedOperationDto> dtos, CancellationToken token)
{
var idUser = await AssertUserHasAccessToWellAsync(idWell, token);
var result = await detectedOperationRepository.UpdateRange(idUser, dtos, token);
return Ok(result);
}
/// <summary>
/// Удалить операции
/// </summary>
/// <param name="idWell"></param>
/// <param name="ids"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpDelete]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
public async Task<IActionResult> DeleteRangeAsync(int idWell, IEnumerable<int> ids, CancellationToken token)
{
await AssertUserHasAccessToWellAsync(idWell, token);
var result = await detectedOperationRepository.DeleteRange(ids, token);
return Ok(result);
}
/// <summary>
@ -39,8 +99,9 @@ namespace AsbCloudWebApi.Controllers.SAUB
/// <param name="idWell">[опционально] id скважины</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("categories")]
[ProducesResponseType(typeof(IEnumerable<WellOperationCategoryDto>), (int)System.Net.HttpStatusCode.OK)]
[HttpGet]
[Route("/api/well/[controller]/categories")]
[ProducesResponseType(typeof(IEnumerable<WellOperationCategoryDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetCategoriesAsync([FromQuery] int? idWell, CancellationToken token)
{
var result = await detectedOperationService.GetCategoriesAsync(idWell, token);
@ -50,73 +111,49 @@ namespace AsbCloudWebApi.Controllers.SAUB
/// <summary>
/// Получить фильтрованный список операций по телеметрии САУБ
/// </summary>
/// <param name="idWell"></param>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(typeof(DetectedOperationListDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(
[FromQuery] DetectedOperationByWellRequest request,
[ProducesResponseType(typeof(DetectedOperationDto), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAsync(int idWell,
[FromQuery] DetectedOperationRequest request,
CancellationToken token)
{
if (!await UserHasAccessToWellAsync(request.IdWell, token))
return Forbid();
await AssertUserHasAccessToWellAsync(idWell, token);
var result = await detectedOperationService.GetAsync(request, token);
var well = await wellService.GetOrDefaultAsync(idWell, token);
if (well?.IdTelemetry is null)
return NoContent();
var requestToService = new DetectedOperationByTelemetryRequest(well.IdTelemetry.Value, request);
var result = await detectedOperationRepository.Get(requestToService, token);
return Ok(result);
}
/// <summary>
/// Получить статистику по фильтрованному списку операций по телеметрии САУБ
/// </summary>
/// <param name="idWell"></param>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("stat")]
[ProducesResponseType(typeof(IEnumerable<DetectedOperationStatDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatAsync(
[FromQuery] DetectedOperationByWellRequest request,
[ProducesResponseType(typeof(IEnumerable<DetectedOperationStatDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetStatAsync(int idWell, [FromQuery] DetectedOperationRequest request,
CancellationToken token)
{
if (!await UserHasAccessToWellAsync(request.IdWell, token))
return Forbid();
await AssertUserHasAccessToWellAsync(idWell, token);
var result = await detectedOperationService.GetOperationsStatAsync(request, token);
var requestToService = new DetectedOperationByWellRequest(idWell, request);
var result = await detectedOperationService.GetOperationsStatAsync(requestToService, token);
return Ok(result);
}
/// <summary>
/// Удалить операции.
/// Удаленные операции будут определены повторно сервисом автоматизированного определения операций.
/// Может потребоваться при изменении алгоритмов определения
/// </summary>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpDelete]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> DeleteAsync(
[FromQuery] DetectedOperationByWellRequest request,
CancellationToken token)
{
if (!await UserHasAccessToWellAsync(request.IdWell, token))
return Forbid();
var result = await detectedOperationService.DeleteAsync(request, token);
return Ok(result);
}
protected async Task<bool> UserHasAccessToWellAsync(int idWell, CancellationToken token)
{
var idCompany = User.GetCompanyId();
if (idCompany is not null &&
await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token)
.ConfigureAwait(false))
return true;
return false;
}
/// <summary>
/// Создает excel файл с операциями по скважине
/// </summary>
@ -124,7 +161,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
/// <param name="token"></param>
[HttpGet("export")]
[Permission]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task<IActionResult> ExportAsync(int idWell, CancellationToken token)
@ -139,5 +176,22 @@ namespace AsbCloudWebApi.Controllers.SAUB
return File(stream, "application/octet-stream", "operations.xlsx");
}
private async Task<int> AssertUserHasAccessToWellAsync(int idWell, CancellationToken token)
{
var idUser = User.GetUserId();
var idCompany = User.GetCompanyId();
if (!idUser.HasValue)
throw new ForbidException("Неизвестный пользователь");
if (!idCompany.HasValue)
throw new ForbidException("Нет доступа к скважине");
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token))
throw new ForbidException("Нет доступа к скважине");
return idUser.Value;
}
}
}