fix(billing): 异步任务退款时同步减少 used_quota (#6795)

* fix(billing): 异步任务退款时同步减少 used_quota
退款时仅恢复了 quota(剩余额度),但未同步减少 used_quota(已用额度),
导致"总额度"(quota + used_quota)随退款次数持续虚增,超出用户实际充值金额。

修复三处退款路径:
- RefundTaskQuota:任务失败完整退款
- RecalculateTaskQuota:差额结算退款分支
- controller/midjourney.go:Midjourney 任务失败退款

新增 model.UpdateUserUsedQuota 公开函数,仅调整 used_quota 不影响 request_count。

* fix(billing): 任务退款时同步扣减渠道 used_quota

* fix(billing): complete async task refund accounting

* style(model): group internal Midjourney fields

---------

Co-authored-by: CaIon <i@caion.me>
This commit is contained in:
wans10
2026-08-13 22:06:40 +08:00
committed by GitHub
co-authored by CaIon
parent ccd535ef8e
commit 58d4e9bd3b
9 changed files with 667 additions and 81 deletions
+16
View File
@@ -23,6 +23,9 @@ type Midjourney struct {
Quota int `json:"quota"`
Buttons string `json:"buttons"`
Properties string `json:"properties"`
TokenId int `json:"-" gorm:"default:0"`
BillingChannelId int `json:"-" gorm:"default:0"`
}
// TaskQueryParams 用于包含所有搜索条件的结构体,可以根据需求添加更多字段
@@ -170,6 +173,19 @@ func (midjourney *Midjourney) Update() error {
return err
}
func (midjourney *Midjourney) UpdateBillingState() error {
return DB.Model(midjourney).
Select("quota", "token_id", "billing_channel_id").
Updates(midjourney).Error
}
func (midjourney *Midjourney) GetBillingChannelId() int {
if midjourney.BillingChannelId > 0 {
return midjourney.BillingChannelId
}
return midjourney.ChannelId
}
// UpdateWithStatus performs a conditional UPDATE guarded by fromStatus (CAS).
// Returns (true, nil) if this caller won the update, (false, nil) if
// another process already moved the task out of fromStatus.