19cc7a778c
- Deleted old gRPC service definitions in `game_grpc.pb.go` and `public.go`. - Added new API server implementations for objectstory, player, and shop services. - Introduced configuration files for new APIs in `etc/*.yaml`. - Created main entry points for each service in `objectstory.go`, `player.go`, and `shop.go`. - Removed unused user update handler and user API files. - Added utility functions for context management and HTTP header parsing. - Introduced PostgreSQL backup configuration in `backup/postgreSql.yaml`.
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
// Code scaffolded by goctl. Safe to edit.
|
|
// goctl 1.9.2
|
|
|
|
package wallet
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"juwan-backend/app/wallet/rpc/pb"
|
|
"juwan-backend/common/utils/contextj"
|
|
|
|
"juwan-backend/app/wallet/api/internal/svc"
|
|
"juwan-backend/app/wallet/api/internal/types"
|
|
|
|
"github.com/shopspring/decimal"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type WithdrawLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 提现
|
|
func NewWithdrawLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WithdrawLogic {
|
|
return &WithdrawLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *WithdrawLogic) Withdraw(req *types.TopupReq) (resp *types.EmptyResp, err error) {
|
|
amount, err := decimal.NewFromString(req.Amount)
|
|
if err != nil {
|
|
return nil, errors.New("invalid amount")
|
|
}
|
|
if !amount.GreaterThan(decimal.Zero) {
|
|
return nil, errors.New("amount must be greater than 0")
|
|
}
|
|
|
|
userID, err := contextj.UserIDFrom(l.ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
walletResp, err := l.svcCtx.WalletRpc.GetWalletsById(l.ctx, &pb.GetWalletsByIdReq{Id: userID})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if walletResp.GetWallets() == nil {
|
|
return nil, errors.New("wallet not found")
|
|
}
|
|
|
|
currentBalance, err := decimal.NewFromString(walletResp.Wallets.Balance)
|
|
if err != nil {
|
|
return nil, errors.New("invalid wallet balance")
|
|
}
|
|
frozenBalance, err := decimal.NewFromString(walletResp.Wallets.FrozenBalance)
|
|
if err != nil {
|
|
return nil, errors.New("invalid wallet frozen balance")
|
|
}
|
|
expectedVersion := walletResp.Wallets.Version
|
|
if currentBalance.LessThan(amount) {
|
|
return nil, errors.New("insufficient balance")
|
|
}
|
|
|
|
newBalance := currentBalance.Sub(amount)
|
|
newBalanceStr := newBalance.String()
|
|
frozenBalanceStr := frozenBalance.String()
|
|
updatedAt := time.Now().Unix()
|
|
_, err = l.svcCtx.WalletRpc.UpdateWallets(l.ctx, &pb.UpdateWalletsReq{
|
|
UserId: userID,
|
|
Balance: &newBalanceStr,
|
|
FrozenBalance: &frozenBalanceStr,
|
|
UpdatedAt: &updatedAt,
|
|
Version: &expectedVersion,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
desc := fmt.Sprintf("withdraw via %s", req.Method)
|
|
_, err = l.svcCtx.WalletRpc.AddWalletTransactions(l.ctx, &pb.AddWalletTransactionsReq{
|
|
UserId: userID,
|
|
Type: "withdrawal",
|
|
Amount: amount.String(),
|
|
BalanceAfter: newBalanceStr,
|
|
Description: desc,
|
|
CreatedAt: updatedAt,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &types.EmptyResp{}, nil
|
|
}
|