gin-example/pkg/domain/entity/user.go
2023-09-14 14:49:47 +08:00

51 lines
889 B
Go

package entity
import (
"errors"
"gorm.io/plugin/soft_delete"
"time"
)
//AccountEntity 用户模型
type AccountEntity struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt soft_delete.DeletedAt
Account string
TelNum string
Currency string
Amount *Money
DailyLimit *Money
}
// 转出
func (a *AccountEntity) Withdraw(money Money) error {
if a.Amount.Currency != money.Currency {
return errors.New("货币不对")
}
if a.Amount.Less(money.Amount) {
return errors.New("余额不足")
}
if a.DailyLimit.Less(money.Amount) {
return errors.New("当日余额不足")
}
a.Amount = a.Amount.Subtract(money.Amount)
return nil
}
// 转入
func (a *AccountEntity) Deposit(money Money) error {
if a.Amount.Currency != money.Currency {
return errors.New("货币不对")
}
a.Amount = a.Amount.Add(money.Amount)
return nil
}