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.

163 lines
5.5 KiB

1 year ago
using GDZZ.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using GDZZ.Application.Entity;
using GDZZ.Application.Help;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
using Furion;
using GDZZ.Core.Service;
using System.IO;
using System.Text;
using System.Web;
using Furion.FriendlyException;
1 year ago
namespace GDZZ.Application
{
/// <summary>
/// 附件管理服务
/// </summary>
[ApiDescriptionSettings("Application",Name = "Annex", Order = 1)]
public class AnnexService : IAnnexService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<Annex> _rep;
private readonly UploadFileOptions _options;
public AnnexService(SqlSugarRepository<Annex> rep, IOptions<UploadFileOptions> options)
{
this._options = options.Value;
_rep = rep;
}
/// <summary>
/// 分页查询附件管理
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/Annex/page")]
public async Task<dynamic> Page([FromQuery] AnnexInput input)
{
var entities = await _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.FileName), u => u.FileName == input.FileName)
.WhereIF(!string.IsNullOrWhiteSpace(input.FileSize), u => u.FileSize == input.FileSize)
.WhereIF(!string.IsNullOrWhiteSpace(input.FileUrl), u => u.FileUrl == input.FileUrl)
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
/// <summary>
/// 下载附件
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/Annex/download")]
public async Task<IActionResult> DownloadFileInfo([FromQuery] AnnexInput input)
{
var annexfile = await this._rep.FirstOrDefaultAsync(x => x.Id == input.Id);
if (annexfile == null)
throw Oops.Oh("文件为空,下载失败");
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, annexfile.FileUrl, annexfile.Id+".pdf");
var fileName = HttpUtility.UrlEncode(annexfile.FileName, Encoding.GetEncoding("UTF-8"));
var result = new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };
return result;
}
1 year ago
/// <summary>
/// 增加附件管理
/// </summary>
/// <param name="file"></param>
/// <param name="FileName"></param>
/// <returns></returns>
[HttpPost("/Mini/Annex/add")]
public async Task<Annex> Add(IFormFile file,string FileName)
{
var entity = await this._rep.AsQueryable().Where(x => x.CreatedUserId == UserManager.UserId).FirstAsync();
var fileInfo =await Utils.UploadFile(file, this._options.Default.path, FileLocation.LOCAL);
if (fileInfo == null)
throw new Exception("文件上传失败!");
entity.FileName = FileName;
entity.FileSize = file.Length.ToString();
entity.FileUrl = fileInfo.FileUrl;
//查询
if(entity != null )
{
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
else
{
await _rep.InsertAsync(entity);
}
return entity;
}
/// <summary>
/// 获取附件
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/Mini/Annex/GetAnnex")]
public async Task<dynamic> GetAnnex()
{
return await _rep.Where(x=>x.CreatedUserId == UserManager.UserId).FirstAsync();
}
/// <summary>
/// 删除附件管理
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/Annex/delete")]
public async Task Delete(DeleteAnnexInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
/// <summary>
/// 更新附件管理
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/Annex/edit")]
public async Task Update(UpdateAnnexInput input)
{
var entity = input.Adapt<Annex>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommandAsync();
}
/// <summary>
/// 获取附件管理
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/Annex/detail")]
public async Task<Annex> Get([FromQuery] QueryeAnnexInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
/// <summary>
/// 获取附件管理列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/Annex/list")]
public async Task<dynamic> List([FromQuery] AnnexInput input)
{
return await _rep.ToListAsync();
}
}
}