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:
@@ -30,8 +30,9 @@ func handleGetStats(c *gin.Context) {
|
|||||||
var totalApps int64
|
var totalApps int64
|
||||||
database.DB.Model(&model.AgentApplication{}).Where("agent_id = ?", userID).Count(&totalApps)
|
database.DB.Model(&model.AgentApplication{}).Where("agent_id = ?", userID).Count(&totalApps)
|
||||||
|
|
||||||
|
// 统计卡密数:包括 agent_id 或 creator_id 等于当前用户的卡密
|
||||||
var totalCards int64
|
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 totalUsers int64
|
||||||
var agentApps []model.AgentApplication
|
var agentApps []model.AgentApplication
|
||||||
@@ -45,22 +46,22 @@ func handleGetStats(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var totalRevenue float64
|
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")
|
today := time.Now().Format("2006-01-02")
|
||||||
var todayCards int64
|
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
|
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{
|
response.Success(c, gin.H{
|
||||||
"totalApps": totalApps,
|
"totalApps": totalApps,
|
||||||
"totalCards": totalCards,
|
"totalCards": totalCards,
|
||||||
"totalUsers": totalUsers,
|
"totalUsers": totalUsers,
|
||||||
"totalRevenue": totalRevenue,
|
"totalRevenue": totalRevenue,
|
||||||
"todayCards": todayCards,
|
"todayCards": todayCards,
|
||||||
"todayRevenue": todayRevenue,
|
"todayRevenue": todayRevenue,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,8 +158,9 @@ func handleGetCards(c *gin.Context) {
|
|||||||
page := c.DefaultQuery("page", "1")
|
page := c.DefaultQuery("page", "1")
|
||||||
pageSize := c.DefaultQuery("page_size", "20")
|
pageSize := c.DefaultQuery("page_size", "20")
|
||||||
|
|
||||||
|
// 统计总数:包括 agent_id 或 creator_id 等于当前��户的卡密
|
||||||
var total int64
|
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
|
var cards []model.Card
|
||||||
offset := 0
|
offset := 0
|
||||||
@@ -171,7 +173,8 @@ func handleGetCards(c *gin.Context) {
|
|||||||
limit = pageSizeInt
|
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)
|
cardTypeIDs := make([]uint, 0)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ const router = useRouter()
|
|||||||
|
|
||||||
const loading = ref(true)
|
const loading = ref(true)
|
||||||
const cards = ref<Card[]>([])
|
const cards = ref<Card[]>([])
|
||||||
|
const total = ref(0)
|
||||||
const tableRef = ref()
|
const tableRef = ref()
|
||||||
const searchFilter = ref('')
|
const searchFilter = ref('')
|
||||||
|
|
||||||
@@ -42,17 +43,22 @@ async function fetchCards() {
|
|||||||
const data = await api.get<any>('/agent/cards')
|
const data = await api.get<any>('/agent/cards')
|
||||||
if (Array.isArray(data)) {
|
if (Array.isArray(data)) {
|
||||||
cards.value = data
|
cards.value = data
|
||||||
|
total.value = data.length
|
||||||
} else if (data?.cards) {
|
} else if (data?.cards) {
|
||||||
cards.value = data.cards
|
cards.value = data.cards
|
||||||
|
total.value = data.total || data.cards.length
|
||||||
} else if (data?.data) {
|
} else if (data?.data) {
|
||||||
cards.value = Array.isArray(data.data) ? data.data : []
|
cards.value = Array.isArray(data.data) ? data.data : []
|
||||||
|
total.value = data.total || cards.value.length
|
||||||
} else {
|
} else {
|
||||||
cards.value = []
|
cards.value = []
|
||||||
|
total.value = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error('Load cards failed:', error)
|
console.error('Load cards failed:', error)
|
||||||
cards.value = []
|
cards.value = []
|
||||||
|
total.value = 0
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
@@ -111,7 +117,7 @@ onMounted(() => {
|
|||||||
</UiCardHeader>
|
</UiCardHeader>
|
||||||
<UiCardContent>
|
<UiCardContent>
|
||||||
<div class="text-2xl font-bold">
|
<div class="text-2xl font-bold">
|
||||||
{{ cards.length }}
|
{{ total }}
|
||||||
</div>
|
</div>
|
||||||
</UiCardContent>
|
</UiCardContent>
|
||||||
</UiCard>
|
</UiCard>
|
||||||
|
|||||||
Reference in New Issue
Block a user