user_charge.go 2.22 KB
Newer Older
1 2 3 4
package main

import (
	"fmt"
hujiebin's avatar
hujiebin committed
5 6
	"git.hilo.cn/hilo-common/script/model"
	"git.hilo.cn/hilo-common/script/mysql"
7 8 9
)

func main() {
hujiebin's avatar
hujiebin committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23
	//uids := []uint64{889621, 904971, 1072931, 1083981, 1108951, 1147471, 1187511, 1365041, 1496521, 1540091, 1888121, 2037771, 2236221, 2396461, 2603721, 2613321, 2648851, 2778081, 3142631, 3219391, 3401561, 3533911, 3602511, 3791481, 3840571, 3845671, 3865481, 3873381, 3892321, 4009941}
	var uids []uint64
	if err := mysql.ProdReadOnlyDB.Raw("select id from user where origin_code <> `code`").Find(&uids).Error; err != nil {
		panic(err)
	}
	var users []model.User
	if err := mysql.ProdReadOnlyDB.Model(model.User{}).Where("id in ?", uids).Find(&users).Error; err != nil {
		panic(err)
	}
	uM := make(map[uint64]model.User)
	for k, u := range users {
		uM[u.Id] = users[k]
	}
	var data = make(map[uint64]int64)
24
	for _, uid := range uids {
hujiebin's avatar
hujiebin committed
25 26 27
		m1, _ := GetUsersChargeMoneySum([]uint64{uid})
		m2, _ := GetUserDealerTransferMoneySum([]uint64{uid})
		data[uid] = m1 + m2
28
	}
hujiebin's avatar
hujiebin committed
29 30
	for uid, num := range data {
		fmt.Printf("%d,%s,%d\n", uid, uM[uid].Country, num)
31 32 33 34 35 36
	}
}

// 获取用户范围内充值金额
// param sinceTime: 过去n时间内
// 返回美分
hujiebin's avatar
hujiebin committed
37
func GetUsersChargeMoneySum(uids []uint64) (int64, error) {
38 39 40 41 42 43 44 45 46
	if len(uids) <= 0 {
		return 0, nil
	}
	type R struct {
		Money int64
	}
	var money R
	if err := mysql.ProdReadOnlyDB.Table("diamond_account_detail AS d").
		Joins("INNER JOIN pay_order AS p ON d.origin_id = p.id").
hujiebin's avatar
hujiebin committed
47 48
		Where("d.user_id in (?) AND operate_type in (?) AND add_reduce = 1",
			uids, []int{int(4), int(42), int(55), int(68)}).
49 50 51 52 53 54 55 56 57
		Select("SUM(p.price) as money").Scan(&money).Error; err != nil {
		return 0, err
	}
	return money.Money, nil
}

// 获取代理范围内总充值金额
// param sinceTime: 过去n时间内
// 返回美分
hujiebin's avatar
hujiebin committed
58
func GetUserDealerTransferMoneySum(uids []uint64) (int64, error) {
59 60 61 62 63 64 65 66 67
	if len(uids) <= 0 {
		return 0, nil
	}
	type R struct {
		Price int64
	}
	var money R
	if err := mysql.ProdReadOnlyDB.Table("diamond_account_detail AS d").
		Joins("INNER JOIN dealer_transfer_detail AS t ON d.origin_id = t.id").
hujiebin's avatar
hujiebin committed
68 69
		Where("d.user_id in (?) AND operate_type = ? AND add_reduce = 1",
			uids, 26).Select("SUM(t.dollar) as price").
70 71 72 73 74
		Scan(&money).Error; err != nil {
		return 0, err
	}
	return money.Price, nil
}