package main import ( "fmt" "math/rand" "github.com/mhaya/game/game_cluster/internal/constant" "github.com/mhaya/game/game_cluster/internal/mdb/eventmodels" "gorm.io/gorm" ) func getAssetFakeData(users []*eventmodels.UserBasic) []*eventmodels.AssetsEventContent { datas := make([]*eventmodels.AssetsEventContent, 0, 8) for i := 0; i < count; i++ { userBasic := getUserBasic(users) eventBasic := getEventBasic() operation_type := func() string { if rand.Intn(2) == 1 { return string(constant.IncreaseOp) } return string(constant.DecreaseOp) } currency := func() string { index := rand.Intn(3) if index == 1 { return string(constant.PointsCurrency) } if index == 2 { return string(constant.TonCurrency) } return string(constant.UsdtCurrency) } 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 datas = append(datas, &eventmodels.AssetsEventContent{ UserBasic: *userBasic, EventBasic: *eventBasic, OperationType: operation_type(), Currency: currency(), Reason: reason(), BeforeBalance: before_balance, Amount: amount, AfterBalance: after_balance, }) } return datas } func sumData(db *gorm.DB, users []*eventmodels.UserBasic) { // sqlStr := `select SUM(amount) as total_amount from assets_event_contents where user_id = ? and currency = ?` var total_amount int64 for _, nameObj := range users { // 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: nameObj.UserId, // 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("User_id:%s total_amount:%v", nameObj.UserId, total_amount) } }