Merge pull request '修改PartialEntity_Entity模板不选统计字段不增加输入参数' (#286) from aq982 into v2

Reviewed-on: https://code.adminnet.top/Admin.NET/Admin.NET.Pro/pulls/286
This commit is contained in:
zuohuaijun 2025-03-05 10:43:25 +08:00
commit a6105cb002
2 changed files with 35 additions and 27 deletions

View File

@ -41,8 +41,10 @@ public partial class @(@Model.ClassName)Output
/// </summary> /// </summary>
public partial class Page@(@Model.ClassName)Input public partial class Page@(@Model.ClassName)Input
{ {
public string[] GroupBy { get; set; } @if(@Model.TableField.Any(x=>x.Statistical == "Y")){
public string[] Sum { get; set; } @:public string[] GroupBy { get; set; }
public IEnumerable<AggregationConfig> Aggregations { get; set; } @:public string[] Sum { get; set; }
@:public IEnumerable<AggregationConfig> Aggregations { get; set; }
}
} }

View File

@ -264,33 +264,39 @@ if (@column.ColumnKey == "True"){
// FROM... // FROM...
// GROUP BY department, project // GROUP BY department, project
// HAVING(SUM(cost) > 10000) AND(AVG(CAST(response_time AS FLOAT)) < 500) // HAVING(SUM(cost) > 10000) AND(AVG(CAST(response_time AS FLOAT)) < 500)
@if(@Model.TableField.Any(x=>x.Statistical == "Y")){
// 处理分组字段 @:// 处理分组字段
var groupFields = AggregationBuilder.ValidateFields<@(@Model.ClassName)Output>(input.GroupBy, typeof(@(@Model.ClassName)Output)); @:var groupFields = AggregationBuilder.ValidateFields<@(@Model.ClassName)Output>(input.GroupBy, typeof(@(@Model.ClassName)Output));
if (groupFields.Count > 0) @:if (groupFields.Count > 0)
{ @:{
query = query.GroupBy(string.Join(",", groupFields)); @: query = query.GroupBy(string.Join(",", groupFields));
@:}
@:// 构建聚合配置
@:var aggregator = new AggregationBuilder(
@: configs: input.Aggregations,
@: entityType: typeof(@(@Model.ClassName)),
@: outputType: typeof(@(@Model.ClassName)Output)
@:);
@:// 组合SELECT语句
@:var selectParts = groupFields.Select(f => $"{f} AS {f}")
@: .Concat(aggregator.SelectParts)
@: .ToList();
@:// 应用HAVING条件
@:if (aggregator.HavingConditions.Count > 0)
@:{
@: query = query.Having(string.Join(" AND ", aggregator.HavingConditions));
@:}
@:// 执行查询
@:return await query.Select<@(@Model.ClassName)Output>(string.Join(", ", selectParts)).ToListAsync();
} }
// 构建聚合配置 else
var aggregator = new AggregationBuilder(
configs: input.Aggregations,
entityType: typeof(@(@Model.ClassName)),
outputType: typeof(@(@Model.ClassName)Output)
);
// 组合SELECT语句
var selectParts = groupFields.Select(f => $"{f} AS {f}")
.Concat(aggregator.SelectParts)
.ToList();
// 应用HAVING条件
if (aggregator.HavingConditions.Count > 0)
{ {
query = query.Having(string.Join(" AND ", aggregator.HavingConditions)); @:return await query.ToListAsync();
} }
// 执行查询
return await query.Select<@(@Model.ClassName)Output>(string.Join(", ", selectParts))
.ToListAsync();
} }
@foreach (var column in Model.TableField){ @foreach (var column in Model.TableField){