fix version 1
This commit is contained in:
@@ -13,18 +13,25 @@ import (
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 申请提现
|
||||
// 在同一事务内先原子扣减余额/可提现余额,再写入提现单与流水,
|
||||
// 并通过条件更新(balance >= amount)保证余额不足时不会生成提现单,避免重复提交超额提现。
|
||||
func ApplyCash(ctx context.Context, in *pb.ApplyCashRequest) (reply *pb.StatusReply, err error) {
|
||||
auth, ok := service.ParseMetaCtx(ctx, nil)
|
||||
if ok != nil {
|
||||
return nil, ok
|
||||
}
|
||||
|
||||
if in.Amount == 0 || in.Channel == 0 {
|
||||
if in.Amount <= 0 || in.Channel == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 提现渠道仅支持 1:微信 2:支付宝 3:银行卡
|
||||
if in.Channel != 1 && in.Channel != 2 && in.Channel != 3 {
|
||||
return nil, excode.ErrPayChannel
|
||||
}
|
||||
|
||||
myWallet, err := models.GetWalletByPassportIdentity(auth.ID, auth.Identity)
|
||||
if err != nil {
|
||||
@@ -32,27 +39,64 @@ func ApplyCash(ctx context.Context, in *pb.ApplyCashRequest) (reply *pb.StatusRe
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if in.Amount > myWallet.Balance {
|
||||
return nil, excode.ErrBalanceNotEnough
|
||||
}
|
||||
// cashNo 同时作为提现单幂等键与内部流水号
|
||||
cashNo := utils.UUID()
|
||||
|
||||
data := &models.WalletApplyCash{
|
||||
WalletIdentity: myWallet.Identity,
|
||||
Amount: in.Amount,
|
||||
Channel: int8(in.Channel),
|
||||
Remark: in.Remark,
|
||||
}
|
||||
data.Identity = utils.UUID()
|
||||
data.PassportID = auth.ID
|
||||
data.PassportIdentity = auth.Identity
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 条件更新:余额与可提现余额均充足且钱包未禁用时才扣减
|
||||
res := tx.Model(&models.WalletBasic{}).
|
||||
Where("identity = ? AND status <> -1 AND balance >= ? AND withdrawal_balance >= ?", myWallet.Identity, in.Amount, in.Amount).
|
||||
UpdateColumns(map[string]interface{}{
|
||||
"balance": gorm.Expr("balance - ?", in.Amount),
|
||||
"withdrawal_balance": gorm.Expr("withdrawal_balance - ?", in.Amount),
|
||||
})
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
return excode.ErrBalanceNotEnough
|
||||
}
|
||||
|
||||
err = impl.DBService.Create(&data).Error
|
||||
// 生成提现单
|
||||
data := &models.WalletApplyCash{
|
||||
WalletIdentity: myWallet.Identity,
|
||||
CashNo: cashNo,
|
||||
Amount: in.Amount,
|
||||
Channel: int8(in.Channel),
|
||||
Remark: in.Remark,
|
||||
}
|
||||
data.Identity = utils.UUID()
|
||||
data.PassportID = auth.ID
|
||||
data.PassportIdentity = auth.Identity
|
||||
if err := tx.Create(data).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 同一事务内写入提现流水
|
||||
record := &models.WalletRecord{
|
||||
WalletIdentity: myWallet.Identity,
|
||||
TransType: -1, // 支出
|
||||
InTradeNo: cashNo, // 内部流水号
|
||||
Money: in.Amount,
|
||||
TradeType: 2, // 提现
|
||||
PayChannel: int8(in.Channel),
|
||||
PayType: "CASH", // 提现
|
||||
Remark: in.Remark,
|
||||
}
|
||||
record.Identity = utils.UUID()
|
||||
record.PassportID = auth.ID
|
||||
record.PassportIdentity = auth.Identity
|
||||
models.FillRecordYmd(record)
|
||||
return tx.Create(record).Error
|
||||
})
|
||||
if err != nil {
|
||||
if err == excode.ErrBalanceNotEnough {
|
||||
return nil, excode.ErrBalanceNotEnough
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
//生成提现单
|
||||
return &pb.StatusReply{
|
||||
Message: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
|
||||
Reference in New Issue
Block a user