diff --git a/Src/Dlhis.Basic/Common/SysData/Services/Implements/SysDataService.cs b/Src/Dlhis.Basic/Common/SysData/Services/Implements/SysDataService.cs index f413900e..1650fa30 100644 --- a/Src/Dlhis.Basic/Common/SysData/Services/Implements/SysDataService.cs +++ b/Src/Dlhis.Basic/Common/SysData/Services/Implements/SysDataService.cs @@ -24,35 +24,42 @@ public class SysDataService : ISysDataService var type = App.ActiveTypes .Where(x => x.IsDefined(typeof(SugarTable), false)) .Where(x => !x.IsDefined(typeof(SkipScanAttribute), false)) - .First(x => x.GetCustomAttribute()!.TableName == tableName); + .FirstOrDefault(x => x.GetCustomAttribute()!.TableName == tableName); - var table = type.GetCustomAttribute()!.TableName; + var table = type?.GetCustomAttribute()!.TableName; var sql = $"select * from {table}"; - if (type.IsAssignableTo(typeof(ITenantId))) + + if (type == null) { - sql += $" where tenantId = '{factory}'"; + table = tableName; + } + else + { + if (type.IsAssignableTo(typeof(ITenantId))) + { + sql += $" where tenantId = '{factory}'"; + } } - switch (tableName) + switch (table) { case "Gainfo": return await _dbContext.Queryable().Where(x => x.Factory == factory).Select(x => (object)x).ToListAsync(); case "GainfoMeal": { - var factories = await GetWalletFactoriesAsync(factory); + var r = await _dbContext.Queryable() + .LeftJoin((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(); - var codes = await _dbContext.Queryable().Where(x => x.Factory == factory).Select(x => x.Code).ToListAsync(); - // return await _dbContext.Queryable() - // .Where(x => codes.Contains(x.UserCode) && factories.Contains(x.TenantId)) - // .Where(x => x.Source != BalanceRecordSourceEnumType.EMealVoucher) - // .GroupBy(x => x.UserCode) - // .Select(x => new - // { - // x.UserCode, - // Balance = SqlFunc.AggregateSum(x.Amount) - // }) - // .ToListAsync(); - return null; + return r.Select(x => (object)x).ToList(); } default: return await _dbContext.Client.SqlQueryable(sql).ToListAsync();