116 lines
2.9 KiB
Go
116 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func main() {
|
|
dsn := os.Getenv("DB_DSN")
|
|
if dsn == "" {
|
|
fmt.Println("请设置 DB_DSN 环境变量")
|
|
return
|
|
}
|
|
|
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
|
if err != nil {
|
|
fmt.Printf("连接数据库失败: %v\n", err)
|
|
return
|
|
}
|
|
|
|
fmt.Println("========== 查询所有活动的奖品配置 ==========\n")
|
|
|
|
// 查询每个活动的奖品数量
|
|
var results []struct {
|
|
ActivityID int64 `gorm:"column:activity_id"`
|
|
PrizeCount int64 `gorm:"column:prize_count"`
|
|
DrawCount int64 `gorm:"column:draw_count"`
|
|
}
|
|
|
|
err = db.Raw(`
|
|
SELECT
|
|
a.id as activity_id,
|
|
a.name as activity_name,
|
|
COUNT(DISTINCT p.id) as prize_count,
|
|
COUNT(DISTINCT d.id) as draw_count
|
|
FROM livestream_activities a
|
|
LEFT JOIN livestream_prizes p ON a.id = p.activity_id
|
|
LEFT JOIN livestream_draw_logs d ON a.id = d.activity_id
|
|
GROUP BY a.id, a.name
|
|
ORDER BY a.id DESC
|
|
LIMIT 10
|
|
`).Scan(&results).Error
|
|
|
|
if err != nil {
|
|
fmt.Printf("查询失败: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// 查询详细信息
|
|
type Activity struct {
|
|
ID int64 `gorm:"column:id"`
|
|
Name string `gorm:"column:name"`
|
|
}
|
|
|
|
type Prize struct {
|
|
ID int64 `gorm:"column:id"`
|
|
Name string `gorm:"column:name"`
|
|
Weight int32 `gorm:"column:weight"`
|
|
}
|
|
|
|
type DrawLog struct {
|
|
ID int64 `gorm:"column:id"`
|
|
PrizeID int64 `gorm:"column:prize_id"`
|
|
PrizeName string `gorm:"column:prize_name"`
|
|
}
|
|
|
|
var activities []Activity
|
|
db.Table("livestream_activities").Order("id DESC").Limit(10).Find(&activities)
|
|
|
|
for _, act := range activities {
|
|
var prizes []Prize
|
|
db.Table("livestream_prizes").Where("activity_id = ?", act.ID).Order("weight ASC").Find(&prizes)
|
|
|
|
var drawLogs []DrawLog
|
|
db.Table("livestream_draw_logs").Where("activity_id = ?", act.ID).Find(&drawLogs)
|
|
|
|
fmt.Printf("\n活动 ID: %d, 名称: %s\n", act.ID, act.Name)
|
|
fmt.Printf(" 奖品数量: %d\n", len(prizes))
|
|
fmt.Printf(" 抽奖次数: %d\n", len(drawLogs))
|
|
|
|
if len(prizes) > 0 {
|
|
fmt.Println(" 奖品配置:")
|
|
var totalWeight int64
|
|
for _, p := range prizes {
|
|
totalWeight += int64(p.Weight)
|
|
}
|
|
for _, p := range prizes {
|
|
prob := float64(p.Weight) / float64(totalWeight) * 100
|
|
fmt.Printf(" - ID:%d, 名称:%s, 权重:%d, 概率:%.3f%%\n", p.ID, p.Name, p.Weight, prob)
|
|
}
|
|
|
|
if len(drawLogs) > 0 {
|
|
// 统计中奖情况
|
|
prizeStats := make(map[int64]int)
|
|
for _, log := range drawLogs {
|
|
prizeStats[log.PrizeID]++
|
|
}
|
|
|
|
fmt.Println(" 中奖统计:")
|
|
for _, p := range prizes {
|
|
count := prizeStats[p.ID]
|
|
theoryProb := float64(p.Weight) / float64(totalWeight) * 100
|
|
actualProb := float64(count) / float64(len(drawLogs)) * 100
|
|
diff := actualProb - theoryProb
|
|
fmt.Printf(" - %s: 理论%.3f%%, 实际%.3f%%, 偏差%+.3f%%, 次数:%d\n",
|
|
p.Name, theoryProb, actualProb, diff, count)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|