You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.3 KiB

3 weeks ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wood.Entity.SystemManage;
using Wood.Entity;
using Wood.Data.Repository;
using Wood.Service.SystemManage.Dto;
using Mapster;
using Wood.Util;
using NetTaste;
namespace Wood.Service.SystemManage.Manager
{
public class FileManager : ApiManager<FileEntity>, ITransient
{
public FileManager(SqlSugarRepository<FileEntity> repository) : base(repository)
{
}
/// <summary>
/// 把指定的批次code的附件进行更新
/// 针对一对多附件时使用
/// </summary>
/// <param name="code">code,为空会生成新的code</param>
/// <param name="newFiles">新附件id列表,空时为清空批次文件</param>
/// <returns></returns>
public async Task<string> UpdateFile(string? code, List<long>? newFiles)
{
if (string.IsNullOrEmpty(code))
return await AddFile(newFiles);
else
{
//更新附件 置为不可用
//把批次 code 不在 newFiles 中的附件标记为删除
await AsRepository().AsUpdateable()
.SetColumns(it => new FileEntity() { IsDelete = true })
.Where(it => it.PackageCode == code)
.WhereIF(newFiles != null && newFiles.Any(), it => !newFiles!.Contains(it.Id))
.ExecuteCommandAsync();
if (newFiles != null && newFiles.Count > 0)
//更新附件 置为可用
//把批次在 newFiles 中的附件标记为可用 并把 批次更新为 code
await AsRepository().AsUpdateable()
.SetColumns(it => new FileEntity() { PackageCode = code, IsDelete = false })
.Where(it => newFiles.Contains(it.Id)).ExecuteCommandAsync();
return code;
}
}
/// <summary>
/// 把指定的批次code的附件进行更新
/// 针对一对一附件时使用
/// </summary>
/// <param name="code">code,为空会生成新的code</param>
/// <param name="newFile">新文件,0时为清空批次文件</param>
/// <returns></returns>
public async Task<string> UpdateFile(string? code, long? newFile)
{
if (string.IsNullOrEmpty(code))
return await AddFile(newFile);
else
{
//更新附件 置为不可用
//把批次 code 不在 newFiles 中的附件标记为删除
await AsRepository().AsUpdateable()
.SetColumns(it => new FileEntity() { IsDelete = true })
.Where(it => it.PackageCode == code)
.ExecuteCommandAsync();
if (newFile > 0)
//更新附件 置为可用
//把批次在 newFiles 中的附件标记为可用 并把 批次更新为 code
await AsRepository().AsUpdateable()
.SetColumns(it => new FileEntity() { PackageCode = code, IsDelete = false })
.Where(it => it.Id == newFile).ExecuteCommandAsync();
return code;
}
}
/// <summary>
/// 把附件加到指定的批次code中
/// 针对一对多附件时使用
/// </summary>
/// <param name="files">要添加的附件id列表</param>
/// <returns>返回一个批次编码</returns>
public async Task<string> AddFile(List<long>? files)
{
if (files != null && files.Count > 0)
{
string code = IdGeneratorHelper.Instance.GetGuid("N");
//更新附件
await AsRepository().AsUpdateable()
.SetColumns(it => new FileEntity() { PackageCode = code, IsDelete = false })
.Where(it => files.Contains(it.Id)).ExecuteCommandAsync();
return code;
}
else
return string.Empty;
}
/// <summary>
/// 把附件加到指定的批次code中
/// 针对一对一附件时使用
/// </summary>
/// <param name="file">要添加的附件</param>
/// <returns>返回一个批次编码</returns>
public async Task<string> AddFile(long? file)
{
if (file != null && file > 0)
{
string code = IdGeneratorHelper.Instance.GetGuid("N");
//更新附件
await AsRepository().AsUpdateable()
.SetColumns(it => new FileEntity() { PackageCode = code, IsDelete = false })
.Where(it => it.Id == file).ExecuteCommandAsync();
return code;
}
else
return string.Empty;
}
/// <summary>
/// 根据批次code获取文件
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public async Task<List<FileDto>> GetByCode(string? code)
{
if (string.IsNullOrEmpty(code))
return new List<FileDto>();
var files = await AsRepository().GetListAsync(it => it.PackageCode == code);
var dtos = files.Adapt<List<FileDto>>();
return dtos;
}
/// <summary>
/// 根据批次code获取文件
/// </summary>
/// <param name="codes"></param>
/// <returns></returns>
public async Task<List<FileDto>> GetByCode(List<string>? codes)
{
if (codes == null || !codes.Any())
return new List<FileDto>();
var files = await AsRepository().GetListAsync(it => codes.Contains(it.PackageCode!));
var dtos = files.Adapt<List<FileDto>>();
return dtos;
}
/// <summary>
/// 返回批次code文件的所有路径
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public async Task<List<string>> GetFilePathsByCode(string? code)
{
if (string.IsNullOrEmpty(code))
return new List<string>();
List<string> result = new List<string>();
var files = await GetByCode(code);
foreach (var item in files)
{
if (item.BucketName == "local")
result.Add("/" + item.FilePath!.Trim('/'));
else
result.Add("/fromBucket/" + item.BucketName + "/" + item.FilePath!.Trim('/'));
}
return result;
}
}
}