refactor: 优化交易类型判断逻辑,使用switch语句增强可读性
This commit is contained in:
@@ -70,7 +70,11 @@ func (s *order) GetUserOrder(ctx context.Context, userID int64, id string) (*use
|
|||||||
return &dto, nil
|
return &dto, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *order) Create(ctx context.Context, userID int64, form *transaction_dto.OrderCreateForm) (*transaction_dto.OrderCreateResponse, error) {
|
func (s *order) Create(
|
||||||
|
ctx context.Context,
|
||||||
|
userID int64,
|
||||||
|
form *transaction_dto.OrderCreateForm,
|
||||||
|
) (*transaction_dto.OrderCreateResponse, error) {
|
||||||
if userID == 0 {
|
if userID == 0 {
|
||||||
return nil, errorx.ErrUnauthorized
|
return nil, errorx.ErrUnauthorized
|
||||||
}
|
}
|
||||||
@@ -166,7 +170,12 @@ func (s *order) Create(ctx context.Context, userID int64, form *transaction_dto.
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *order) Pay(ctx context.Context, userID int64, id string, form *transaction_dto.OrderPayForm) (*transaction_dto.OrderPayResponse, error) {
|
func (s *order) Pay(
|
||||||
|
ctx context.Context,
|
||||||
|
userID int64,
|
||||||
|
id string,
|
||||||
|
form *transaction_dto.OrderPayForm,
|
||||||
|
) (*transaction_dto.OrderPayResponse, error) {
|
||||||
if userID == 0 {
|
if userID == 0 {
|
||||||
return nil, errorx.ErrUnauthorized
|
return nil, errorx.ErrUnauthorized
|
||||||
}
|
}
|
||||||
@@ -174,7 +183,9 @@ func (s *order) Pay(ctx context.Context, userID int64, id string, form *transact
|
|||||||
oid := cast.ToInt64(id)
|
oid := cast.ToInt64(id)
|
||||||
|
|
||||||
// Fetch Order
|
// Fetch Order
|
||||||
o, err := models.OrderQuery.WithContext(ctx).Where(models.OrderQuery.ID.Eq(oid), models.OrderQuery.UserID.Eq(uid)).First()
|
o, err := models.OrderQuery.WithContext(ctx).
|
||||||
|
Where(models.OrderQuery.ID.Eq(oid), models.OrderQuery.UserID.Eq(uid)).
|
||||||
|
First()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errorx.ErrRecordNotFound
|
return nil, errorx.ErrRecordNotFound
|
||||||
}
|
}
|
||||||
@@ -253,7 +264,9 @@ func (s *order) settleOrder(ctx context.Context, o *models.Order, method, extern
|
|||||||
items, _ := tx.OrderItem.WithContext(ctx).Where(tx.OrderItem.OrderID.Eq(o.ID)).Find()
|
items, _ := tx.OrderItem.WithContext(ctx).Where(tx.OrderItem.OrderID.Eq(o.ID)).Find()
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
// Check if access already exists (idempotency)
|
// Check if access already exists (idempotency)
|
||||||
exists, _ := tx.ContentAccess.WithContext(ctx).Where(tx.ContentAccess.UserID.Eq(o.UserID), tx.ContentAccess.ContentID.Eq(item.ContentID)).Exists()
|
exists, _ := tx.ContentAccess.WithContext(ctx).
|
||||||
|
Where(tx.ContentAccess.UserID.Eq(o.UserID), tx.ContentAccess.ContentID.Eq(item.ContentID)).
|
||||||
|
Exists()
|
||||||
if exists {
|
if exists {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,10 +45,11 @@ func (s *wallet) GetWallet(ctx context.Context, userID int64) (*user_dto.WalletR
|
|||||||
for _, o := range orders {
|
for _, o := range orders {
|
||||||
var txType string
|
var txType string
|
||||||
var title string
|
var title string
|
||||||
if o.Type == consts.OrderTypeContentPurchase {
|
switch o.Type {
|
||||||
|
case consts.OrderTypeContentPurchase:
|
||||||
txType = "expense"
|
txType = "expense"
|
||||||
title = "购买内容"
|
title = "购买内容"
|
||||||
} else if o.Type == consts.OrderTypeRecharge {
|
case consts.OrderTypeRecharge:
|
||||||
txType = "income"
|
txType = "income"
|
||||||
title = "钱包充值"
|
title = "钱包充值"
|
||||||
}
|
}
|
||||||
@@ -68,7 +69,11 @@ func (s *wallet) GetWallet(ctx context.Context, userID int64) (*user_dto.WalletR
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *wallet) Recharge(ctx context.Context, userID int64, form *user_dto.RechargeForm) (*user_dto.RechargeResponse, error) {
|
func (s *wallet) Recharge(
|
||||||
|
ctx context.Context,
|
||||||
|
userID int64,
|
||||||
|
form *user_dto.RechargeForm,
|
||||||
|
) (*user_dto.RechargeResponse, error) {
|
||||||
amount := int64(form.Amount * 100)
|
amount := int64(form.Amount * 100)
|
||||||
if amount <= 0 {
|
if amount <= 0 {
|
||||||
return nil, errorx.ErrBadRequest.WithMsg("金额无效")
|
return nil, errorx.ErrBadRequest.WithMsg("金额无效")
|
||||||
|
|||||||
Reference in New Issue
Block a user