feat 提供数据下载功能

This commit is contained in:
2024-06-26 11:09:11 +08:00
parent d1b7dfae1e
commit fb797908e0
3 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
using Dlhis.Common.SysData.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Dlhis.Common.SysData.Controller;
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class SysDataController : ControllerBase
{
private readonly ISysDataService _sysDataService;
public SysDataController(ISysDataService sysDataService)
{
_sysDataService = sysDataService;
}
[HttpGet("download")]
public async Task<List<object>> DownloadAsync(string factory, string tableName)
{
return await _sysDataService.DownloadAsync(factory, tableName);
}
}

View File

@@ -0,0 +1,6 @@
namespace Dlhis.Common.SysData.Services;
public interface ISysDataService : IScopeDependency
{
Task<List<object>> DownloadAsync(string factory, string tableName);
}

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using Kurisu.AspNetCore;
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using SqlSugar;
namespace Dlhis.Common.SysData.Services.Implements;
public class SysDataService : ISysDataService
{
private readonly IDbContext _dbContext;
public SysDataService(IDbContext dbContext)
{
_dbContext = dbContext;
}
/// <inheritdoc />
public async Task<List<object>> DownloadAsync(string factory, string tableName)
{
var type = App.ActiveTypes
.Where(x => x.IsDefined(typeof(SugarTable), false))
.Where(x => !x.IsDefined(typeof(SkipScanAttribute), false))
.FirstOrDefault(x => x.GetCustomAttribute<SugarTable>()!.TableName == tableName);
var table = type.GetCustomAttribute<SugarTable>()!.TableName;
var sql = $"select * from {table}";
if (type.IsAssignableTo(typeof(ITenantId)))
{
sql += $" where tenantId = '{tableName}'";
}
return await _dbContext.Client.SqlQueryable<object>(sql).ToListAsync();
}
}