feat(v2): LimitAware 真实用量源——DBUsageSource 快照 + 对账 job 周期聚合当日已收(替 NopUsage)

This commit is contained in:
wangjia
2026-07-10 17:32:48 +08:00
parent e04cd73983
commit 4ac4e6ca9f
4 changed files with 142 additions and 0 deletions
+21
View File
@@ -126,3 +126,24 @@ func (s *OrderStore) ExpireStaleOrders(cutoff time.Time, limit int) (int64, erro
}
return res.RowsAffected, nil
}
// SumPaidAttemptMinorByAccountSince 聚合各账户自 since 起的已付金额(minor),供 LimitAware
// 判当日用量。量纲:attempt.AmountMinor 即账户所属渠道结算币种 minor(与 DailyLimit 同量纲)。
func (s *OrderStore) SumPaidAttemptMinorByAccountSince(since time.Time) (map[string]int64, error) {
type row struct {
AccountID string
Total int64
}
var rows []row
if err := s.db.Model(&model.Attempt{}).
Select("account_id, SUM(amount_minor) AS total").
Where("status = ? AND account_id <> '' AND paid_at >= ?", model.AttemptPaid, since).
Group("account_id").Scan(&rows).Error; err != nil {
return nil, fmt.Errorf("store.SumPaidAttemptMinorByAccountSince: %w", err)
}
out := make(map[string]int64, len(rows))
for _, r := range rows {
out[r.AccountID] = r.Total
}
return out, nil
}