girl_support.go 5.77 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 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
package main

import (
	"fmt"
	"git.hilo.cn/hilo-common/_const/enum/timezone_e"
	"git.hilo.cn/hilo-common/script/model"
	"git.hilo.cn/hilo-common/script/mysql"
	"github.com/tealeg/xlsx"
	"strconv"
	"time"
)

/**
 * 用户财富分数
 **/
type MatchWealthUserScore struct {
	UserId uint64
	Score  uint32
	Grade  int
}

//获取财富等级
func MGetWealthGrade(userIds []uint64) (map[uint64]MatchWealthUserScore, error) {
	var res = make(map[uint64]MatchWealthUserScore)
	var rows []MatchWealthUserScore
	if err := mysql.ProdReadOnlyDB.Model(&MatchWealthUserScore{}).Where("user_id in ?", userIds).Find(&rows).Error; err != nil {
		return res, err
	}
	for i, v := range rows {
		res[v.UserId] = rows[i]
	}
	return res, nil
}

// 用户上麦
type UserOnMic struct {
	Date      string
	UserId    uint64
	Seconds   uint32
	LastCalTs int64
	Tz        timezone_e.Timezone
}

// 批量获取用户上麦时长
// @return userId->seconds
func MGetUserOnMicSecondsRange(beginDate, endDate string, tz timezone_e.Timezone, userIds []uint64) (map[uint64]uint32, error) {
	var rows []UserOnMic
	res := make(map[uint64]uint32)
	if err := mysql.ProdReadOnlyDB.Model(UserOnMic{}).Where("`date` >= ? AND `date` <= ? AND tz = ? AND user_id in ?", beginDate, endDate, tz, userIds).Find(&rows).Error; err != nil {
		return res, err
	}
	for _, r := range rows {
		res[r.UserId] = r.Seconds
	}
	return res, nil
}

// 批量获取家族信息
func MGetGroupPowerInfoMapByUser(userIds []uint64) (map[uint64]string, error) {
	type GroupPowerWithUser struct {
		Name   string
		UserId uint64
	}
	var rows []GroupPowerWithUser
	res := make(map[uint64]string)
	if err := mysql.ProdReadOnlyDB.Table("group_power p").
		Joins("INNER JOIN group_power_user u ON p.id = u.group_power_id").
		Where("u.user_id in ?", userIds).
		Select("p.name,u.user_id").
		Find(&rows).Error; err != nil {
		return res, err
	}
	for i, v := range rows {
		res[v.UserId] = rows[i].Name
	}
	return res, nil
}

type UserGift struct {
	UserId  uint64
	Diamond uint32
	Send    uint32
}

// 批量获取收礼
func MGetUserGiftOperate(beginTime, endTime time.Time, userIds []uint64) (map[uint64]UserGift, error) {
	var rows []UserGift
	res := make(map[uint64]UserGift)
	if err := mysql.ProdReadOnlyDB.Table("gift_operate").Where("receive_user_id in ? AND created_time >= ? AND created_time < ?", userIds, beginTime, endTime).
		Select("receive_user_id as user_id,SUM(send_user_diamond) diamond,COUNT(distinct send_user_id) send").Group("receive_user_id").Find(&rows).Error; err != nil {
		return res, err
	}
	for i, v := range rows {
		res[v.UserId] = rows[i]
	}
	return res, nil
}

type GirlSupport struct {
	UserId      uint64
	CreatedTime time.Time
}

func main() {
	var girlSupport []GirlSupport
	var userIds []uint64
	// 4月13日到4月16日的报名用户的数据
	beginTime := time.Date(2023, time.April, 13, 0, 0, 0, 0, timezone_e.BeijingTimezoneLoc)
	endTime := time.Date(2023, time.April, 17, 0, 0, 0, 0, timezone_e.BeijingTimezoneLoc)
	if err := mysql.ProdReadOnlyDB.Table("girl_support_user").Where("created_time >= ? AND created_time < ?", beginTime, endTime).Find(&girlSupport).Error; err != nil {
		panic(err)
	}
	// 4月3日到4月9日的数据
	beginTime = time.Date(2023, time.April, 3, 0, 0, 0, 0, timezone_e.BeijingTimezoneLoc)
	endTime = time.Date(2023, time.April, 10, 0, 0, 0, 0, timezone_e.BeijingTimezoneLoc)
	gm := make(map[uint64]string)
	for _, v := range girlSupport {
		userIds = append(userIds, v.UserId)
		gm[v.UserId] = v.CreatedTime.Format("2006-01-02")
	}
	var users []model.User
	if err := mysql.ProdReadOnlyDB.Model(model.User{}).Where("id in ?", userIds).Find(&users).Error; err != nil {
		panic(err)
	}
	uM := make(map[uint64]model.User)
	for k, u := range users {
		uM[u.Id] = users[k]
	}
	grade, err := MGetWealthGrade(userIds)
	if err != nil {
		panic(err)
	}
	mic, err := MGetUserOnMicSecondsRange(beginTime.Format("2006-01-02"), endTime.AddDate(0, 0, -1).Format("2006-01-02"), timezone_e.TimezoneBeijing, userIds)
	if err != nil {
		panic(err)
	}
	gifts, err := MGetUserGiftOperate(beginTime, endTime, userIds)
	if err != nil {
		panic(err)
	}
	groupPower, err := MGetGroupPowerInfoMapByUser(userIds)
	if err != nil {
		panic(err)
	}
	type Data struct {
		ID         string
		Country    string
		Grade      string
		Name       string
		Mic        string
		SupportNum string
		Diamond    string
		Sign       string
	}
	var data []Data
	for _, v := range userIds {
		data = append(data, Data{
			ID:         uM[v].Code,
			Country:    uM[v].Country,
			Grade:      fmt.Sprintf("%d", grade[v].Grade),
			Name:       groupPower[v],
			Mic:        Decimal(float64(mic[v]) / 3600),
			SupportNum: fmt.Sprintf("%d", gifts[v].Send),
			Diamond:    fmt.Sprintf("%d", gifts[v].Diamond),
			Sign:       gm[v],
		})
	}
	excelFileName := fmt.Sprintf("./女孩扶持4月3-9日.xlsx")
	xlFile := xlsx.NewFile()
	sheet, err := xlFile.AddSheet("charge")
	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 = "用户ID", "国家", "财富等级", "家族名称", "上麦时长 h"
	c6.Value, c7.Value, c8.Value = "支持者人数", "收礼钻石数", "报名时间"
	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 = d.ID, d.Country, d.Grade, d.Name, d.Mic
		c6.Value, c7.Value, c8.Value = d.SupportNum, d.Diamond, d.Sign
	}
	_ = xlFile.Save(excelFileName)
}

// 保留两位小数
func Decimal(value float64) string {
	newValue, err := strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
	if err != nil {
		return ""
	}
	return fmt.Sprintf("%v", newValue)
}