2021-08-09 15:41:42 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2021-08-13 17:26:19 +05:00
|
|
|
|
using Mapster;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using System.IO;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using System.Linq;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public class FileService : IFileService
|
|
|
|
|
{
|
|
|
|
|
public string RootPath { get; private set; }
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
|
2021-08-09 15:41:42 +05:00
|
|
|
|
public FileService(IAsbCloudDbContext db)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
RootPath = "files";
|
|
|
|
|
this.db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 17:32:22 +05:00
|
|
|
|
public IDictionary<string, int> SaveFileInfos(int idWell, int idUser,
|
|
|
|
|
IEnumerable<FileInfoDto> filesInfo)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
var fileIdsToNames = new Dictionary<string, int>();
|
|
|
|
|
|
2021-08-09 15:41:42 +05:00
|
|
|
|
foreach (var fileInfo in filesInfo)
|
|
|
|
|
{
|
2021-08-19 16:32:04 +05:00
|
|
|
|
var file = new AsbCloudDb.Model.FileInfo()
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-19 17:32:22 +05:00
|
|
|
|
Name = fileInfo.Name,
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdCategory = fileInfo.IdCategory,
|
|
|
|
|
UploadDate = fileInfo.UploadDate,
|
|
|
|
|
IdAuthor = idUser
|
2021-07-23 17:40:31 +05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
db.Files.Add(file);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
fileIdsToNames.Add(file.Name, file.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileIdsToNames;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task SaveFile(int idWell, int idCategory, int fileId,
|
2021-08-19 16:32:04 +05:00
|
|
|
|
string fileExtension, Stream fileStream)
|
|
|
|
|
{
|
|
|
|
|
var relativePath = Path.Combine(RootPath, $"{idWell}",
|
|
|
|
|
$"{idCategory}", $"{fileId}" + $"{fileExtension}");
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(relativePath));
|
|
|
|
|
|
|
|
|
|
using (var newfileStream = new FileStream(relativePath, FileMode.Create))
|
|
|
|
|
{
|
|
|
|
|
await fileStream.CopyToAsync(newfileStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
public async Task<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
|
2021-08-24 10:59:10 +05:00
|
|
|
|
int idCategory, IEnumerable<int> companies = default, DateTime begin = default,
|
2021-08-20 11:20:24 +05:00
|
|
|
|
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var query = db.Files
|
|
|
|
|
.Include(f => f.Author)
|
|
|
|
|
.Where(e => e.IdWell == idWell &&
|
|
|
|
|
e.IdCategory == idCategory);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
query = query.Where(e => !e.IsDeleted);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-20 11:20:24 +05:00
|
|
|
|
if (companies.Any())
|
|
|
|
|
query.Include(file => file.Author).ThenInclude(a => a.Company)
|
|
|
|
|
.Where(e => companies.Contains(e.Author.Company.Id));
|
|
|
|
|
|
2021-07-26 11:54:50 +05:00
|
|
|
|
if (begin != default)
|
2021-08-13 17:26:19 +05:00
|
|
|
|
query = query.Where(e => e.UploadDate >= begin);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
|
|
|
|
if (end != default)
|
2021-08-13 17:26:19 +05:00
|
|
|
|
query = query.Where(e => e.UploadDate <= end);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var count = await query.CountAsync(token).ConfigureAwait(false);
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
var result = new PaginationContainer<FileInfoDto>(count)
|
|
|
|
|
{
|
|
|
|
|
Skip = skip,
|
2021-08-13 17:26:19 +05:00
|
|
|
|
Take = take,
|
|
|
|
|
Count = count,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (count <= skip)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
query = query.OrderBy(e => e.UploadDate);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
|
|
|
|
if (skip > 0)
|
2021-08-13 17:26:19 +05:00
|
|
|
|
query = query.Skip(skip);
|
|
|
|
|
query = query.Take(take);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var entities = await query
|
|
|
|
|
.Take(take).AsNoTracking().ToListAsync(token)
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.ConfigureAwait(false);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
foreach (var item in entities)
|
2021-07-26 11:54:50 +05:00
|
|
|
|
{
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var filePropertiesDto = item.Adapt<FileInfoDto>();
|
2021-08-20 13:56:20 +05:00
|
|
|
|
filePropertiesDto.AuthorName = item.Author?.Name;
|
|
|
|
|
filePropertiesDto.CompanyId = item.Author?.IdCompany ?? 0;
|
2021-08-13 17:26:19 +05:00
|
|
|
|
result.Items.Add(filePropertiesDto);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
public async Task<FileInfoDto> GetFileInfoAsync(int fileId,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
CancellationToken token = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var entity = await db.Files
|
|
|
|
|
.Include(f => f.Author)
|
|
|
|
|
.AsNoTracking()
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.FirstOrDefaultAsync(f => f.Id == fileId, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
if (entity is null)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var dto = entity.Adapt<FileInfoDto>();
|
|
|
|
|
dto.AuthorName = entity.Author.Name;
|
|
|
|
|
return dto;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<int> DeleteFileAsync(int idFile,
|
2021-08-19 16:58:26 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
var fileInfo = db.Files.FirstOrDefault(f => f.Id == idFile);
|
|
|
|
|
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fileInfo.IsDeleted = true;
|
|
|
|
|
|
|
|
|
|
return await db.SaveChangesAsync(token);
|
|
|
|
|
}
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
}
|