svip_history_top.go 3.05 KB
Newer Older
hujiebin's avatar
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
package main

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

type SvipHistoryData struct {
	UserId       uint64
	Code         string
	Area         string
	Country      string
	SvipTopLevel int

	SvipLevel int
	Dollar    float64 // 累充金额$
}

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

func main() {
	var data []SvipHistoryData
	if err := mysql.ProdReadOnlyDB.Raw("select t.*,CASE area WHEN 1 THEN '阿语' ELSE '非阿语' END as area FROM (SELECT user_id,replace(MAX(remark),\"Become SVIP\",\"\") svip_top_level,code,country FROM `user_svip_detail` d,user u where u.id = d.user_id AND remark like '%Become SVIP%' GROUP BY user_id) t,res_country r where t.country = r.name").Find(&data).Error; err != nil {
		panic(err)
	}
	var userIds []uint64
	for _, d := range data {
		userIds = append(userIds, d.UserId)
	}
	type ChargeData struct {
		UserId  uint64
		Code    string
		Dollar  float64
		Country string
	}
	var chargeDatas []ChargeData
	chargeMap := make(map[uint64]ChargeData)
	if err := mysql.ProdReadOnlyDB.Raw("select id as user_id,code,SUM(dollar) as dollar,country FROM "+
		"(select u.id,u.`code`,SUM(price) / 100 as dollar,u.country FROM pay_order p, `user` u  where  u.id = p.user_id AND p.`status` = 2 AND `type` = 0 group by user_id UNION ALL select u.id,u.code,SUM(dollar) / 100 as dollar,u.country FROM dealer_transfer_detail d, `user` u where u.id = d.receiver_id group by receiver_id UNION ALL "+
		"select u.id,u.code,SUM(dollar) / 100 as dollar,u.country FROM dealer_transfer_detail_pink d, `user` u where u.id = d.receiver_id group by receiver_id) t "+
		"where t.id in ? group by code order by dollar DESC", userIds).Find(&chargeDatas).Error; err != nil {
		panic(err)
	}
	for i, v := range chargeDatas {
		chargeMap[v.UserId] = chargeDatas[i]
	}
	for i, uc := range data {
		data[i].SvipLevel = GetUserSvip2(uc.UserId)
		if d, ok := chargeMap[uc.UserId]; ok {
			data[i].Dollar = d.Dollar
		}
		//break
	}

	excelFileName := fmt.Sprintf("./svip历史充值.xlsx")
	xlFile := xlsx.NewFile()
	sheet, err := xlFile.AddSheet("charge")
	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等级", "区域", "国家", "目前SVIP等级", "累计充值$"
	for _, d := range data {
		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 = d.Code, ats41(d.SvipTopLevel), ats41(d.Area), ats41(d.Country), ats41(d.SvipLevel), ats41(d.Dollar)
	}
	_ = xlFile.Save(excelFileName)
}

func GetUserSvip2(uid uint64) int {
	type UserSvip struct {
		UserId uint64
		Level  int
	}
	var res UserSvip
	if err := mysql.ProdReadOnlyDB.Table("user_svip").Where("user_id = ?", uid).First(&res).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			panic(err)
		}
	}
	return res.Level
}