diff --git a/go.mod b/go.mod index cb486a3e5356934b63573f3bf32d9d4ca7f4c2ff..d2d733472f67609aea8e0ae90a6419d83649c2fb 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/satori/go.uuid v1.2.0 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/tealeg/xlsx v1.0.5 // indirect gorm.io/driver/mysql v1.4.3 // indirect diff --git a/go.sum b/go.sum index aaeb733c21eae22280169158a23c7d3e5527c5b5..ba12d8b9b2d73b6b1bc75cd8996280d418058fb4 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU= github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= diff --git a/model/user.go b/model/user.go new file mode 100644 index 0000000000000000000000000000000000000000..ad961e1f76b9e57d197b423c8ef11892312735bb --- /dev/null +++ b/model/user.go @@ -0,0 +1,50 @@ +package model + +import "time" + +type User struct { + Id uint64 + ExternalId string + Avatar string + DefaultAvatar bool + Nick string + Sex uint8 + Birthday uint64 + Country string + CountryIcon string + Language string + Description string + Code string + OriginCode string + IsPush int + IsShowAge int + Status int + DeviceType string + LogoutTime int64 + CreatedTime time.Time +} + +type UserNoId struct { + ExternalId string + Avatar string + DefaultAvatar bool + Nick string + Sex uint8 + Birthday uint64 + Country string + CountryIcon string + Language string + Description string + Code string + OriginCode string + IsPush int + IsShowAge int + Status int + DeviceType string + LogoutTime int64 + CreatedTime time.Time +} + +func (UserNoId) TableName() string { + return "user" +} diff --git a/mysql/mysql.go b/mysql/mysql.go index 48cc9baea38fc6487ae454d1237b757ede7db6ca..8ec1acad5871b00e4dd0deae8beefb0dee6fd9c3 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -10,14 +10,16 @@ import ( "net/url" ) -var Db *gorm.DB +var ProdReadOnlyDB *gorm.DB +var ProdWriteDB *gorm.DB +var TestDB *gorm.DB func init() { var err error options := "?charset=utf8mb4&parseTime=True&loc=Local&time_zone=" + url.QueryEscape("'+8:00'") dsn := "read_only:hilo1632@(rm-eb3w787dzn9c8g07vuo.mysql.dubai.rds.aliyuncs.com)/hilo" + options - Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ + ProdReadOnlyDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ NamingStrategy: schema.NamingStrategy{SingularTable: true}, }) if err != nil { @@ -26,7 +28,37 @@ func init() { log.Println("mysql connect success") } - if Db.Error != nil { - fmt.Printf("database error %v", Db.Error) + if ProdReadOnlyDB.Error != nil { + fmt.Printf("database error %v", ProdReadOnlyDB.Error) + } + + dsn = "root:yX0jPAhO0I4s2zlA@(47.244.34.27:3306)/hilo" + options + + TestDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ + NamingStrategy: schema.NamingStrategy{SingularTable: true}, + }) + if err != nil { + log.Fatalf("mysql connect error %v", err) + } else { + log.Println("mysql connect success") + } + + if TestDB.Error != nil { + fmt.Printf("database error %v", TestDB.Error) + } + + dsn = "nextvideo:ihlUwI4nhi9W88MI@(rm-eb3w787dzn9c8g07vuo.mysql.dubai.rds.aliyuncs.com)/hilo" + options + + ProdWriteDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ + NamingStrategy: schema.NamingStrategy{SingularTable: true}, + }) + if err != nil { + log.Fatalf("mysql connect error %v", err) + } else { + log.Println("mysql connect success") + } + + if ProdWriteDB.Error != nil { + fmt.Printf("database error %v", ProdWriteDB.Error) } } diff --git a/script/group_power_india.go b/script/group_power_india.go index 52ff2deeb31ac93300a4207c19c78559609aa5a5..7adbb60c0172411feb5830ae77105fc1efe11ebf 100644 --- a/script/group_power_india.go +++ b/script/group_power_india.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/hilo-common/model" "github.com/hilo-common/mysql" "github.com/spf13/cast" "github.com/tealeg/xlsx" @@ -27,13 +28,6 @@ type GroupPower struct { UserChargeSum11 float64 // 11月1号后注册所有势力成员累积充值 } -type User struct { - Id uint64 - Code string - Country string - CreatedTime time.Time -} - type GroupPowerUser struct { GroupPowerId uint64 UserId uint64 @@ -42,7 +36,7 @@ type GroupPowerUser struct { func main() { groupPowerSql := "SELECT p.id as group_power_id,g.`owner` from group_power p,group_info g where p.group_uid = g.im_group_id" var groupPowers []GroupPower - if err := mysql.Db.Raw(groupPowerSql).Find(&groupPowers).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(groupPowerSql).Find(&groupPowers).Error; err != nil { panic(err) } @@ -50,11 +44,11 @@ func main() { for _, v := range groupPowers { userIds = append(userIds, v.Owner) } - var users []User - if err := mysql.Db.Model(User{}).Where("id in ?", userIds).Find(&users).Error; err != nil { + var users []model.User + if err := mysql.ProdReadOnlyDB.Model(model.User{}).Where("id in ?", userIds).Find(&users).Error; err != nil { panic(err) } - userMap := make(map[uint64]User) + userMap := make(map[uint64]model.User) for i := range users { userMap[users[i].Id] = users[i] } @@ -71,7 +65,7 @@ func main() { } groupPowerUsersSql := "SELECT group_power_id,user_id FROM `group_power_user` where group_power_id in (?);" var groupPowerUsers []GroupPowerUser - if err := mysql.Db.Raw(groupPowerUsersSql, targetGroupPowerId).Find(&groupPowerUsers).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(groupPowerUsersSql, targetGroupPowerId).Find(&groupPowerUsers).Error; err != nil { panic(err) } var groupPowerUserMap = make(map[uint64][]uint64) // group_power_id -> []userId @@ -102,7 +96,7 @@ func main() { for i, groupPower := range targetGroupPower { userChargeNumSql := "SELECT count(DISTINCT user_id) as count,sum(price) as price FROM `pay_order` where status = 2 AND platform in (1,2,3,4,5) AND user_id in ? ;" var userChargeNumSum NumSum - if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds).First(&userChargeNumSum).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(userChargeNumSql, groupPower.UserIds).First(&userChargeNumSum).Error; err != nil { panic(err) } else { targetGroupPower[i].UserChargeNum += userChargeNumSum.Count @@ -110,7 +104,7 @@ func main() { } if len(groupPower.UserIds9) > 0 { var userChargeNumSum9 NumSum - if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds9).First(&userChargeNumSum9).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(userChargeNumSql, groupPower.UserIds9).First(&userChargeNumSum9).Error; err != nil { panic(err) } else { targetGroupPower[i].UserChargeNum9 += userChargeNumSum9.Count @@ -119,7 +113,7 @@ func main() { } if len(groupPower.UserIds10) > 0 { var userChargeNumSum10 NumSum - if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds10).First(&userChargeNumSum10).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(userChargeNumSql, groupPower.UserIds10).First(&userChargeNumSum10).Error; err != nil { panic(err) } else { targetGroupPower[i].UserChargeNum10 += userChargeNumSum10.Count @@ -128,7 +122,7 @@ func main() { } if len(groupPower.UserIds11) > 0 { var userChargeNumSum11 NumSum - if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds11).First(&userChargeNumSum11).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(userChargeNumSql, groupPower.UserIds11).First(&userChargeNumSum11).Error; err != nil { panic(err) } else { targetGroupPower[i].UserChargeNum11 += userChargeNumSum11.Count @@ -140,7 +134,7 @@ func main() { for i, groupPower := range targetGroupPower { userChargeNumSql := "SELECT count(DISTINCT receiver_id) as count,sum(dollar) as price FROM `dealer_transfer_detail` where receiver_id in ? ;" var userChargeNumSum NumSum - if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds).First(&userChargeNumSum).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(userChargeNumSql, groupPower.UserIds).First(&userChargeNumSum).Error; err != nil { panic(err) } else { targetGroupPower[i].UserChargeNum += userChargeNumSum.Count @@ -148,7 +142,7 @@ func main() { } if len(groupPower.UserIds9) > 0 { var userChargeNumSum9 NumSum - if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds9).First(&userChargeNumSum9).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(userChargeNumSql, groupPower.UserIds9).First(&userChargeNumSum9).Error; err != nil { panic(err) } else { targetGroupPower[i].UserChargeNum9 += userChargeNumSum9.Count @@ -157,7 +151,7 @@ func main() { } if len(groupPower.UserIds10) > 0 { var userChargeNumSum10 NumSum - if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds10).First(&userChargeNumSum10).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(userChargeNumSql, groupPower.UserIds10).First(&userChargeNumSum10).Error; err != nil { panic(err) } else { targetGroupPower[i].UserChargeNum10 += userChargeNumSum10.Count @@ -166,7 +160,7 @@ func main() { } if len(groupPower.UserIds11) > 0 { var userChargeNumSum11 NumSum - if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds11).First(&userChargeNumSum11).Error; err != nil { + if err := mysql.ProdReadOnlyDB.Raw(userChargeNumSql, groupPower.UserIds11).First(&userChargeNumSum11).Error; err != nil { panic(err) } else { targetGroupPower[i].UserChargeNum11 += userChargeNumSum11.Count diff --git a/script/prod_game_robot.go b/script/prod_game_robot.go new file mode 100644 index 0000000000000000000000000000000000000000..b15aa05a40137f87703d8b8535beb660ef109b80 --- /dev/null +++ b/script/prod_game_robot.go @@ -0,0 +1,36 @@ +package main + +import ( + "github.com/hilo-common/model" + "github.com/hilo-common/mysql" + uuid "github.com/satori/go.uuid" + "strings" +) + +/* +# redis 事务拿出3000个code +MULTI +LRANGE USER_CODE 0 2999 +LTRIM USER_CODE 3000 -1 +exec +*/ +var codes = []string{"123997623", "88912321"} // 3000个from redis pop USER_CODE todo + +func main() { + var testRobots []model.UserNoId + sql := "SELECT * FROM `user` where id BETWEEN 4564 and 7615 limit 3000" + if err := mysql.TestDB.Raw(sql).Find(&testRobots).Error; err != nil { + panic(err) + } + for i, user := range testRobots { + user.ExternalId = GetUUID() // 重新生成新的externalId + user.Code = codes[i] // code赋值 + user.OriginCode = user.Code + break + //mysql.ProdWriteDB.Create(user) // todo final + } +} + +func GetUUID() string { + return strings.Replace(uuid.NewV4().String(), "-", "", -1) +} diff --git a/script/test_game_robot.go b/script/test_game_robot.go new file mode 100644 index 0000000000000000000000000000000000000000..2e90f86d3c8afd8846dd63dd1756e7a5265e5846 --- /dev/null +++ b/script/test_game_robot.go @@ -0,0 +1,17 @@ +package main + +import ( + "github.com/hilo-common/model" + "github.com/hilo-common/mysql" +) + +func main() { + var prodUser []model.UserNoId + sql := "SELECT * FROM `user` where id BETWEEN 888888 and 999999 AND avatar <> '' and default_avatar = 0 limit 10000 ;" + if err := mysql.ProdReadOnlyDB.Raw(sql).Find(&prodUser).Error; err != nil { + panic(err) + } + for _, user := range prodUser { + mysql.TestDB.Create(user) + } +}