act_710_data.go 2.25 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
package main

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

type Act710Data struct {
	Code        string
	SendUserId  uint64
	ActDiamond  uint64
	Levels      []int `gorm:"-"`
	RealDiamond uint64
}

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

type ActUserAward struct {
	Level int
}

func main() {
	var data []Act710Data
	if err := mysql.ProdReadOnlyDB.Table("act_gift_record").
		Where("activity_id = 710").
		Select("send_user_id,SUM(diamond) act_diamond").
		Group("send_user_id").Find(&data).Error; err != nil {
		panic(err)
	}
	for i, v := range data {
		var user model.User
		if err := mysql.ProdReadOnlyDB.Model(model.User{}).Where("id = ? ", v.SendUserId).First(&user).Error; err != nil {
			panic(err)
		}
		var realDiamond uint64
		if err := mysql.ProdReadOnlyDB.Table("gift_operate").
			Where("send_user_id = ? AND res_gift_id in (4766,4767,4768,4769) AND created_time >= \"2024-01-01 05:00:00\" AND created_time <= \"2024-01-08 05:00:00\"", v.SendUserId).
			Select("SUM(send_user_diamond) real_diamond").Scan(&realDiamond).Error; err != nil {
			panic(err)
		}
		data[i].Code = user.Code
		data[i].RealDiamond = realDiamond
		var levels []ActUserAward
		if err := mysql.ProdReadOnlyDB.Table("act_user_award").Where("activity_id = 710 AND user_id = ?", v.SendUserId).Select("level").Find(&levels).Error; err != nil {
			panic(err)
		}
		var l []int
		for _, v := range levels {
			l = append(l, v.Level)
		}
		data[i].Levels = l
		//break
	}
	excelFileName := fmt.Sprintf("./710数据.xlsx")
	xlFile := xlsx.NewFile()
	sheet, _ := xlFile.AddSheet("charge")
	row := sheet.AddRow()
	c1, c2, c3, c4 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
	c1.Value, c2.Value, c3.Value, c4.Value = "用户", "实际送礼", "活动统计送礼", "已经领取的任务"
	for _, d := range data {
		row := sheet.AddRow()
		c1, c2, c3, c4 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
		var l []string
		for _, v := range d.Levels {
			l = append(l, fmt.Sprintf("%v", v))
		}
		levels := strings.Join(l, ",")
		c1.Value, c2.Value, c3.Value, c4.Value = ats39(d.Code), ats39(d.RealDiamond), ats39(d.ActDiamond), ats39(levels)
	}
	_ = xlFile.Save(excelFileName)
}