2022-09-05 09:13:45 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-09-08 12:05:56 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
2022-09-05 09:13:45 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using AsbCloudInfrastructure.Repository;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис "Дело скважины"
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class WellFinalDocumentsService : CrudServiceBase<WellFinalDocumentsDBDto, WellFinalDocuments>, IWellFinalDocumentsService
|
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext context;
|
|
|
|
|
private readonly IFileService fileService;
|
|
|
|
|
private readonly IUserService userService;
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
private readonly IConfiguration configuration;
|
|
|
|
|
private readonly IEmailService emailService;
|
2022-09-08 12:05:56 +05:00
|
|
|
|
private readonly IFileCategoryService fileCategoryService;
|
|
|
|
|
private const int ResultSaveCategoryFile = 0;
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
|
|
|
|
public WellFinalDocumentsService(IAsbCloudDbContext context,
|
|
|
|
|
IFileService fileService,
|
|
|
|
|
IUserService userService,
|
|
|
|
|
IWellService wellService,
|
|
|
|
|
IConfiguration configuration,
|
2022-09-08 12:05:56 +05:00
|
|
|
|
IEmailService emailService,
|
|
|
|
|
IFileCategoryService fileCategoryService)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
: base(context)
|
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
|
|
|
|
this.fileService = fileService;
|
|
|
|
|
this.userService = userService;
|
|
|
|
|
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-08 12:05:56 +05:00
|
|
|
|
public override async Task<int> InsertRangeAsync(IEnumerable<WellFinalDocumentsDBDto> dtos, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
|
|
|
|
var data = await base.InsertRangeAsync(dtos, token);
|
|
|
|
|
if (data > 0)
|
|
|
|
|
{
|
2022-09-08 12:05:56 +05:00
|
|
|
|
var message = "от Вас ожидается загрузка на портал документа «{0}»";
|
|
|
|
|
await GenerateMessageAsync(dtos, message, token);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 12:05:56 +05:00
|
|
|
|
public override async Task<int> DeleteAsync(int dtoId, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2022-09-08 12:05:56 +05:00
|
|
|
|
var entity = await context.WellFinalDocuments
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.FirstOrDefaultAsync(x => x.Id == dtoId);
|
|
|
|
|
var dto = Convert(entity);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
|
|
|
|
var data = await base.DeleteAsync(dtoId, token);
|
|
|
|
|
if (data > 0)
|
|
|
|
|
{
|
|
|
|
|
var message = "Вас удалили из ответственных за загрузку документа «{0}»";
|
2022-09-08 12:05:56 +05:00
|
|
|
|
await GenerateMessageAsync(new List<WellFinalDocumentsDBDto> { dto }, message, token);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 12:05:56 +05:00
|
|
|
|
public async Task<IEnumerable<WellFinalDocumentsDto>> GetByWellId(int idWell, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
|
|
|
|
var result = new List<WellFinalDocumentsDto>();
|
|
|
|
|
|
|
|
|
|
var wells = await context.WellFinalDocuments.Where(x => x.IdWell == idWell)
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (wells.Any())
|
|
|
|
|
{
|
|
|
|
|
var category = await context.FileCategories
|
|
|
|
|
.Where(x => wells.Select(w => w.IdCategory).Contains(x.Id))
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var wellFinalDocs = category
|
|
|
|
|
.GroupJoin(wells,
|
|
|
|
|
fc => fc.Id,
|
|
|
|
|
w => w.IdCategory,
|
|
|
|
|
(o, i) => new {
|
|
|
|
|
NameCategory = o.Name,
|
|
|
|
|
Wells = i
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
wellFinalDocs.ForEach(async item => {
|
|
|
|
|
var userIds = item.Wells
|
|
|
|
|
.Select(x => x.IdUser)
|
|
|
|
|
.ToList();
|
|
|
|
|
var allUsers = await userService.GetAllAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var fileIds = item.Wells
|
|
|
|
|
.Where(x => x.IdFile != null)
|
|
|
|
|
.Select(x => x.IdFile)
|
|
|
|
|
.OrderByDescending(x => x)
|
|
|
|
|
.ToList();
|
|
|
|
|
FileInfoDto actualFile = null;
|
|
|
|
|
if (fileIds.Any())
|
|
|
|
|
{
|
|
|
|
|
var actualFileId = fileIds.FirstOrDefault();
|
|
|
|
|
actualFile = fileService.GetInfoAsync(actualFileId ?? default(int), token).Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Add(new WellFinalDocumentsDto {
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
NameCategory = item.NameCategory,
|
2022-09-08 12:05:56 +05:00
|
|
|
|
Publishers = allUsers.Where(x => userIds.Contains(x.Id)),
|
|
|
|
|
FilesCount = fileIds.Count(x => x.HasValue),
|
2022-09-05 09:13:45 +05:00
|
|
|
|
File = actualFile
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 12:05:56 +05:00
|
|
|
|
public async Task<IEnumerable<UserDto>> GetPublishersAsync(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);
|
|
|
|
|
|
|
|
|
|
var allUsers = await userService.GetAllAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return allUsers.Where(x => {
|
|
|
|
|
var idCompany = x.IdCompany ?? default(int);
|
|
|
|
|
return companyIds.Contains(idCompany);
|
|
|
|
|
})
|
|
|
|
|
.OrderBy(x => x.Surname)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 12:05:56 +05:00
|
|
|
|
public async Task<int> SaveCategoryFile(int idDto, 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()
|
|
|
|
|
.FirstOrDefaultAsync(x => x.Id == idDto);
|
|
|
|
|
|
|
|
|
|
if (entity.IdUser != idUser)
|
|
|
|
|
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-08 12:05:56 +05:00
|
|
|
|
if (file is not null)
|
|
|
|
|
{
|
|
|
|
|
dto.IdFile = file.Id;
|
|
|
|
|
return await base.UpdateAsync(dto, token);
|
|
|
|
|
}
|
|
|
|
|
else return ResultSaveCategoryFile;
|
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
|
|
|
|
{
|
|
|
|
|
var wellsIds = await context.WellFinalDocuments
|
|
|
|
|
.Where(x => idWell == x.IdWell && x.IdCategory == idCategory && x.IdFile != null)
|
|
|
|
|
.Select(x => (int)x.IdFile)
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var files = await fileService.GetInfoByIdsAsync(wellsIds, token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return new WellFinalDocumentsHistoryDto {
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
File = files
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 12:05:56 +05:00
|
|
|
|
private async Task GenerateMessageAsync(IEnumerable<WellFinalDocumentsDBDto> dtos, string message, CancellationToken token)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
|
|
|
|
foreach (var item in dtos)
|
|
|
|
|
{
|
2022-09-08 12:05:56 +05:00
|
|
|
|
var user = await userService.GetOrDefaultAsync(item.IdUser, token);
|
|
|
|
|
var category = await fileCategoryService.GetOrDefaultAsync(item.IdCategory, token);
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(item.IdWell, token);
|
|
|
|
|
|
|
|
|
|
SendMessage(well, user, category.Name, message, token);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 12:05:56 +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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static WellFinalDocumentsDBDto Convert(WellFinalDocuments dto)
|
|
|
|
|
=> new WellFinalDocumentsDBDto
|
|
|
|
|
{
|
|
|
|
|
Id = dto.Id,
|
|
|
|
|
IdCategory = dto.IdCategory,
|
|
|
|
|
IdWell = dto.IdWell,
|
|
|
|
|
IdUser = dto.IdUser,
|
|
|
|
|
IdFile = dto.IdFile
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|