package main import ( "fmt" "git.hilo.cn/hilo-common/script/model" "git.hilo.cn/hilo-common/script/mysql" "github.com/tealeg/xlsx" ) type GameRacePlayer struct { Date string Round int Award int64 Stake int64 Code string UserId uint64 } func main() { var rows []GameRacePlayer if err := mysql.ProdReadOnlyDB.Table("game_race_player").Select("`date`,round,user_id,award,u.code"). Joins("JOIN user u ON u.id = user_id").Where("award >= 1000000 AND `date` = '2023-11-06'").Find(&rows).Error; err != nil { panic(err) } for i, v := range rows { var user model.User if err := mysql.ProdReadOnlyDB.Table("user").Where("id = ?", v.UserId).First(&user).Error; err != nil { panic(err) } rows[i].Code = user.Code var stake int64 if err := mysql.ProdReadOnlyDB.Table("game_race_stake").Select("SUM(stake)"). Where("`date` = ? AND round = ? AND user_id = ?", v.Date, v.Round, v.UserId).Scan(&stake).Error; err != nil { panic(err) } rows[i].Stake = stake } excelFileName := fmt.Sprintf("./赛车1M.xlsx") xlFile := xlsx.NewFile() sheet, _ := xlFile.AddSheet("race") row := sheet.AddRow() c1, c2, c3, c4 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() c1.Value, c2.Value, c3.Value, c4.Value = "用户ID", "轮次", "投注", "中奖" for _, d := range rows { row := sheet.AddRow() c1, c2, c3, c4 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() c1.Value, c2.Value, c3.Value, c4.Value = ats37(d.Code), ats37(d.Round), ats37(d.Stake), ats37(d.Award) } _ = xlFile.Save(excelFileName) } func ats37(d interface{}) string { return fmt.Sprintf("%v", d) }