2025-01-25 18:05:37 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_server/gorm-gen/dao/model"
|
|
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_server/gorm-gen/dao/query"
|
|
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_server/gorm-gen/querier"
|
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
"gorm.io/gen"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AppAccountQuerier
|
|
|
|
|
|
|
|
var DefaultUsername = []string{
|
|
|
|
"甜蜜糖果",
|
|
|
|
"糖果爱好者",
|
|
|
|
"软糖粉丝",
|
|
|
|
"巧克力糖果控",
|
|
|
|
"棒棒糖迷",
|
|
|
|
"小熊软糖达人",
|
|
|
|
"硬糖狂人",
|
|
|
|
"焦糖糖果控",
|
|
|
|
"水果糖行家",
|
|
|
|
"棉花糖达人",
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRandomUsername 随机获取一个糖果相关的用户名
|
|
|
|
func GetRandomUsername() string {
|
|
|
|
// 初始化随机数种子
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
// 生成一个 0 到列表长度减 1 之间的随机索引
|
|
|
|
randomIndex := r.Intn(len(DefaultUsername))
|
|
|
|
// 根据随机索引返回对应的用户名
|
|
|
|
return DefaultUsername[randomIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
g := gen.NewGenerator(gen.Config{
|
2025-01-25 18:19:07 +08:00
|
|
|
OutPath: "./dao/query",
|
2025-01-25 18:05:37 +08:00
|
|
|
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
|
|
|
|
})
|
|
|
|
dsn := "root:youtu!0113@tcp(localhost:3306)/ecpm?charset=utf8&parseTime=True&loc=Local"
|
|
|
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// gormdb, _ := gorm.Open(mysql.Open("root:@(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local"))
|
|
|
|
g.UseDB(db) // reuse your gorm db
|
|
|
|
|
|
|
|
//g.ApplyBasic(model.AppAccount{}, model.AppUser{}, model.DouyinEcpmConfig{}, model.GameScore{})
|
|
|
|
// Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.User` and `model.Company`
|
|
|
|
g.ApplyInterface(func(querier.AppAccountQuerier) {}, model.AppAccount{})
|
|
|
|
|
|
|
|
g.ApplyInterface(func(querier.GameScoreQuerier) {}, model.GameScore{})
|
|
|
|
|
|
|
|
g.ApplyBasic(model.AppUser{}, model.DouyinEcpmConfig{})
|
|
|
|
|
|
|
|
// Generate the code
|
|
|
|
g.Execute()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func DataGen(db *gorm.DB) {
|
|
|
|
//生成20条用户及分数数据
|
|
|
|
|
|
|
|
u := query.Use(db).AppUser
|
|
|
|
user, err := u.WithContext(context.Background()).Where(u.Openid.Like("test_openid%")).Find()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
//生成20条游戏得分数据
|
|
|
|
var score []*model.GameScore
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
score = append(score, &model.GameScore{
|
|
|
|
AppAccount: 3,
|
|
|
|
AppUserID: user[i].ID,
|
|
|
|
Score: uint32(Abs(rand.New(rand.NewSource(time.Now().UnixNano())).Intn(1000))),
|
|
|
|
T: 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err = query.Use(db).GameScore.WithContext(context.Background()).CreateInBatches(score, len(score))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Abs(x int) int {
|
|
|
|
if x < 0 {
|
|
|
|
return -x
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|