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.
paopao-ce/internal/dao/jinzhu/wallet.go

160 lines
3.8 KiB

// Copyright 2022 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package jinzhu
3 years ago
import (
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/core/ms"
"github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr"
3 years ago
"gorm.io/gorm"
)
var (
_ core.WalletService = (*walletSrv)(nil)
)
type walletSrv struct {
db *gorm.DB
}
func newWalletService(db *gorm.DB) core.WalletService {
return &walletSrv{
db: db,
}
}
func (s *walletSrv) GetRechargeByID(id int64) (*ms.WalletRecharge, error) {
recharge := &dbr.WalletRecharge{
Model: &dbr.Model{
3 years ago
ID: id,
},
}
return recharge.Get(s.db)
3 years ago
}
func (s *walletSrv) CreateRecharge(userId, amount int64) (*ms.WalletRecharge, error) {
recharge := &dbr.WalletRecharge{
3 years ago
UserID: userId,
Amount: amount,
}
return recharge.Create(s.db)
}
func (s *walletSrv) GetUserWalletBills(userID int64, offset, limit int) ([]*ms.WalletStatement, error) {
statement := &dbr.WalletStatement{
UserID: userID,
}
return statement.List(s.db, &dbr.ConditionsT{
"ORDER": "id DESC",
}, offset, limit)
}
func (s *walletSrv) GetUserWalletBillCount(userID int64) (int64, error) {
statement := &dbr.WalletStatement{
UserID: userID,
}
return statement.Count(s.db, &dbr.ConditionsT{})
3 years ago
}
func (s *walletSrv) HandleRechargeSuccess(recharge *ms.WalletRecharge, tradeNo string) error {
user, _ := (&dbr.User{
Model: &dbr.Model{
3 years ago
ID: recharge.UserID,
},
}).Get(s.db)
3 years ago
return s.db.Transaction(func(tx *gorm.DB) error {
3 years ago
// 扣除金额
if err := tx.Model(user).Update("balance", gorm.Expr("balance + ?", recharge.Amount)).Error; err != nil {
// 返回任何错误都会回滚事务
return err
}
// 新增账单
if err := tx.Create(&dbr.WalletStatement{
3 years ago
UserID: user.ID,
ChangeAmount: recharge.Amount,
BalanceSnapshot: user.Balance + recharge.Amount,
Reason: "用户充值",
}).Error; err != nil {
return err
}
// 标记为已付款
if err := tx.Model(recharge).Updates(map[string]any{
3 years ago
"trade_no": tradeNo,
"trade_status": "TRADE_SUCCESS",
}).Error; err != nil {
return err
}
// 返回 nil 提交事务
return nil
})
}
func (s *walletSrv) HandlePostAttachmentBought(post *ms.Post, user *ms.User) error {
return s.db.Transaction(func(tx *gorm.DB) error {
3 years ago
// 扣除金额
if err := tx.Model(user).Update("balance", gorm.Expr("balance - ?", post.AttachmentPrice)).Error; err != nil {
// 返回任何错误都会回滚事务
return err
}
// 新增账单
if err := tx.Create(&dbr.WalletStatement{
3 years ago
PostID: post.ID,
UserID: user.ID,
ChangeAmount: -post.AttachmentPrice,
BalanceSnapshot: user.Balance - post.AttachmentPrice,
Reason: "购买附件支出",
}).Error; err != nil {
return err
}
// 新增附件购买记录
if err := tx.Create(&dbr.PostAttachmentBill{
3 years ago
PostID: post.ID,
UserID: user.ID,
PaidAmount: post.AttachmentPrice,
}).Error; err != nil {
return err
}
// 对附件主新增账单
income := int64(float64(post.AttachmentPrice) * conf.AppSetting.AttachmentIncomeRate)
3 years ago
if income > 0 {
master := &dbr.User{
Model: &dbr.Model{
3 years ago
ID: post.UserID,
},
}
master, _ = master.Get(s.db)
3 years ago
if err := tx.Model(master).Update("balance", gorm.Expr("balance + ?", income)).Error; err != nil {
// 返回任何错误都会回滚事务
return err
}
// 新增账单
if err := tx.Create(&dbr.WalletStatement{
3 years ago
PostID: post.ID,
UserID: master.ID,
ChangeAmount: income,
BalanceSnapshot: master.Balance + income,
Reason: "出售附件收入",
}).Error; err != nil {
return err
}
}
// 返回 nil 提交事务
return nil
})
}