2024-10-15 17:19:23 +08:00
|
|
|
package walletservicelogic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
|
|
"tianyuan-api/apps/user/internal/model"
|
|
|
|
"tianyuan-api/pkg/generate"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tianyuan-api/apps/user/internal/svc"
|
|
|
|
"tianyuan-api/apps/user/user"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RechargeWalletLogic struct {
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
logx.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRechargeWalletLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeWalletLogic {
|
|
|
|
return &RechargeWalletLogic{
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 定义充值请求接口
|
|
|
|
func (l *RechargeWalletLogic) RechargeWallet(in *user.RechargeWalletRequest) (*user.RechargeWalletResponse, error) {
|
|
|
|
if in.Amount <= 0 {
|
|
|
|
return nil, errors.New("金额不能小于0")
|
|
|
|
}
|
|
|
|
// 启动事务
|
|
|
|
err := l.svcCtx.WalletsModel.TransCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
|
|
|
var err error
|
|
|
|
// 更新余额(充值金额是正数,增加余额)
|
|
|
|
for i := 0; i < maxRetries; i++ {
|
|
|
|
err = l.svcCtx.WalletsModel.UpdateBalance(session, l.ctx, in.UserId, float64(in.Amount))
|
|
|
|
if err == nil {
|
|
|
|
// 成功,退出循环
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if errors.Is(err, model.ErrVersionMismatch) {
|
|
|
|
// 版本号不匹配,重试
|
|
|
|
time.Sleep(100 * time.Millisecond) // 重试前的延迟
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
// 其他错误,直接返回
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// 生成交易单号
|
|
|
|
TransactionId := generate.GenerateTransactionID()
|
|
|
|
// 更新余额成功后,插入充值记录
|
|
|
|
_, err = l.svcCtx.RechargeModel.InsertRechargeTrans(ctx, &model.Recharge{
|
|
|
|
TransactionId: TransactionId,
|
|
|
|
OutTradeNo: in.OutTradeNo,
|
|
|
|
UserId: in.UserId,
|
|
|
|
Amount: float64(in.Amount),
|
2024-10-21 17:07:25 +08:00
|
|
|
PaymentMethod: in.PaymentMethod,
|
2024-10-15 17:19:23 +08:00
|
|
|
}, session)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-16 20:46:46 +08:00
|
|
|
|
|
|
|
wallet, findWalletErr := l.svcCtx.WalletsModel.FindOneByUserId(l.ctx, in.UserId)
|
|
|
|
if findWalletErr != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if wallet.Balance > 0 {
|
|
|
|
userModel, findUserErr := l.svcCtx.UserModel.FindOne(l.ctx, in.UserId)
|
|
|
|
if findUserErr != nil {
|
|
|
|
return findUserErr
|
|
|
|
}
|
|
|
|
userModel.QuotaExceeded = 0
|
|
|
|
_, updateUserErr := l.svcCtx.UserModel.UpdateUserTrans(l.ctx, userModel, session)
|
|
|
|
if updateUserErr != nil {
|
|
|
|
return updateUserErr
|
|
|
|
}
|
|
|
|
}
|
2024-10-15 17:19:23 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回成功的充值响应
|
|
|
|
return &user.RechargeWalletResponse{}, nil
|
|
|
|
}
|