| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- namespace Admin.NET.Plugin.AiDOP.Infrastructure.S8;
- /// <summary>
- /// 首版按服务器自然周期对齐大屏统计口径;显式日期范围优先于 period。
- /// </summary>
- public static class S8PeriodHelper
- {
- /// <summary>
- /// 将 period 字符串解析为半开区间 [from, to)。
- /// 支持 today / this_week / this_month / this_quarter(大小写容错)。
- /// 非法或空值返回 (null, null),调用方保持全量行为。
- /// </summary>
- public static (DateTime? from, DateTime? to) Resolve(string? period)
- {
- if (string.IsNullOrWhiteSpace(period)) return (null, null);
- var today = DateTime.Today;
- return period.Trim().ToLowerInvariant() switch
- {
- "today" => (today, today.AddDays(1)),
- "this_week" => ResolveThisWeek(today),
- "this_month" => (new DateTime(today.Year, today.Month, 1), new DateTime(today.Year, today.Month, 1).AddMonths(1)),
- "this_quarter" => ResolveThisQuarter(today),
- "last_24h" => (DateTime.Now.AddHours(-24), DateTime.Now),
- "last_7d" => (today.AddDays(-6), today.AddDays(1)),
- "last_30d" => (today.AddDays(-29), today.AddDays(1)),
- _ => (null, null),
- };
- }
- private static (DateTime? from, DateTime? to) ResolveThisWeek(DateTime today)
- {
- // 周一作为周起始(DayOfWeek.Monday = 1)
- var offset = ((int)today.DayOfWeek - (int)DayOfWeek.Monday + 7) % 7;
- var monday = today.AddDays(-offset);
- return (monday, monday.AddDays(7));
- }
- private static (DateTime? from, DateTime? to) ResolveThisQuarter(DateTime today)
- {
- // 季度起始月:1 / 4 / 7 / 10
- var quarterStartMonth = ((today.Month - 1) / 3) * 3 + 1;
- var start = new DateTime(today.Year, quarterStartMonth, 1);
- return (start, start.AddMonths(3));
- }
- }
|