From dd8cc98f21b9f9c9f86dd4deb4596b93786c3686 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Thu, 8 Dec 2022 13:38:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- go.mod | 10 ++ go.sum | 20 ++++ mysql/mysql.go | 32 ++++++ script/group_power_india.go | 202 ++++++++++++++++++++++++++++++++++++ utils/utils.go | 2 +- utils/utils_test.go | 10 ++ 7 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 go.sum create mode 100644 mysql/mysql.go create mode 100644 script/group_power_india.go create mode 100644 utils/utils_test.go diff --git a/.gitignore b/.gitignore index 723ef36..1dbe768 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +*.xlsx \ No newline at end of file diff --git a/go.mod b/go.mod index c1b148e..cb486a3 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,13 @@ module github.com/hilo-common go 1.17 + +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/spf13/cast v1.5.0 // indirect + github.com/tealeg/xlsx v1.0.5 // indirect + gorm.io/driver/mysql v1.4.3 // indirect + gorm.io/gorm v1.23.8 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..aaeb733 --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= +github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +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/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= +github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gorm.io/driver/mysql v1.4.3 h1:/JhWJhO2v17d8hjApTltKNADm7K7YI2ogkR7avJUL3k= +gorm.io/driver/mysql v1.4.3/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c= +gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE= +gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= diff --git a/mysql/mysql.go b/mysql/mysql.go new file mode 100644 index 0000000..48cc9ba --- /dev/null +++ b/mysql/mysql.go @@ -0,0 +1,32 @@ +package mysql + +import ( + "fmt" + _ "github.com/go-sql-driver/mysql" //加载mysql驱动 + "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/schema" + "log" + "net/url" +) + +var Db *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{ + NamingStrategy: schema.NamingStrategy{SingularTable: true}, + }) + if err != nil { + log.Fatalf("mysql connect error %v", err) + } else { + log.Println("mysql connect success") + } + + if Db.Error != nil { + fmt.Printf("database error %v", Db.Error) + } +} diff --git a/script/group_power_india.go b/script/group_power_india.go new file mode 100644 index 0000000..52ff2de --- /dev/null +++ b/script/group_power_india.go @@ -0,0 +1,202 @@ +package main + +import ( + "fmt" + "github.com/hilo-common/mysql" + "github.com/spf13/cast" + "github.com/tealeg/xlsx" + "time" +) + +type GroupPower struct { + GroupPowerId uint64 + Owner uint64 + OwnerCode string + Country string + UserIds []uint64 // 所有uid + UserIds9 []uint64 // 9月1号注册后的uid + UserIds10 []uint64 // 10月1号注册后的uid + UserIds11 []uint64 // 11月1号注册后的uid + UserChargeNum int // 所有进行了充值的势力成员数量 + UserChargeSum float64 // 所有势力成员累积充值 + UserChargeNum9 int // 9月1号后注册所有进行了充值的势力成员数量 + UserChargeSum9 float64 // 9月1号后注册所有势力成员累积充值 + UserChargeNum10 int // 10月1号后注册所有进行了充值的势力成员数量 + UserChargeSum10 float64 // 10月1号后注册所有势力成员累积充值 + UserChargeNum11 int // 11月1号后注册所有进行了充值的势力成员数量 + UserChargeSum11 float64 // 11月1号后注册所有势力成员累积充值 +} + +type User struct { + Id uint64 + Code string + Country string + CreatedTime time.Time +} + +type GroupPowerUser struct { + GroupPowerId uint64 + UserId uint64 +} + +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 { + panic(err) + } + + var userIds []uint64 + 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 { + panic(err) + } + userMap := make(map[uint64]User) + for i := range users { + userMap[users[i].Id] = users[i] + } + var targetGroupPower []GroupPower + var targetGroupPowerId []uint64 + for i, p := range groupPowers { + if userMap[p.Owner].Country == "India" { + groupPowers[i].Country = "India" + groupPowers[i].Owner = userMap[p.Owner].Id + groupPowers[i].OwnerCode = userMap[p.Owner].Code + targetGroupPower = append(targetGroupPower, groupPowers[i]) + targetGroupPowerId = append(targetGroupPowerId, p.GroupPowerId) + } + } + 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 { + panic(err) + } + var groupPowerUserMap = make(map[uint64][]uint64) // group_power_id -> []userId + for _, v := range groupPowerUsers { + groupPowerUserMap[v.GroupPowerId] = append(groupPowerUserMap[v.GroupPowerId], v.UserId) + } + for i, v := range targetGroupPower { + targetGroupPower[i].UserIds = groupPowerUserMap[v.GroupPowerId] + for _, userId := range targetGroupPower[i].UserIds { + if user, ok := userMap[userId]; ok { + if user.CreatedTime.After(time.Date(2022, time.September, 1, 0, 0, 0, 0, time.Local)) { + targetGroupPower[i].UserIds9 = append(targetGroupPower[i].UserIds9, userId) + } + if user.CreatedTime.After(time.Date(2022, time.October, 1, 0, 0, 0, 0, time.Local)) { + targetGroupPower[i].UserIds10 = append(targetGroupPower[i].UserIds10, userId) + } + if user.CreatedTime.After(time.Date(2022, time.November, 1, 0, 0, 0, 0, time.Local)) { + targetGroupPower[i].UserIds11 = append(targetGroupPower[i].UserIds11, userId) + } + } + } + } + type NumSum struct { + Count int + Price int + } + // 直接充值 + 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 { + panic(err) + } else { + targetGroupPower[i].UserChargeNum += userChargeNumSum.Count + targetGroupPower[i].UserChargeSum += float64(userChargeNumSum.Price) / 100 + } + if len(groupPower.UserIds9) > 0 { + var userChargeNumSum9 NumSum + if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds9).First(&userChargeNumSum9).Error; err != nil { + panic(err) + } else { + targetGroupPower[i].UserChargeNum9 += userChargeNumSum9.Count + targetGroupPower[i].UserChargeSum9 += float64(userChargeNumSum9.Price) / 100 + } + } + if len(groupPower.UserIds10) > 0 { + var userChargeNumSum10 NumSum + if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds10).First(&userChargeNumSum10).Error; err != nil { + panic(err) + } else { + targetGroupPower[i].UserChargeNum10 += userChargeNumSum10.Count + targetGroupPower[i].UserChargeSum10 += float64(userChargeNumSum10.Price) / 100 + } + } + if len(groupPower.UserIds11) > 0 { + var userChargeNumSum11 NumSum + if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds11).First(&userChargeNumSum11).Error; err != nil { + panic(err) + } else { + targetGroupPower[i].UserChargeNum11 += userChargeNumSum11.Count + targetGroupPower[i].UserChargeSum11 += float64(userChargeNumSum11.Price) / 100 + } + } + } + // 代理充值 + 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 { + panic(err) + } else { + targetGroupPower[i].UserChargeNum += userChargeNumSum.Count + targetGroupPower[i].UserChargeSum += float64(userChargeNumSum.Price) / 100 + } + if len(groupPower.UserIds9) > 0 { + var userChargeNumSum9 NumSum + if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds9).First(&userChargeNumSum9).Error; err != nil { + panic(err) + } else { + targetGroupPower[i].UserChargeNum9 += userChargeNumSum9.Count + targetGroupPower[i].UserChargeSum9 += float64(userChargeNumSum9.Price) / 100 + } + } + if len(groupPower.UserIds10) > 0 { + var userChargeNumSum10 NumSum + if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds10).First(&userChargeNumSum10).Error; err != nil { + panic(err) + } else { + targetGroupPower[i].UserChargeNum10 += userChargeNumSum10.Count + targetGroupPower[i].UserChargeSum10 += float64(userChargeNumSum10.Price) / 100 + } + } + if len(groupPower.UserIds11) > 0 { + var userChargeNumSum11 NumSum + if err := mysql.Db.Raw(userChargeNumSql, groupPower.UserIds11).First(&userChargeNumSum11).Error; err != nil { + panic(err) + } else { + targetGroupPower[i].UserChargeNum11 += userChargeNumSum11.Count + targetGroupPower[i].UserChargeSum11 += float64(userChargeNumSum11.Price) / 100 + } + } + } + println(targetGroupPower) + excelFileName := fmt.Sprintf("./india_power%s.xlsx", time.Now().Format("20060102150405")) + xlFile := xlsx.NewFile() + sheet, err := xlFile.AddSheet("india_charge_data") + if err != nil { + panic(err) + } + row := sheet.AddRow() + c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() + c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value = + "印度国籍的势力主ID", "所有势力成员数量", "所有势力成员累积充值", "所有进行了充值的势力成员数量", "9月1日之后注册的势力成员累积充值", + "9月1日之后注册的进行了充值的势力成员数量", "10月1日之后注册的势力成员累积充值", "10月1日之后注册的进行了充值的势力成员数量", "11月1日之后注册的势力成员累积充值", "11月1日之后注册的进行了充值的势力成员数量" + for _, power := range targetGroupPower { + row := sheet.AddRow() + c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() + c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value = + toString(power.OwnerCode), toString(len(power.UserIds)), toString(power.UserChargeSum), toString(power.UserChargeNum), + toString(power.UserChargeSum9), toString(power.UserChargeNum9), toString(power.UserChargeSum10), toString(power.UserChargeNum10), + toString(power.UserChargeSum11), toString(power.UserChargeNum11) + } + _ = xlFile.Save(excelFileName) +} + +func toString(n interface{}) string { + return cast.ToString(n) +} diff --git a/utils/utils.go b/utils/utils.go index 6cc3cfd..c3913c6 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,5 +1,5 @@ package utils func TestCommon() { - + println("hello") } diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 0000000..21a4198 --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,10 @@ +package utils + +import ( + "testing" + "time" +) + +func TestTestCommon(t *testing.T) { + t.Logf(time.Date(2022, time.November, 1, 0, 0, 0, 0, time.Local).String()) +} -- 2.22.0