package queries import ( "fmt" "time" ) // StatisticsQuery 统计查询对象 type StatisticsQuery struct { // 基础查询条件 MetricType string `json:"metric_type" form:"metric_type"` // 指标类型 MetricName string `json:"metric_name" form:"metric_name"` // 指标名称 Dimension string `json:"dimension" form:"dimension"` // 统计维度 StartDate time.Time `json:"start_date" form:"start_date"` // 开始日期 EndDate time.Time `json:"end_date" form:"end_date"` // 结束日期 // 分页参数 Limit int `json:"limit" form:"limit"` // 限制数量 Offset int `json:"offset" form:"offset"` // 偏移量 // 排序参数 SortBy string `json:"sort_by" form:"sort_by"` // 排序字段 SortOrder string `json:"sort_order" form:"sort_order"` // 排序顺序 (asc/desc) // 过滤条件 MinValue float64 `json:"min_value" form:"min_value"` // 最小值 MaxValue float64 `json:"max_value" form:"max_value"` // 最大值 // 聚合参数 AggregateBy string `json:"aggregate_by" form:"aggregate_by"` // 聚合维度 (hour/day/week/month) GroupBy string `json:"group_by" form:"group_by"` // 分组维度 } // StatisticsReportQuery 统计报告查询对象 type StatisticsReportQuery struct { // 基础查询条件 ReportType string `json:"report_type" form:"report_type"` // 报告类型 UserRole string `json:"user_role" form:"user_role"` // 用户角色 Status string `json:"status" form:"status"` // 报告状态 Period string `json:"period" form:"period"` // 统计周期 StartDate time.Time `json:"start_date" form:"start_date"` // 开始日期 EndDate time.Time `json:"end_date" form:"end_date"` // 结束日期 // 分页参数 Limit int `json:"limit" form:"limit"` // 限制数量 Offset int `json:"offset" form:"offset"` // 偏移量 // 排序参数 SortBy string `json:"sort_by" form:"sort_by"` // 排序字段 SortOrder string `json:"sort_order" form:"sort_order"` // 排序顺序 (asc/desc) // 过滤条件 GeneratedBy string `json:"generated_by" form:"generated_by"` // 生成者ID AccessLevel string `json:"access_level" form:"access_level"` // 访问级别 } // StatisticsDashboardQuery 统计仪表板查询对象 type StatisticsDashboardQuery struct { // 基础查询条件 UserRole string `json:"user_role" form:"user_role"` // 用户角色 IsDefault *bool `json:"is_default" form:"is_default"` // 是否默认 IsActive *bool `json:"is_active" form:"is_active"` // 是否激活 AccessLevel string `json:"access_level" form:"access_level"` // 访问级别 // 分页参数 Limit int `json:"limit" form:"limit"` // 限制数量 Offset int `json:"offset" form:"offset"` // 偏移量 // 排序参数 SortBy string `json:"sort_by" form:"sort_by"` // 排序字段 SortOrder string `json:"sort_order" form:"sort_order"` // 排序顺序 (asc/desc) // 过滤条件 CreatedBy string `json:"created_by" form:"created_by"` // 创建者ID Name string `json:"name" form:"name"` // 仪表板名称 } // RealtimeStatisticsQuery 实时统计查询对象 type RealtimeStatisticsQuery struct { // 查询条件 MetricType string `json:"metric_type" form:"metric_type"` // 指标类型 TimeRange string `json:"time_range" form:"time_range"` // 时间范围 (last_hour/last_day/last_week) // 过滤条件 Dimension string `json:"dimension" form:"dimension"` // 统计维度 } // HistoricalStatisticsQuery 历史统计查询对象 type HistoricalStatisticsQuery struct { // 查询条件 MetricType string `json:"metric_type" form:"metric_type"` // 指标类型 StartDate time.Time `json:"start_date" form:"start_date"` // 开始日期 EndDate time.Time `json:"end_date" form:"end_date"` // 结束日期 Period string `json:"period" form:"period"` // 统计周期 // 分页参数 Limit int `json:"limit" form:"limit"` // 限制数量 Offset int `json:"offset" form:"offset"` // 偏移量 // 聚合参数 AggregateBy string `json:"aggregate_by" form:"aggregate_by"` // 聚合维度 GroupBy string `json:"group_by" form:"group_by"` // 分组维度 // 过滤条件 Dimension string `json:"dimension" form:"dimension"` // 统计维度 MinValue float64 `json:"min_value" form:"min_value"` // 最小值 MaxValue float64 `json:"max_value" form:"max_value"` // 最大值 } // DashboardDataQuery 仪表板数据查询对象 type DashboardDataQuery struct { // 查询条件 UserRole string `json:"user_role" form:"user_role"` // 用户角色 Period string `json:"period" form:"period"` // 统计周期 // 时间范围 StartDate time.Time `json:"start_date" form:"start_date"` // 开始日期 EndDate time.Time `json:"end_date" form:"end_date"` // 结束日期 // 过滤条件 MetricTypes []string `json:"metric_types" form:"metric_types"` // 指标类型列表 Dimensions []string `json:"dimensions" form:"dimensions"` // 统计维度列表 } // ReportGenerationQuery 报告生成查询对象 type ReportGenerationQuery struct { // 报告配置 ReportType string `json:"report_type" form:"report_type"` // 报告类型 Title string `json:"title" form:"title"` // 报告标题 Period string `json:"period" form:"period"` // 统计周期 UserRole string `json:"user_role" form:"user_role"` // 用户角色 // 时间范围 StartDate time.Time `json:"start_date" form:"start_date"` // 开始日期 EndDate time.Time `json:"end_date" form:"end_date"` // 结束日期 // 过滤条件 Filters map[string]interface{} `json:"filters" form:"filters"` // 过滤条件 // 生成配置 GeneratedBy string `json:"generated_by" form:"generated_by"` // 生成者ID Format string `json:"format" form:"format"` // 输出格式 (json/pdf/excel) } // ExportQuery 导出查询对象 type ExportQuery struct { // 导出配置 Format string `json:"format" form:"format"` // 导出格式 (excel/csv/pdf) MetricType string `json:"metric_type" form:"metric_type"` // 指标类型 // 时间范围 StartDate time.Time `json:"start_date" form:"start_date"` // 开始日期 EndDate time.Time `json:"end_date" form:"end_date"` // 结束日期 // 过滤条件 Dimension string `json:"dimension" form:"dimension"` // 统计维度 GroupBy string `json:"group_by" form:"group_by"` // 分组维度 // 导出配置 IncludeCharts bool `json:"include_charts" form:"include_charts"` // 是否包含图表 Columns []string `json:"columns" form:"columns"` // 导出列 } // Validate 验证统计查询对象 func (q *StatisticsQuery) Validate() error { if q.MetricType == "" { return fmt.Errorf("指标类型不能为空") } if q.StartDate.IsZero() || q.EndDate.IsZero() { return fmt.Errorf("开始日期和结束日期不能为空") } if q.StartDate.After(q.EndDate) { return fmt.Errorf("开始日期不能晚于结束日期") } if q.Limit <= 0 { q.Limit = 20 // 默认限制 } if q.Limit > 1000 { q.Limit = 1000 // 最大限制 } if q.SortOrder != "" && q.SortOrder != "asc" && q.SortOrder != "desc" { q.SortOrder = "desc" // 默认降序 } return nil } // Validate 验证统计报告查询对象 func (q *StatisticsReportQuery) Validate() error { if q.Limit <= 0 { q.Limit = 20 // 默认限制 } if q.Limit > 1000 { q.Limit = 1000 // 最大限制 } if q.SortOrder != "" && q.SortOrder != "asc" && q.SortOrder != "desc" { q.SortOrder = "desc" // 默认降序 } return nil } // Validate 验证统计仪表板查询对象 func (q *StatisticsDashboardQuery) Validate() error { if q.Limit <= 0 { q.Limit = 20 // 默认限制 } if q.Limit > 1000 { q.Limit = 1000 // 最大限制 } if q.SortOrder != "" && q.SortOrder != "asc" && q.SortOrder != "desc" { q.SortOrder = "desc" // 默认降序 } return nil } // Validate 验证实时统计查询对象 func (q *RealtimeStatisticsQuery) Validate() error { if q.MetricType == "" { return fmt.Errorf("指标类型不能为空") } if q.TimeRange == "" { q.TimeRange = "last_hour" // 默认最近1小时 } validTimeRanges := []string{"last_hour", "last_day", "last_week"} for _, validRange := range validTimeRanges { if q.TimeRange == validRange { return nil } } return fmt.Errorf("无效的时间范围: %s", q.TimeRange) } // Validate 验证历史统计查询对象 func (q *HistoricalStatisticsQuery) Validate() error { if q.MetricType == "" { return fmt.Errorf("指标类型不能为空") } if q.StartDate.IsZero() || q.EndDate.IsZero() { return fmt.Errorf("开始日期和结束日期不能为空") } if q.StartDate.After(q.EndDate) { return fmt.Errorf("开始日期不能晚于结束日期") } if q.Limit <= 0 { q.Limit = 20 // 默认限制 } if q.Limit > 1000 { q.Limit = 1000 // 最大限制 } return nil } // Validate 验证仪表板数据查询对象 func (q *DashboardDataQuery) Validate() error { if q.UserRole == "" { return fmt.Errorf("用户角色不能为空") } if q.Period == "" { q.Period = "today" // 默认今天 } return nil } // Validate 验证报告生成查询对象 func (q *ReportGenerationQuery) Validate() error { if q.ReportType == "" { return fmt.Errorf("报告类型不能为空") } if q.Title == "" { return fmt.Errorf("报告标题不能为空") } if q.Period == "" { return fmt.Errorf("统计周期不能为空") } if q.UserRole == "" { return fmt.Errorf("用户角色不能为空") } return nil } // Validate 验证导出查询对象 func (q *ExportQuery) Validate() error { if q.Format == "" { return fmt.Errorf("导出格式不能为空") } if q.MetricType == "" { return fmt.Errorf("指标类型不能为空") } if q.StartDate.IsZero() || q.EndDate.IsZero() { return fmt.Errorf("开始日期和结束日期不能为空") } if q.StartDate.After(q.EndDate) { return fmt.Errorf("开始日期不能晚于结束日期") } validFormats := []string{"excel", "csv", "pdf"} for _, validFormat := range validFormats { if q.Format == validFormat { return nil } } return fmt.Errorf("无效的导出格式: %s", q.Format) }