charge_max.go 5.9 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8
package main

import (
	"fmt"
	"git.hilo.cn/hilo-common/script/model"
	"git.hilo.cn/hilo-common/script/mysql"
	"github.com/tealeg/xlsx"
	"gorm.io/gorm"
hujiebin's avatar
hujiebin committed
9
	"time"
hujiebin's avatar
hujiebin committed
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
)

type ChargeMaxData struct {
	Dollar         float64 // 最高单笔充值金额$
	Area           string  // 区域
	Code           string  // 用户ID
	MoneyMaxDollar float64 // 最高当月充值金额$
	LastLoginTime  string  // 最后登录时间
	LastChargeTime string  // 最近一次充值时间
	GroupPowerCode string  // 家族长ID
	CpCode         string  // CP ID
}

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

func main() {
	type ChargeMaxUid struct {
		UserId uint64
		Dollar int64
	}
	var chargeUids []ChargeMaxUid
	if err := mysql.ProdReadOnlyDB.Raw("select user_id,MAX(dollar) as dollar " +
		"FROM (SELECT user_id,MAX(price) as dollar FROM `pay_order` WHERE `status` = '2' AND `type` = '0' AND created_time >= \"2022-07-01\" GROUP BY user_id " +
		"union ALL SELECT receiver_id as user_id,MAX(dollar) as dollar FROM dealer_transfer_detail WHERE created_time >= \"2022-07-01\" GROUP BY receiver_id) t GROUP BY user_id").Find(&chargeUids).Error; err != nil {
		panic(err)
	}
	var data []ChargeMaxData
	for _, uc := range chargeUids {
		if uc.UserId <= 0 {
			continue
		}
		u, err := GetUserById25(uc.UserId)
		if err != nil {
			panic(err)
		}
		data = append(data, ChargeMaxData{
			Dollar:         float64(uc.Dollar) / 100,
			Code:           u.Code,
			Area:           GetAreaByCode(u.Code),
			MoneyMaxDollar: GetUserMonthMaxCharge(uc.UserId),
			LastLoginTime:  GetLastLoginTime(uc.UserId),
			LastChargeTime: GetLastChargeTime(uc.UserId),
			GroupPowerCode: GetGroupPowerCode(uc.UserId),
			CpCode:         GetCpCode(uc.UserId),
		})
		//break
	}

	excelFileName := fmt.Sprintf("./202207充值.xlsx")
	xlFile := xlsx.NewFile()
	sheet, err := xlFile.AddSheet("slot")
	if err != nil {
		panic(err)
	}
	row := sheet.AddRow()
	c1, c2, c3, c4, c5, c6, c7, c8 := 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 = "最高单笔充值金额$", "区域", "用户ID", "最高当月充值金额$", "最后登录时间", "最近一次充值时间", "家族长ID", "CP ID"
	for _, d := range data {
		row := sheet.AddRow()
		c1, c2, c3, c4, c5, c6, c7, c8 := 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 = ats25(d.Dollar), ats25(d.Area), ats25(d.Code), ats25(d.MoneyMaxDollar), ats25(d.LastLoginTime), ats25(d.LastChargeTime), ats25(d.GroupPowerCode), ats25(d.CpCode)
	}
	_ = xlFile.Save(excelFileName)
}

func GetUserById25(id uint64) (model.User, error) {
	var u model.User
	if err := mysql.ProdReadOnlyDB.Model(model.User{}).Where("id = ?", id).First(&u).Error; err != nil {
		//panic(err)
	}
	return u, nil
}

func GetUserMonthMaxCharge(uid uint64) float64 {
	var dollar int64
hujiebin's avatar
hujiebin committed
87
	if err := mysql.ProdReadOnlyDB.Raw("select MAX(dollar) FROM\n(\nselect d,SUM(dollar) as dollar FROM (\nSELECT\n\tuser_id,\n\tDATE_FORMAT(created_time,\"%Y-%m\") as d,\n\tSUM(price) as dollar\nFROM\n\t`pay_order`\nWHERE\n\t`status` = '2'\nAND `type` = '0'\nAND created_time >= \"2022-07-01\"\nAND user_id = ?\nGROUP BY user_id,d \nUNION ALL \nSELECT\n\treceiver_id user_id,\n\tDATE_FORMAT(created_time, \"%Y-%m\") AS d,\n\tSUM(dollar) AS dollar\nFROM\n\t`dealer_transfer_detail`\nWHERE\n\tcreated_time >= \"2022-07-01\"\nAND receiver_id = ?\nGROUP BY\n\treceiver_id,\n\td\n) t GROUP BY d\n) t2", uid, uid).Scan(&dollar).Error; err != nil {
hujiebin's avatar
hujiebin committed
88 89 90 91 92 93 94 95 96 97 98
		panic(err)
	}
	return float64(dollar) / 100
}

func GetGroupPowerCode(uid uint64) string {
	var groupPowerId uint64
	if err := mysql.ProdReadOnlyDB.Table("group_power_user").Where("user_id = ?", uid).Select("group_power_id").Scan(&groupPowerId).Error; err != nil {
		panic(err)
	}
	if groupPowerId <= 0 {
hujiebin's avatar
hujiebin committed
99
		return "0"
hujiebin's avatar
hujiebin committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	}
	var ownerId uint64
	if err := mysql.ProdReadOnlyDB.Table("group_power_user").Where("group_power_id = ? AND role = 2", groupPowerId).Select("user_id").Scan(&ownerId).Error; err != nil {
		panic(err)
	}
	user, err := GetUserById25(ownerId)
	if err != nil {
		panic(err)
	}
	return user.Code
}

func GetCpCode(uid uint64) string {
	type Cp struct {
		Id      uint64
		UserId1 uint64
		UserId2 uint64
	}
	var cp Cp
	if err := mysql.ProdReadOnlyDB.Table("cp").Where("user_id1 = ? OR user_id2 = ?", uid, uid).Order("score DESC").Limit(1).First(&cp).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
hujiebin's avatar
hujiebin committed
121
			return "0"
hujiebin's avatar
hujiebin committed
122 123 124 125
		}
		panic(err)
	}
	if cp.Id <= 0 {
hujiebin's avatar
hujiebin committed
126
		return "0"
hujiebin's avatar
hujiebin committed
127 128 129 130 131 132 133 134 135 136 137 138 139
	}
	cpUid := cp.UserId2
	if cpUid == uid {
		cpUid = cp.UserId1
	}
	user, err := GetUserById25(cpUid)
	if err != nil {
		panic(err)
	}
	return user.Code
}

func GetLastLoginTime(uid uint64) string {
hujiebin's avatar
hujiebin committed
140
	var t time.Time
hujiebin's avatar
hujiebin committed
141 142 143
	if err := mysql.ProdReadOnlyDB.Table("user_request_last").Where("user_id = ?", uid).Select("time_last").Scan(&t).Error; err != nil {
		panic(err)
	}
hujiebin's avatar
hujiebin committed
144
	return t.Format("2006-01-02")
hujiebin's avatar
hujiebin committed
145 146 147
}

func GetLastChargeTime(uid uint64) string {
hujiebin's avatar
hujiebin committed
148
	var t time.Time
hujiebin's avatar
hujiebin committed
149 150 151
	if err := mysql.ProdReadOnlyDB.Raw("SELECT\n\tmax(created_time)\nFROM\n\t(\n\t\tSELECT\n\t\t\tMAX(created_time) created_time\n\t\tFROM\n\t\t\t`pay_order`\n\t\tWHERE\n\t\t\t`status` = '2'\n\t\tAND `type` = '0'\n\t\tAND created_time >= \"2022-07-01\"\n\t\tAND user_id = ?\n\t\tUNION ALL\n\t\t\tSELECT\n\t\t\t\tMAX(created_time) created_time\n\t\t\tFROM\n\t\t\t\t`dealer_transfer_detail`\n\t\t\tWHERE\n\t\t\t\tcreated_time >= \"2022-07-01\"\n\t\t\tAND receiver_id = ?\n\t) t", uid, uid).Scan(&t).Error; err != nil {
		panic(err)
	}
hujiebin's avatar
hujiebin committed
152
	return t.Format("2006-01-02")
hujiebin's avatar
hujiebin committed
153 154 155 156 157 158 159 160 161 162 163 164 165
}

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 "非阿语区"
}