chore: 同步管理端构建与余额模块调整

- 更新 soul-admin dist 构建产物
- 调整 soul-api balance 路由与模型文件

Made-with: Cursor
This commit is contained in:
卡若
2026-03-17 16:12:04 +08:00
parent e222d254be
commit b625fd5aa1
11 changed files with 946 additions and 1900 deletions

View File

@@ -61,8 +61,8 @@ func BalanceTransactionsGet(c *gin.Context) {
out := make([]gin.H, 0, len(list))
for _, t := range list {
orderID := ""
if t.OrderID != nil {
orderID = *t.OrderID
if t.RelatedOrder != nil {
orderID = *t.RelatedOrder
}
out = append(out, gin.H{
"id": t.ID, "type": t.Type, "amount": t.Amount,
@@ -127,16 +127,14 @@ func BalanceRechargeConfirmPost(c *gin.Context) {
}
// 幂等:检查是否已处理
var cnt int64
tx.Model(&model.BalanceTransaction{}).Where("order_id = ? AND type = ?", req.OrderSn, "recharge").Count(&cnt)
tx.Model(&model.BalanceTransaction{}).Where("related_order = ? AND type = ?", req.OrderSn, "recharge").Count(&cnt)
if cnt > 0 {
return nil // 已处理,直接成功
return nil
}
// 增加余额
tx.Exec("INSERT INTO user_balances (user_id, balance, updated_at) VALUES (?, 0, NOW()) ON DUPLICATE KEY UPDATE balance = balance + ?, updated_at = NOW()", order.UserID, order.Amount)
txID := fmt.Sprintf("bt_%d", time.Now().UnixNano()%100000000000)
tx.Create(&model.BalanceTransaction{
ID: txID, UserID: order.UserID, Type: "recharge", Amount: order.Amount,
OrderID: &req.OrderSn, CreatedAt: time.Now(),
UserID: order.UserID, Type: "recharge", Amount: order.Amount,
RelatedOrder: &req.OrderSn, CreatedAt: time.Now(),
})
return nil
})
@@ -246,10 +244,9 @@ func BalanceConsumePost(c *gin.Context) {
if err := tx.Create(&order).Error; err != nil {
return err
}
txID := fmt.Sprintf("bt_%d", time.Now().UnixNano()%100000000000)
tx.Create(&model.BalanceTransaction{
ID: txID, UserID: req.UserID, Type: "consume", Amount: -amount,
OrderID: &orderSn, CreatedAt: now,
UserID: req.UserID, Type: "consume", Amount: -amount,
RelatedOrder: &orderSn, CreatedAt: now,
})
// 激活权益
if req.ProductType == "fullbook" {
@@ -298,9 +295,8 @@ func BalanceRefundPost(c *gin.Context) {
return fmt.Errorf("余额不足")
}
tx.Model(&model.UserBalance{}).Where("user_id = ?", req.UserID).Update("balance", gorm.Expr("balance - ?", req.Amount))
txID := fmt.Sprintf("bt_%d", time.Now().UnixNano()%100000000000)
tx.Create(&model.BalanceTransaction{
ID: txID, UserID: req.UserID, Type: "refund", Amount: -req.Amount,
UserID: req.UserID, Type: "refund", Amount: -req.Amount,
CreatedAt: time.Now(),
})
return nil
@@ -330,8 +326,8 @@ func AdminUserBalanceGet(c *gin.Context) {
transactions := make([]gin.H, 0, len(list))
for _, t := range list {
orderID := ""
if t.OrderID != nil {
orderID = *t.OrderID
if t.RelatedOrder != nil {
orderID = *t.RelatedOrder
}
transactions = append(transactions, gin.H{
"id": t.ID, "type": t.Type, "amount": t.Amount,
@@ -375,9 +371,8 @@ func AdminUserBalanceAdjust(c *gin.Context) {
return fmt.Errorf("调整后余额不能为负,当前余额 %.2f", ub.Balance)
}
tx.Exec("INSERT INTO user_balances (user_id, balance, updated_at) VALUES (?, 0, NOW()) ON DUPLICATE KEY UPDATE balance = ?, updated_at = NOW()", userID, newBalance)
txID := fmt.Sprintf("bt_adj_%d", time.Now().UnixNano()%100000000000)
return tx.Create(&model.BalanceTransaction{
ID: txID, UserID: userID, Type: "admin_adjust", Amount: req.Amount,
UserID: userID, Type: "admin_adjust", Amount: req.Amount,
CreatedAt: time.Now(),
}).Error
})
@@ -401,10 +396,9 @@ func ConfirmBalanceRechargeByOrder(db *gorm.DB, order *model.Order) error {
return nil // 已处理,幂等
}
tx.Exec("INSERT INTO user_balances (user_id, balance, updated_at) VALUES (?, 0, NOW()) ON DUPLICATE KEY UPDATE balance = balance + ?, updated_at = NOW()", order.UserID, order.Amount)
txID := fmt.Sprintf("bt_%d", time.Now().UnixNano()%100000000000)
return tx.Create(&model.BalanceTransaction{
ID: txID, UserID: order.UserID, Type: "recharge", Amount: order.Amount,
OrderID: &orderSn, CreatedAt: time.Now(),
UserID: order.UserID, Type: "recharge", Amount: order.Amount,
RelatedOrder: &orderSn, CreatedAt: time.Now(),
}).Error
})
}