Files
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

147 lines
4.4 KiB
Go

package walletser
import (
"91porn-server/app/appg"
"91porn-server/common/db"
"91porn-server/common/log"
"91porn-server/common/stderr"
"91porn-server/models/v/txnmod"
"91porn-server/models/v/walletmod"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func debitPlan(w *walletmod.Wallet, amt int64) *walletmod.DebitPlan {
p := walletmod.DebitPlan{}
l1 := w.Amount - amt
if l1 >= 0 { //amount够了
p.Amount = amt
return &p
} else { // l1 < 0
//amount 扣完
l2 := w.Income + l1
if l2 >= 0 {
p.Amount = w.Amount
p.Income = -l1
return &p
} else {
return nil //余额不足
}
}
}
const (
StoreBuyGoods = 1
StoreBuyNudeChat = 2
StorePublishWish = 3
StoreGoodsOrderRefund = 4
StoreNudeChatOrderRefund = 5
StoreWishRefund = 6
StoreWishEdit = 7
)
// StoreDeductBalance 商城相关扣款
func StoreDeductBalance(uid uint64, amount int64, orderId primitive.ObjectID, source int, desc string) (balance int64, code stderr.Code) {
w, err := walletmod.GetWallet(uid)
if err != nil || w == nil {
return 0, stderr.ErrNetWorkBusy
}
plan := debitPlan(w, amount)
if plan == nil {
return 0, stderr.InsufficientBalance
}
var tranType txnmod.TransType
switch source {
case StoreBuyGoods:
tranType = txnmod.StoreBuyGoods
case StoreBuyNudeChat:
tranType = txnmod.StoreBuyNudeChat
case StorePublishWish:
tranType = txnmod.StorePublishWish
case StoreWishEdit:
tranType = txnmod.StoreWishEdit
default:
return 0, stderr.ErrParamError
}
err = appg.VideoDB.Trans(func(t *db.MongoTool) (err error) {
wallet, err := walletmod.Debit(t, plan, uid)
if err != nil {
log.Error("StoreDeductBalance walletmod.Debit fail", log.Any("uid", uid), log.Any("orderId", orderId), log.Any("amount", amount), log.Any("source", source))
return
}
// 写入流水
txnLog := &txnmod.TransactionLog{UID: uid,
Amount: -amount,
ActualAmount: float64(-amount),
TranType: tranType.Key(),
TranTypeInt: int64(tranType),
TransNo: orderId,
Desc: desc,
//DiscDoc: discDoc,
//SysType: u.SysType,
RealAmount: walletmod.GetRealAmount(wallet),
}
err = txnmod.InsertTransactionLog(t, txnLog)
if err != nil {
log.Error("StoreDeductBalance txnmod.InsertTransactionLog fail", log.Any("uid", uid), log.Any("orderId", orderId), log.Any("amount", amount), log.Any("source", source))
return
}
balance = wallet.Amount + wallet.Income
return nil
})
if err != nil {
log.Error("StoreDeductBalance fail", log.Any("uid", uid), log.Any("orderId", orderId), log.Any("amount", amount), log.Any("source", source))
return 0, stderr.Failure //通知消息
}
return balance, stderr.Success
}
// StoreRefund 商城相关退款
func StoreRefund(uid uint64, amount int64, orderId primitive.ObjectID, source int, desc string) (balance int64, code stderr.Code) {
w, err := walletmod.GetWallet(uid)
if err != nil || w == nil {
return 0, stderr.ErrNetWorkBusy
}
var tranType txnmod.TransType
switch source {
case StoreGoodsOrderRefund:
tranType = txnmod.StoreGoodsOrderRefund
case StoreNudeChatOrderRefund:
tranType = txnmod.StoreNudeChatOrderRefund
case StoreWishRefund:
tranType = txnmod.StoreWishRefund
default:
return 0, stderr.ErrParamError
}
err = appg.VideoDB.Trans(func(t *db.MongoTool) (err error) {
wallet, err := walletmod.CreditAmount(t, amount, uid)
if err != nil {
log.Error("StoreRefund walletmod.CreditAmount fail", log.Any("uid", uid), log.Any("orderId", orderId), log.Any("amount", amount), log.Any("source", source))
return
}
// 写入流水
txnLog := &txnmod.TransactionLog{UID: uid,
Amount: amount,
ActualAmount: float64(amount),
TranType: tranType.Key(),
TranTypeInt: int64(tranType),
TransNo: orderId,
Desc: desc,
//DiscDoc: discDoc,
//SysType: u.SysType,
RealAmount: walletmod.GetRealAmount(wallet),
}
err = txnmod.InsertTransactionLog(t, txnLog)
if err != nil {
log.Error("StoreRefund txnmod.InsertTransactionLog fail", log.Any("uid", uid), log.Any("orderId", orderId), log.Any("amount", amount), log.Any("source", source))
return
}
balance = wallet.Amount + wallet.Income
return nil
})
if err != nil {
log.Error("StoreRefund fail", log.Any("uid", uid), log.Any("orderId", orderId), log.Any("amount", amount), log.Any("source", source))
return 0, stderr.Failure //通知消息
}
return balance, stderr.Success
}