fix: 修复代理卡密统计和列表查询逻辑

- 前端使用后端返回的total字段显示总数
- 后端查询条件兼容旧数据(agent_id IS NULL AND creator_id = ?)
- handleGetStats统计接口使用相同查询逻辑

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-10 02:36:26 +08:00
parent d83e7dfac9
commit 83cd822717
2 changed files with 21 additions and 12 deletions
+14 -11
View File
@@ -30,8 +30,9 @@ func handleGetStats(c *gin.Context) {
var totalApps int64
database.DB.Model(&model.AgentApplication{}).Where("agent_id = ?", userID).Count(&totalApps)
// 统计卡密数:包括 agent_id 或 creator_id 等于当前用户的卡密
var totalCards int64
database.DB.Model(&model.Card{}).Where("agent_id = ?", userID).Count(&totalCards)
database.DB.Model(&model.Card{}).Where("agent_id = ? OR (agent_id IS NULL AND creator_id = ?)", userID, userID).Count(&totalCards)
var totalUsers int64
var agentApps []model.AgentApplication
@@ -45,22 +46,22 @@ func handleGetStats(c *gin.Context) {
}
var totalRevenue float64
database.DB.Model(&model.Card{}).Where("agent_id = ?", userID).Select("COALESCE(SUM(price), 0)").Scan(&totalRevenue)
database.DB.Model(&model.Card{}).Where("agent_id = ? OR (agent_id IS NULL AND creator_id = ?)", userID, userID).Select("COALESCE(SUM(price), 0)").Scan(&totalRevenue)
today := time.Now().Format("2006-01-02")
var todayCards int64
database.DB.Model(&model.Card{}).Where("agent_id = ? AND DATE(created_at) = ?", userID, today).Count(&todayCards)
database.DB.Model(&model.Card{}).Where("(agent_id = ? OR (agent_id IS NULL AND creator_id = ?)) AND DATE(created_at) = ?", userID, userID, today).Count(&todayCards)
var todayRevenue float64
database.DB.Model(&model.Card{}).Where("agent_id = ? AND DATE(created_at) = ?", userID, today).Select("COALESCE(SUM(price), 0)").Scan(&todayRevenue)
database.DB.Model(&model.Card{}).Where("(agent_id = ? OR (agent_id IS NULL AND creator_id = ?)) AND DATE(created_at) = ?", userID, userID, today).Select("COALESCE(SUM(price), 0)").Scan(&todayRevenue)
response.Success(c, gin.H{
"totalApps": totalApps,
"totalCards": totalCards,
"totalUsers": totalUsers,
"totalRevenue": totalRevenue,
"todayCards": todayCards,
"todayRevenue": todayRevenue,
"totalCards": totalCards,
"totalUsers": totalUsers,
"totalRevenue": totalRevenue,
"todayCards": todayCards,
"todayRevenue": todayRevenue,
})
}
@@ -157,8 +158,9 @@ func handleGetCards(c *gin.Context) {
page := c.DefaultQuery("page", "1")
pageSize := c.DefaultQuery("page_size", "20")
// 统计总数:包括 agent_id 或 creator_id 等于当前户的卡密
var total int64
database.DB.Model(&model.Card{}).Where("agent_id = ?", userID).Count(&total)
database.DB.Model(&model.Card{}).Where("agent_id = ? OR (agent_id IS NULL AND creator_id = ?)", userID, userID).Count(&total)
var cards []model.Card
offset := 0
@@ -171,7 +173,8 @@ func handleGetCards(c *gin.Context) {
limit = pageSizeInt
}
database.DB.Where("agent_id = ?", userID).Order("created_at DESC").Limit(limit).Offset(offset).Find(&cards)
// 查询卡密:包括 agent_id 或 creator_id 等于当前用户的卡密
database.DB.Where("agent_id = ? OR (agent_id IS NULL AND creator_id = ?)", userID, userID).Order("created_at DESC").Limit(limit).Offset(offset).Find(&cards)
// 获取关联数据
cardTypeIDs := make([]uint, 0)
+7 -1
View File
@@ -15,6 +15,7 @@ const router = useRouter()
const loading = ref(true)
const cards = ref<Card[]>([])
const total = ref(0)
const tableRef = ref()
const searchFilter = ref('')
@@ -42,17 +43,22 @@ async function fetchCards() {
const data = await api.get<any>('/agent/cards')
if (Array.isArray(data)) {
cards.value = data
total.value = data.length
} else if (data?.cards) {
cards.value = data.cards
total.value = data.total || data.cards.length
} else if (data?.data) {
cards.value = Array.isArray(data.data) ? data.data : []
total.value = data.total || cards.value.length
} else {
cards.value = []
total.value = 0
}
}
catch (error) {
console.error('Load cards failed:', error)
cards.value = []
total.value = 0
}
finally {
loading.value = false
@@ -111,7 +117,7 @@ onMounted(() => {
</UiCardHeader>
<UiCardContent>
<div class="text-2xl font-bold">
{{ cards.length }}
{{ total }}
</div>
</UiCardContent>
</UiCard>