Files
jinko.yikatong/Src/Dlhis.Basic/Common/SysData/Services/Implements/SysDataService.cs
2024-11-07 13:52:40 +08:00

76 lines
2.6 KiB
C#

using System.Reflection;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Enums;
using Kurisu.AspNetCore;
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using SqlSugar;
namespace Dlhis.Basic.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 == null)
{
table = tableName;
}
else
{
if (type.IsAssignableTo(typeof(ITenantId)))
{
sql += $" where tenantId = '{factory}'";
}
}
switch (table)
{
case "Gainfo":
return await _dbContext.Queryable<GainfoEntity>().Where(x => x.Factory == factory).Select(x => (object)x).ToListAsync();
case "GainfoMeal":
{
var r = await _dbContext.Queryable<GainfoEntity>()
.LeftJoin<BalanceRecordEntity>((x, b) => x.Code == b.UserCode)
.Where(x => x.Factory == factory)
.Where((x, b) => b.Source != BalanceRecordSourceEnumType.EMealVoucher && b.Status == BalanceRecordStatusEnumType.Valid && b.IsDeleted == false)
.GroupBy(x => x.Code)
.Select((x, b) => new
{
UserCode = x.Code,
Balance = SqlFunc.AggregateSum(b.Amount)
})
.ToListAsync();
return r.Select(x => (object)x).ToList();
}
default:
return await _dbContext.Client.SqlQueryable<object>(sql).ToListAsync();
}
}
public async Task<List<string>> GetWalletFactoriesAsync(string factory)
{
var wallets = await _dbContext.Queryable<WalletEntity>().ToListAsync();
var code = wallets.First(x => x.Factory == factory).Code;
return wallets.Where(x => x.Code == code).Select(x => x.Factory).ToList();
}
}