From cfad2bce093b252fa86c0c47747ceda806d51b28 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Mon, 15 Sep 2025 21:15:23 +0800 Subject: [PATCH] fix rank --- .../statistics_application_service_impl.go | 6 +-- .../gorm_recharge_record_repository.go | 44 ++++++++++--------- .../repositories/user/gorm_user_repository.go | 8 +++- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/internal/application/statistics/statistics_application_service_impl.go b/internal/application/statistics/statistics_application_service_impl.go index 4df66d3..708fe60 100644 --- a/internal/application/statistics/statistics_application_service_impl.go +++ b/internal/application/statistics/statistics_application_service_impl.go @@ -2002,11 +2002,9 @@ func (s *StatisticsApplicationServiceImpl) getWalletTransactionsByDateRange(ctx return 0.0, nil } -// getRechargeRecordsByDateRange 获取指定日期范围内的充值金额 +// getRechargeRecordsByDateRange 获取指定日期范围内的充值金额(排除赠送) func (s *StatisticsApplicationServiceImpl) getRechargeRecordsByDateRange(ctx context.Context, userID string, startDate, endDate time.Time) (float64, error) { - // 这里需要实现按日期范围查询充值金额的逻辑 - // 暂时返回0,实际实现需要扩展仓储接口或使用原生SQL查询 - return 0.0, nil + return s.rechargeRecordRepo.GetTotalAmountByUserIdAndDateRange(ctx, userID, startDate, endDate) } // ================ 独立统计接口实现 ================ 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 cdd2b22..9d78307 100644 --- a/internal/infrastructure/database/repositories/finance/gorm_recharge_record_repository.go +++ b/internal/infrastructure/database/repositories/finance/gorm_recharge_record_repository.go @@ -186,27 +186,27 @@ func (r *GormRechargeRecordRepository) Restore(ctx context.Context, id string) e return r.RestoreEntity(ctx, id, &entities.RechargeRecord{}) } -// GetTotalAmountByUserId 获取用户总充值金额 +// GetTotalAmountByUserId 获取用户总充值金额(排除赠送) func (r *GormRechargeRecordRepository) GetTotalAmountByUserId(ctx context.Context, userId string) (float64, error) { var total float64 err := r.GetDB(ctx).Model(&entities.RechargeRecord{}). Select("COALESCE(SUM(amount), 0)"). - Where("user_id = ? AND status = ?", userId, entities.RechargeStatusSuccess). + Where("user_id = ? AND status = ? AND recharge_type != ?", userId, entities.RechargeStatusSuccess, entities.RechargeTypeGift). Scan(&total).Error return total, err } -// GetTotalAmountByUserIdAndDateRange 按用户ID和日期范围获取总充值金额 +// GetTotalAmountByUserIdAndDateRange 按用户ID和日期范围获取总充值金额(排除赠送) func (r *GormRechargeRecordRepository) GetTotalAmountByUserIdAndDateRange(ctx context.Context, userId string, startDate, endDate time.Time) (float64, error) { var total float64 err := r.GetDB(ctx).Model(&entities.RechargeRecord{}). Select("COALESCE(SUM(amount), 0)"). - Where("user_id = ? AND status = ? AND created_at >= ? AND created_at < ?", userId, entities.RechargeStatusSuccess, startDate, endDate). + Where("user_id = ? AND status = ? AND recharge_type != ? AND created_at >= ? AND created_at < ?", userId, entities.RechargeStatusSuccess, entities.RechargeTypeGift, startDate, endDate). Scan(&total).Error return total, err } -// GetDailyStatsByUserId 获取用户每日充值统计 +// GetDailyStatsByUserId 获取用户每日充值统计(排除赠送) func (r *GormRechargeRecordRepository) GetDailyStatsByUserId(ctx context.Context, userId string, startDate, endDate time.Time) ([]map[string]interface{}, error) { var results []map[string]interface{} @@ -218,13 +218,14 @@ func (r *GormRechargeRecordRepository) GetDailyStatsByUserId(ctx context.Context FROM recharge_records WHERE user_id = $1 AND status = $2 - AND DATE(created_at) >= $3 - AND DATE(created_at) <= $4 + AND recharge_type != $3 + AND DATE(created_at) >= $4 + AND DATE(created_at) <= $5 GROUP BY DATE(created_at) ORDER BY date ASC ` - err := r.GetDB(ctx).Raw(sql, userId, entities.RechargeStatusSuccess, startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).Scan(&results).Error + err := r.GetDB(ctx).Raw(sql, userId, entities.RechargeStatusSuccess, entities.RechargeTypeGift, startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).Scan(&results).Error if err != nil { return nil, err } @@ -232,7 +233,7 @@ func (r *GormRechargeRecordRepository) GetDailyStatsByUserId(ctx context.Context return results, nil } -// GetMonthlyStatsByUserId 获取用户每月充值统计 +// GetMonthlyStatsByUserId 获取用户每月充值统计(排除赠送) func (r *GormRechargeRecordRepository) GetMonthlyStatsByUserId(ctx context.Context, userId string, startDate, endDate time.Time) ([]map[string]interface{}, error) { var results []map[string]interface{} @@ -244,13 +245,14 @@ func (r *GormRechargeRecordRepository) GetMonthlyStatsByUserId(ctx context.Conte FROM recharge_records WHERE user_id = $1 AND status = $2 - AND created_at >= $3 - AND created_at <= $4 + AND recharge_type != $3 + AND created_at >= $4 + AND created_at <= $5 GROUP BY TO_CHAR(created_at, 'YYYY-MM') ORDER BY month ASC ` - err := r.GetDB(ctx).Raw(sql, userId, entities.RechargeStatusSuccess, startDate, endDate).Scan(&results).Error + err := r.GetDB(ctx).Raw(sql, userId, entities.RechargeStatusSuccess, entities.RechargeTypeGift, startDate, endDate).Scan(&results).Error if err != nil { return nil, err } @@ -258,27 +260,27 @@ func (r *GormRechargeRecordRepository) GetMonthlyStatsByUserId(ctx context.Conte return results, nil } -// GetSystemTotalAmount 获取系统总充值金额 +// GetSystemTotalAmount 获取系统总充值金额(排除赠送) func (r *GormRechargeRecordRepository) GetSystemTotalAmount(ctx context.Context) (float64, error) { var total float64 err := r.GetDB(ctx).Model(&entities.RechargeRecord{}). - Where("status = ?", entities.RechargeStatusSuccess). + Where("status = ? AND recharge_type != ?", entities.RechargeStatusSuccess, entities.RechargeTypeGift). Select("COALESCE(SUM(amount), 0)"). Scan(&total).Error return total, err } -// GetSystemAmountByDateRange 获取系统指定时间范围内的充值金额 +// GetSystemAmountByDateRange 获取系统指定时间范围内的充值金额(排除赠送) 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 created_at >= ? AND created_at <= ?", entities.RechargeStatusSuccess, 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 } -// GetSystemDailyStats 获取系统每日充值统计 +// GetSystemDailyStats 获取系统每日充值统计(排除赠送) func (r *GormRechargeRecordRepository) GetSystemDailyStats(ctx context.Context, startDate, endDate time.Time) ([]map[string]interface{}, error) { var results []map[string]interface{} @@ -288,13 +290,14 @@ func (r *GormRechargeRecordRepository) GetSystemDailyStats(ctx context.Context, COALESCE(SUM(amount), 0) as amount FROM recharge_records WHERE status = ? + AND recharge_type != ? AND DATE(created_at) >= ? AND DATE(created_at) <= ? GROUP BY DATE(created_at) ORDER BY date ASC ` - err := r.GetDB(ctx).Raw(sql, entities.RechargeStatusSuccess, startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).Scan(&results).Error + err := r.GetDB(ctx).Raw(sql, entities.RechargeStatusSuccess, entities.RechargeTypeGift, startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).Scan(&results).Error if err != nil { return nil, err } @@ -302,7 +305,7 @@ func (r *GormRechargeRecordRepository) GetSystemDailyStats(ctx context.Context, return results, nil } -// GetSystemMonthlyStats 获取系统每月充值统计 +// GetSystemMonthlyStats 获取系统每月充值统计(排除赠送) func (r *GormRechargeRecordRepository) GetSystemMonthlyStats(ctx context.Context, startDate, endDate time.Time) ([]map[string]interface{}, error) { var results []map[string]interface{} @@ -312,13 +315,14 @@ func (r *GormRechargeRecordRepository) GetSystemMonthlyStats(ctx context.Context COALESCE(SUM(amount), 0) as amount FROM recharge_records WHERE status = ? + AND recharge_type != ? AND created_at >= ? AND created_at <= ? GROUP BY TO_CHAR(created_at, 'YYYY-MM') ORDER BY month ASC ` - err := r.GetDB(ctx).Raw(sql, entities.RechargeStatusSuccess, startDate, endDate).Scan(&results).Error + err := r.GetDB(ctx).Raw(sql, entities.RechargeStatusSuccess, entities.RechargeTypeGift, startDate, endDate).Scan(&results).Error if err != nil { return nil, err } diff --git a/internal/infrastructure/database/repositories/user/gorm_user_repository.go b/internal/infrastructure/database/repositories/user/gorm_user_repository.go index b9b8d7c..2d85a89 100644 --- a/internal/infrastructure/database/repositories/user/gorm_user_repository.go +++ b/internal/infrastructure/database/repositories/user/gorm_user_repository.go @@ -596,7 +596,7 @@ func (r *GormUserRepository) GetUserCallRankingByConsumption(ctx context.Context return results, nil } -// GetRechargeRanking 获取充值排行 +// GetRechargeRanking 获取充值排行(排除赠送,只统计成功状态) func (r *GormUserRepository) GetRechargeRanking(ctx context.Context, period string, limit int) ([]map[string]interface{}, error) { var sql string var args []interface{} @@ -612,6 +612,8 @@ func (r *GormUserRepository) GetRechargeRanking(ctx context.Context, period stri LEFT JOIN enterprise_infos ei ON u.id = ei.user_id LEFT JOIN recharge_records rr ON u.id = rr.user_id AND DATE(rr.created_at) = CURRENT_DATE + AND rr.status = 'success' + AND rr.recharge_type != 'gift' WHERE u.deleted_at IS NULL GROUP BY u.id, ei.company_name, u.username, u.phone HAVING COALESCE(SUM(rr.amount), 0) > 0 @@ -629,6 +631,8 @@ func (r *GormUserRepository) GetRechargeRanking(ctx context.Context, period stri LEFT JOIN enterprise_infos ei ON u.id = ei.user_id LEFT JOIN recharge_records rr ON u.id = rr.user_id AND DATE_TRUNC('month', rr.created_at) = DATE_TRUNC('month', CURRENT_DATE) + AND rr.status = 'success' + AND rr.recharge_type != 'gift' WHERE u.deleted_at IS NULL GROUP BY u.id, ei.company_name, u.username, u.phone HAVING COALESCE(SUM(rr.amount), 0) > 0 @@ -645,6 +649,8 @@ func (r *GormUserRepository) GetRechargeRanking(ctx context.Context, period stri FROM users u LEFT JOIN enterprise_infos ei ON u.id = ei.user_id LEFT JOIN recharge_records rr ON u.id = rr.user_id + AND rr.status = 'success' + AND rr.recharge_type != 'gift' WHERE u.deleted_at IS NULL GROUP BY u.id, ei.company_name, u.username, u.phone HAVING COALESCE(SUM(rr.amount), 0) > 0