diff --git a/common/util/billing.go b/common/util/billing.go index b9dabe6..85e8eed 100644 --- a/common/util/billing.go +++ b/common/util/billing.go @@ -117,23 +117,8 @@ func calculateVideoResolutionBilling(config map[string]any, data map[string]any) } func calculateTTSBilling(config map[string]any, data map[string]any) map[string]any { - unit, _ := config["unit"].(string) - usage := gconv.Float64(data["usage"]) - - tiers := config["pricing"].(map[string]any)["tiers"].([]any) - var matched map[string]any - for _, t := range tiers { - tier := t.(map[string]any) - if usage >= gconv.Float64(tier["min"]) && usage <= gconv.Float64(tier["max"]) { - matched = tier - break - } - } - if matched == nil { - return nil - } - - unitPrice := gconv.Float64(matched["unit_price"]) + usage := gconv.Float64(data["synthesize_text_length"]) + unitPrice := gconv.Float64(config["pricing"]) totalFee := usage * unitPrice return map[string]any{ @@ -143,10 +128,7 @@ func calculateTTSBilling(config map[string]any, data map[string]any) map[string] // 明细 "prompt_tokens": 0, "completion_tokens": int64(usage), - "usage": usage, - "unit": unit, "unit_price": unitPrice, - "tier": fmt.Sprintf("[%v, %v]", matched["min"], matched["max"]), } } diff --git a/service/task/worker.go b/service/task/worker.go index 5f466c8..357c8ce 100644 --- a/service/task/worker.go +++ b/service/task/worker.go @@ -126,7 +126,13 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa task.BillingData[0] = billingResult totalFee := gconv.Float64(billingResult["total_fee"]) if totalFee > 0 { - _ = gateway.DeductBalance(util.AsyncCtx(ctx), model.TenantId, -totalFee) + for attempt := 0; attempt <= maxRetry; attempt++ { + err = gateway.DeductBalance(util.AsyncCtx(ctx), model.TenantId, -totalFee) + if err == nil { + break + } + g.Log().Warningf(ctx, "[handleOne] 扣除余额失败 taskId=%s attempt=%d/%d err=%v", task.TaskID, attempt, maxRetry, err) + } } } } @@ -337,7 +343,13 @@ func (w *asyncWorker) parseAndRetry(ctx context.Context, body map[string]any, mo task.BillingData = append(task.BillingData, billingResult) totalFee := gconv.Float64(billingResult["total_fee"]) if totalFee > 0 { - _ = gateway.DeductBalance(util.AsyncCtx(ctx), task.TenantId, -totalFee) + for a := 0; a <= maxRetry; a++ { + errr := gateway.DeductBalance(util.AsyncCtx(ctx), task.TenantId, -totalFee) + if errr == nil { + break + } + g.Log().Warningf(ctx, "[handleOne] 扣除余额失败 taskId=%s attempt=%d/%d err=%v", task.TaskID, a, maxRetry, errr) + } } }