65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using YD_AllHeartRates.Api.Entitys;
|
|
using YD_AllHeartRates.Commons.Dto;
|
|
using YD_AllHeartRates.Commons.Utils;
|
|
|
|
namespace YD_AllHeartRates.Api.Services.Impl
|
|
{
|
|
public static class HeartRateReportHelper
|
|
{
|
|
public static (DateTime Start, DateTime End, DateTime Effective) GetTimeRange(DateTime? input)
|
|
{
|
|
var effective = input ?? DateTime.Now;
|
|
return (effective.Date, effective.Date.AddDays(1).AddSeconds(-1), effective);
|
|
}
|
|
|
|
public static ChartDataDto BuildHeartRateTrend(List<HeartRateData> data)
|
|
{
|
|
var chart = new ChartDataDto
|
|
{
|
|
AxisX = new List<string>(),
|
|
AxisY = new List<int>()
|
|
};
|
|
|
|
if (data == null || data.Count == 0)
|
|
return chart;
|
|
|
|
// 按小时分组计算平均值(四舍五入)
|
|
var hourlyAvg = data
|
|
.GroupBy(x => x.ScoreTime.Hour)
|
|
.ToDictionary(g => g.Key, g => (int)Math.Round(g.Average(x => x.Value)));
|
|
|
|
// 计算最小和最大小时
|
|
int minHour = data.Min(x => x.ScoreTime.Hour);
|
|
int maxHour = data.Max(x => x.ScoreTime.Hour);
|
|
|
|
for (int hour = minHour; hour <= maxHour; hour++)
|
|
{
|
|
chart.AxisX.Add($"{hour}:00");
|
|
chart.AxisY.Add(hourlyAvg.TryGetValue(hour, out int val) ? val : 0);
|
|
}
|
|
|
|
return chart;
|
|
}
|
|
|
|
|
|
|
|
public static int CalculateReachRate(int reachCount, int total)
|
|
=> total == 0 ? 0 : (int)((double)reachCount / total * 100);
|
|
|
|
public static int CalculateAverage(List<DateTime> timestamps)
|
|
=> timestamps.CalculateDuration();
|
|
|
|
public static Dictionary<T, int> CalculateDurations<T>(List<HeartRateData> data, Func<HeartRateData, T> keySelector)
|
|
where T : notnull
|
|
{
|
|
return data
|
|
.Where(x => x.Strength >= 50)
|
|
.GroupBy(keySelector)
|
|
.ToDictionary(
|
|
g => g.Key,
|
|
g => g.Select(x => x.ScoreTime).ToList().CalculateDuration()
|
|
);
|
|
}
|
|
}
|
|
}
|