|
@@ -0,0 +1,158 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "log"
|
|
|
+ "math/rand"
|
|
|
+ "os"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/bxcodec/faker/v3"
|
|
|
+ "github.com/mhaya/game/game_cluster/internal/mdb/eventmodels"
|
|
|
+ "gorm.io/driver/clickhouse"
|
|
|
+ "gorm.io/gorm"
|
|
|
+ "gorm.io/gorm/logger"
|
|
|
+)
|
|
|
+
|
|
|
+// `insert into assets_event_contents (user_id, user_name, tg_id, x_id, parent_user_id, is_robot, points_rank_seq,
|
|
|
+// throw_dice_rank_seq, invite_user_rank_seq, is_success, create_at, operation_type,
|
|
|
+// currency, reason, before_balance, amount, after_balance, server_id)
|
|
|
+// values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
|
+
|
|
|
+func main() {
|
|
|
+ dsn := "clickhouse://default:123456@192.168.0.193:9000/default?dial_timeout=10s&read_timeout=20s"
|
|
|
+
|
|
|
+ newLogger := logger.New(
|
|
|
+ log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer(日志输出的地方,这里是标准输出)
|
|
|
+ logger.Config{
|
|
|
+ SlowThreshold: time.Second, // 慢 SQL 阈值
|
|
|
+ LogLevel: logger.Info, // 日志级别
|
|
|
+ Colorful: true, // 彩色打印
|
|
|
+ },
|
|
|
+ )
|
|
|
+
|
|
|
+ db, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{
|
|
|
+ Logger: newLogger,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ panic("failed to connect database")
|
|
|
+ }
|
|
|
+
|
|
|
+ datas := make([]*eventmodels.AssetsEventContent, 0, 8)
|
|
|
+ count := 100
|
|
|
+
|
|
|
+ names := make([]string, 0, 8)
|
|
|
+ for i := 0; i < 10; i++ {
|
|
|
+ name := faker.Name()
|
|
|
+ names = append(names, name)
|
|
|
+ }
|
|
|
+
|
|
|
+ server_id := "m-web-admin-1"
|
|
|
+ for i := 0; i < count; i++ {
|
|
|
+ index := rand.Intn(len(names))
|
|
|
+ user_id := names[index]
|
|
|
+ user_name := names[index]
|
|
|
+ tg_id := faker.Name()
|
|
|
+ x_id := faker.Name()
|
|
|
+ parent_user_id := names[index]
|
|
|
+ is_robot := false
|
|
|
+ points_rank_seq := faker.RandomUnixTime()
|
|
|
+ throw_dice_rank_seq := faker.RandomUnixTime()
|
|
|
+ invite_user_rank_seq := faker.RandomUnixTime()
|
|
|
+ is_success := func() bool {
|
|
|
+ return rand.Intn(2) == 1
|
|
|
+ }
|
|
|
+ create_at := faker.RandomUnixTime()
|
|
|
+ operation_type := func() string {
|
|
|
+ if rand.Intn(2) == 1 {
|
|
|
+ return "increase"
|
|
|
+ }
|
|
|
+ return "decrease"
|
|
|
+ }
|
|
|
+ currency := func() string {
|
|
|
+ index := rand.Intn(3)
|
|
|
+ if index == 1 {
|
|
|
+ return "POINTS"
|
|
|
+ }
|
|
|
+ if index == 2 {
|
|
|
+ return "TON"
|
|
|
+ }
|
|
|
+ return "USDT"
|
|
|
+ }
|
|
|
+ reason := func() string {
|
|
|
+ if rand.Intn(2) == 1 {
|
|
|
+ return "reason"
|
|
|
+ }
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ var before_balance int64
|
|
|
+ var amount int64
|
|
|
+ var after_balance int64
|
|
|
+ before_balance = rand.Int63n(100000)
|
|
|
+ amount = rand.Int63n(1000)
|
|
|
+ after_balance = before_balance + amount
|
|
|
+
|
|
|
+ // email := faker.Email()
|
|
|
+ // address := faker.Address()
|
|
|
+ // phone := faker.Phone()
|
|
|
+
|
|
|
+ datas = append(datas, &eventmodels.AssetsEventContent{
|
|
|
+ UserBasic: eventmodels.UserBasic{
|
|
|
+ UserId: user_id,
|
|
|
+ UserName: user_name,
|
|
|
+ TgId: tg_id,
|
|
|
+ XId: x_id,
|
|
|
+ ParentUserId: parent_user_id,
|
|
|
+ IsRobot: is_robot,
|
|
|
+ PointsRankSeq: points_rank_seq,
|
|
|
+ ThrowDiceRankSeq: throw_dice_rank_seq,
|
|
|
+ InviteUserRankSeq: invite_user_rank_seq,
|
|
|
+ },
|
|
|
+ EventBasic: eventmodels.EventBasic{
|
|
|
+ ServerId: server_id,
|
|
|
+ IsSuccess: is_success(),
|
|
|
+ CreateAt: create_at,
|
|
|
+ },
|
|
|
+ OperationType: operation_type(),
|
|
|
+ Currency: currency(),
|
|
|
+ Reason: reason(),
|
|
|
+ BeforeBalance: before_balance,
|
|
|
+ Amount: amount,
|
|
|
+ AfterBalance: after_balance,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ err = db.CreateInBatches(datas, len(datas)).Error
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("CreateInBatches error:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // sqlStr := `select SUM(amount) as total_amount from assets_event_contents where user_id = ? and currency = ?`
|
|
|
+ var total_amount int64
|
|
|
+ for _, name := range names {
|
|
|
+ // err := db.Raw(sqlStr, name, "USDT").Pluck("SUM(amount) as total_amount", &total_amount).Error
|
|
|
+ // if err != nil {
|
|
|
+ // fmt.Printf("Pluck error:%v", err)
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+
|
|
|
+ where := &eventmodels.AssetsEventContent{
|
|
|
+ UserBasic: eventmodels.UserBasic{
|
|
|
+ UserId: name,
|
|
|
+ // UserId: "1", // 不存在的用户
|
|
|
+ },
|
|
|
+ EventBasic: eventmodels.EventBasic{
|
|
|
+ ServerId: server_id,
|
|
|
+ },
|
|
|
+ Currency: "USDT",
|
|
|
+ }
|
|
|
+ err := db.Model(&eventmodels.AssetsEventContent{}).Where(where).Pluck("SUM(amount) as total_amount", &total_amount).Error
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Pluck error:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Printf("name:%s total_amount:%v", name, total_amount)
|
|
|
+ }
|
|
|
+}
|