Refactor order and tenant ledger models to use consts for Currency and Type fields; add new UserStatus values; implement comprehensive test cases for content, creator, order, super, and wallet services.
This commit is contained in:
@@ -2,17 +2,110 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
user_dto "quyun/v2/app/http/v1/dto"
|
||||
"quyun/v2/database/fields"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/spf13/cast"
|
||||
"go.ipao.vip/gen/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type wallet struct{}
|
||||
|
||||
func (s *wallet) GetWallet(ctx context.Context) (*user_dto.WalletResponse, error) {
|
||||
return &user_dto.WalletResponse{}, nil
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return nil, errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
|
||||
// Get Balance
|
||||
u, err := models.UserQuery.WithContext(ctx).Where(models.UserQuery.ID.Eq(uid)).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errorx.ErrRecordNotFound
|
||||
}
|
||||
return nil, errorx.ErrDatabaseError
|
||||
}
|
||||
|
||||
// Get Transactions (Orders)
|
||||
// Both purchase (expense) and recharge (income - if paid)
|
||||
tbl, q := models.OrderQuery.QueryContext(ctx)
|
||||
orders, err := q.Where(tbl.UserID.Eq(uid), tbl.Status.Eq(consts.OrderStatusPaid)).
|
||||
Order(tbl.CreatedAt.Desc()).
|
||||
Limit(20). // Limit to recent 20
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError
|
||||
}
|
||||
|
||||
var txs []user_dto.Transaction
|
||||
for _, o := range orders {
|
||||
var txType string
|
||||
var title string
|
||||
if o.Type == consts.OrderTypeContentPurchase {
|
||||
txType = "expense"
|
||||
title = "购买内容"
|
||||
} else if o.Type == consts.OrderTypeRecharge {
|
||||
txType = "income"
|
||||
title = "钱包充值"
|
||||
}
|
||||
|
||||
txs = append(txs, user_dto.Transaction{
|
||||
ID: cast.ToString(o.ID),
|
||||
Title: title,
|
||||
Amount: float64(o.AmountPaid) / 100.0,
|
||||
Type: txType,
|
||||
Date: o.CreatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
return &user_dto.WalletResponse{
|
||||
Balance: float64(u.Balance) / 100.0,
|
||||
Transactions: txs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *wallet) Recharge(ctx context.Context, form *user_dto.RechargeForm) (*user_dto.RechargeResponse, error) {
|
||||
return &user_dto.RechargeResponse{}, nil
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return nil, errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
|
||||
amount := int64(form.Amount * 100)
|
||||
if amount <= 0 {
|
||||
return nil, errorx.ErrBadRequest.WithMsg("金额无效")
|
||||
}
|
||||
|
||||
// Create Recharge Order
|
||||
order := &models.Order{
|
||||
TenantID: 0, // Platform / System
|
||||
UserID: uid,
|
||||
Type: consts.OrderTypeRecharge,
|
||||
Status: consts.OrderStatusCreated,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountOriginal: amount,
|
||||
AmountPaid: amount,
|
||||
IdempotencyKey: uuid.NewString(),
|
||||
Snapshot: types.NewJSONType(fields.OrdersSnapshot{}),
|
||||
}
|
||||
|
||||
if err := models.OrderQuery.WithContext(ctx).Create(order); err != nil {
|
||||
return nil, errorx.ErrDatabaseError
|
||||
}
|
||||
|
||||
// Mock Pay Params
|
||||
return &user_dto.RechargeResponse{
|
||||
PayParams: "mock_recharge_url",
|
||||
OrderID: cast.ToString(order.ID),
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user