diff --git a/Src/Dlhis.Common/SysData/Controller/SysDataController.cs b/Src/Dlhis.Common/SysData/Controller/SysDataController.cs new file mode 100644 index 00000000..a7a7f895 --- /dev/null +++ b/Src/Dlhis.Common/SysData/Controller/SysDataController.cs @@ -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> DownloadAsync(string factory, string tableName) + { + return await _sysDataService.DownloadAsync(factory, tableName); + } +} \ No newline at end of file diff --git a/Src/Dlhis.Common/SysData/Services/ISysDataService.cs b/Src/Dlhis.Common/SysData/Services/ISysDataService.cs new file mode 100644 index 00000000..422e7136 --- /dev/null +++ b/Src/Dlhis.Common/SysData/Services/ISysDataService.cs @@ -0,0 +1,6 @@ +namespace Dlhis.Common.SysData.Services; + +public interface ISysDataService : IScopeDependency +{ + Task> DownloadAsync(string factory, string tableName); +} \ No newline at end of file diff --git a/Src/Dlhis.Common/SysData/Services/Implements/SysDataService.cs b/Src/Dlhis.Common/SysData/Services/Implements/SysDataService.cs new file mode 100644 index 00000000..acac4114 --- /dev/null +++ b/Src/Dlhis.Common/SysData/Services/Implements/SysDataService.cs @@ -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; + } + + /// + public async Task> 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()!.TableName == tableName); + + var table = type.GetCustomAttribute()!.TableName; + var sql = $"select * from {table}"; + if (type.IsAssignableTo(typeof(ITenantId))) + { + sql += $" where tenantId = '{tableName}'"; + } + + return await _dbContext.Client.SqlQueryable(sql).ToListAsync(); + } +} \ No newline at end of file