sync_group_code.go 1.36 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4
package main

import (
	"fmt"
hujiebin's avatar
hujiebin committed
5 6
	model2 "git.hilo.cn/hilo-common/script/model"
	"git.hilo.cn/hilo-common/script/mysql"
hujiebin's avatar
hujiebin committed
7
	"time"
hujiebin's avatar
hujiebin committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
)

func main() {
	type GroupConsume struct {
		GroupId string
		Total   int
	}
	type UpdateGroupCode struct {
		GroupId string
		OldCode string
		NewCode string
	}
	var updates []UpdateGroupCode
	var rows []GroupConsume
	sql := "SELECT group_id,SUM(diamond) AS total FROM `room_month_consume` group by group_id HAVING total > 1000000;"
	if err := mysql.ProdReadOnlyDB.Raw(sql).Find(&rows).Error; err != nil {
		panic(err)
	}
	for _, row := range rows {
hujiebin's avatar
hujiebin committed
27 28
		groupInfo := new(model2.GroupInfo)
		if err := mysql.ProdReadOnlyDB.Model(model2.GroupInfo{}).Where("im_group_id = ?", row.GroupId).First(groupInfo).Error; err != nil {
hujiebin's avatar
hujiebin committed
29 30
			panic(err)
		}
hujiebin's avatar
hujiebin committed
31 32
		owner := new(model2.User)
		if err := mysql.ProdReadOnlyDB.Model(model2.User{}).Where("id = ?", groupInfo.Owner).First(owner).Error; err != nil {
hujiebin's avatar
hujiebin committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
			panic(err)
		}
		if owner.Code != owner.OriginCode && owner.Code != groupInfo.Code {
			updates = append(updates, UpdateGroupCode{
				GroupId: groupInfo.ImGroupId,
				OldCode: groupInfo.Code,
				NewCode: owner.Code,
			})
		}
	}
	for _, update := range updates {
		sql := fmt.Sprintf("UPDATE group_info SET code = '%s' WHERE im_group_id = '%s' AND code = '%s' limit 1", update.NewCode, update.GroupId, update.OldCode)
		fmt.Println(sql)
	}
hujiebin's avatar
hujiebin committed
47
	time.Sleep(time.Second)
hujiebin's avatar
hujiebin committed
48
}