feat: enhance superadmin dashboard overview
This commit is contained in:
@@ -2059,6 +2059,74 @@ func (s *super) BatchReviewContents(ctx context.Context, operatorID int64, form
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *super) ContentStatistics(ctx context.Context, filter *super_dto.SuperContentStatisticsFilter) (*super_dto.SuperContentStatisticsResponse, error) {
|
||||
// 统一统计时间范围与粒度,默认最近 7 天。
|
||||
reportFilter := &super_dto.SuperReportOverviewFilter{}
|
||||
if filter != nil {
|
||||
reportFilter.TenantID = filter.TenantID
|
||||
reportFilter.StartAt = filter.StartAt
|
||||
reportFilter.EndAt = filter.EndAt
|
||||
reportFilter.Granularity = filter.Granularity
|
||||
}
|
||||
rg, err := s.normalizeReportRange(reportFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tenantID := int64(0)
|
||||
if filter != nil && filter.TenantID != nil {
|
||||
tenantID = *filter.TenantID
|
||||
}
|
||||
|
||||
// 统计内容总量,支持租户维度过滤。
|
||||
tbl, q := models.ContentQuery.QueryContext(ctx)
|
||||
if tenantID > 0 {
|
||||
q = q.Where(tbl.TenantID.Eq(tenantID))
|
||||
}
|
||||
total, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
// 按天聚合新增内容数量,补齐趋势序列。
|
||||
type contentAggRow struct {
|
||||
Day time.Time `gorm:"column:day"`
|
||||
Count int64 `gorm:"column:count"`
|
||||
}
|
||||
rows := make([]contentAggRow, 0)
|
||||
query := models.ContentQuery.WithContext(ctx).
|
||||
UnderlyingDB().
|
||||
Model(&models.Content{}).
|
||||
Select("date_trunc('day', created_at) as day, count(*) as count").
|
||||
Where("created_at >= ? AND created_at < ?", rg.startDay, rg.endNext)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Group("day").Scan(&rows).Error; err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
trendMap := make(map[string]int64, len(rows))
|
||||
for _, row := range rows {
|
||||
key := row.Day.Format("2006-01-02")
|
||||
trendMap[key] = row.Count
|
||||
}
|
||||
|
||||
trend := make([]super_dto.SuperContentTrendItem, 0)
|
||||
for day := rg.startDay; !day.After(rg.endDay); day = day.AddDate(0, 0, 1) {
|
||||
key := day.Format("2006-01-02")
|
||||
trend = append(trend, super_dto.SuperContentTrendItem{
|
||||
Date: key,
|
||||
CreatedCount: trendMap[key],
|
||||
})
|
||||
}
|
||||
|
||||
return &super_dto.SuperContentStatisticsResponse{
|
||||
TotalCount: total,
|
||||
Trend: trend,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *super) ListOrders(ctx context.Context, filter *super_dto.SuperOrderListFilter) (*requests.Pager, error) {
|
||||
tbl, q := models.OrderQuery.QueryContext(ctx)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user