public async Task <VsdListViewModel> Handle(GetVetDocumentListQuery request, CancellationToken cancellationToken) { try { var userName = _httpContextAccessor.HttpContext?.User.Identity?.Name; if (userName is null) { throw new Exception("Невозможно определить пользователя"); } var user = await _userManager.FindByNameAsync(userName) ?? throw new Exception($@"Пользователь с именем {userName} не найден."); var enterprise = await _context.Enterprises.AsNoTracking() .FirstOrDefaultAsync(x => x.Id == request.EnterpriseId, cancellationToken) ?? throw new Exception($@"Предприятие с идентификатором {request.EnterpriseId} не найдено."); var offset = request.PageSize * (request.Page - 1); var vm = await _mercuryService.GetVetDocumentList( "a10003", user, enterprise, request.PageSize, offset, request.Type, request.Status ); vm.PageSize = request.PageSize; vm.CurrentPage = request.Page; vm.PageCount = vm.ElementCount / vm.PageSize + (vm.ElementCount % vm.PageSize > 0 ? 1 : 0); return(vm); } catch (Exception e) { Console.WriteLine(e); throw; } }
private async Task ProcessVsd(User user, CancellationToken cancellationToken) { try { if (user.Enterprises is null) { lock (_autoVsdProcessDataService.Locker) { _autoVsdProcessDataService.Users.Remove(user); } _logger.LogInformation("Завершение процедуры автогашения для пользователя {0}", user.UserName); return; } foreach (var enterprise in user.Enterprises) { List <VsdViewModel> vsdList; try { var offset = 0; lock (_autoVsdProcessDataService.Locker) { offset = _autoVsdProcessDataService.VsdBlackList.Count; } vsdList = (await _mercuryService.GetVetDocumentList("a10003", user, enterprise, 10, offset, 3, 1)) .result; } catch (MercuryEnterpriseNotFoundException) { user.Enterprises.Remove(enterprise); continue; } catch (MercuryOffsetException) { user.Enterprises.Remove(enterprise); continue; } if (vsdList is null || vsdList.Count == 0) { user.Enterprises.Remove(enterprise); continue; } _logger.LogInformation( "Начало сеанса автогашения. Пользователь: {0}, Предприятие: {1}, Количество ВСД: {2}", user.UserName, enterprise.Name, vsdList.Count); await _mediator.Send(new ProcessIncomingVsdListAutoCommand { Enterprise = enterprise, User = user, Vsds = vsdList.Select(vsd => new VsdForProcessModel { VsdId = vsd.Id, ProcessDate = vsd.ProcessDate }).ToList() }, cancellationToken); _logger.LogInformation( "Сеанс автогашения успешно завершён. Пользователь: {0}, Предприятие: {1}, Количество ВСД: {2}", user.UserName, enterprise.Name, vsdList.Count); } if (user.Enterprises.Count == 0) { lock (_autoVsdProcessDataService.Locker) { _autoVsdProcessDataService.Users.Remove(user); } _logger.LogInformation("Завершение процедуры автогашения для пользователя {0}", user.UserName); } } catch (MercuryRequestRejectedException) { throw; } catch (OperationCanceledException) { throw; } catch (AggregateException e) { foreach (var innerException in e.InnerExceptions) { if (innerException is MercuryServiceException mercuryServiceException) { lock (_autoVsdProcessDataService.Locker) { _autoVsdProcessDataService.VsdBlackList.Add(mercuryServiceException.VsdId); _logger.LogInformation("Произошла ошибка при обработке автогашения. Пользователь: {0}", user.UserName); _logger.LogError(mercuryServiceException.Message, mercuryServiceException); } } } } catch (Exception e) { _logger.LogInformation("Произошла ошибка при обработке автогашения. Пользователь: {0}", user.UserName); _logger.LogError(e.Message, e); } }