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; namespace GDZZ.Application { /// /// 附件管理服务 /// [ApiDescriptionSettings("Application",Name = "Annex", Order = 1)] public class AnnexService : IAnnexService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly UploadFileOptions _options; public AnnexService(SqlSugarRepository rep, IOptions options) { this._options = options.Value; _rep = rep; } /// /// 分页查询附件管理 /// /// /// [HttpGet("/Annex/page")] public async Task Page([FromQuery] AnnexInput input) { var entities = await _rep.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(input.FileName), u => u.FileName.Contains(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(); } /// /// 下载附件 /// /// /// [HttpGet("/Annex/download")] public async Task DownloadFileInfo([FromQuery] AnnexInput input) { var annexfile = await this._rep.FirstOrDefaultAsync(x => x.Id == input.Id); if (annexfile == null) throw Oops.Oh("文件为空,下载失败"); var filePath = App.WebHostEnvironment.WebRootPath+annexfile.FileUrl; 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; } /// /// 增加附件管理 /// /// /// /// [HttpPost("/Mini/Annex/add")] public async Task Add(IFormFile file,string FileName) { var entity = await this._rep.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("文件上传失败!"); if (entity.IsEmpty()) { entity = new Annex(); entity.FileName = FileName; entity.FileSize = file.Length.ToString(); entity.FileUrl = fileInfo.FileUrl; await _rep.InsertAsync(entity); } else { entity.FileName = FileName; entity.FileSize = file.Length.ToString(); entity.FileUrl = fileInfo.FileUrl; await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } return entity; } /// /// 获取附件 /// /// /// [HttpGet("/Mini/Annex/GetAnnex")] public async Task GetAnnex() { return await _rep.Where(x=>x.CreatedUserId == UserManager.UserId).FirstAsync(); } /// /// 删除附件管理 /// /// /// [HttpPost("/Annex/delete")] public async Task Delete(DeleteAnnexInput input) { var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id); await _rep.DeleteAsync(entity); } /// /// 更新附件管理 /// /// /// [HttpPost("/Annex/edit")] public async Task Update(UpdateAnnexInput input) { var entity = input.Adapt(); await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommandAsync(); } /// /// 获取附件管理 /// /// /// [HttpGet("/Annex/Detail")] public async Task Get([FromQuery] QueryeAnnexInput input) { return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id); } /// /// 获取附件管理列表 /// /// /// [HttpGet("/Annex/list")] public async Task List([FromQuery] AnnexInput input) { return await _rep.ToListAsync(); } } }