package main import ( "fmt" "git.hilo.cn/hilo-common/script/model" "git.hilo.cn/hilo-common/script/mysql" "github.com/tealeg/xlsx" "strings" ) type Act710Data struct { Code string SendUserId uint64 ActDiamond uint64 Levels []int `gorm:"-"` RealDiamond uint64 } func ats39(a interface{}) string { return fmt.Sprintf("%v", a) } type ActUserAward struct { Level int } func main() { var data []Act710Data if err := mysql.ProdReadOnlyDB.Table("act_gift_record"). Where("activity_id = 710"). Select("send_user_id,SUM(diamond) act_diamond"). Group("send_user_id").Find(&data).Error; err != nil { panic(err) } for i, v := range data { var user model.User if err := mysql.ProdReadOnlyDB.Model(model.User{}).Where("id = ? ", v.SendUserId).First(&user).Error; err != nil { panic(err) } var realDiamond uint64 if err := mysql.ProdReadOnlyDB.Table("gift_operate"). Where("send_user_id = ? AND res_gift_id in (4766,4767,4768,4769) AND created_time >= \"2024-01-01 05:00:00\" AND created_time <= \"2024-01-08 05:00:00\"", v.SendUserId). Select("SUM(send_user_diamond) real_diamond").Scan(&realDiamond).Error; err != nil { panic(err) } data[i].Code = user.Code data[i].RealDiamond = realDiamond var levels []ActUserAward if err := mysql.ProdReadOnlyDB.Table("act_user_award").Where("activity_id = 710 AND user_id = ?", v.SendUserId).Select("level").Find(&levels).Error; err != nil { panic(err) } var l []int for _, v := range levels { l = append(l, v.Level) } data[i].Levels = l //break } excelFileName := fmt.Sprintf("./710数据.xlsx") xlFile := xlsx.NewFile() sheet, _ := xlFile.AddSheet("charge") row := sheet.AddRow() c1, c2, c3, c4 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() c1.Value, c2.Value, c3.Value, c4.Value = "用户", "实际送礼", "活动统计送礼", "已经领取的任务" for _, d := range data { row := sheet.AddRow() c1, c2, c3, c4 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() var l []string for _, v := range d.Levels { l = append(l, fmt.Sprintf("%v", v)) } levels := strings.Join(l, ",") c1.Value, c2.Value, c3.Value, c4.Value = ats39(d.Code), ats39(d.RealDiamond), ats39(d.ActDiamond), ats39(levels) } _ = xlFile.Save(excelFileName) }