From af88bcc8eb033932f0e7c917289c423bb9c596b3 Mon Sep 17 00:00:00 2001 From: 18278715334 <18278715334@163.com> Date: Sat, 6 Dec 2025 15:46:46 +0800 Subject: [PATCH] 18278715334@163.com --- .../statistics_application_service_impl.go | 56 +++++++++++++++---- .../api/gorm_api_call_repository.go | 5 +- .../gorm_recharge_record_repository.go | 5 +- .../gorm_wallet_transaction_repository.go | 5 +- 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/internal/application/statistics/statistics_application_service_impl.go b/internal/application/statistics/statistics_application_service_impl.go index 708fe60..a0c0cf3 100644 --- a/internal/application/statistics/statistics_application_service_impl.go +++ b/internal/application/statistics/statistics_application_service_impl.go @@ -2275,6 +2275,10 @@ func (s *StatisticsApplicationServiceImpl) AdminGetApiDomainStatistics(ctx conte s.logger.Error("解析开始日期失败", zap.Error(err)) return nil, err } + // 如果是月统计,将开始日期调整为当月1号00:00:00 + if period == "month" { + startTime = time.Date(startTime.Year(), startTime.Month(), 1, 0, 0, 0, 0, startTime.Location()) + } } if endDate != "" { endTime, err = time.Parse("2006-01-02", endDate) @@ -2282,6 +2286,14 @@ func (s *StatisticsApplicationServiceImpl) AdminGetApiDomainStatistics(ctx conte s.logger.Error("解析结束日期失败", zap.Error(err)) return nil, err } + if period == "month" { + // 如果是月统计,将结束日期调整为下个月1号00:00:00 + // 这样在查询时使用 created_at < endTime 可以包含整个月份的数据(到本月最后一天23:59:59.999) + endTime = time.Date(endTime.Year(), endTime.Month()+1, 1, 0, 0, 0, 0, endTime.Location()) + } else { + // 日统计:将结束日期设置为次日00:00:00,这样在查询时使用 created_at < endTime 可以包含当天的所有数据 + endTime = endTime.AddDate(0, 0, 1) + } } // 获取API调用统计数据 @@ -2318,6 +2330,10 @@ func (s *StatisticsApplicationServiceImpl) AdminGetConsumptionDomainStatistics(c s.logger.Error("解析开始日期失败", zap.Error(err)) return nil, err } + // 如果是月统计,将开始日期调整为当月1号00:00:00 + if period == "month" { + startTime = time.Date(startTime.Year(), startTime.Month(), 1, 0, 0, 0, 0, startTime.Location()) + } } if endDate != "" { endTime, err = time.Parse("2006-01-02", endDate) @@ -2325,6 +2341,14 @@ func (s *StatisticsApplicationServiceImpl) AdminGetConsumptionDomainStatistics(c s.logger.Error("解析结束日期失败", zap.Error(err)) return nil, err } + if period == "month" { + // 如果是月统计,将结束日期调整为下个月1号00:00:00 + // 这样在查询时使用 created_at < endTime 可以包含整个月份的数据(到本月最后一天23:59:59.999) + endTime = time.Date(endTime.Year(), endTime.Month()+1, 1, 0, 0, 0, 0, endTime.Location()) + } else { + // 日统计:将结束日期设置为次日00:00:00,这样在查询时使用 created_at < endTime 可以包含当天的所有数据 + endTime = endTime.AddDate(0, 0, 1) + } } // 获取消费统计数据 @@ -2370,7 +2394,7 @@ func (s *StatisticsApplicationServiceImpl) AdminGetConsumptionDomainStatistics(c defaultEndDate := time.Now() defaultStartDate := defaultEndDate.AddDate(0, 0, -7) consumptionTrend, err = s.walletTransactionRepo.GetSystemDailyStats(ctx, defaultStartDate, defaultEndDate) - if err != nil { + if err != nil { s.logger.Error("获取消费每日趋势失败", zap.Error(err)) return nil, err } @@ -2406,6 +2430,10 @@ func (s *StatisticsApplicationServiceImpl) AdminGetRechargeDomainStatistics(ctx s.logger.Error("解析开始日期失败", zap.Error(err)) return nil, err } + // 如果是月统计,将开始日期调整为当月1号00:00:00 + if period == "month" { + startTime = time.Date(startTime.Year(), startTime.Month(), 1, 0, 0, 0, 0, startTime.Location()) + } } if endDate != "" { endTime, err = time.Parse("2006-01-02", endDate) @@ -2413,6 +2441,14 @@ func (s *StatisticsApplicationServiceImpl) AdminGetRechargeDomainStatistics(ctx s.logger.Error("解析结束日期失败", zap.Error(err)) return nil, err } + if period == "month" { + // 如果是月统计,将结束日期调整为下个月1号00:00:00 + // 这样在查询时使用 created_at < endTime 可以包含整个月份的数据(到本月最后一天23:59:59.999) + endTime = time.Date(endTime.Year(), endTime.Month()+1, 1, 0, 0, 0, 0, endTime.Location()) + } else { + // 日统计:将结束日期设置为次日00:00:00,这样在查询时使用 created_at < endTime 可以包含当天的所有数据 + endTime = endTime.AddDate(0, 0, 1) + } } // 获取充值统计数据 @@ -2716,15 +2752,15 @@ func (s *StatisticsApplicationServiceImpl) AdminGetTodayCertifiedEnterprises(ctx } enterprise := map[string]interface{}{ - "id": cert.ID, - "user_id": cert.UserID, - "username": user.Username, - "enterprise_name": enterpriseInfo.CompanyName, - "legal_person_name": enterpriseInfo.LegalPersonName, - "legal_person_phone": enterpriseInfo.LegalPersonPhone, - "unified_social_code": enterpriseInfo.UnifiedSocialCode, - "enterprise_address": enterpriseInfo.EnterpriseAddress, - "certified_at": cert.CompletedAt.Format(time.RFC3339), + "id": cert.ID, + "user_id": cert.UserID, + "username": user.Username, + "enterprise_name": enterpriseInfo.CompanyName, + "legal_person_name": enterpriseInfo.LegalPersonName, + "legal_person_phone": enterpriseInfo.LegalPersonPhone, + "unified_social_code": enterpriseInfo.UnifiedSocialCode, + "enterprise_address": enterpriseInfo.EnterpriseAddress, + "certified_at": cert.CompletedAt.Format(time.RFC3339), } enterprises = append(enterprises, enterprise) } diff --git a/internal/infrastructure/database/repositories/api/gorm_api_call_repository.go b/internal/infrastructure/database/repositories/api/gorm_api_call_repository.go index 8d0a8fe..7ec6489 100644 --- a/internal/infrastructure/database/repositories/api/gorm_api_call_repository.go +++ b/internal/infrastructure/database/repositories/api/gorm_api_call_repository.go @@ -431,10 +431,11 @@ func (r *GormApiCallRepository) GetSystemTotalCalls(ctx context.Context) (int64, } // GetSystemCallsByDateRange 获取系统指定时间范围内的API调用次数 +// endDate 应该是结束日期当天的次日00:00:00(日统计)或下个月1号00:00:00(月统计),使用 < 而不是 <= func (r *GormApiCallRepository) GetSystemCallsByDateRange(ctx context.Context, startDate, endDate time.Time) (int64, error) { var count int64 err := r.GetDB(ctx).Model(&entities.ApiCall{}). - Where("created_at >= ? AND created_at <= ?", startDate, endDate). + Where("created_at >= ? AND created_at < ?", startDate, endDate). Count(&count).Error return count, err } @@ -472,7 +473,7 @@ func (r *GormApiCallRepository) GetSystemMonthlyStats(ctx context.Context, start COUNT(*) as calls FROM api_calls WHERE created_at >= $1 - AND created_at <= $2 + AND created_at < $2 GROUP BY TO_CHAR(created_at, 'YYYY-MM') ORDER BY month ASC ` 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 2605d48..fb141ef 100644 --- a/internal/infrastructure/database/repositories/finance/gorm_recharge_record_repository.go +++ b/internal/infrastructure/database/repositories/finance/gorm_recharge_record_repository.go @@ -426,10 +426,11 @@ func (r *GormRechargeRecordRepository) GetSystemTotalAmount(ctx context.Context) } // GetSystemAmountByDateRange 获取系统指定时间范围内的充值金额(排除赠送) +// endDate 应该是结束日期当天的次日00:00:00(日统计)或下个月1号00:00:00(月统计),使用 < 而不是 <= func (r *GormRechargeRecordRepository) GetSystemAmountByDateRange(ctx context.Context, startDate, endDate time.Time) (float64, error) { var total float64 err := r.GetDB(ctx).Model(&entities.RechargeRecord{}). - Where("status = ? AND recharge_type != ? AND created_at >= ? AND created_at <= ?", entities.RechargeStatusSuccess, entities.RechargeTypeGift, startDate, endDate). + Where("status = ? AND recharge_type != ? AND created_at >= ? AND created_at < ?", entities.RechargeStatusSuccess, entities.RechargeTypeGift, startDate, endDate). Select("COALESCE(SUM(amount), 0)"). Scan(&total).Error return total, err @@ -472,7 +473,7 @@ func (r *GormRechargeRecordRepository) GetSystemMonthlyStats(ctx context.Context WHERE status = ? AND recharge_type != ? AND created_at >= ? - 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 b9b14f5..8508c7c 100644 --- a/internal/infrastructure/database/repositories/finance/gorm_wallet_transaction_repository.go +++ b/internal/infrastructure/database/repositories/finance/gorm_wallet_transaction_repository.go @@ -586,10 +586,11 @@ func (r *GormWalletTransactionRepository) GetSystemTotalAmount(ctx context.Conte } // GetSystemAmountByDateRange 获取系统指定时间范围内的消费金额 +// endDate 应该是结束日期当天的次日00:00:00(日统计)或下个月1号00:00:00(月统计),使用 < 而不是 <= func (r *GormWalletTransactionRepository) GetSystemAmountByDateRange(ctx context.Context, startDate, endDate time.Time) (float64, error) { var total float64 err := r.GetDB(ctx).Model(&entities.WalletTransaction{}). - Where("created_at >= ? AND created_at <= ?", startDate, endDate). + Where("created_at >= ? AND created_at < ?", startDate, endDate). Select("COALESCE(SUM(amount), 0)"). Scan(&total).Error return total, err @@ -628,7 +629,7 @@ func (r *GormWalletTransactionRepository) GetSystemMonthlyStats(ctx context.Cont COALESCE(SUM(amount), 0) as amount FROM wallet_transactions WHERE created_at >= ? - AND created_at <= ? + AND created_at < ? GROUP BY TO_CHAR(created_at, 'YYYY-MM') ORDER BY month ASC `