2022-09-05 09:13:45 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
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 AsbCloudDb.Model;
|
2022-09-28 10:46:12 +05:00
|
|
|
|
using AsbCloudInfrastructure.Repository;
|
2022-09-12 08:22:46 +05:00
|
|
|
|
using Mapster;
|
2022-09-05 09:13:45 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
2022-09-12 08:22:46 +05:00
|
|
|
|
#nullable enable
|
2022-09-05 09:13:45 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис "Дело скважины"
|
|
|
|
|
/// </summary>
|
2022-09-12 08:22:46 +05:00
|
|
|
|
public class WellFinalDocumentsService : IWellFinalDocumentsService
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext context;
|
2022-10-06 14:37:03 +05:00
|
|
|
|
private readonly FileService fileService;
|
2022-10-27 11:22:39 +05:00
|
|
|
|
private readonly IUserRepository userRepository;
|
2022-09-05 09:13:45 +05:00
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
private readonly IConfiguration configuration;
|
|
|
|
|
private readonly IEmailService emailService;
|
2022-09-08 12:05:56 +05:00
|
|
|
|
private readonly IFileCategoryService fileCategoryService;
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2022-09-13 12:43:22 +05:00
|
|
|
|
private const int FileServiceThrewException = -1;
|
|
|
|
|
|
2022-09-05 09:13:45 +05:00
|
|
|
|
public WellFinalDocumentsService(IAsbCloudDbContext context,
|
2022-10-06 14:37:03 +05:00
|
|
|
|
FileService fileService,
|
2022-10-27 11:22:39 +05:00
|
|
|
|
IUserRepository userRepository,
|
2022-09-05 09:13:45 +05:00
|
|
|
|
IWellService wellService,
|
|
|
|
|
IConfiguration configuration,
|
2022-09-08 12:05:56 +05:00
|
|
|
|
IEmailService emailService,
|
2022-09-30 13:34:50 +05:00
|
|
|
|
IFileCategoryService fileCategoryService)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
|
|
|
|
this.fileService = fileService;
|
2022-10-27 11:22:39 +05:00
|
|
|
|
this.userRepository = userRepository;
|
2022-09-05 09:13:45 +05:00
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.configuration = configuration;
|
|
|
|
|
this.emailService = emailService;
|
2022-09-08 12:05:56 +05:00
|
|
|
|
this.fileCategoryService = fileCategoryService;
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 08:22:46 +05:00
|
|
|
|
public async Task<int> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto>? dtos, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2022-09-12 08:22:46 +05:00
|
|
|
|
if (dtos is not null)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2022-09-12 08:22:46 +05:00
|
|
|
|
var entities = dtos
|
|
|
|
|
.Where(dto => dto.IdsPublishers?.Any() == true)
|
|
|
|
|
.SelectMany(dto => dto.IdsPublishers
|
|
|
|
|
.Select(idUser => new WellFinalDocument
|
|
|
|
|
{
|
|
|
|
|
IdCategory = dto.IdCategory,
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdUser = idUser
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
var itemsToDelete = context.WellFinalDocuments.Where(d => d.IdWell == idWell);
|
|
|
|
|
context.WellFinalDocuments.RemoveRange(itemsToDelete);
|
|
|
|
|
|
|
|
|
|
await context.WellFinalDocuments.AddRangeAsync(entities).ConfigureAwait(false);
|
|
|
|
|
var data = await context.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (data > 0)
|
|
|
|
|
{
|
|
|
|
|
var message = "от Вас ожидается загрузка на портал документа «{0}»";
|
|
|
|
|
await GenerateMessageAsync(entities.Select(x => Convert(x)), message, token);
|
|
|
|
|
}
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2022-09-12 08:22:46 +05:00
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
throw new ArgumentInvalidException("Данные по категориям отсутствуют.");
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-13 11:47:12 +05:00
|
|
|
|
public async Task<WellCaseDto> GetByWellId(int idWell, int idUser, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var entities = await context.WellFinalDocuments
|
|
|
|
|
.Include(d => d.Category)
|
|
|
|
|
.Include(d => d.User)
|
|
|
|
|
.Where(d => d.IdWell == idWell)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToArrayAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var entitiesGroups = entities
|
2022-09-13 15:45:15 +05:00
|
|
|
|
.GroupBy(d => d.IdCategory);
|
2022-09-13 11:47:12 +05:00
|
|
|
|
|
|
|
|
|
var categoriesIds = entitiesGroups
|
2022-09-13 15:45:15 +05:00
|
|
|
|
.Select(g => g.Key);
|
2022-09-13 11:47:12 +05:00
|
|
|
|
|
2022-09-30 13:34:50 +05:00
|
|
|
|
var files = (await fileService
|
2022-10-17 14:42:47 +05:00
|
|
|
|
.GetInfosAsync(new FileRequest { IdWell = idWell}, token)
|
2022-09-13 11:47:12 +05:00
|
|
|
|
.ConfigureAwait(false))
|
|
|
|
|
.Where(f => categoriesIds.Contains(f.IdCategory))
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var docs = entitiesGroups.Select((g) => new WellFinalDocumentDto
|
|
|
|
|
{
|
2022-09-13 15:45:15 +05:00
|
|
|
|
IdCategory = g.Key,
|
2022-09-13 11:47:12 +05:00
|
|
|
|
FilesCount = files
|
2022-09-13 15:45:15 +05:00
|
|
|
|
.Where(f => f.IdCategory == g.Key)
|
2022-09-13 11:47:12 +05:00
|
|
|
|
.Count(),
|
|
|
|
|
File = files
|
2022-09-13 15:45:15 +05:00
|
|
|
|
.Where(f => f.IdCategory == g.Key)
|
2022-09-13 11:47:12 +05:00
|
|
|
|
.OrderBy(f => f.UploadDate)
|
|
|
|
|
.LastOrDefault(),
|
2022-09-13 15:45:15 +05:00
|
|
|
|
NameCategory = g.First().Category.Name,
|
2022-09-13 11:47:12 +05:00
|
|
|
|
Publishers = g.Select(i => i.User.Adapt<UserDto>()),
|
|
|
|
|
PermissionToUpload = g.Any(i => i.IdUser == idUser),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var result = new WellCaseDto
|
|
|
|
|
{
|
|
|
|
|
IdWell = idWell,
|
2022-11-18 14:48:10 +05:00
|
|
|
|
PermissionToSetPubliher = userRepository.HasPermission(idUser, "WellFinalDocuments.editPublisher"),
|
2022-09-13 11:47:12 +05:00
|
|
|
|
WellFinalDocuments = docs,
|
|
|
|
|
};
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 08:22:46 +05:00
|
|
|
|
public async Task<IEnumerable<UserDto>> GetAvailableUsersAsync(int idWell, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
|
|
|
|
var companyIds = await context.RelationCompaniesWells
|
|
|
|
|
.Where(x => x.IdWell == idWell).Select(x => x.IdCompany)
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2022-10-27 11:22:39 +05:00
|
|
|
|
var allUsers = await userRepository.GetAllAsync(token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return allUsers.Where(x => {
|
|
|
|
|
var idCompany = x.IdCompany ?? default(int);
|
|
|
|
|
return companyIds.Contains(idCompany);
|
|
|
|
|
})
|
|
|
|
|
.OrderBy(x => x.Surname)
|
2022-09-13 12:43:22 +05:00
|
|
|
|
.ToArray();
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 08:22:46 +05:00
|
|
|
|
public async Task<int> SaveCategoryFile(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2022-09-08 12:05:56 +05:00
|
|
|
|
var entity = await context.WellFinalDocuments
|
|
|
|
|
.AsNoTracking()
|
2022-09-12 08:22:46 +05:00
|
|
|
|
.FirstOrDefaultAsync(x => x.IdWell == idWell && x.IdCategory == idCategory && x.IdUser == idUser);
|
2022-09-08 12:05:56 +05:00
|
|
|
|
|
2022-09-12 08:22:46 +05:00
|
|
|
|
if (entity is null)
|
2022-09-08 12:05:56 +05:00
|
|
|
|
throw new ArgumentInvalidException("Пользователь не является ответственным за загрузку файла для данной категории.");
|
|
|
|
|
|
|
|
|
|
var dto = Convert(entity);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
|
|
|
|
var file = await fileService.SaveAsync(dto.IdWell, dto.IdUser, dto.IdCategory, fileName,
|
|
|
|
|
fileStream, token).ConfigureAwait(false);
|
|
|
|
|
|
2022-09-13 12:43:22 +05:00
|
|
|
|
return file?.Id ?? FileServiceThrewException; //TODO: изменить когда файловый сервис будет переведен на nullable
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 12:05:56 +05:00
|
|
|
|
public async Task<WellFinalDocumentsHistoryDto> GetFilesHistoryByIdCategory(int idWell, int idCategory, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2022-10-17 14:42:47 +05:00
|
|
|
|
var request = new FileRequest
|
|
|
|
|
{
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
};
|
|
|
|
|
var files = await fileService.GetInfosAsync(request, token).ConfigureAwait(false);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
|
|
|
|
return new WellFinalDocumentsHistoryDto {
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
File = files
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 08:22:46 +05:00
|
|
|
|
private async Task GenerateMessageAsync(IEnumerable<WellFinalDocumentDBDto> dtos, string message, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
|
|
|
|
foreach (var item in dtos)
|
|
|
|
|
{
|
2022-10-27 11:22:39 +05:00
|
|
|
|
var user = await userRepository.GetOrDefaultAsync(item.IdUser, token);
|
2022-09-12 08:22:46 +05:00
|
|
|
|
if (user?.Email is not null)
|
|
|
|
|
{
|
|
|
|
|
var category = await fileCategoryService.GetOrDefaultAsync(item.IdCategory, token);
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(item.IdWell, token);
|
2022-09-08 12:05:56 +05:00
|
|
|
|
|
2022-09-12 08:22:46 +05:00
|
|
|
|
SendMessage(well, user, category.Name, message, token);
|
|
|
|
|
}
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 09:11:20 +05:00
|
|
|
|
private void SendMessage(WellDto? well, UserDto user, string documentCategory, string message, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2022-09-08 12:05:56 +05:00
|
|
|
|
var factory = new WellFinalDocumentMailBodyFactory(configuration);
|
|
|
|
|
var subject = factory.MakeSubject(well, documentCategory);
|
|
|
|
|
var body = factory.MakeMailBodyForWellFinalDocument(well, user.Name ?? user.Surname, string.Format(message, documentCategory));
|
2022-09-05 09:13:45 +05:00
|
|
|
|
emailService.EnqueueSend(user.Email, subject, body);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 09:11:20 +05:00
|
|
|
|
private static WellFinalDocumentDBDto Convert(WellFinalDocument entity)
|
2022-09-12 08:22:46 +05:00
|
|
|
|
=> entity.Adapt<WellFinalDocumentDBDto>();
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
2022-09-12 08:22:46 +05:00
|
|
|
|
#nullable disable
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|