2024-07-04 11:02:45 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2023-06-21 12:33:18 +05:00
|
|
|
|
using AsbCloudApp.Data.User;
|
2022-09-08 12:05:56 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
2022-10-27 11:22:39 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2022-10-17 14:42:47 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2022-09-05 09:13:45 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-07-19 14:24:22 +05:00
|
|
|
|
using AsbCloudApp.Services.Notifications;
|
2023-12-19 12:28:41 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services;
|
|
|
|
|
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис "Дело скважины"
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class WellFinalDocumentsService : IWellFinalDocumentsService
|
|
|
|
|
{
|
|
|
|
|
private readonly FileService fileService;
|
|
|
|
|
private readonly IUserRepository userRepository;
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
private readonly IConfiguration configuration;
|
|
|
|
|
private readonly IFileCategoryService fileCategoryService;
|
|
|
|
|
private readonly IWellFinalDocumentsRepository wellFinalDocumentsRepository;
|
|
|
|
|
private readonly NotificationService notificationService;
|
|
|
|
|
|
|
|
|
|
public WellFinalDocumentsService(FileService fileService,
|
|
|
|
|
IUserRepository userRepository,
|
|
|
|
|
IWellService wellService,
|
|
|
|
|
IConfiguration configuration,
|
|
|
|
|
IFileCategoryService fileCategoryService,
|
|
|
|
|
IWellFinalDocumentsRepository wellFinalDocumentsRepository,
|
|
|
|
|
NotificationService notificationService)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
this.fileService = fileService;
|
|
|
|
|
this.userRepository = userRepository;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.configuration = configuration;
|
|
|
|
|
this.fileCategoryService = fileCategoryService;
|
|
|
|
|
this.wellFinalDocumentsRepository = wellFinalDocumentsRepository;
|
|
|
|
|
this.notificationService = notificationService;
|
|
|
|
|
}
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
///<inheritdoc/>
|
|
|
|
|
public async Task<int> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto> dtos, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var data = await wellFinalDocumentsRepository.UpdateRangeAsync(idWell, dtos, token);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var message = "от Вас ожидается загрузка на портал документа «{0}»";
|
|
|
|
|
await NotifyUsersAsync(data, message, token);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return data.Count();
|
|
|
|
|
}
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
///<inheritdoc/>
|
|
|
|
|
public async Task<int> SaveCategoryFileAsync(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var dto = await wellFinalDocumentsRepository.GetCategoryAsync(idWell, idCategory, idUser, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var file = await fileService.SaveAsync(dto.IdWell, dto.IdUser, dto.IdCategory, fileName,
|
|
|
|
|
fileStream, token).ConfigureAwait(false);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return file.Id;
|
|
|
|
|
}
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
///<inheritdoc/>
|
|
|
|
|
public async Task<WellFinalDocumentsHistoryDto> GetFilesHistoryByIdCategoryAsync(int idWell, int idCategory, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var request = new FileRequest
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
};
|
|
|
|
|
var files = await fileService.GetInfosAsync(request, token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return new WellFinalDocumentsHistoryDto {
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
Files = files
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
///<inheritdoc/>
|
|
|
|
|
public async Task<int> ReNotifyPublishersAsync(int idWell, int idUser, int idCategory, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var wellCase = await wellFinalDocumentsRepository.GetByWellIdAsync(idWell, idUser, token);
|
2022-11-23 14:03:08 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (!wellCase.PermissionToSetPubliher)
|
|
|
|
|
throw new ForbidException("Повторная отправка оповещений Вам не разрешена");
|
2022-11-23 14:03:08 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var requester = await userRepository.GetOrDefaultAsync(idUser, token);
|
|
|
|
|
if (requester is null)
|
|
|
|
|
throw new ForbidException("Не удается вас опознать");
|
2022-11-23 14:03:08 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var docs = wellCase.WellFinalDocuments
|
|
|
|
|
.Where(doc => doc.IdCategory == idCategory)
|
|
|
|
|
.Where(doc => doc.File is null)
|
|
|
|
|
.SelectMany(doc => doc.Publishers
|
|
|
|
|
.Select(pub => new WellFinalDocumentDBDto {
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
IdUser = pub.Id,
|
|
|
|
|
IdWell = idWell
|
|
|
|
|
}));
|
2022-11-23 14:03:08 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if(!docs.Any())
|
|
|
|
|
throw new ArgumentInvalidException(nameof(idCategory), "Нет такой категории, или в нее уже загружен документ");
|
2022-11-23 14:03:08 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var message = requester.MakeDisplayName() + " ожидает от Вас загрузку на портал документа «{{0}}»";
|
|
|
|
|
await NotifyUsersAsync(docs, message, token);
|
2022-11-23 14:03:08 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return docs.Count();
|
|
|
|
|
}
|
2022-11-23 14:03:08 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
private async Task NotifyUsersAsync(IEnumerable<WellFinalDocumentDBDto> dtos, string message, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in dtos)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var user = await userRepository.GetOrDefaultAsync(item.IdUser, token);
|
|
|
|
|
if (user?.Email is not null)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var category = await fileCategoryService.GetOrDefaultAsync(item.IdCategory, token);
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(item.IdWell, token)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(item.IdWell), "idWell doesn`t exist");
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(well, user, category?.Name ?? string.Empty, message,
|
|
|
|
|
token);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-19 10:01:07 +05:00
|
|
|
|
}
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
private async Task SendMessageAsync(WellDto well, UserDto user, string documentCategory, string message,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
const int idTransportType = 1;
|
|
|
|
|
|
|
|
|
|
var factory = new WellFinalDocumentMailBodyFactory(configuration);
|
|
|
|
|
var subject = factory.MakeSubject(well, documentCategory);
|
|
|
|
|
|
|
|
|
|
var body = factory.MakeMailBodyForWellFinalDocument(
|
|
|
|
|
well,
|
|
|
|
|
(user.Name ?? user.Surname ?? string.Empty),
|
|
|
|
|
string.Format(message, documentCategory)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await notificationService.NotifyAsync(new NotifyRequest
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
IdUser = user.Id,
|
|
|
|
|
IdNotificationCategory = NotificationCategory.IdSystemNotificationCategory,
|
|
|
|
|
Title = subject,
|
|
|
|
|
Message = body,
|
|
|
|
|
IdTransportType = idTransportType
|
|
|
|
|
}, cancellationToken);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
}
|