You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
5.1 KiB
159 lines
5.1 KiB
package wallet
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/shopspring/decimal"
|
|
"gorm.io/gorm"
|
|
"recook/internal/v2/model/gys"
|
|
)
|
|
|
|
var DepositLogic = &depositLogic{}
|
|
|
|
type depositLogic struct {
|
|
}
|
|
|
|
// Add @Title 预存款充值
|
|
func (d *depositLogic) Add(tx *gorm.DB, sourceId uint, amount decimal.Decimal, status uint) error {
|
|
sourceWallet := gys.GysSourceWallet{SourceId: sourceId}
|
|
tx.Where(&sourceWallet).First(&sourceWallet)
|
|
return tx.Transaction(func(tx *gorm.DB) error {
|
|
if sourceWallet.Id > 0 {
|
|
if status == 1 {
|
|
//充值
|
|
if tx.Model(&sourceWallet).UpdateColumns(map[string]interface{}{
|
|
"deposit_amount": gorm.Expr("deposit_amount + ?", amount),
|
|
"deposit_total_amount": gorm.Expr("deposit_total_amount + ?", amount),
|
|
}).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
} else {
|
|
//退款
|
|
if tx.Model(&sourceWallet).UpdateColumns(map[string]interface{}{
|
|
"deposit_amount": gorm.Expr("deposit_amount - ?", amount),
|
|
"deposit_total_amount": gorm.Expr("deposit_total_amount - ?", amount),
|
|
}).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
}
|
|
|
|
} else {
|
|
if status == 1 {
|
|
sourceWallet.DepositAmount = amount
|
|
sourceWallet.DepositTotalAmount = amount
|
|
if tx.Create(&sourceWallet).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
} else {
|
|
sourceWallet.DepositAmount = amount.Mul(decimal.NewFromInt32(-1))
|
|
sourceWallet.DepositTotalAmount = amount.Mul(decimal.NewFromInt32(-1))
|
|
if tx.Create(&sourceWallet).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
}
|
|
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Sub @Title 预存款退款
|
|
func (d *depositLogic) Sub(tx *gorm.DB, sourceId uint, amount decimal.Decimal) error {
|
|
sourceWallet := gys.GysSourceWallet{SourceId: sourceId}
|
|
tx.Where(&sourceWallet).First(&sourceWallet)
|
|
return tx.Transaction(func(tx *gorm.DB) error {
|
|
if sourceWallet.Id > 0 {
|
|
if tx.Model(&sourceId).UpdateColumns(map[string]interface{}{
|
|
"deposit_amount": gorm.Expr("deposit_amount - ?", amount),
|
|
"deposit_total_amount": gorm.Expr("deposit_total_amount - ?", amount),
|
|
}).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
} else {
|
|
sourceWallet.DepositAmount = amount.Mul(decimal.NewFromInt32(-1))
|
|
sourceWallet.DepositTotalAmount = amount.Mul(decimal.NewFromInt32(-1))
|
|
if tx.Create(&sourceWallet).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Freeze @Title 预存款充值冻结
|
|
func (d *depositLogic) Freeze(tx *gorm.DB, sourceId uint, amount decimal.Decimal) error {
|
|
sourceWallet := gys.GysSourceWallet{SourceId: sourceId}
|
|
tx.Where(&sourceWallet).First(&sourceWallet)
|
|
return tx.Transaction(func(tx *gorm.DB) error {
|
|
if sourceWallet.Id > 0 {
|
|
if tx.Model(&sourceId).UpdateColumns(map[string]interface{}{
|
|
"deposit_amount": gorm.Expr("deposit_amount - ?", amount),
|
|
"deposit_freeze_amount": gorm.Expr("deposit_freeze_amount + ?", amount),
|
|
}).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
} else {
|
|
sourceWallet.DepositAmount = amount.Mul(decimal.NewFromInt32(-1))
|
|
sourceWallet.DepositFreezeAmount = amount
|
|
if tx.Create(&sourceWallet).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// UnFreeze @Title 预存款充值解冻
|
|
func (d *depositLogic) UnFreeze(tx *gorm.DB, sourceId uint, amount decimal.Decimal) error {
|
|
sourceWallet := gys.GysSourceWallet{SourceId: sourceId}
|
|
tx.Where(&sourceWallet).First(&sourceWallet)
|
|
return tx.Transaction(func(tx *gorm.DB) error {
|
|
if sourceWallet.Id > 0 {
|
|
if tx.Model(&sourceId).UpdateColumns(map[string]interface{}{
|
|
"deposit_amount": gorm.Expr("deposit_amount + ?", amount),
|
|
"deposit_freeze_amount": gorm.Expr("deposit_freeze_amount - ?", amount),
|
|
}).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
} else {
|
|
sourceWallet.DepositAmount = amount
|
|
sourceWallet.DepositFreezeAmount = amount.Mul(decimal.NewFromInt32(-1))
|
|
if tx.Create(&sourceWallet).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Bill @Title 预存款结算
|
|
func (d *depositLogic) Bill(tx *gorm.DB, sourceId uint, amount decimal.Decimal, payImg string) error {
|
|
sourceWallet := gys.GysSourceWallet{SourceId: sourceId}
|
|
tx.Where(&sourceWallet).First(&sourceWallet)
|
|
return tx.Transaction(func(tx *gorm.DB) error {
|
|
if sourceWallet.Id > 0 {
|
|
if tx.Model(&sourceId).UpdateColumns(map[string]interface{}{
|
|
"deposit_freeze_amount": gorm.Expr("deposit_freeze_amount - ?", amount),
|
|
"deposit_bill_amount": gorm.Expr("deposit_bill_amount + ?", amount),
|
|
}).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
} else {
|
|
sourceWallet.DepositFreezeAmount = amount.Mul(decimal.NewFromInt32(-1))
|
|
sourceWallet.DepositBillAmount = amount
|
|
if tx.Create(&sourceWallet).Error != nil {
|
|
return errors.New("系统错误")
|
|
}
|
|
}
|
|
//更新动态记录
|
|
if err := tx.Model(&gys.GysSourceDepositHistory{}).Create(&gys.GysSourceDepositHistory{
|
|
SourceId: sourceId,
|
|
Type: 2,
|
|
Proof: payImg,
|
|
Amount: amount,
|
|
AfterAmount: sourceWallet.DepositAmount.Sub(amount),
|
|
}).Error; err != nil {
|
|
}
|
|
return nil
|
|
})
|
|
}
|