perf: 性能优化与错误修复
- 修复 N+1 查询: users/devices/agents 批量 GROUP BY 替代循环查询 - 添加分页: cards/finance/agents/devices API - Redis 初始化根据安装配置 redis.enabled 决定是否连接 - 修复 DefaultVal 解析错误: 使用 sql.NullString 处理 NULL 值 - Dashboard 优化: 替换 ECharts 世界地图为 Chart.js 环形饼图 - 适配前端 cards 页面新 API 响应格式 - 添加数据库索引优化查询性能 - 实现可配置的数据清理定时任务
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"verification-platform-backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func SetupAccountRoutes(r *gin.RouterGroup) {
|
||||
@@ -126,14 +127,15 @@ func handleAppHeartbeat(c *gin.Context) {
|
||||
}
|
||||
log.Printf("[DEBUG] User %d subscription valid, skip balance deduction", user.ID)
|
||||
} else {
|
||||
if user.Balance >= app.DeductionAmount {
|
||||
user.Balance -= app.DeductionAmount
|
||||
log.Printf("[DEBUG] Deducted %.2f from user %d, new balance: %.2f", app.DeductionAmount, user.ID, user.Balance)
|
||||
} else {
|
||||
result := database.DB.Model(&model.AppUser{}).Where("id = ? AND balance >= ?", user.ID, app.DeductionAmount).
|
||||
Update("balance", gorm.Expr("balance - ?", app.DeductionAmount))
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
log.Printf("[DEBUG] User %d has insufficient balance: %.2f < %.2f", user.ID, user.Balance, app.DeductionAmount)
|
||||
response.Error(c, 403, "余额不足")
|
||||
return
|
||||
}
|
||||
database.DB.Where("id = ?", user.ID).First(&user)
|
||||
log.Printf("[DEBUG] Deducted %.2f from user %d, new balance: %.2f", app.DeductionAmount, user.ID, user.Balance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func SetupDynamicRoutes(r *gin.RouterGroup) {
|
||||
@@ -408,9 +409,8 @@ func handleExtendTime(appID uint, actionMap map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
user.Balance += float64(days)
|
||||
|
||||
return database.DB.Save(&user).Error
|
||||
return database.DB.Model(&model.AppUser{}).Where("id = ?", user.ID).
|
||||
Update("balance", gorm.Expr("balance + ?", float64(days))).Error
|
||||
}
|
||||
|
||||
func handleDeductPoints(appID uint, actionMap map[string]interface{}) error {
|
||||
@@ -433,12 +433,16 @@ func handleDeductPoints(appID uint, actionMap map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
user.Balance -= points
|
||||
if user.Balance < 0 {
|
||||
user.Balance = 0
|
||||
result := database.DB.Model(&model.AppUser{}).Where("id = ? AND balance >= ?", user.ID, points).
|
||||
Update("balance", gorm.Expr("balance - ?", points))
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return database.DB.Save(&user).Error
|
||||
if result.RowsAffected == 0 {
|
||||
return database.DB.Model(&model.AppUser{}).Where("id = ?", user.ID).
|
||||
Update("balance", 0).Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleUpdateUserVariable(appID *uint, userID *uint, actionMap map[string]interface{}) error {
|
||||
|
||||
Reference in New Issue
Block a user