namespace Admin.NET.Plugin.AiDOP.Infrastructure.S8;
///
/// 首版按服务器自然周期对齐大屏统计口径;显式日期范围优先于 period。
///
public static class S8PeriodHelper
{
///
/// 将 period 字符串解析为半开区间 [from, to)。
/// 支持 today / this_week / this_month(大小写容错)。
/// 非法或空值返回 (null, null),调用方保持全量行为。
///
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)),
_ => (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));
}
}