refactor: ♻️ 抽取公共导出方法 (#152)

[skip ci]

Co-authored-by: tk <fiyne1a@dingtalk.com>
This commit is contained in:
2024-07-04 09:29:18 +08:00
committed by GitHub
parent 8b01112f42
commit 67eaa5b783
14 changed files with 77 additions and 343 deletions

View File

@@ -1,5 +1,3 @@
using CsvHelper;
using Microsoft.Net.Http.Headers;
using NetAdmin.Application.Repositories;
using NetAdmin.Application.Services;
using NetAdmin.Domain.Dto.Dependency;
@@ -88,36 +86,10 @@ public sealed class RoleService(BasicRepository<Sys_Role, long> rpo) //
}
/// <inheritdoc />
public async Task<IActionResult> ExportAsync(QueryReq<QueryRoleReq> req)
public Task<IActionResult> ExportAsync(QueryReq<QueryRoleReq> req)
{
req.ThrowIfInvalid();
var data = await QueryInternal(req)
#if DBTYPE_SQLSERVER
.WithLock(SqlServerLock.NoLock | SqlServerLock.NoWait)
#endif
.Take(Numbers.MAX_LIMIT_EXPORT)
.ToListAsync()
.ConfigureAwait(false);
var list = data.Adapt<List<ExportRoleRsp>>();
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.WriteHeader<ExportRoleRsp>();
await csv.NextRecordAsync().ConfigureAwait(false);
foreach (var item in list) {
csv.WriteRecord(item);
await csv.NextRecordAsync().ConfigureAwait(false);
}
await csv.FlushAsync().ConfigureAwait(false);
_ = stream.Seek(0, SeekOrigin.Begin);
App.HttpContext.Response.Headers.ContentDisposition
= new ContentDispositionHeaderValue(Chars.FLG_HTTP_HEADER_VALUE_ATTACHMENT) {
FileNameStar = $"{Ln.角色导出}_{DateTime.Now:yyyy.MM.dd-HH.mm.ss}.csv"
}.ToString();
return new FileStreamResult(stream, Chars.FLG_HTTP_HEADER_VALUE_APPLICATION_OCTET_STREAM);
return ExportAsync<QueryRoleReq, ExportRoleRsp>(QueryInternal, req, Ln.);
}
/// <inheritdoc />