mirror of https://github.com/rocboss/paopao-ce
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.
156 lines
3.7 KiB
156 lines
3.7 KiB
2 years ago
|
package jinzhu
|
||
3 years ago
|
|
||
|
import (
|
||
3 years ago
|
"github.com/rocboss/paopao-ce/internal/conf"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
3 years ago
|
"github.com/rocboss/paopao-ce/internal/model"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/pkg/types"
|
||
3 years ago
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
2 years ago
|
var (
|
||
|
_ core.WalletService = (*walletServant)(nil)
|
||
|
)
|
||
|
|
||
|
type walletServant struct {
|
||
|
db *gorm.DB
|
||
|
}
|
||
|
|
||
|
func newWalletService(db *gorm.DB) core.WalletService {
|
||
|
return &walletServant{
|
||
|
db: db,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (d *walletServant) GetRechargeByID(id int64) (*model.WalletRecharge, error) {
|
||
3 years ago
|
recharge := &model.WalletRecharge{
|
||
|
Model: &model.Model{
|
||
|
ID: id,
|
||
|
},
|
||
|
}
|
||
|
|
||
2 years ago
|
return recharge.Get(d.db)
|
||
3 years ago
|
}
|
||
2 years ago
|
func (d *walletServant) CreateRecharge(userId, amount int64) (*model.WalletRecharge, error) {
|
||
3 years ago
|
recharge := &model.WalletRecharge{
|
||
|
UserID: userId,
|
||
|
Amount: amount,
|
||
|
}
|
||
|
|
||
2 years ago
|
return recharge.Create(d.db)
|
||
|
}
|
||
|
|
||
|
func (d *walletServant) GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, error) {
|
||
|
statement := &model.WalletStatement{
|
||
|
UserID: userID,
|
||
|
}
|
||
|
|
||
|
return statement.List(d.db, &model.ConditionsT{
|
||
|
"ORDER": "id DESC",
|
||
|
}, offset, limit)
|
||
|
}
|
||
|
|
||
|
func (d *walletServant) GetUserWalletBillCount(userID int64) (int64, error) {
|
||
|
statement := &model.WalletStatement{
|
||
|
UserID: userID,
|
||
|
}
|
||
|
return statement.Count(d.db, &model.ConditionsT{})
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func (d *walletServant) HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo string) error {
|
||
3 years ago
|
user, _ := (&model.User{
|
||
|
Model: &model.Model{
|
||
|
ID: recharge.UserID,
|
||
|
},
|
||
2 years ago
|
}).Get(d.db)
|
||
3 years ago
|
|
||
2 years ago
|
return d.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(&model.WalletStatement{
|
||
|
UserID: user.ID,
|
||
|
ChangeAmount: recharge.Amount,
|
||
|
BalanceSnapshot: user.Balance + recharge.Amount,
|
||
|
Reason: "用户充值",
|
||
|
}).Error; err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 标记为已付款
|
||
2 years ago
|
if err := tx.Model(recharge).Updates(map[string]types.Any{
|
||
3 years ago
|
"trade_no": tradeNo,
|
||
|
"trade_status": "TRADE_SUCCESS",
|
||
|
}).Error; err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 返回 nil 提交事务
|
||
|
return nil
|
||
|
})
|
||
|
}
|
||
|
|
||
2 years ago
|
func (d *walletServant) HandlePostAttachmentBought(post *model.Post, user *model.User) error {
|
||
|
return d.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(&model.WalletStatement{
|
||
|
PostID: post.ID,
|
||
|
UserID: user.ID,
|
||
|
ChangeAmount: -post.AttachmentPrice,
|
||
|
BalanceSnapshot: user.Balance - post.AttachmentPrice,
|
||
|
Reason: "购买附件支出",
|
||
|
}).Error; err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 新增附件购买记录
|
||
|
if err := tx.Create(&model.PostAttachmentBill{
|
||
|
PostID: post.ID,
|
||
|
UserID: user.ID,
|
||
|
PaidAmount: post.AttachmentPrice,
|
||
|
}).Error; err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 对附件主新增账单
|
||
3 years ago
|
income := int64(float64(post.AttachmentPrice) * conf.AppSetting.AttachmentIncomeRate)
|
||
3 years ago
|
if income > 0 {
|
||
|
master := &model.User{
|
||
|
Model: &model.Model{
|
||
|
ID: post.UserID,
|
||
|
},
|
||
|
}
|
||
2 years ago
|
master, _ = master.Get(d.db)
|
||
3 years ago
|
|
||
|
if err := tx.Model(master).Update("balance", gorm.Expr("balance + ?", income)).Error; err != nil {
|
||
|
// 返回任何错误都会回滚事务
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 新增账单
|
||
|
if err := tx.Create(&model.WalletStatement{
|
||
|
PostID: post.ID,
|
||
|
UserID: master.ID,
|
||
|
ChangeAmount: income,
|
||
|
BalanceSnapshot: master.Balance + income,
|
||
|
Reason: "出售附件收入",
|
||
|
}).Error; err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 返回 nil 提交事务
|
||
|
return nil
|
||
|
})
|
||
|
}
|