diff --git a/Admin.NET/Admin.NET.Core/Utils/AggregationBuilder.cs b/Admin.NET/Admin.NET.Core/Utils/AggregationBuilder.cs index 235346ac..87bfda5f 100644 --- a/Admin.NET/Admin.NET.Core/Utils/AggregationBuilder.cs +++ b/Admin.NET/Admin.NET.Core/Utils/AggregationBuilder.cs @@ -91,7 +91,7 @@ public class AggregationBuilder /// /// 验证字段有效性 /// - public static List ValidateFields(string[] fields, Type targetType) + public static List ValidateFields(string[] fields, Type targetType) { if (fields == null || fields.Length == 0) return new List(); @@ -99,7 +99,7 @@ public class AggregationBuilder .Where(f => !string.IsNullOrWhiteSpace(f)) .Select(f => f.Trim()) .Where(f => targetType.GetProperty( - targetType == typeof(UsagetokenOutput) ? + targetType == typeof(T) ? $"{f}Sum" : f) != null) .ToList(); } diff --git a/Admin.NET/Admin.NET.Web.Entry/wwwroot/template/service_Service.cs.vm b/Admin.NET/Admin.NET.Web.Entry/wwwroot/template/service_Service.cs.vm index 22a284d8..e0eb56c2 100644 --- a/Admin.NET/Admin.NET.Web.Entry/wwwroot/template/service_Service.cs.vm +++ b/Admin.NET/Admin.NET.Web.Entry/wwwroot/template/service_Service.cs.vm @@ -36,18 +36,28 @@ namespace @Model.NameSpace; [ApiDescriptionSettings(@(@Model.ProjectLastName)Const.GroupName, Name = "@(@Model.LowerClassName)", Order = 100)] public partial class @(@Model.ClassName)Service : IDynamicApiController, ITransient { + private readonly SysCacheService _sysCacheService;//默认CacheService + private readonly UserManager _userManager;//默认用户管理 + private readonly IEventPublisher _eventPublisher;//默认事件总线 private readonly SqlSugarRepository<@(@Model.ClassName)> _@(@Model.LowerClassName)Rep; @foreach (var column in Model.TableField){ if(@column.EffectType == "ForeignKey"||@column.EffectType == "ApiTreeSelector"){ + if(@column.FkEntityName != @Model.ClassName){ @:private readonly SqlSugarRepository<@(@column.FkEntityName)> _@(@column.LowerFkEntityName)Rep; + } } } private TypeAdapterConfig _typeAdapterConfig = TypeAdapterConfig.GlobalSettings; public @(@Model.ClassName)Service(SqlSugarRepository<@(@Model.ClassName)> @(@Model.LowerClassName)Rep + ,SysCacheService sysCacheService + , UserManager userManager + ,IEventPublisher eventPublisher @foreach (var column in Model.TableField){ if(@column.EffectType == "ForeignKey"||@column.EffectType == "ApiTreeSelector"){ + if(@column.FkEntityName != @Model.ClassName){ @:,SqlSugarRepository<@(@column.FkEntityName)> @(@column.LowerFkEntityName)Rep + } } } @@ -56,10 +66,15 @@ public partial class @(@Model.ClassName)Service : IDynamicApiController, ITransi _@(@Model.LowerClassName)Rep = @(@Model.LowerClassName)Rep; @foreach (var column in Model.TableField){ if(@column.EffectType == "ForeignKey"||@column.EffectType == "ApiTreeSelector"){ + if(@column.FkEntityName != @Model.ClassName){ @:_@(@column.LowerFkEntityName)Rep = @(@column.LowerFkEntityName)Rep; + } } } _typeAdapterConfig.ForType().IgnoreNullValues(true); + _sysCacheService = sysCacheService; + _eventPublisher = eventPublisher; + _userManager = userManager; } /// @@ -168,28 +183,28 @@ if (@column.ColumnKey == "True"){ return await @(@Model.ClassName)Mid.GetQuery(_@(@Model.LowerClassName)Rep, input).OrderBuilder(input).ToListAsync(); } -@if(@Model.TableField.Any(x=>x.Statistical == "Y")){ - @:/// - @:/// 获取@(@Model.BusName) - @:/// - @:/// - @:/// - @:[ApiDescriptionSettings(Name = "GetTotalSum", Description = "获取@(@Model.BusName)统计", Order = 960), HttpPost] - @:[DisplayName("获取@(@Model.BusName)统计")] - @:public async Task> GetTotalSum(Page@(@Model.ClassName)Input input) - @:{ - @:// 单次查询同时获取统计值 - @:var querystats = @(@Model.ClassName)Mid.GetQuery(_@(@Model.LowerClassName)Rep, input) + /// + /// 获取@(@Model.BusName) + /// + /// + /// + [ApiDescriptionSettings(Name = "GetTotalSum", Description = "获取@(@Model.BusName)统计", Order = 960), HttpPost] + [DisplayName("获取@(@Model.BusName)统计")] + public async Task> GetTotalSum(Page@(@Model.ClassName)Input input) + { + // 单次查询同时获取统计值 + var querystats = @(@Model.ClassName)Mid.GetQuery(_@(@Model.LowerClassName)Rep, input) + @if(@Model.TableField.Any(x=>x.IsGroupBy == "Y")){ @foreach (var column in Model.TableField){ if (@column.IsGroupBy == "Y"){ @: .GroupByIF(input.GroupBy.Contains("@column.PropertyName"), u => u.@column.PropertyName) } } @: //.Having(it => SqlFunc.AggregateCount(it.Id) > 0)//聚合函数过滤 - @: .Select(it => new @(@Model.ClassName)Output - @: { - @: count = SqlFunc.AggregateCount(it.Id), - + } + .Select(it => new @(@Model.ClassName)Output + { + count = SqlFunc.AggregateCount(it.Id), @foreach (var column in Model.TableField){ if (@column.IsGroupBy == "Y"){ @: @(@column.PropertyName) = it.@(@column.PropertyName), @@ -198,10 +213,9 @@ if (@column.ColumnKey == "True"){ @: @(@column.PropertyName) = SqlFunc.AggregateSum(it.@(@column.PropertyName)) ?? 0, } } - @: }); - @:return await querystats.ToListAsync(); - @:} -} + }); + return await querystats.ToListAsync(); + } /// /// 根据输入参数获取@(@Model.BusName)统计 @@ -220,7 +234,7 @@ if (@column.ColumnKey == "True"){ public async Task> GetAggregTotalSum(Page@(@Model.ClassName)Input input) { - @:var query = @(@Model.ClassName)Mid.GetQuery(_@(@Model.LowerClassName)Rep, input) + var query = @(@Model.ClassName)Mid.GetQuery(_@(@Model.LowerClassName)Rep, input) // 输入参数示例 //input = new Page@(@Model.ClassName)Input //{ @@ -253,7 +267,7 @@ if (@column.ColumnKey == "True"){ // HAVING(SUM(cost) > 10000) AND(AVG(CAST(response_time AS FLOAT)) < 500) // 处理分组字段 - var groupFields = AggregationBuilder.ValidateFields(input.GroupBy, typeof(@(@Model.ClassName)Output)); + var groupFields = AggregationBuilder.ValidateFields<@(@Model.ClassName)Output>(input.GroupBy, typeof(@(@Model.ClassName)Output)); if (groupFields.Count > 0) { query = query.GroupBy(string.Join(",", groupFields));