new
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package walletservicelogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetRechargeListLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetRechargeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRechargeListLogic {
|
||||
return &GetRechargeListLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 充值记录列表篇
|
||||
func (l *GetRechargeListLogic) GetRechargeList(in *user.RechargeRequest) (*user.RechargeResponse, error) {
|
||||
list, total, err := l.svcCtx.RechargeModel.FindRechargeListByUserId(l.ctx, in.UserId, in.Page, in.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var rechargeItemList []*user.RechargeItem
|
||||
for _, w := range list {
|
||||
rechargeItemList = append(rechargeItemList, &user.RechargeItem{
|
||||
Id: w.Id,
|
||||
UserId: w.UserId,
|
||||
TransactionId: w.TransactionId,
|
||||
OutTradeNo: w.OutTradeNo,
|
||||
Amount: float32(w.Amount),
|
||||
PaymentMethod: w.PaymentMethod,
|
||||
CreatedAt: w.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: w.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
return &user.RechargeResponse{
|
||||
List: rechargeItemList,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
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),
|
||||
}, session)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 返回成功的充值响应
|
||||
return &user.RechargeWalletResponse{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user