package main import ( "git.hilo.cn/hilo-common/script/mysql" "gorm.io/gorm" "gorm.io/gorm/clause" ) type RaceDayRankKsa struct { Period string UserId uint64 Stake uint64 Award uint64 } type TmpRank struct { D string UserId uint64 Stake uint64 Award uint64 } func main() { stakeSql := `SELECT DATE_FORMAT(created_time,"%Y-%m-%d") d,user_id,SUM(stake) stake FROM game_race_stake GROUP BY d,user_id ;` var stakeRanks []TmpRank if err := mysql.ProdWriteDB.Raw(stakeSql).Scan(&stakeRanks).Error; err != nil { panic(err) } for _, v := range stakeRanks { if err := mysql.ProdWriteDB.Model(RaceDayRankKsa{}).Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "period"}, {Name: "user_id"}}, DoUpdates: clause.Assignments(map[string]interface{}{ "stake": gorm.Expr("stake+ ?", v.Stake), }), }).Create(&RaceDayRankKsa{ Period: v.D, UserId: v.UserId, Stake: v.Stake, }).Error; err != nil { panic(err) } } awardSql := `SELECT DATE_FORMAT(created_time,"%Y-%m-%d") d,user_id,SUM(award) award FROM game_race_player GROUP BY d,user_id;` var awardRanks []TmpRank if err := mysql.ProdWriteDB.Raw(awardSql).Scan(&awardRanks).Error; err != nil { panic(err) } for _, v := range awardRanks { if err := mysql.ProdWriteDB.Model(RaceDayRankKsa{}).Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "period"}, {Name: "user_id"}}, DoUpdates: clause.Assignments(map[string]interface{}{ "award": gorm.Expr("award+ ?", v.Award), }), }).Create(&RaceDayRankKsa{ Period: v.D, UserId: v.UserId, Award: v.Award, }).Error; err != nil { panic(err) } } }