group_power_data.go 4.76 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package main

import (
	"encoding/json"
	"fmt"
hujiebin's avatar
hujiebin committed
6
	"git.hilo.cn/hilo-common/script/model"
hujiebin's avatar
hujiebin committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
	"git.hilo.cn/hilo-common/script/mysql"
	"github.com/tealeg/xlsx"
	"io/ioutil"
	"net/http"
)

type GroupPowerResponse struct {
	Data struct {
		Total int64            `json:"total"`
		Data  []GroupPowerData `json:"data"`
	}
}

type GroupPowerData struct {
hujiebin's avatar
hujiebin committed
21
	Id                        uint64  `json:"id"`   // 家族id
hujiebin's avatar
hujiebin committed
22 23
	Code                      string  `json:"code"` // 家族长ID
	Area                      string  `json:"-"`
hujiebin's avatar
hujiebin committed
24
	GroupPowerName            string  `json:"groupPowerName"`            // 家族昵称
hujiebin's avatar
hujiebin committed
25 26 27 28 29 30
	GroupPowerExp             int64   `json:"groupPowerExp"`             // 经验值
	GroupPowerConsume         int64   `json:"groupPowerConsume"`         // 家族房间流水
	GroupPowerMemberNum       int     `json:"groupPowerMemberNum"`       // 成员数
	GroupPowerMemberCharge    float64 `json:"groupPowerMemberCharge"`    // 成员充值$
	GroupPowerNewMemberNum    int     `json:"groupPowerNewMemberNum"`    // 新成员数
	GroupPowerNewMemberCharge float64 `json:"groupPowerNewMemberCharge"` // 新成员充值$
hujiebin's avatar
hujiebin committed
31 32
	CodeSvip                  int     `json:"-"`                         // 家族长svip等级
	Svip3UpCnt                int     `json:"-"`                         // svip3以上的成员数
hujiebin's avatar
hujiebin committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
}

func ats23(a interface{}) string {
	return fmt.Sprintf("%v", a)
}

func getAreaByCode(code string) string {
	sql := "SELECT area FROM res_country c,user u WHERE u.country = c.name AND u.code = ?"
	var area int
	if err := mysql.ProdReadOnlyDB.Raw(sql, code).Scan(&area).Error; err != nil {
		panic(err)
	}
	if area == 1 {
		return "阿语区"
	}
	return "非阿语区"
}

hujiebin's avatar
hujiebin committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
func getUserSvipLevel(code string) int {
	var user model.User
	if err := mysql.ProdReadOnlyDB.Model(model.User{}).Where("code = ?", code).First(&user).Error; err != nil {
		panic(err)
	}
	var level int
	err := mysql.ProdReadOnlyDB.Table("user_svip").Where("user_id = ?", user.Id).Select("level").Scan(&level).Error
	if err != nil {
		panic(err)
	}
	return level
}

func getUserSvip3Cnt(groupPowerId uint64) int {
	var total int
	err := mysql.ProdReadOnlyDB.Raw("SELECT count(*) FROM `group_power_user` p,user_svip s where p.user_id = s.user_id AND s.`level` >= 3 AND p.group_power_id = ?", groupPowerId).Scan(&total).Error
	if err != nil {
		panic(err)
	}
	return total
}

hujiebin's avatar
hujiebin committed
73 74 75 76 77
func main() {
	var data []GroupPowerData
	pageIndex := 1
	pageSize := 500
	for {
hujiebin's avatar
hujiebin committed
78
		url := fmt.Sprintf("https://apiv2.faceline.live/v1/groupPower/page?lang=zh-cn&ownerCode=&beginDate=2023-06-01&endDate=2023-06-12&pageIndex=%d&pageSize=%d&status=1", pageIndex, pageSize)
hujiebin's avatar
hujiebin committed
79 80 81 82 83 84 85 86 87 88
		method := "GET"

		client := &http.Client{}
		req, err := http.NewRequest(method, url, nil)

		if err != nil {
			fmt.Println(err)
			return
		}
		req.Header.Add("nonce", "hilo")
hujiebin's avatar
hujiebin committed
89
		req.Header.Add("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjI1MSwiRXh0ZXJuYWxJZCI6IiIsImV4cCI6MTY4ODU0MDUxOX0.VWJAIv0iWtOgnefrfE5Wbeqc1MvmGjaxuRc47M1PvH8")
hujiebin's avatar
hujiebin committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

		res, err := client.Do(req)
		if err != nil {
			fmt.Println(err)
			return
		}

		body, err := ioutil.ReadAll(res.Body)
		if err != nil {
			fmt.Println(err)
			return
		}
		_ = res.Body.Close()
		var resp GroupPowerResponse
		err = json.Unmarshal(body, &resp)
		if err != nil {
			panic(err)
		}
		if len(resp.Data.Data) <= 0 {
			break
		}
		data = append(data, resp.Data.Data...)
		pageIndex++
	}
hujiebin's avatar
hujiebin committed
114
	excelFileName := fmt.Sprintf("./家族6月数据.xlsx")
hujiebin's avatar
hujiebin committed
115 116 117 118 119 120
	xlFile := xlsx.NewFile()
	sheet, err := xlFile.AddSheet("slot")
	if err != nil {
		panic(err)
	}
	row := sheet.AddRow()
hujiebin's avatar
hujiebin committed
121 122
	c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11 := row.AddCell(), 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, c11.Value = "家族长ID", "家族名称", "区域", "经验值", "家族房间流水", "成员数", "成员充值$", "新成员数", "新成员充值$", "家族长svip等级", "家族超过SVIP3的人数"
hujiebin's avatar
hujiebin committed
123
	for _, d := range data {
hujiebin's avatar
hujiebin committed
124 125 126
		d.Area = getAreaByCode(d.Code)
		d.CodeSvip = getUserSvipLevel(d.Code)
		d.Svip3UpCnt = getUserSvip3Cnt(d.Id)
hujiebin's avatar
hujiebin committed
127
		row := sheet.AddRow()
hujiebin's avatar
hujiebin committed
128 129 130 131
		c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11 := row.AddCell(), 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, c11.Value =
			d.Code, d.GroupPowerName, d.Area, ats23(d.GroupPowerExp), ats23(d.GroupPowerConsume), ats23(d.GroupPowerMemberNum),
			ats23(d.GroupPowerMemberCharge), ats23(d.GroupPowerNewMemberNum), ats23(d.GroupPowerNewMemberCharge), ats23(d.CodeSvip), ats23(d.Svip3UpCnt)
hujiebin's avatar
hujiebin committed
132 133 134
	}
	_ = xlFile.Save(excelFileName)
}