v0.1
This commit is contained in:
@@ -9,9 +9,12 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Wallet 钱包实体
|
||||
// Wallet 钱包聚合根
|
||||
// 用户数字钱包的核心信息,支持多种钱包类型和精确的余额管理
|
||||
// 使用decimal类型确保金额计算的精确性,避免浮点数精度问题
|
||||
// 支持欠费(余额<0),但只允许扣到小于0一次,之后不能再扣
|
||||
// 新建钱包时可配置默认额度
|
||||
|
||||
type Wallet struct {
|
||||
// 基础标识
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"钱包唯一标识"`
|
||||
@@ -20,10 +23,7 @@ type Wallet struct {
|
||||
// 钱包状态 - 钱包的基本状态信息
|
||||
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
|
||||
Version int64 `gorm:"version" json:"version" comment:"乐观锁版本号"`
|
||||
|
||||
// 时间戳字段
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
|
||||
@@ -37,36 +37,53 @@ func (Wallet) TableName() string {
|
||||
}
|
||||
|
||||
// IsZeroBalance 检查余额是否为零
|
||||
// 判断钱包余额是否为零,用于业务逻辑判断
|
||||
func (w *Wallet) IsZeroBalance() bool {
|
||||
return w.Balance.IsZero()
|
||||
}
|
||||
|
||||
// HasSufficientBalance 检查是否有足够余额
|
||||
// 判断钱包余额是否足够支付指定金额,用于交易前的余额验证
|
||||
// HasSufficientBalance 检查是否有足够余额(允许透支额度)
|
||||
func (w *Wallet) HasSufficientBalance(amount decimal.Decimal) bool {
|
||||
return w.Balance.GreaterThanOrEqual(amount)
|
||||
// 允许扣到额度下限
|
||||
return w.Balance.Sub(amount).GreaterThanOrEqual(decimal.Zero)
|
||||
}
|
||||
|
||||
// AddBalance 增加余额
|
||||
// 向钱包增加指定金额,用于充值、收入等场景
|
||||
// IsArrears 是否欠费(余额<0)
|
||||
func (w *Wallet) IsArrears() bool {
|
||||
return w.Balance.LessThan(decimal.Zero)
|
||||
}
|
||||
|
||||
// IsLowBalance 是否余额较低(余额<300)
|
||||
func (w *Wallet) IsLowBalance() bool {
|
||||
return w.Balance.LessThan(decimal.NewFromInt(300))
|
||||
}
|
||||
|
||||
// GetBalanceStatus 获取余额状态
|
||||
func (w *Wallet) GetBalanceStatus() string {
|
||||
if w.IsArrears() {
|
||||
return "arrears" // 欠费
|
||||
} else if w.IsLowBalance() {
|
||||
return "low" // 余额较低
|
||||
} else {
|
||||
return "normal" // 正常
|
||||
}
|
||||
}
|
||||
|
||||
// AddBalance 增加余额(只做加法,业务规则由服务层控制是否允许充值)
|
||||
func (w *Wallet) AddBalance(amount decimal.Decimal) {
|
||||
w.Balance = w.Balance.Add(amount)
|
||||
}
|
||||
|
||||
// SubtractBalance 减少余额
|
||||
// 从钱包扣除指定金额,用于消费、转账等场景
|
||||
// 如果余额不足会返回错误,确保资金安全
|
||||
// SubtractBalance 扣减余额,含欠费业务规则
|
||||
func (w *Wallet) SubtractBalance(amount decimal.Decimal) error {
|
||||
if !w.HasSufficientBalance(amount) {
|
||||
return fmt.Errorf("余额不足")
|
||||
if w.Balance.LessThan(decimal.Zero) {
|
||||
return fmt.Errorf("已欠费,不能再扣款")
|
||||
}
|
||||
w.Balance = w.Balance.Sub(amount)
|
||||
newBalance := w.Balance.Sub(amount)
|
||||
w.Balance = newBalance
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetFormattedBalance 获取格式化的余额字符串
|
||||
// 将decimal类型的余额转换为字符串格式,便于显示和传输
|
||||
func (w *Wallet) GetFormattedBalance() string {
|
||||
return w.Balance.String()
|
||||
}
|
||||
@@ -78,3 +95,13 @@ func (w *Wallet) BeforeCreate(tx *gorm.DB) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewWallet 工厂方法
|
||||
func NewWallet(userID string, defaultCreditLimit decimal.Decimal) *Wallet {
|
||||
return &Wallet{
|
||||
UserID: userID,
|
||||
IsActive: true,
|
||||
Balance: defaultCreditLimit,
|
||||
Version: 0,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user