Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 13, 2024
2 parents 09d9f09 + 1e144d5 commit 81cb64d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
52 changes: 43 additions & 9 deletions XCode/Entity/Entity_Operate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NewLife.Data;
using XCode.Configuration;
using XCode.Shards;
using XCode.Statistics;

namespace XCode;

Expand Down Expand Up @@ -277,12 +278,45 @@ public String? SelectStat
{
// 找到所有数字字段,进行求和统计
var concat = new ConcatExpression();
foreach (var item in StatFields)
{
concat &= item.Mode switch
{
StatModes.Max => item.Field.Max(),
StatModes.Min => item.Field.Min(),
StatModes.Avg => item.Field.Avg(),
StatModes.Sum => item.Field.Sum(),
StatModes.Count => item.Field.Count(),
_ => item.Field.Max(),
};
}

// 至少有个空字符串,避免重入
_SelectStat = concat + "";
}

return _SelectStat;
}
set => _SelectStat = value;
}

private IList<StatField> _StatFields;
/// <summary>统计字段集合</summary>
public IList<StatField> StatFields
{
get
{
if (_StatFields == null)
{
var list = new List<StatField>();

//// 先来个行数
//if (!Fields.Any(e => e.Name.EqualIgnoreCase("Count"))) concat &= "Count(*) as Count";
foreach (var item in Fields)
{
// 自增和主键不参与
if (item.IsIdentity || item.PrimaryKey) continue;

// 只要Int32和Int64,一般Int16太小不适合聚合
if (item.Type != typeof(Int32) &&
item.Type != typeof(Int64) &&
Expand All @@ -303,23 +337,23 @@ public String? SelectStat
while (name2.Length > 1 && Char.IsDigit(name2[^1])) name2 = name2[0..^1];

if (name.StartsWith("Max") && name.Length > 3 && Char.IsUpper(name[3]))
concat &= item.Max();
list.Add(new(item, StatModes.Max));
else if (name.StartsWith("Min") && name.Length > 3 && Char.IsUpper(name[3]))
concat &= item.Min();
list.Add(new(item, StatModes.Min));
else if (name.StartsWith("Avg") && name.Length > 3 && Char.IsUpper(name[3]))
concat &= item.Avg();
list.Add(new(item, StatModes.Avg));
else if (name2.EndsWith("Rate") || name2.EndsWith("Ratio"))
concat &= item.Max();
list.Add(new(item, StatModes.Max));
else
concat &= item.Sum();
list.Add(new(item, StatModes.Sum));
}
// 至少有个空字符串,避免重入
_SelectStat = concat + "";

_StatFields = list;
}

return _SelectStat;
return _StatFields;
}
set => _SelectStat = value;
set => _StatFields = value;
}

/// <summary>实体模块集合</summary>
Expand Down
4 changes: 4 additions & 0 deletions XCode/Entity/IEntityOperate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NewLife.Data;
using XCode.Configuration;
using XCode.Shards;
using XCode.Statistics;

namespace XCode;

Expand Down Expand Up @@ -221,6 +222,9 @@ public interface IEntityFactory
/// <summary>默认选择统计语句</summary>
String? SelectStat { get; set; }

/// <summary>统计字段集合</summary>
IList<StatField> StatFields { get; set; }

/// <summary>实体模块集合</summary>
EntityModules Modules { get; }

Expand Down
32 changes: 32 additions & 0 deletions XCode/Statistics/StatField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using XCode.Configuration;

namespace XCode.Statistics;

/// <summary>聚合方式</summary>
public enum StatModes
{
/// <summary>最大值</summary>
Max = 1,

/// <summary>最小值</summary>
Min = 2,

/// <summary>平均值</summary>
Avg = 3,

/// <summary>求和</summary>
Sum = 4,

/// <summary>计数</summary>
Count = 5,
}

/// <summary>统计字段</summary>
public class StatField(FieldItem field, StatModes mode)
{
/// <summary>字段</summary>
public FieldItem Field { get; set; } = field;

/// <summary>统计模式</summary>
public StatModes Mode { get; set; } = mode;
}

0 comments on commit 81cb64d

Please sign in to comment.