#5998816 дело скважины, исправления

This commit is contained in:
ai.astrakhantsev 2022-09-12 08:22:46 +05:00
parent 2d0fe6245b
commit c4d88401fd
22 changed files with 6916 additions and 178 deletions

View File

@ -12,12 +12,12 @@
/// <summary>
/// полное название
/// </summary>
public string Name { get; set; }
public string Name { get; set; } = string.Empty;
/// <summary>
/// сокращенное название
/// </summary>
public string ShortName { get; set; }
public string ShortName { get; set; } = string.Empty;
}
#nullable disable
}

View File

@ -4,13 +4,8 @@
/// <summary>
/// DTO Дело скважины
/// </summary>
public class WellFinalDocumentsDBDto : IId
public class WellFinalDocumentDBDto
{
/// <summary>
/// Идентификатор
/// </summary>
public int Id { get; set; }
/// <summary>
/// Идентификатор скважины
/// </summary>
@ -25,11 +20,6 @@
/// Идентификатор категории файла
/// </summary>
public int IdCategory { get; set; }
/// <summary>
/// Идентификатор файла
/// </summary>
public int? IdFile { get; set; }
}
#nullable disable
}

View File

@ -7,7 +7,7 @@ namespace AsbCloudApp.Data
/// <summary>
/// DTO Документ дела скважины
/// </summary>
public class WellFinalDocumentsDto
public class WellFinalDocumentDto
{
/// <summary>
/// Скважина
@ -17,7 +17,7 @@ namespace AsbCloudApp.Data
/// <summary>
/// Наименование категории файла
/// </summary>
public string NameCategory { get; set; }
public string NameCategory { get; set; } = string.Empty;
/// <summary>
/// Список ответственных
@ -32,7 +32,7 @@ namespace AsbCloudApp.Data
/// <summary>
/// Актуальный файл
/// </summary>
public FileInfoDto File { get; set; }
public FileInfoDto? File { get; set; }
}
#nullable disable
}

View File

@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Linq;
namespace AsbCloudApp.Data
{
#nullable enable
/// <summary>
/// DTO Для сохранения категорий дела скважины
/// </summary>
public class WellFinalDocumentInputDto
{
/// <summary>
/// Идентификатор категории файла
/// </summary>
public int IdCategory { get; set; }
/// <summary>
/// Список ответственных
/// </summary>
public IEnumerable<int> IdsPublishers { get; set; } = Enumerable.Empty<int>();
}
#nullable disable
}

View File

@ -21,7 +21,7 @@ namespace AsbCloudApp.Data
/// <summary>
/// Файлы
/// </summary>
public List<FileInfoDto> File { get; set; }
public IEnumerable<FileInfoDto>? File { get; set; }
}
#nullable disable
}

View File

@ -167,5 +167,13 @@ namespace AsbCloudApp.Services
/// <param name="token"></param>
/// <returns></returns>
Task<List<FileInfoDto>> GetInfoByIdsAsync(List<int> idsFile, CancellationToken token);
/// <summary>
/// Получение файлов по скважине
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(int idWell, CancellationToken token);
}
}

View File

@ -10,15 +10,24 @@ namespace AsbCloudApp.Services
/// <summary>
/// Сервис "Дело скважины"
/// </summary>
public interface IWellFinalDocumentsService : ICrudService<WellFinalDocumentsDBDto>
public interface IWellFinalDocumentsService
{
/// <summary>
/// Обновление записей дела скважины
/// </summary>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto>? dtos, CancellationToken token);
/// <summary>
/// Получение всех записей
/// </summary>
/// <param name = "idWell" ></param >
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<WellFinalDocumentsDto>> GetByWellId(int idWell, CancellationToken token);
Task<IEnumerable<WellFinalDocumentDto>> GetByWellId(int idWell, CancellationToken token);
/// <summary>
/// Получение списка ответственных
@ -26,7 +35,7 @@ namespace AsbCloudApp.Services
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<UserDto>> GetPublishersAsync(int idWell, CancellationToken token);
Task<IEnumerable<UserDto>> GetAvailableUsersAsync(int idWell, CancellationToken token);
/// <summary>
/// Получение истории файлов
@ -46,7 +55,7 @@ namespace AsbCloudApp.Services
/// <param name="fileName"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> SaveCategoryFile(int idDto, int idUser, Stream fileStream, string fileName, CancellationToken token);
Task<int> SaveCategoryFile(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token);
}
#nullable disable
}

View File

@ -4205,7 +4205,7 @@ namespace AsbCloudDb.Migrations
b.HasComment("Композитная скважина");
});
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocuments", b =>
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocument", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -6335,7 +6335,7 @@ namespace AsbCloudDb.Migrations
b.Navigation("WellSrc");
});
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocuments", b =>
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocument", b =>
{
b.HasOne("AsbCloudDb.Model.FileCategory", "Category")
.WithMany()

View File

@ -1892,19 +1892,19 @@ namespace AsbCloudDb.Migrations
{
Id = 503,
Description = "Разрешение удалять Дело скважины",
Name = "WellFinalDocuments.delete"
Name = "WellFinalDocument.delete"
},
new
{
Id = 504,
Description = "Разрешение редактировать Дело скважины",
Name = "WellFinalDocuments.edit"
Name = "WellFinalDocument.edit"
},
new
{
Id = 505,
Description = "Разрешение просматривать Дело скважины",
Name = "WellFinalDocuments.get"
Name = "WellFinalDocument.get"
});
});
@ -4271,7 +4271,7 @@ namespace AsbCloudDb.Migrations
b.HasComment("Композитная скважина");
});
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocuments", b =>
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocument", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
@ -6401,7 +6401,7 @@ namespace AsbCloudDb.Migrations
b.Navigation("WellSrc");
});
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocuments", b =>
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocument", b =>
{
b.HasOne("AsbCloudDb.Model.FileCategory", "Category")
.WithMany()

View File

@ -16,9 +16,9 @@ namespace AsbCloudDb.Migrations
{ 500, "Разрешение удалять Категорий документов файлов", "FileCategory.delete" },
{ 501, "Разрешение редактировать Категорий документов файлов", "FileCategory.edit" },
{ 502, "Разрешение просматривать Категорий документов файлов", "FileCategory.get" },
{ 503, "Разрешение удалять Дело скважины", "WellFinalDocuments.delete" },
{ 504, "Разрешение редактировать Дело скважины", "WellFinalDocuments.edit" },
{ 505, "Разрешение просматривать Дело скважины", "WellFinalDocuments.get" }
{ 503, "Разрешение удалять Дело скважины", "WellFinalDocument.delete" },
{ 504, "Разрешение редактировать Дело скважины", "WellFinalDocument.edit" },
{ 505, "Разрешение просматривать Дело скважины", "WellFinalDocument.get" }
});
migrationBuilder.InsertData(

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,85 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace AsbCloudDb.Migrations
{
public partial class Update_t_well_final_documents_r : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_t_well_final_documents_t_file_info_id_file",
table: "t_well_final_documents");
migrationBuilder.DropPrimaryKey(
name: "t_well_final_documents_pk",
table: "t_well_final_documents");
migrationBuilder.DropIndex(
name: "IX_t_well_final_documents_id_file",
table: "t_well_final_documents");
migrationBuilder.DropIndex(
name: "IX_t_well_final_documents_id_well",
table: "t_well_final_documents");
migrationBuilder.DropColumn(
name: "id",
table: "t_well_final_documents");
migrationBuilder.DropColumn(
name: "id_file",
table: "t_well_final_documents");
migrationBuilder.AddPrimaryKey(
name: "t_well_final_documents_pk",
table: "t_well_final_documents",
columns: new[] { "id_well", "id_user", "id_category" });
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "t_well_final_documents_pk",
table: "t_well_final_documents");
migrationBuilder.AddColumn<int>(
name: "id",
table: "t_well_final_documents",
type: "integer",
nullable: false,
defaultValue: 0)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
migrationBuilder.AddColumn<int>(
name: "id_file",
table: "t_well_final_documents",
type: "integer",
nullable: true);
migrationBuilder.AddPrimaryKey(
name: "t_well_final_documents_pk",
table: "t_well_final_documents",
column: "id");
migrationBuilder.CreateIndex(
name: "IX_t_well_final_documents_id_file",
table: "t_well_final_documents",
column: "id_file");
migrationBuilder.CreateIndex(
name: "IX_t_well_final_documents_id_well",
table: "t_well_final_documents",
column: "id_well");
migrationBuilder.AddForeignKey(
name: "FK_t_well_final_documents_t_file_info_id_file",
table: "t_well_final_documents",
column: "id_file",
principalTable: "t_file_info",
principalColumn: "id");
}
}
}

View File

@ -1890,19 +1890,19 @@ namespace AsbCloudDb.Migrations
{
Id = 503,
Description = "Разрешение удалять Дело скважины",
Name = "WellFinalDocuments.delete"
Name = "WellFinalDocument.delete"
},
new
{
Id = 504,
Description = "Разрешение редактировать Дело скважины",
Name = "WellFinalDocuments.edit"
Name = "WellFinalDocument.edit"
},
new
{
Id = 505,
Description = "Разрешение просматривать Дело скважины",
Name = "WellFinalDocuments.get"
Name = "WellFinalDocument.get"
});
});
@ -4269,42 +4269,27 @@ namespace AsbCloudDb.Migrations
b.HasComment("Композитная скважина");
});
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocuments", b =>
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocument", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
b.Property<int>("IdWell")
.HasColumnType("integer")
.HasColumnName("id");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("IdCategory")
.HasColumnType("integer")
.HasColumnName("id_category");
b.Property<int?>("IdFile")
.HasColumnType("integer")
.HasColumnName("id_file");
.HasColumnName("id_well");
b.Property<int>("IdUser")
.HasColumnType("integer")
.HasColumnName("id_user");
b.Property<int>("IdWell")
b.Property<int>("IdCategory")
.HasColumnType("integer")
.HasColumnName("id_well");
.HasColumnName("id_category");
b.HasKey("Id")
b.HasKey("IdWell", "IdUser", "IdCategory")
.HasName("t_well_final_documents_pk");
b.HasIndex("IdCategory");
b.HasIndex("IdFile");
b.HasIndex("IdUser");
b.HasIndex("IdWell");
b.ToTable("t_well_final_documents");
b.HasComment("Дело скважины");
@ -6399,7 +6384,7 @@ namespace AsbCloudDb.Migrations
b.Navigation("WellSrc");
});
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocuments", b =>
modelBuilder.Entity("AsbCloudDb.Model.WellFinalDocument", b =>
{
b.HasOne("AsbCloudDb.Model.FileCategory", "Category")
.WithMany()
@ -6407,10 +6392,6 @@ namespace AsbCloudDb.Migrations
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AsbCloudDb.Model.FileInfo", "File")
.WithMany()
.HasForeignKey("IdFile");
b.HasOne("AsbCloudDb.Model.User", "User")
.WithMany()
.HasForeignKey("IdUser")
@ -6425,8 +6406,6 @@ namespace AsbCloudDb.Migrations
b.Navigation("Category");
b.Navigation("File");
b.Navigation("User");
b.Navigation("Well");

View File

@ -47,7 +47,7 @@ namespace AsbCloudDb.Model
public virtual DbSet<Driller> Drillers => Set<Driller>();
public virtual DbSet<Schedule> Schedule => Set<Schedule>();
public virtual DbSet<OperationValue> OperationValues => Set<OperationValue>();
public virtual DbSet<WellFinalDocuments> WellFinalDocuments => Set<WellFinalDocuments>();
public virtual DbSet<WellFinalDocument> WellFinalDocuments => Set<WellFinalDocument>();
// WITS
public DbSet<WITS.Record1> Record1 => Set<WITS.Record1>();
@ -343,6 +343,12 @@ namespace AsbCloudDb.Model
entity.HasKey(nameof(UserSetting.IdUser), nameof(UserSetting.Key));
});
modelBuilder.Entity<WellFinalDocument>(entity =>
{
entity.HasKey(x => new { x.IdWell, x.IdUser, x.IdCategory })
.HasName("t_well_final_documents_pk");
});
DefaultData.DefaultContextData.Fill(modelBuilder);
}

View File

@ -126,9 +126,9 @@
new (){ Id = 500, Name="FileCategory.delete", Description="Разрешение удалять Категорий документов файлов"},
new (){ Id = 501, Name="FileCategory.edit", Description="Разрешение редактировать Категорий документов файлов"},
new (){ Id = 502, Name="FileCategory.get", Description="Разрешение просматривать Категорий документов файлов"},
new (){ Id = 503, Name="WellFinalDocuments.delete", Description="Разрешение удалять Дело скважины"},
new (){ Id = 504, Name="WellFinalDocuments.edit", Description="Разрешение редактировать Дело скважины"},
new (){ Id = 505, Name="WellFinalDocuments.get", Description="Разрешение просматривать Дело скважины"},
new (){ Id = 503, Name="WellFinalDocument.delete", Description="Разрешение удалять Дело скважины"},
new (){ Id = 504, Name="WellFinalDocument.edit", Description="Разрешение редактировать Дело скважины"},
new (){ Id = 505, Name="WellFinalDocument.get", Description="Разрешение просматривать Дело скважины"},
};
}
}

View File

@ -46,7 +46,7 @@ namespace AsbCloudDb.Model
DbSet<Driller> Drillers { get; }
DbSet<Schedule> Schedule { get; }
DbSet<OperationValue> OperationValues { get; }
DbSet<WellFinalDocuments> WellFinalDocuments { get; }
DbSet<WellFinalDocument> WellFinalDocuments { get; }
DbSet<Record1> Record1 { get; }
DbSet<Record7> Record7 { get; }

View File

@ -6,12 +6,8 @@ namespace AsbCloudDb.Model
{
#nullable disable
[Table("t_well_final_documents"), Comment("Дело скважины")]
public class WellFinalDocuments : IId, IWellRelated
public class WellFinalDocument : IWellRelated
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("id_well")]
public int IdWell { get; set; }
@ -21,9 +17,6 @@ namespace AsbCloudDb.Model
[Column("id_category")]
public int IdCategory { get; set; }
[Column("id_file")]
public int? IdFile { get; set; }
[ForeignKey(nameof(IdWell))]
public virtual Well Well { get; set; }
@ -32,8 +25,5 @@ namespace AsbCloudDb.Model
[ForeignKey(nameof(IdCategory))]
public virtual FileCategory Category { get; set; }
[ForeignKey(nameof(IdFile))]
public virtual FileInfo File { get; set; }
}
}

View File

@ -72,7 +72,7 @@ namespace AsbCloudInfrastructure
.ForType<FileCategoryDto, FileCategory>();
TypeAdapterConfig.GlobalSettings.Default.Config
.ForType<WellFinalDocumentsDto, WellFinalDocuments>();
.ForType<WellFinalDocumentDto, WellFinalDocument>();
}
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)

View File

@ -363,5 +363,17 @@ namespace AsbCloudInfrastructure.Services
return result;
}
public async Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(int idWell, CancellationToken token)
{
var entities = await dbSetConfigured
.Where(e => e.IdWell == idWell && e.IsDeleted == false)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Select(e => Convert(e));
return dtos;
}
}
}

View File

@ -3,6 +3,7 @@ using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Repository;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
@ -14,10 +15,11 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
#nullable enable
/// <summary>
/// Сервис "Дело скважины"
/// </summary>
public class WellFinalDocumentsService : CrudServiceBase<WellFinalDocumentsDBDto, WellFinalDocuments>, IWellFinalDocumentsService
public class WellFinalDocumentsService : IWellFinalDocumentsService
{
private readonly IAsbCloudDbContext context;
private readonly IFileService fileService;
@ -26,7 +28,6 @@ namespace AsbCloudInfrastructure.Services
private readonly IConfiguration configuration;
private readonly IEmailService emailService;
private readonly IFileCategoryService fileCategoryService;
private const int ResultSaveCategoryFile = 0;
public WellFinalDocumentsService(IAsbCloudDbContext context,
IFileService fileService,
@ -35,7 +36,6 @@ namespace AsbCloudInfrastructure.Services
IConfiguration configuration,
IEmailService emailService,
IFileCategoryService fileCategoryService)
: base(context)
{
this.context = context;
this.fileService = fileService;
@ -46,38 +46,40 @@ namespace AsbCloudInfrastructure.Services
this.fileCategoryService = fileCategoryService;
}
public override async Task<int> InsertRangeAsync(IEnumerable<WellFinalDocumentsDBDto> dtos, CancellationToken token)
{
var data = await base.InsertRangeAsync(dtos, token);
if (data > 0)
public async Task<int> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto>? dtos, CancellationToken token)
{
if (dtos is not null)
{
var message = "от Вас ожидается загрузка на портал документа «{0}»";
await GenerateMessageAsync(dtos, message, token);
}
var entities = dtos
.Where(dto => dto.IdsPublishers?.Any() == true)
.SelectMany(dto => dto.IdsPublishers
.Select(idUser => new WellFinalDocument
{
IdCategory = dto.IdCategory,
IdWell = idWell,
IdUser = idUser
}));
return data;
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);
}
return data;
}
throw new ArgumentInvalidException("Данные по категориям отсутствуют.");
}
public override async Task<int> DeleteAsync(int dtoId, CancellationToken token)
public async Task<IEnumerable<WellFinalDocumentDto>> GetByWellId(int idWell, CancellationToken token)
{
var entity = await context.WellFinalDocuments
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == dtoId);
var dto = Convert(entity);
var data = await base.DeleteAsync(dtoId, token);
if (data > 0)
{
var message = "Вас удалили из ответственных за загрузку документа «{0}»";
await GenerateMessageAsync(new List<WellFinalDocumentsDBDto> { dto }, message, token);
}
return data;
}
public async Task<IEnumerable<WellFinalDocumentsDto>> GetByWellId(int idWell, CancellationToken token)
{
var result = new List<WellFinalDocumentsDto>();
var result = new List<WellFinalDocumentDto>();
var wells = await context.WellFinalDocuments.Where(x => x.IdWell == idWell)
.ToListAsync(token)
@ -95,11 +97,14 @@ namespace AsbCloudInfrastructure.Services
fc => fc.Id,
w => w.IdCategory,
(o, i) => new {
IdCategory = o.Id,
NameCategory = o.Name,
Wells = i
})
.ToList();
var wellFiles = await fileService.GetInfosByWellIdAsync(idWell, token).ConfigureAwait(false);
wellFinalDocs.ForEach(async item => {
var userIds = item.Wells
.Select(x => x.IdUser)
@ -107,23 +112,20 @@ namespace AsbCloudInfrastructure.Services
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 allFiles = wellFiles.Where(x => x.IdCategory == item.IdCategory);
FileInfoDto? actualFile = null;
if (allFiles.Any())
{
var actualFileId = fileIds.FirstOrDefault();
actualFile = fileService.GetInfoAsync(actualFileId ?? default(int), token).Result;
actualFile = allFiles.OrderByDescending(x => x.Id)
.FirstOrDefault();
}
result.Add(new WellFinalDocumentsDto {
result.Add(new WellFinalDocumentDto {
IdWell = idWell,
NameCategory = item.NameCategory,
Publishers = allUsers.Where(x => userIds.Contains(x.Id)),
FilesCount = fileIds.Count(x => x.HasValue),
FilesCount = allFiles.Count(),
File = actualFile
});
});
@ -132,7 +134,7 @@ namespace AsbCloudInfrastructure.Services
return result;
}
public async Task<IEnumerable<UserDto>> GetPublishersAsync(int idWell, CancellationToken token)
public async Task<IEnumerable<UserDto>> GetAvailableUsersAsync(int idWell, CancellationToken token)
{
var companyIds = await context.RelationCompaniesWells
.Where(x => x.IdWell == idWell).Select(x => x.IdCompany)
@ -150,13 +152,13 @@ namespace AsbCloudInfrastructure.Services
.ToList();
}
public async Task<int> SaveCategoryFile(int idDto, int idUser, Stream fileStream, string fileName, CancellationToken token)
public async Task<int> SaveCategoryFile(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token)
{
var entity = await context.WellFinalDocuments
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == idDto);
.FirstOrDefaultAsync(x => x.IdWell == idWell && x.IdCategory == idCategory && x.IdUser == idUser);
if (entity.IdUser != idUser)
if (entity is null)
throw new ArgumentInvalidException("Пользователь не является ответственным за загрузку файла для данной категории.");
var dto = Convert(entity);
@ -164,23 +166,12 @@ namespace AsbCloudInfrastructure.Services
var file = await fileService.SaveAsync(dto.IdWell, dto.IdUser, dto.IdCategory, fileName,
fileStream, token).ConfigureAwait(false);
if (file is not null)
{
dto.IdFile = file.Id;
return await base.UpdateAsync(dto, token);
}
else return ResultSaveCategoryFile;
return file?.Id ?? -1;
}
public async Task<WellFinalDocumentsHistoryDto> GetFilesHistoryByIdCategory(int idWell, int idCategory, CancellationToken token)
{
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);
var files = await fileService.GetInfosByCategoryAsync(idWell, idCategory, token).ConfigureAwait(false);
return new WellFinalDocumentsHistoryDto {
IdWell = idWell,
@ -189,15 +180,18 @@ namespace AsbCloudInfrastructure.Services
};
}
private async Task GenerateMessageAsync(IEnumerable<WellFinalDocumentsDBDto> dtos, string message, CancellationToken token)
private async Task GenerateMessageAsync(IEnumerable<WellFinalDocumentDBDto> dtos, string message, CancellationToken token)
{
foreach (var item in dtos)
{
var user = await userService.GetOrDefaultAsync(item.IdUser, token);
var category = await fileCategoryService.GetOrDefaultAsync(item.IdCategory, token);
var well = await wellService.GetOrDefaultAsync(item.IdWell, token);
if (user?.Email is not null)
{
var category = await fileCategoryService.GetOrDefaultAsync(item.IdCategory, token);
var well = await wellService.GetOrDefaultAsync(item.IdWell, token);
SendMessage(well, user, category.Name, message, token);
SendMessage(well, user, category.Name, message, token);
}
}
}
@ -209,14 +203,11 @@ namespace AsbCloudInfrastructure.Services
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
};
private WellFinalDocument Convert(WellFinalDocumentDBDto dto)
=> dto.Adapt<WellFinalDocument>();
private WellFinalDocumentDBDto Convert(WellFinalDocument entity)
=> entity.Adapt<WellFinalDocumentDBDto>();
}
#nullable disable
}

View File

@ -77,7 +77,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact]
public async Task GetListResponsibles_return_cnt_users()
{
var data = await service.GetPublishersAsync(90, CancellationToken.None);
var data = await service.GetAvailableUsersAsync(90, CancellationToken.None);
Assert.NotNull(data);
}
@ -92,7 +92,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
public async Task SaveCategoryFile_return_id_edit_record()
{
var stream = new FileStream("D:\\test\\test.txt", FileMode.Open);
var data = await service.SaveCategoryFile(21, 78, stream, "test.txt", CancellationToken.None);
var data = await service.SaveCategoryFile(21, 10018, 78, stream, "test.txt", CancellationToken.None);
Assert.Equal(21, data);
}
}

View File

@ -6,9 +6,11 @@ using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using AsbCloudInfrastructure.Services;
namespace AsbCloudWebApi.Controllers
{
#nullable enable
/// <summary>
/// Дело скважины
/// </summary>
@ -18,9 +20,11 @@ namespace AsbCloudWebApi.Controllers
public class WellFinalDocumentsController : ControllerBase
{
private readonly IWellFinalDocumentsService wellFinalDocumentsService;
public WellFinalDocumentsController(IWellFinalDocumentsService wellFinalDocumentsService)
private readonly IWellService wellService;
public WellFinalDocumentsController(IWellFinalDocumentsService wellFinalDocumentsService, IWellService wellService)
{
this.wellFinalDocumentsService = wellFinalDocumentsService;
this.wellService = wellService;
}
/// <summary>
@ -31,9 +35,13 @@ namespace AsbCloudWebApi.Controllers
/// <returns></returns>
[HttpGet]
[Permission]
[ProducesResponseType(typeof(IEnumerable<WellFinalDocumentsDto>), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(IEnumerable<WellFinalDocumentDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token = default)
{
var idCompany = User.GetCompanyId();
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync(idCompany ?? default, idWell, token).ConfigureAwait(false))
return Forbid();
var data = await this.wellFinalDocumentsService.GetByWellId(idWell, token);
return Ok(data);
}
@ -48,39 +56,29 @@ namespace AsbCloudWebApi.Controllers
[Permission]
[Route("publishers")]
[ProducesResponseType(typeof(IEnumerable<UserDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetPublishersAsync(int idWell, CancellationToken token = default)
public async Task<IActionResult> GetAvailableUsersAsync(int idWell, CancellationToken token = default)
{
var data = await this.wellFinalDocumentsService.GetPublishersAsync(idWell, token);
var idCompany = User.GetCompanyId();
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync(idCompany ?? default, idWell, token).ConfigureAwait(false))
return Forbid();
var data = await this.wellFinalDocumentsService.GetAvailableUsersAsync(idWell, token);
return Ok(data);
}
/// <summary>
/// Добавление записи
/// </summary>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPut]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> InsertRangeAsync(IEnumerable<WellFinalDocumentsDBDto> dtos, CancellationToken token = default)
public async Task<IActionResult> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto> dtos, CancellationToken token = default)
{
var data = await this.wellFinalDocumentsService.InsertRangeAsync(dtos, token);
return Ok(data);
}
/// <summary>
/// Удалить запись
/// </summary>
/// <param name="iDdto"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpDelete]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> DeleteAsync(int iDdto, CancellationToken token = default)
{
var data = await this.wellFinalDocumentsService.DeleteAsync(iDdto, token);
var data = await this.wellFinalDocumentsService.UpdateRangeAsync(idWell, dtos, token);
return Ok(data);
}
@ -99,6 +97,10 @@ namespace AsbCloudWebApi.Controllers
int idCategory,
CancellationToken token = default)
{
var idCompany = User.GetCompanyId();
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync(idCompany ?? default, idWell, token).ConfigureAwait(false))
return Forbid();
var data = await this.wellFinalDocumentsService.GetFilesHistoryByIdCategory(idWell, idCategory, token);
return Ok(data);
}
@ -106,20 +108,21 @@ namespace AsbCloudWebApi.Controllers
/// <summary>
/// Сохранение файла
/// </summary>
/// <param name="idDto"></param>
/// <param name="idUser"></param>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="file"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> SaveCategoryFile(int idDto, int idUser,
IFormFile file, CancellationToken token = default)
public async Task<IActionResult> SaveCategoryFile(int idWell, int idCategory, IFormFile file, CancellationToken token = default)
{
var idUser = User.GetUserId() ?? -1;
var fileStream = file.OpenReadStream();
var data = await this.wellFinalDocumentsService.SaveCategoryFile(idDto, idUser, fileStream, file.FileName, token);
var data = await this.wellFinalDocumentsService.SaveCategoryFile(idWell, idCategory, idUser, fileStream, file.FileName, token);
return Ok(data);
}
}
#nullable disable
}