From f286410effc485669c6f51906329527e0fa61d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Mon, 28 Feb 2022 14:44:15 +0500 Subject: [PATCH] Add DrillingProgramCreateError --- AsbCloudApp/Data/DrillingProgramStateDto.cs | 7 ++++ .../Services/IDrillingProgramService.cs | 1 + .../DrillingProgram/DrillingProgramService.cs | 38 +++++++++++++++++-- .../Controllers/DrillingProgramController.cs | 18 +++++++-- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/AsbCloudApp/Data/DrillingProgramStateDto.cs b/AsbCloudApp/Data/DrillingProgramStateDto.cs index 0581afea..93f5282c 100644 --- a/AsbCloudApp/Data/DrillingProgramStateDto.cs +++ b/AsbCloudApp/Data/DrillingProgramStateDto.cs @@ -11,8 +11,15 @@ namespace AsbCloudApp.Data /// 3 - готова /// public int IdState { get; set; } + public DrillingProgramCreateError Error { get; set; } public FileInfoDto Program { get; set; } public bool PermissionToEdit { get; set; } public IEnumerable Parts { get; set; } } + + public class DrillingProgramCreateError + { + public string Message { get; set; } + public string Exception { get; set; } + } } diff --git a/AsbCloudApp/Services/IDrillingProgramService.cs b/AsbCloudApp/Services/IDrillingProgramService.cs index b3f54b55..12cfe35f 100644 --- a/AsbCloudApp/Services/IDrillingProgramService.cs +++ b/AsbCloudApp/Services/IDrillingProgramService.cs @@ -19,5 +19,6 @@ namespace AsbCloudApp.Services Task RemoveUserAsync(int idWell, int idFileCategory, int idUser, int idUserRole, CancellationToken token = default); Task AddOrReplaceFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token); Task MarkAsDeletedFileMarkAsync(int idFileMark, CancellationToken token); + void ClearError(int idWell); } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs index 4404e468..2ea4c533 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs @@ -16,10 +16,13 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram { public class DrillingProgramService : IDrillingProgramService { + private static Dictionary drillingProgramCreateErrors = new Dictionary(); + private readonly IAsbCloudDbContext context; private readonly IFileService fileService; private readonly IUserService userService; private readonly IWellService wellService; + private readonly IConfiguration configuration; private readonly IBackgroundWorkerService backgroundWorker; private readonly string connectionString; @@ -41,6 +44,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram private const int idStateApproving = 1; private const int idStateCreating = 2; private const int idStateReady = 3; + private const int idStateError = 4; public DrillingProgramService( IAsbCloudDbContext context, @@ -54,6 +58,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram this.fileService = fileService; this.userService = userService; this.wellService = wellService; + this.configuration = configuration; this.backgroundWorker = backgroundWorker; this.connectionString = configuration.GetConnectionString("DefaultConnection"); } @@ -125,9 +130,20 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram if(parts.All(p=>p.IdState == idPartStateApproved)) { if (state.Program is not null) + { state.IdState = idStateReady; - else - state.IdState = idStateCreating; + } + else + { + var workId = MakeWorkId(idWell); + if (drillingProgramCreateErrors.ContainsKey(workId)) + { + state.IdState = idStateError; + state.Error = drillingProgramCreateErrors[workId]; + } + else + state.IdState = idStateCreating; + } } else state.IdState = idStateApproving; @@ -355,6 +371,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram var well = await wellService.GetAsync(idWell, token); var resultFileName = $"Программа бурения {well.Cluster} {well.Caption}.xlsx"; var tempResultFilePath = Path.Combine(Path.GetTempPath(), "drillingProgram", resultFileName); + var mailService = new EmailService(backgroundWorker, configuration); async Task funcProgramMake(string id, CancellationToken token) { var contextOptions = new DbContextOptionsBuilder() @@ -367,11 +384,26 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram await fileService.MoveAsync(idWell, null, idFileCategoryDrillingProgram, resultFileName, tempResultFilePath, token); } - backgroundWorker.Enqueue(funcProgramMake); + Task funcOnErrorProgramMake(string workId, Exception exception, CancellationToken token) { + var message = $"Не удалось сформировать программу бурения по скважине {well?.Caption}"; + drillingProgramCreateErrors[workId] = new() { + Message = message, + Exception = exception.Message, + }; + return Task.CompletedTask; + } + + backgroundWorker.Enqueue(workId, funcProgramMake, funcOnErrorProgramMake); } } } + public void ClearError(int idWell) + { + var workId = MakeWorkId(idWell); + drillingProgramCreateErrors.Remove(workId); + } + private async Task RemoveDrillingProgramAsync(int idWell, CancellationToken token) { var workId = MakeWorkId(idWell); diff --git a/AsbCloudWebApi/Controllers/DrillingProgramController.cs b/AsbCloudWebApi/Controllers/DrillingProgramController.cs index 4c1b72b5..2f5336d5 100644 --- a/AsbCloudWebApi/Controllers/DrillingProgramController.cs +++ b/AsbCloudWebApi/Controllers/DrillingProgramController.cs @@ -4,11 +4,8 @@ using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.IO; -using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -70,7 +67,7 @@ namespace AsbCloudWebApi.Controllers var idUser = User.GetUserId(); - if (idCompany is null || idUser is null || + if (idCompany is null || idUser is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false)) return Forbid(); @@ -79,6 +76,19 @@ namespace AsbCloudWebApi.Controllers return Ok(result); } + /// + /// Сброс ошибки формирования ПБ + /// + /// + /// + [HttpPost("ClearError")] + [Permission] + public IActionResult ClearError(int idWell) + { + drillingProgramService.ClearError(idWell); + return Ok(); + } + /// /// Загрузка файла для части программы бурения ///