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

@@ -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
}