This commit is contained in:
2026-04-25 19:17:19 +08:00
parent ba463ae38d
commit 18c92584d9
10 changed files with 533 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
package entities
import (
"time"
"github.com/google/uuid"
"github.com/shopspring/decimal"
"gorm.io/gorm"
)
const (
// QuotaLedgerChangeTypePurchaseForSub 主账号为子账号购买额度
QuotaLedgerChangeTypePurchaseForSub = "purchase_for_sub"
)
// SubordinateQuotaPurchase 主账号为子账号购买额度记录
type SubordinateQuotaPurchase struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
ParentUserID string `gorm:"type:varchar(36);not null;index" json:"parent_user_id"`
ChildUserID string `gorm:"type:varchar(36);not null;index" json:"child_user_id"`
ProductID string `gorm:"type:varchar(36);not null;index" json:"product_id"`
CallCount int64 `gorm:"type:bigint;not null" json:"call_count"`
UnitPrice decimal.Decimal `gorm:"type:decimal(20,8);not null" json:"unit_price"`
TotalAmount decimal.Decimal `gorm:"type:decimal(20,8);not null" json:"total_amount"`
BusinessRef string `gorm:"type:varchar(64);not null;uniqueIndex" json:"business_ref"`
OperatorUserID string `gorm:"type:varchar(36);not null" json:"operator_user_id"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
func (SubordinateQuotaPurchase) TableName() string {
return "subordinate_quota_purchases"
}
func (q *SubordinateQuotaPurchase) BeforeCreate(tx *gorm.DB) error {
if q.ID == "" {
q.ID = uuid.New().String()
}
return nil
}
// UserProductQuotaAccount 用户产品额度账户(通用模型,适配所有用户)
type UserProductQuotaAccount struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
UserID string `gorm:"type:varchar(36);not null;index:idx_user_product,unique" json:"user_id"`
ProductID string `gorm:"type:varchar(36);not null;index:idx_user_product,unique" json:"product_id"`
TotalQuota int64 `gorm:"type:bigint;not null;default:0" json:"total_quota"`
UsedQuota int64 `gorm:"type:bigint;not null;default:0" json:"used_quota"`
AvailableQuota int64 `gorm:"type:bigint;not null;default:0" json:"available_quota"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
func (UserProductQuotaAccount) TableName() string {
return "user_product_quota_accounts"
}
func (a *UserProductQuotaAccount) BeforeCreate(tx *gorm.DB) error {
if a.ID == "" {
a.ID = uuid.New().String()
}
return nil
}
// UserProductQuotaLedger 用户产品额度流水(通用模型,适配所有用户)
type UserProductQuotaLedger struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
UserID string `gorm:"type:varchar(36);not null;index" json:"user_id"`
ProductID string `gorm:"type:varchar(36);not null;index" json:"product_id"`
ChangeType string `gorm:"type:varchar(50);not null;index" json:"change_type"`
DeltaQuota int64 `gorm:"type:bigint;not null" json:"delta_quota"`
BeforeQuota int64 `gorm:"type:bigint;not null" json:"before_quota"`
AfterQuota int64 `gorm:"type:bigint;not null" json:"after_quota"`
SourceID string `gorm:"type:varchar(36);index" json:"source_id"`
OperatorID string `gorm:"type:varchar(36);not null" json:"operator_id"`
Remark string `gorm:"type:varchar(255)" json:"remark"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
func (UserProductQuotaLedger) TableName() string {
return "user_product_quota_ledgers"
}
func (l *UserProductQuotaLedger) BeforeCreate(tx *gorm.DB) error {
if l.ID == "" {
l.ID = uuid.New().String()
}
return nil
}

View File

@@ -28,4 +28,15 @@ type SubordinateRepository interface {
// 划拨
CreateWalletAllocation(ctx context.Context, a *entities.SubordinateWalletAllocation) error
ListWalletAllocationsByParentAndChild(ctx context.Context, parentUserID, childUserID string, limit, offset int) ([]*entities.SubordinateWalletAllocation, int64, error)
// 额度购买
CreateQuotaPurchase(ctx context.Context, p *entities.SubordinateQuotaPurchase) error
ListQuotaPurchasesByParentAndChild(ctx context.Context, parentUserID, childUserID string, limit, offset int) ([]*entities.SubordinateQuotaPurchase, int64, error)
// 额度账户
FindQuotaAccount(ctx context.Context, userID, productID string) (*entities.UserProductQuotaAccount, error)
CreateQuotaAccount(ctx context.Context, account *entities.UserProductQuotaAccount) error
UpdateQuotaAccount(ctx context.Context, account *entities.UserProductQuotaAccount) error
ListQuotaAccountsByUser(ctx context.Context, userID string) ([]*entities.UserProductQuotaAccount, error)
CreateQuotaLedger(ctx context.Context, ledger *entities.UserProductQuotaLedger) error
}