This commit is contained in:
2025-09-15 21:15:23 +08:00
parent 6874f67c45
commit cfad2bce09
3 changed files with 33 additions and 25 deletions

View File

@@ -2002,11 +2002,9 @@ func (s *StatisticsApplicationServiceImpl) getWalletTransactionsByDateRange(ctx
return 0.0, nil return 0.0, nil
} }
// getRechargeRecordsByDateRange 获取指定日期范围内的充值金额 // getRechargeRecordsByDateRange 获取指定日期范围内的充值金额(排除赠送)
func (s *StatisticsApplicationServiceImpl) getRechargeRecordsByDateRange(ctx context.Context, userID string, startDate, endDate time.Time) (float64, error) { func (s *StatisticsApplicationServiceImpl) getRechargeRecordsByDateRange(ctx context.Context, userID string, startDate, endDate time.Time) (float64, error) {
// 这里需要实现按日期范围查询充值金额的逻辑 return s.rechargeRecordRepo.GetTotalAmountByUserIdAndDateRange(ctx, userID, startDate, endDate)
// 暂时返回0实际实现需要扩展仓储接口或使用原生SQL查询
return 0.0, nil
} }
// ================ 独立统计接口实现 ================ // ================ 独立统计接口实现 ================

View File

@@ -186,27 +186,27 @@ func (r *GormRechargeRecordRepository) Restore(ctx context.Context, id string) e
return r.RestoreEntity(ctx, id, &entities.RechargeRecord{}) return r.RestoreEntity(ctx, id, &entities.RechargeRecord{})
} }
// GetTotalAmountByUserId 获取用户总充值金额 // GetTotalAmountByUserId 获取用户总充值金额(排除赠送)
func (r *GormRechargeRecordRepository) GetTotalAmountByUserId(ctx context.Context, userId string) (float64, error) { func (r *GormRechargeRecordRepository) GetTotalAmountByUserId(ctx context.Context, userId string) (float64, error) {
var total float64 var total float64
err := r.GetDB(ctx).Model(&entities.RechargeRecord{}). err := r.GetDB(ctx).Model(&entities.RechargeRecord{}).
Select("COALESCE(SUM(amount), 0)"). 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 Scan(&total).Error
return total, err return total, err
} }
// GetTotalAmountByUserIdAndDateRange 按用户ID和日期范围获取总充值金额 // GetTotalAmountByUserIdAndDateRange 按用户ID和日期范围获取总充值金额(排除赠送)
func (r *GormRechargeRecordRepository) GetTotalAmountByUserIdAndDateRange(ctx context.Context, userId string, startDate, endDate time.Time) (float64, error) { func (r *GormRechargeRecordRepository) GetTotalAmountByUserIdAndDateRange(ctx context.Context, userId string, startDate, endDate time.Time) (float64, error) {
var total float64 var total float64
err := r.GetDB(ctx).Model(&entities.RechargeRecord{}). err := r.GetDB(ctx).Model(&entities.RechargeRecord{}).
Select("COALESCE(SUM(amount), 0)"). 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 Scan(&total).Error
return total, err return total, err
} }
// GetDailyStatsByUserId 获取用户每日充值统计 // GetDailyStatsByUserId 获取用户每日充值统计(排除赠送)
func (r *GormRechargeRecordRepository) GetDailyStatsByUserId(ctx context.Context, userId string, startDate, endDate time.Time) ([]map[string]interface{}, error) { func (r *GormRechargeRecordRepository) GetDailyStatsByUserId(ctx context.Context, userId string, startDate, endDate time.Time) ([]map[string]interface{}, error) {
var results []map[string]interface{} var results []map[string]interface{}
@@ -218,13 +218,14 @@ func (r *GormRechargeRecordRepository) GetDailyStatsByUserId(ctx context.Context
FROM recharge_records FROM recharge_records
WHERE user_id = $1 WHERE user_id = $1
AND status = $2 AND status = $2
AND DATE(created_at) >= $3 AND recharge_type != $3
AND DATE(created_at) <= $4 AND DATE(created_at) >= $4
AND DATE(created_at) <= $5
GROUP BY DATE(created_at) GROUP BY DATE(created_at)
ORDER BY date ASC 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 { if err != nil {
return nil, err return nil, err
} }
@@ -232,7 +233,7 @@ func (r *GormRechargeRecordRepository) GetDailyStatsByUserId(ctx context.Context
return results, nil return results, nil
} }
// GetMonthlyStatsByUserId 获取用户每月充值统计 // GetMonthlyStatsByUserId 获取用户每月充值统计(排除赠送)
func (r *GormRechargeRecordRepository) GetMonthlyStatsByUserId(ctx context.Context, userId string, startDate, endDate time.Time) ([]map[string]interface{}, error) { func (r *GormRechargeRecordRepository) GetMonthlyStatsByUserId(ctx context.Context, userId string, startDate, endDate time.Time) ([]map[string]interface{}, error) {
var results []map[string]interface{} var results []map[string]interface{}
@@ -244,13 +245,14 @@ func (r *GormRechargeRecordRepository) GetMonthlyStatsByUserId(ctx context.Conte
FROM recharge_records FROM recharge_records
WHERE user_id = $1 WHERE user_id = $1
AND status = $2 AND status = $2
AND created_at >= $3 AND recharge_type != $3
AND created_at <= $4 AND created_at >= $4
AND created_at <= $5
GROUP BY TO_CHAR(created_at, 'YYYY-MM') GROUP BY TO_CHAR(created_at, 'YYYY-MM')
ORDER BY month ASC 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 { if err != nil {
return nil, err return nil, err
} }
@@ -258,27 +260,27 @@ func (r *GormRechargeRecordRepository) GetMonthlyStatsByUserId(ctx context.Conte
return results, nil return results, nil
} }
// GetSystemTotalAmount 获取系统总充值金额 // GetSystemTotalAmount 获取系统总充值金额(排除赠送)
func (r *GormRechargeRecordRepository) GetSystemTotalAmount(ctx context.Context) (float64, error) { func (r *GormRechargeRecordRepository) GetSystemTotalAmount(ctx context.Context) (float64, error) {
var total float64 var total float64
err := r.GetDB(ctx).Model(&entities.RechargeRecord{}). 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)"). Select("COALESCE(SUM(amount), 0)").
Scan(&total).Error Scan(&total).Error
return total, err return total, err
} }
// GetSystemAmountByDateRange 获取系统指定时间范围内的充值金额 // GetSystemAmountByDateRange 获取系统指定时间范围内的充值金额(排除赠送)
func (r *GormRechargeRecordRepository) GetSystemAmountByDateRange(ctx context.Context, startDate, endDate time.Time) (float64, error) { func (r *GormRechargeRecordRepository) GetSystemAmountByDateRange(ctx context.Context, startDate, endDate time.Time) (float64, error) {
var total float64 var total float64
err := r.GetDB(ctx).Model(&entities.RechargeRecord{}). 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)"). Select("COALESCE(SUM(amount), 0)").
Scan(&total).Error Scan(&total).Error
return total, err return total, err
} }
// GetSystemDailyStats 获取系统每日充值统计 // GetSystemDailyStats 获取系统每日充值统计(排除赠送)
func (r *GormRechargeRecordRepository) GetSystemDailyStats(ctx context.Context, startDate, endDate time.Time) ([]map[string]interface{}, error) { func (r *GormRechargeRecordRepository) GetSystemDailyStats(ctx context.Context, startDate, endDate time.Time) ([]map[string]interface{}, error) {
var results []map[string]interface{} var results []map[string]interface{}
@@ -288,13 +290,14 @@ func (r *GormRechargeRecordRepository) GetSystemDailyStats(ctx context.Context,
COALESCE(SUM(amount), 0) as amount COALESCE(SUM(amount), 0) as amount
FROM recharge_records FROM recharge_records
WHERE status = ? WHERE status = ?
AND recharge_type != ?
AND DATE(created_at) >= ? AND DATE(created_at) >= ?
AND DATE(created_at) <= ? AND DATE(created_at) <= ?
GROUP BY DATE(created_at) GROUP BY DATE(created_at)
ORDER BY date ASC 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 { if err != nil {
return nil, err return nil, err
} }
@@ -302,7 +305,7 @@ func (r *GormRechargeRecordRepository) GetSystemDailyStats(ctx context.Context,
return results, nil return results, nil
} }
// GetSystemMonthlyStats 获取系统每月充值统计 // GetSystemMonthlyStats 获取系统每月充值统计(排除赠送)
func (r *GormRechargeRecordRepository) GetSystemMonthlyStats(ctx context.Context, startDate, endDate time.Time) ([]map[string]interface{}, error) { func (r *GormRechargeRecordRepository) GetSystemMonthlyStats(ctx context.Context, startDate, endDate time.Time) ([]map[string]interface{}, error) {
var results []map[string]interface{} var results []map[string]interface{}
@@ -312,13 +315,14 @@ func (r *GormRechargeRecordRepository) GetSystemMonthlyStats(ctx context.Context
COALESCE(SUM(amount), 0) as amount COALESCE(SUM(amount), 0) as amount
FROM recharge_records FROM recharge_records
WHERE status = ? WHERE status = ?
AND recharge_type != ?
AND created_at >= ? AND created_at >= ?
AND created_at <= ? AND created_at <= ?
GROUP BY TO_CHAR(created_at, 'YYYY-MM') GROUP BY TO_CHAR(created_at, 'YYYY-MM')
ORDER BY month ASC 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 { if err != nil {
return nil, err return nil, err
} }

View File

@@ -596,7 +596,7 @@ func (r *GormUserRepository) GetUserCallRankingByConsumption(ctx context.Context
return results, nil return results, nil
} }
// GetRechargeRanking 获取充值排行 // GetRechargeRanking 获取充值排行(排除赠送,只统计成功状态)
func (r *GormUserRepository) GetRechargeRanking(ctx context.Context, period string, limit int) ([]map[string]interface{}, error) { func (r *GormUserRepository) GetRechargeRanking(ctx context.Context, period string, limit int) ([]map[string]interface{}, error) {
var sql string var sql string
var args []interface{} 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 enterprise_infos ei ON u.id = ei.user_id
LEFT JOIN recharge_records rr ON u.id = rr.user_id LEFT JOIN recharge_records rr ON u.id = rr.user_id
AND DATE(rr.created_at) = CURRENT_DATE AND DATE(rr.created_at) = CURRENT_DATE
AND rr.status = 'success'
AND rr.recharge_type != 'gift'
WHERE u.deleted_at IS NULL WHERE u.deleted_at IS NULL
GROUP BY u.id, ei.company_name, u.username, u.phone GROUP BY u.id, ei.company_name, u.username, u.phone
HAVING COALESCE(SUM(rr.amount), 0) > 0 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 enterprise_infos ei ON u.id = ei.user_id
LEFT JOIN recharge_records rr ON u.id = rr.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 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 WHERE u.deleted_at IS NULL
GROUP BY u.id, ei.company_name, u.username, u.phone GROUP BY u.id, ei.company_name, u.username, u.phone
HAVING COALESCE(SUM(rr.amount), 0) > 0 HAVING COALESCE(SUM(rr.amount), 0) > 0
@@ -645,6 +649,8 @@ func (r *GormUserRepository) GetRechargeRanking(ctx context.Context, period stri
FROM users u FROM users u
LEFT JOIN enterprise_infos ei ON u.id = ei.user_id LEFT JOIN enterprise_infos ei ON u.id = ei.user_id
LEFT JOIN recharge_records rr ON u.id = rr.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 WHERE u.deleted_at IS NULL
GROUP BY u.id, ei.company_name, u.username, u.phone GROUP BY u.id, ei.company_name, u.username, u.phone
HAVING COALESCE(SUM(rr.amount), 0) > 0 HAVING COALESCE(SUM(rr.amount), 0) > 0