temp
This commit is contained in:
67
internal/domains/finance/entities/user_secrets.go
Normal file
67
internal/domains/finance/entities/user_secrets.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UserSecrets 用户密钥实体
|
||||
// 存储用户的API访问密钥信息,用于第三方服务集成和API调用
|
||||
// 支持密钥的生命周期管理,包括激活状态、过期时间、使用统计等
|
||||
type UserSecrets struct {
|
||||
// 基础标识
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" comment:"密钥记录唯一标识"`
|
||||
UserID string `gorm:"type:varchar(36);not null;uniqueIndex" comment:"关联用户ID"`
|
||||
AccessID string `gorm:"type:varchar(100);not null;uniqueIndex" comment:"访问ID(用于API认证)"`
|
||||
AccessKey string `gorm:"type:varchar(255);not null" comment:"访问密钥(加密存储)"`
|
||||
|
||||
// 密钥状态 - 密钥的生命周期管理
|
||||
IsActive bool `gorm:"default:true" comment:"密钥是否激活"`
|
||||
LastUsedAt *time.Time `comment:"最后使用时间"`
|
||||
ExpiresAt *time.Time `comment:"密钥过期时间"`
|
||||
|
||||
// 时间戳字段
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (UserSecrets) TableName() string {
|
||||
return "user_secrets"
|
||||
}
|
||||
|
||||
// IsExpired 检查密钥是否已过期
|
||||
// 判断密钥是否超过有效期,过期后需要重新生成或续期
|
||||
func (u *UserSecrets) IsExpired() bool {
|
||||
if u.ExpiresAt == nil {
|
||||
return false // 没有过期时间表示永不过期
|
||||
}
|
||||
return time.Now().After(*u.ExpiresAt)
|
||||
}
|
||||
|
||||
// IsValid 检查密钥是否有效
|
||||
// 综合判断密钥是否可用,包括激活状态和过期状态检查
|
||||
func (u *UserSecrets) IsValid() bool {
|
||||
return u.IsActive && !u.IsExpired()
|
||||
}
|
||||
|
||||
// UpdateLastUsedAt 更新最后使用时间
|
||||
// 在密钥被使用时调用,记录最新的使用时间,用于使用统计和监控
|
||||
func (u *UserSecrets) UpdateLastUsedAt() {
|
||||
now := time.Now()
|
||||
u.LastUsedAt = &now
|
||||
}
|
||||
|
||||
// Deactivate 停用密钥
|
||||
// 将密钥设置为非激活状态,禁止使用该密钥进行API调用
|
||||
func (u *UserSecrets) Deactivate() {
|
||||
u.IsActive = false
|
||||
}
|
||||
|
||||
// Activate 激活密钥
|
||||
// 重新启用密钥,允许使用该密钥进行API调用
|
||||
func (u *UserSecrets) Activate() {
|
||||
u.IsActive = true
|
||||
}
|
||||
71
internal/domains/finance/entities/wallet.go
Normal file
71
internal/domains/finance/entities/wallet.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Wallet 钱包实体
|
||||
// 用户数字钱包的核心信息,支持多种钱包类型和精确的余额管理
|
||||
// 使用decimal类型确保金额计算的精确性,避免浮点数精度问题
|
||||
type Wallet struct {
|
||||
// 基础标识
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"钱包唯一标识"`
|
||||
UserID string `gorm:"type:varchar(36);not null;uniqueIndex" json:"user_id" comment:"关联用户ID"`
|
||||
|
||||
// 钱包状态 - 钱包的基本状态信息
|
||||
IsActive bool `gorm:"default:true" json:"is_active" comment:"钱包是否激活"`
|
||||
Balance decimal.Decimal `gorm:"type:decimal(20,8);default:0" json:"balance" comment:"钱包余额(精确到8位小数)"`
|
||||
|
||||
// 钱包信息 - 钱包的详细配置信息
|
||||
WalletAddress string `gorm:"type:varchar(255)" json:"wallet_address,omitempty" comment:"钱包地址"`
|
||||
WalletType string `gorm:"type:varchar(50);default:'MAIN'" json:"wallet_type" comment:"钱包类型(MAIN/DEPOSIT/WITHDRAWAL)"` // MAIN, DEPOSIT, WITHDRAWAL
|
||||
|
||||
// 时间戳字段
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-" comment:"软删除时间"`
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (Wallet) TableName() string {
|
||||
return "wallets"
|
||||
}
|
||||
|
||||
// IsZeroBalance 检查余额是否为零
|
||||
// 判断钱包余额是否为零,用于业务逻辑判断
|
||||
func (w *Wallet) IsZeroBalance() bool {
|
||||
return w.Balance.IsZero()
|
||||
}
|
||||
|
||||
// HasSufficientBalance 检查是否有足够余额
|
||||
// 判断钱包余额是否足够支付指定金额,用于交易前的余额验证
|
||||
func (w *Wallet) HasSufficientBalance(amount decimal.Decimal) bool {
|
||||
return w.Balance.GreaterThanOrEqual(amount)
|
||||
}
|
||||
|
||||
// AddBalance 增加余额
|
||||
// 向钱包增加指定金额,用于充值、收入等场景
|
||||
func (w *Wallet) AddBalance(amount decimal.Decimal) {
|
||||
w.Balance = w.Balance.Add(amount)
|
||||
}
|
||||
|
||||
// SubtractBalance 减少余额
|
||||
// 从钱包扣除指定金额,用于消费、转账等场景
|
||||
// 如果余额不足会返回错误,确保资金安全
|
||||
func (w *Wallet) SubtractBalance(amount decimal.Decimal) error {
|
||||
if !w.HasSufficientBalance(amount) {
|
||||
return fmt.Errorf("余额不足")
|
||||
}
|
||||
w.Balance = w.Balance.Sub(amount)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetFormattedBalance 获取格式化的余额字符串
|
||||
// 将decimal类型的余额转换为字符串格式,便于显示和传输
|
||||
func (w *Wallet) GetFormattedBalance() string {
|
||||
return w.Balance.String()
|
||||
}
|
||||
Reference in New Issue
Block a user