race_ksa_rank.go 1.59 KB
Newer Older
hujiebin's avatar
1  
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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)
		}
	}
}