diff --git a/internal/application/statistics/statistics_application_service_impl.go b/internal/application/statistics/statistics_application_service_impl.go index ad5dd72..6137e58 100644 --- a/internal/application/statistics/statistics_application_service_impl.go +++ b/internal/application/statistics/statistics_application_service_impl.go @@ -2356,15 +2356,26 @@ func (s *StatisticsApplicationServiceImpl) AdminGetConsumptionDomainStatistics(c } var consumptionTrend []map[string]interface{} - if period == "day" { - consumptionTrend, err = s.walletTransactionRepo.GetSystemDailyStats(ctx, startTime, endTime) - } else if period == "month" { - consumptionTrend, err = s.walletTransactionRepo.GetSystemMonthlyStats(ctx, startTime, endTime) - } + if !startTime.IsZero() && !endTime.IsZero() { + if period == "day" { + consumptionTrend, err = s.walletTransactionRepo.GetSystemDailyStats(ctx, startTime, endTime) + } else if period == "month" { + consumptionTrend, err = s.walletTransactionRepo.GetSystemMonthlyStats(ctx, startTime, endTime) + } - if err != nil { - s.logger.Error("获取消费趋势数据失败", zap.Error(err)) - return nil, err + if err != nil { + s.logger.Error("获取消费趋势数据失败", zap.Error(err)) + return nil, err + } + } else { + // 如果没有指定时间范围,获取最近7天的数据 + defaultEndDate := time.Now() + defaultStartDate := defaultEndDate.AddDate(0, 0, -7) + consumptionTrend, err = s.walletTransactionRepo.GetSystemDailyStats(ctx, defaultStartDate, defaultEndDate) + if err != nil { + s.logger.Error("获取消费每日趋势失败", zap.Error(err)) + return nil, err + } } result := map[string]interface{}{ @@ -2433,15 +2444,26 @@ func (s *StatisticsApplicationServiceImpl) AdminGetRechargeDomainStatistics(ctx } var rechargeTrend []map[string]interface{} - if period == "day" { - rechargeTrend, err = s.rechargeRecordRepo.GetSystemDailyStats(ctx, startTime, endTime) - } else if period == "month" { - rechargeTrend, err = s.rechargeRecordRepo.GetSystemMonthlyStats(ctx, startTime, endTime) - } + if !startTime.IsZero() && !endTime.IsZero() { + if period == "day" { + rechargeTrend, err = s.rechargeRecordRepo.GetSystemDailyStats(ctx, startTime, endTime) + } else if period == "month" { + rechargeTrend, err = s.rechargeRecordRepo.GetSystemMonthlyStats(ctx, startTime, endTime) + } - if err != nil { - s.logger.Error("获取充值趋势数据失败", zap.Error(err)) - return nil, err + if err != nil { + s.logger.Error("获取充值趋势数据失败", zap.Error(err)) + return nil, err + } + } else { + // 如果没有指定时间范围,获取最近7天的数据 + defaultEndDate := time.Now() + defaultStartDate := defaultEndDate.AddDate(0, 0, -7) + rechargeTrend, err = s.rechargeRecordRepo.GetSystemDailyStats(ctx, defaultStartDate, defaultEndDate) + if err != nil { + s.logger.Error("获取充值每日趋势失败", zap.Error(err)) + return nil, err + } } result := map[string]interface{}{ diff --git a/internal/infrastructure/database/repositories/finance/gorm_recharge_record_repository.go b/internal/infrastructure/database/repositories/finance/gorm_recharge_record_repository.go index 2866953..cdd2b22 100644 --- a/internal/infrastructure/database/repositories/finance/gorm_recharge_record_repository.go +++ b/internal/infrastructure/database/repositories/finance/gorm_recharge_record_repository.go @@ -287,9 +287,9 @@ func (r *GormRechargeRecordRepository) GetSystemDailyStats(ctx context.Context, DATE(created_at) as date, COALESCE(SUM(amount), 0) as amount FROM recharge_records - WHERE status = $1 - AND DATE(created_at) >= $2 - AND DATE(created_at) <= $3 + WHERE status = ? + AND DATE(created_at) >= ? + AND DATE(created_at) <= ? GROUP BY DATE(created_at) ORDER BY date ASC ` @@ -311,9 +311,9 @@ func (r *GormRechargeRecordRepository) GetSystemMonthlyStats(ctx context.Context TO_CHAR(created_at, 'YYYY-MM') as month, COALESCE(SUM(amount), 0) as amount FROM recharge_records - WHERE status = $1 - AND created_at >= $2 - AND created_at <= $3 + WHERE status = ? + AND created_at >= ? + AND created_at <= ? GROUP BY TO_CHAR(created_at, 'YYYY-MM') ORDER BY month ASC ` diff --git a/internal/infrastructure/database/repositories/finance/gorm_wallet_transaction_repository.go b/internal/infrastructure/database/repositories/finance/gorm_wallet_transaction_repository.go index 24ab184..b67084a 100644 --- a/internal/infrastructure/database/repositories/finance/gorm_wallet_transaction_repository.go +++ b/internal/infrastructure/database/repositories/finance/gorm_wallet_transaction_repository.go @@ -581,8 +581,8 @@ func (r *GormWalletTransactionRepository) GetSystemDailyStats(ctx context.Contex DATE(created_at) as date, COALESCE(SUM(amount), 0) as amount FROM wallet_transactions - WHERE DATE(created_at) >= $1 - AND DATE(created_at) <= $2 + WHERE DATE(created_at) >= ? + AND DATE(created_at) <= ? GROUP BY DATE(created_at) ORDER BY date ASC ` @@ -604,8 +604,8 @@ func (r *GormWalletTransactionRepository) GetSystemMonthlyStats(ctx context.Cont TO_CHAR(created_at, 'YYYY-MM') as month, COALESCE(SUM(amount), 0) as amount FROM wallet_transactions - WHERE created_at >= $1 - AND created_at <= $2 + WHERE created_at >= ? + AND created_at <= ? GROUP BY TO_CHAR(created_at, 'YYYY-MM') ORDER BY month ASC `