user_wealth_high.go 3.15 KB
Newer Older
hujiebin's avatar
1  
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
package main

import (
	"fmt"
	"git.hilo.cn/hilo-common/script/mysql"
	"github.com/spf13/cast"
	"github.com/tealeg/xlsx"
)

type UserWealthHigh struct {
	UserId    uint64
	Grade     int
	Code      string
	Country   string
	Area      string
	Charge    int64
	SvipLevel int64
}

type ResArea struct {
	Name string
	Area int
}

type UserSvip22 struct {
	UserId uint64
	Level  int64
}

func main() {
	var users []UserWealthHigh
	if err := mysql.ProdReadOnlyDB.Table("match_wealth_user_score").Joins("INNER JOIN user ON user.id = match_wealth_user_score.user_id").Where("grade >= 20").
		Select("user_id,grade,country,code").Find(&users).Error; err != nil {
		panic(err)
	}
	var resAreas []ResArea
	if err := mysql.ProdReadOnlyDB.Table("res_country").Find(&resAreas).Error; err != nil {
		panic(err)
	}
	areaMap := make(map[string]string)
	for _, v := range resAreas {
		if v.Area == 1 {
			areaMap[v.Name] = "阿语区"
		} else {
			areaMap[v.Name] = "非阿语"
		}
	}
	var userIds []uint64
	for _, v := range users {
		userIds = append(userIds, v.UserId)
	}
	var svips []UserSvip22
	if err := mysql.ProdReadOnlyDB.Table("user_svip").Where("user_id in ?", userIds).Find(&svips).Error; err != nil {
		panic(err)
	}
	sm := make(map[uint64]int64)
	for _, v := range svips {
		sm[v.UserId] = v.Level
	}
	for i, u := range users {
		users[i].Area = areaMap[u.Country]
		users[i].SvipLevel = sm[u.UserId]
		users[i].Charge = GetUserChargeMoneySum(u.UserId)
		if i >= 2 {
			//break
		}
	}
	excelFileName := fmt.Sprintf("./财富等级用户.xlsx")
	xlFile := xlsx.NewFile()
	sheet, err := xlFile.AddSheet("wealth")
	if err != nil {
		panic(err)
	}
	row := sheet.AddRow()
	c1, c2, c3, c4, c5, c6 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
	c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value = "id", "财富等级", "区域", "国家", "累计充值金额(美分)", "svip等级"
	for _, d := range users {
		row := sheet.AddRow()
		c1, c2, c3, c4, c5, c6 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
		c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value =
			cast.ToString(d.Code), cast.ToString(d.Grade), cast.ToString(d.Area), cast.ToString(d.Country), cast.ToString(d.Charge), cast.ToString(d.SvipLevel)
	}
	_ = xlFile.Save(excelFileName)
}

// 获取用户累计充值
func GetUserChargeMoneySum(userId uint64) int64 {
	type R struct {
		Money int64
	}
	var money R
	var total int64
	if err := mysql.ProdReadOnlyDB.Table("pay_order AS p").
		Where("p.status = 2 AND p.`type` = 0"). // type=0 就是用户给自己充值,status=2成功
		Where("p.user_id = ?", userId).
		Select("SUM(p.price) as money").Scan(&money).Error; err != nil {
	}
	total += money.Money
	var money2 R
	if err := mysql.ProdReadOnlyDB.Table("dealer_transfer_detail AS t").
		Where("t.receiver_id = ?", userId).
		Select("SUM(t.dollar) as money").
		Scan(&money2).Error; err != nil {
	}
	total += money2.Money
	var money3 R
	if err := mysql.ProdReadOnlyDB.Table("dealer_transfer_detail_pink AS t").
		Where("t.receiver_id = ?", userId).
		Select("SUM(t.dollar) as money").
		Scan(&money3).Error; err != nil {
	}
	total += money3.Money
	return total
}