sk_gm_server/library/task/taskUtil.go

974 lines
34 KiB
Go
Raw Normal View History

2023-03-20 13:38:28 +08:00
package task
import (
"context"
"encoding/csv"
"fmt"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/os/gcron"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/util/gutil"
"gmanager/library/codeutil"
"gmanager/library/mongo/gameinfo"
"log"
"strconv"
"strings"
"time"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
var serverMap *gmap.TreeMap
func init() {
serverMap = gmap.NewTreeMap(gutil.ComparatorInt, true)
glog.Info("timer start")
gcron.AddSingleton("0 30 1 * * *", Liucun)
gcron.AddSingleton("0 30 1 * * *", LiucunByServer)
// 每日凌晨一点 统计计算LTV需要的值取每天新增的角色数及他们的每日充值金额
gcron.AddSingleton("0 0 1 * * ?", StatisticalLTV)
gcron.AddSingleton("0 0 1 * * ?", StatisticalLTV_Server)
/**
5
*/
// 新增注册人数
gcron.AddSingleton("5 0 * * * ?", Hour)
// 新增登录人数
gcron.AddSingleton("5 0 * * * ?", HourLogin)
/**
30
*/
// 统计充值人数、充值总额(刷新统计值)
gcron.AddSingleton("0 50 23 * * ?", initServerPayInfo)
2023-03-20 13:38:28 +08:00
gcron.AddSingleton("0 0,30 * * * ?", StatisticalRecharge)
gcron.AddSingleton("0 0,30 * * * ?", StatisticalRecharge_Server)
// 判断是否需要导出表格
if "1" == g.Config().GetString("game.export") {
// 按天执行 0 10 0 * * *
// 订单信息表: log_charge
gcron.AddSingleton("0 10 0 * * *", logCharge)
// 道具消耗表: log_throw
gcron.AddSingleton("0 10 0 * * *", logThrow)
// 道具发放表: log_goods
gcron.AddSingleton("0 10 0 * * *", logGoods)
// 按月度执行 0 0 0 1 * *
// 道具信息表: goods_info
gcron.AddSingleton("0 0 0 1 * *", goodsInfo)
// 用户账号与角色对应关系表: log_role
gcron.AddSingleton("0 0 0 1 * *", logRole)
}
}
// 处理留存数据
func Liucun() {
//只算今天的情况
date := g.Config().GetString("startday")
startTime := gtime.NewFromStr(date)
for {
daystartTime := startTime.Local().TimestampMilli()
dayendTime := startTime.Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
//fmt.Println(daystartTime, dayendTime)
//fmt.Println(startTime.Date())
//获得今天的新增的玩家
users := gameinfo.ModelRegister.FindNew(daystartTime, dayendTime)
usersList := gconv.SliceAny(users)
userStr := gconv.Ints(usersList)
regStartTime := startTime.Clone()
days := 0
tempDate := startTime.Format("Y-m-d")
for {
regdaystartTime := regStartTime.Local().TimestampMilli()
regdayendTime := regStartTime.Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
if regStartTime.DayOfYear() == gtime.Now().DayOfYear() {
break
}
bs := bson.D{{"creatday", tempDate}, {"day", days}}
info := gameinfo.ModelLiucun.FindOneBson(bs)
if info != 0 {
regStartTime = regStartTime.AddDate(0, 0, 1)
days++
continue
}
userLoginNum := gameinfo.ModeLogin.FindLogin(regdaystartTime, regdayendTime, userStr)
//fmt.Println("login", days, userLoginNum)
logLiucun := new(gameinfo.LogLiucun)
logLiucun.Id = primitive.NewObjectID()
logLiucun.CreatDay = tempDate
logLiucun.NewUser = len(usersList)
logLiucun.DayCount = userLoginNum
logLiucun.Day = days
gameinfo.ModelLiucun.InsertOne(logLiucun)
regStartTime2 := regStartTime.Clone().Format("Y-m-d")
payTimeS := regStartTime2 + " 00:00:00"
payTimeE := regStartTime2 + " 23:59:59"
payUserNum := gameinfo.ModelPay.FindPay(payTimeS, payTimeE, userStr, "")
logPayLiucun := new(gameinfo.LogPayLiucun)
logPayLiucun.Id = primitive.NewObjectID()
logPayLiucun.CreatDay = tempDate
logPayLiucun.NewUser = len(usersList)
logPayLiucun.DayCount = payUserNum
logPayLiucun.Day = days
gameinfo.ModelPayLiucun.InsertOne(logPayLiucun)
regStartTime = regStartTime.AddDate(0, 0, 1)
days++
}
//fmt.Println("new user", users)
startTime = startTime.AddDate(0, 0, 1)
if startTime.DayOfYear() == gtime.Now().DayOfYear() {
break
}
continue
}
fmt.Println("Liucun end")
}
// 处理各服务器的留存数据
func LiucunByServer() {
date := g.Config().GetString("startday")
startTime := gtime.NewFromStr(date)
for {
daystartTime := startTime.Local().TimestampMilli()
dayendTime := startTime.Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
//获得今天的新增的各服务器玩家
result := gameinfo.ModelRegister.FindRegisterByServer(daystartTime, dayendTime)
var showsWithInfo []bson.M
if err := result.All(context.TODO(), &showsWithInfo); err != nil {
fmt.Println(err)
}
for _, v := range showsWithInfo {
serverId := gconv.String(v["_id"])
days := 0
tempDate := startTime.Format("Y-m-d")
regStartTime := startTime.Clone()
//根据时间范围和区服id查询注册的用户
list := garray.New(false)
bs := bson.D{{"register.registerTime", bson.D{{"$gte", daystartTime}, {"$lte", dayendTime}}}, {"register.serverId", serverId}}
datas := gameinfo.ModelRegister.FindAllBson(bs)
var showsWithInfo1 []bson.M
if err := datas.All(context.TODO(), &showsWithInfo1); err != nil {
fmt.Println(err)
}
for _, v1 := range showsWithInfo1 {
s2 := v1["register"].(primitive.M)
list.Append(s2["uid"])
}
usersList := gconv.SliceAny(list)
for {
regdaystartTime := regStartTime.Local().TimestampMilli()
regdayendTime := regStartTime.Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
if regStartTime.DayOfYear() == gtime.Now().DayOfYear() {
break
}
bs1 := bson.D{{"creatday", tempDate}, {"day", days}}
info := gameinfo.ModelLiucun.FindOneBson_ServerId(bs1, serverId)
if info != 0 {
regStartTime = regStartTime.AddDate(0, 0, 1)
days++
continue
}
userLoginNum := gameinfo.ModeLogin.FindLogin1(regdaystartTime, regdayendTime, gconv.Ints(usersList), serverId)
//fmt.Println("login", days, userLoginNum)
logLiucun := new(gameinfo.LogLiucun)
logLiucun.Id = primitive.NewObjectID()
logLiucun.CreatDay = tempDate
logLiucun.NewUser = len(usersList)
logLiucun.DayCount = userLoginNum
logLiucun.Day = days
gameinfo.ModelLiucun.InsertOne_ServerId(logLiucun, serverId)
regStartTime2 := regStartTime.Clone().Format("Y-m-d")
payTimeS := regStartTime2 + " 00:00:00"
payTimeE := regStartTime2 + " 23:59:59"
payUserNum := gameinfo.ModelPay.FindPay(payTimeS, payTimeE, gconv.Ints(usersList), serverId)
logPayLiucun := new(gameinfo.LogPayLiucun)
logPayLiucun.Id = primitive.NewObjectID()
logPayLiucun.CreatDay = tempDate
logPayLiucun.NewUser = len(usersList)
logPayLiucun.DayCount = payUserNum
logPayLiucun.Day = days
gameinfo.ModelPayLiucun.InsertOne_ServerId(logPayLiucun, serverId)
regStartTime = regStartTime.AddDate(0, 0, 1)
days++
}
}
//结束条件
startTime = startTime.AddDate(0, 0, 1)
if startTime.DayOfYear() == gtime.Now().DayOfYear() {
break
}
continue
}
fmt.Println("LiucunByServer end")
}
// 新增注册人数
func Hour() {
// 获取当前系统时间 取值到小时 后面拼接00分00秒
newTime := time.Now().Format("2006-01-02 15")
newTimeStr := string(newTime + ":00:00")
fmt.Println("当前时间整点:" + newTimeStr)
// 时间格式化
timeLayout := "2006-01-02 15:04:05"
// 字符串转成time
t, _ := time.ParseInLocation(timeLayout, newTimeStr, time.Local)
// 1小时前
h, _ := time.ParseDuration("-1h")
h1 := t.Add(h)
fmt.Printf("查询时间范围 %d点 - %d点\n", h1.Hour(), t.Hour())
// 转成时间戳
hourStratTime := h1.UnixNano() / 1e6
hourEndTime := t.UnixNano() / 1e6
tempDate := gtime.NewFromTimeStamp(hourStratTime).Format("Y-m-d")
bs := bson.D{{"creatday", tempDate}, {"hour", h1.Hour()}}
info := gameinfo.ModelHour.FindOneBson(bs)
if info != 0 {
return
}
count := 0
result := gameinfo.ModelRegister.FindRegisterByServer(hourStratTime, hourEndTime)
var showsWithInfo []bson.M
if err := result.All(context.TODO(), &showsWithInfo); err != nil {
fmt.Println(err)
}
for _, v := range showsWithInfo {
count = count + gconv.Int(v["count"])
logHour := new(gameinfo.LogHour)
logHour.Id = primitive.NewObjectID()
logHour.CreatDay = tempDate
logHour.NewUser = gconv.Int(v["count"])
logHour.Hour = h1.Hour()
gameinfo.ModelHour.InsertOneByServer(logHour, gconv.String(v["_id"]))
fmt.Println("服务器", gconv.String(v["_id"]), " 新增注册人数", gconv.Int(v["count"]))
}
logHour := new(gameinfo.LogHour)
logHour.Id = primitive.NewObjectID()
logHour.CreatDay = tempDate
logHour.NewUser = count
logHour.Hour = h1.Hour()
gameinfo.ModelHour.InsertOne(logHour)
fmt.Println("新增总注册人数", count)
fmt.Println("Hour end")
}
// 新增登录人数
func HourLogin() {
// 获取当前系统时间 取值到小时 后面拼接00分00秒
newTime := time.Now().Format("2006-01-02 15")
newTimeStr := string(newTime + ":00:00")
// 时间格式化
timeLayout := "2006-01-02 15:04:05"
// 字符串转成time
t, _ := time.ParseInLocation(timeLayout, newTimeStr, time.Local)
// 1小时前
h, _ := time.ParseDuration("-1h")
h1 := t.Add(h)
// 转成时间戳
hourStratTime := h1.UnixNano() / 1e6
hourEndTime := t.UnixNano() / 1e6
// 根据开始时间取年月日
tempDate := gtime.NewFromTimeStamp(hourStratTime).Format("Y-m-d")
bs := bson.D{{"creatday", tempDate}, {"hour", h1.Hour()}}
info := gameinfo.ModelHourLogin.FindOneBson(bs)
if info != 0 {
return
}
//查询各服务器的登录人数
result := gameinfo.ModeLogin.FindLoginByServer(hourStratTime, hourEndTime)
var showsWithInfo []bson.M
if err := result.All(context.TODO(), &showsWithInfo); err != nil {
fmt.Println(err)
}
serverList := garray.New(false)
userList := garray.New(false)
//遍历数据到集合
for _, v := range showsWithInfo {
if !serverList.Contains(v["serverId"]) {
serverList.Append(v["serverId"])
}
if !userList.Contains(v["uid"]) {
userList.Append(v["uid"])
}
}
//遍历服务器集合
serverList.Iterator(func(k int, v interface{}) bool {
count := 0
for _, v1 := range showsWithInfo {
if v == v1["serverId"] {
count = count + 1
}
}
logHourLogin := new(gameinfo.LogHourLogin)
logHourLogin.Id = primitive.NewObjectID()
logHourLogin.CreatDay = tempDate
logHourLogin.NewUser = count
logHourLogin.Hour = h1.Hour()
gameinfo.ModelHourLogin.InsertOneByServer(logHourLogin, gconv.String(v))
fmt.Println("服务器", gconv.String(v), " 新增登录人数", count)
return true
})
logHourLogin := new(gameinfo.LogHourLogin)
logHourLogin.Id = primitive.NewObjectID()
logHourLogin.CreatDay = tempDate
logHourLogin.NewUser = userList.Len()
logHourLogin.Hour = h1.Hour()
gameinfo.ModelHourLogin.InsertOne(logHourLogin)
fmt.Println("新增总登录人数", userList.Len())
fmt.Println("HourLogin end")
}
// 统计充值信息
func StatisticalRecharge() {
nowTime := time.Now().Format("2006-01-02")
// 判断用户去重集合
userList := garray.New(false)
var newRechargeNum, newRechargeAmount, newRechargeCount, rechargeNum, rechargeAmount, rechargeCount int64
// 新注册的用户
daystartTime := gtime.NewFromStr(nowTime).Local().TimestampMilli()
dayendTime := gtime.NewFromStr(nowTime).Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
users := gameinfo.ModelRegister.FindNew(daystartTime, dayendTime)
// 查询当天支付数据
stime := nowTime + " 00:00:00"
etime := nowTime + " 23:59:59"
filter := bson.D{
{"creattime", bson.D{{"$gte", stime}, {"$lte", etime}}},
{"status", 1},
{"handlestatus", 1}}
payList := gameinfo.ModelPay.GetPayInfoTask(filter)
for payList.Next(context.TODO()) {
var pay gameinfo.Pay
err := payList.Decode(&pay)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
if !userList.Contains(pay.Uid) {
rechargeNum = rechargeNum + 1
}
rechargeCount = rechargeCount + 1
rechargeAmount = rechargeAmount + gconv.Int64(pay.Money)
//是新注册的用户
if users.Contains(pay.Uid) {
if !userList.Contains(pay.Uid) {
newRechargeNum = newRechargeNum + 1
}
newRechargeCount = newRechargeCount + 1
newRechargeAmount = newRechargeAmount + gconv.Int64(pay.Money)
}
userList.Append(pay.Uid)
}
// 统计充值
statisticalRecharge := gameinfo.StatisticalRecharge{}
// 查询是否已经有当天数据
one := gameinfo.ModelStatisticalRecharge.FindOne("creatday", nowTime)
var sr gameinfo.StatisticalRecharge
err := one.Decode(&sr)
if err != nil {
statisticalRecharge.Id = primitive.NewObjectID()
} else {
statisticalRecharge.Id = sr.Id
}
statisticalRecharge.CreatDay = nowTime
//统计新增充值(当日注册且付费的人数和金额、付费次数)
statisticalRecharge.NewRechargeNum = gconv.Int(newRechargeNum)
statisticalRecharge.NewRechargeAmount = gconv.Float64(newRechargeAmount)
statisticalRecharge.NewRechargeCount = gconv.Int(newRechargeCount)
//统计充值总数(人数和金额、付费次数)
statisticalRecharge.RechargeNum = gconv.Int(rechargeNum)
statisticalRecharge.RechargeAmount = gconv.Float64(rechargeAmount)
statisticalRecharge.RechargeCount = gconv.Int(rechargeCount)
gameinfo.ModelStatisticalRecharge.InsertAndUpdateStatisticalRecharge(statisticalRecharge.Id, statisticalRecharge, "")
fmt.Println("StatisticalRecharge end")
}
// 统计各服务器充值信息
func StatisticalRecharge_Server() {
nowTime := time.Now().Format("2006-01-02")
stime := nowTime + " 00:00:00"
etime := nowTime + " 23:59:59"
// 判断用户去重集合
userList := garray.New(false)
//获得今天的新增的玩家
daystartTime := gtime.NewFromStr(nowTime).Local().TimestampMilli()
dayendTime := gtime.NewFromStr(nowTime).Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
users := gameinfo.ModelRegister.FindNew(daystartTime, dayendTime)
// 取pay表符合条件的服务器id
filter := bson.D{
{"creattime", bson.D{{"$gte", stime}, {"$lte", etime}}},
{"status", 1},
{"handlestatus", 1}}
serverList := gameinfo.ModelPay.GetPayServerId(filter)
var showsWithInfo []bson.M
if err := serverList.All(context.TODO(), &showsWithInfo); err != nil {
fmt.Println(err)
}
// 遍历服务器id
for _, m := range showsWithInfo {
var newRechargeNum, newRechargeAmount, newRechargeCount, rechargeNum, rechargeAmount, rechargeCount int64
//查询支付数据
serverFilter := bson.D{
{"creattime", bson.D{{"$gte", stime}, {"$lte", etime}}},
{"status", gconv.Int32(1)},
{"region", gconv.Int32(m["region"])},
{"handlestatus", gconv.Int32(1)}}
payList := gameinfo.ModelPay.GetPayInfoTask(serverFilter)
for payList.Next(context.TODO()) {
var pay gameinfo.Pay
err := payList.Decode(&pay)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
if !userList.Contains(pay.Uid) {
rechargeNum = rechargeNum + 1
}
rechargeCount = rechargeCount + 1
rechargeAmount = rechargeAmount + gconv.Int64(pay.Money)
//是新注册的用户
if users.Contains(pay.Uid) {
if !userList.Contains(pay.Uid) {
newRechargeNum = newRechargeNum + 1
}
newRechargeCount = newRechargeCount + 1
newRechargeAmount = newRechargeAmount + gconv.Int64(pay.Money)
}
userList.Append(pay.Uid)
}
// 统计充值
statisticalRecharge := gameinfo.StatisticalRecharge{}
// 查询是否已经有当天数据
one := gameinfo.ModelStatisticalRecharge.FindOne_ServerId("creatday", nowTime, gconv.String(m["region"]))
var sr gameinfo.StatisticalRecharge
err := one.Decode(&sr)
if err != nil {
statisticalRecharge.Id = primitive.NewObjectID()
} else {
statisticalRecharge.Id = sr.Id
}
statisticalRecharge.CreatDay = nowTime
//统计新增充值(当日注册且付费的人数和金额、付费次数)
statisticalRecharge.NewRechargeNum = gconv.Int(newRechargeNum)
statisticalRecharge.NewRechargeAmount = gconv.Float64(newRechargeAmount)
statisticalRecharge.NewRechargeCount = gconv.Int(newRechargeCount)
//统计充值总数(人数和金额、付费次数)
statisticalRecharge.RechargeNum = gconv.Int(rechargeNum)
statisticalRecharge.RechargeAmount = gconv.Float64(rechargeAmount)
statisticalRecharge.RechargeCount = gconv.Int(rechargeCount)
gameinfo.ModelStatisticalRecharge.InsertAndUpdateStatisticalRecharge(statisticalRecharge.Id, statisticalRecharge, gconv.String(m["region"]))
}
fmt.Println("StatisticalRecharge_Server end")
}
// 统计计算LTV需要金额
func StatisticalLTV() {
date := g.Config().GetString("startday")
startTime := gtime.NewFromStr(date)
for {
daystartTime := startTime.Local().TimestampMilli()
dayendTime := startTime.Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
//获得今天的新增的角色
users := gameinfo.ModelRegister.FindNew(daystartTime, dayendTime)
usersList := gconv.SliceAny(users)
tempDate := startTime.Clone().Format("Y-m-d")
regStartTime := startTime.Clone()
days := 0
for {
if regStartTime.DayOfYear() == gtime.Now().DayOfYear() {
break
}
// 查询 已存在当天的第n天数据 跳过
bs := bson.D{{"creatday", tempDate}, {"day", days}}
info := gameinfo.ModelLtv.FindOneBson(bs)
if info != 0 {
regStartTime = regStartTime.AddDate(0, 0, 1)
days++
continue
}
tempDate2 := regStartTime.Clone().Format("Y-m-d")
stime2 := tempDate2 + " 00:00:00"
etime2 := tempDate2 + " 23:59:59"
totleAmount := gameinfo.ModelPay.GetLtvStatistical(stime2, etime2, gconv.Ints(usersList), "")
logLTV := new(gameinfo.LogLTV)
logLTV.Id = primitive.NewObjectID()
logLTV.CreatDay = tempDate
logLTV.NewUser = len(usersList)
logLTV.DayAmount = totleAmount
logLTV.Day = days
logLTV.LogTime = gtime.Now().TimestampMilli()
gameinfo.ModelLtv.InsertOne(logLTV)
regStartTime = regStartTime.AddDate(0, 0, 1)
days++
}
startTime = startTime.AddDate(0, 0, 1)
if startTime.DayOfYear() == gtime.Now().DayOfYear() {
break
}
}
fmt.Println("StatisticalLTV end")
}
// 统计计算各服务器LTV需要金额
func StatisticalLTV_Server() {
date := g.Config().GetString("startday")
startTime := gtime.NewFromStr(date)
for {
daystartTime := startTime.Local().TimestampMilli()
dayendTime := startTime.Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
//获得各服务器的新增角色数
users := gameinfo.ModelRegister.FindRegisterByServer(daystartTime, dayendTime)
var showsWithInfo []bson.M
if err := users.All(context.TODO(), &showsWithInfo); err != nil {
fmt.Println(err)
}
for _, v := range showsWithInfo {
tempDate := startTime.Clone().Format("Y-m-d")
regStartTime := startTime.Clone()
days := 0
list := garray.New(false)
serverId := gconv.String(v["_id"])
//count := gconv.String(v["count"])
filter := bson.D{
{"register.registerTime", bson.D{{"$gte", daystartTime}, {"$lte", dayendTime}}},
{"register.serverId", gconv.String(serverId)},
}
datas := gameinfo.ModelRegister.FindAllBson(filter)
var showsWithInfo1 []bson.M
if err := datas.All(context.TODO(), &showsWithInfo1); err != nil {
fmt.Println(err)
}
for _, v1 := range showsWithInfo1 {
s2 := v1["register"].(primitive.M)
list.Append(s2["uid"])
}
usersList := gconv.SliceAny(list)
for {
if regStartTime.DayOfYear() == gtime.Now().DayOfYear() {
break
}
// 查询 已存在当天的第n天数据 跳过
bs := bson.D{{"creatday", tempDate}, {"day", days}}
info := gameinfo.ModelLtv.FindOneBson_ServerId(bs, serverId)
if info != 0 {
regStartTime = regStartTime.AddDate(0, 0, 1)
days++
continue
}
tempDate2 := regStartTime.Clone().Format("Y-m-d")
stime2 := tempDate2 + " 00:00:00"
etime2 := tempDate2 + " 23:59:59"
totleAmount := gameinfo.ModelPay.GetLtvStatistical(stime2, etime2, gconv.Ints(usersList), serverId)
logLTV := new(gameinfo.LogLTV)
logLTV.Id = primitive.NewObjectID()
logLTV.CreatDay = tempDate
logLTV.NewUser = len(usersList)
logLTV.DayAmount = totleAmount
logLTV.Day = days
logLTV.LogTime = gtime.Now().TimestampMilli()
gameinfo.ModelLtv.InsertOne_ServerId(logLTV, serverId)
regStartTime = regStartTime.AddDate(0, 0, 1)
days++
}
}
//fmt.Println(startTime)
startTime = startTime.AddDate(0, 0, 1)
if startTime.DayOfYear() == gtime.Now().DayOfYear() {
break
}
}
fmt.Println("StatisticalLTV_Server end")
}
// 订单信息表: log_charge_{日期}_{区服号}_{游戏名}.txt (按天级别)
func logCharge() {
// 读取配置表
itemConfig := codeutil.GetItemConfig()
rechargeCommodityConfig := codeutil.GetRechargeCommodityConfig()
// 取游戏名称
gameName := g.Config().GetString("game.name")
// 格式化时间
nowDate := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
stime := nowDate + " 00:00:00"
etime := nowDate + " 23:59:59"
// 遍历需要导出表格的服务器
servers := gameinfo.ModelServer.FindAllBson(bson.D{{"exportdata", "1"}})
for servers.Next(context.TODO()) {
var serverInfo gameinfo.ServerInfo
err := servers.Decode(&serverInfo)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
// 导出文件名称
fileName := "log_charge_" + nowDate + "_" + gconv.String(serverInfo.ServerId) + "_" + gameName + ".txt"
file, err := gfile.Create(fileName)
if err != nil {
fmt.Println("open file is failed, err: ", err.Error())
}
w := csv.NewWriter(file)
// 查询订单信息
filter := bson.D{
{"creattime", bson.D{{"$gte", stime}, {"$lte", etime}}},
{"status", gconv.Int32(1)},
{"region", gconv.Int32(serverInfo.ServerId)},
{"handlestatus", gconv.Int32(1)}}
payList := gameinfo.ModelPay.GetPayInfoTask(filter)
for payList.Next(context.TODO()) {
var pay gameinfo.Pay
err := payList.Decode(&pay)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
// 拼接内容
bugType := ""
if gconv.Int(pay.PayItem) >= 1 && gconv.Int(pay.PayItem) <= 6 {
2023-03-20 13:38:28 +08:00
bugType = "1"
} else {
bugType = "2"
}
// 从缓存中取对应value
baseRewards, _ := rechargeCommodityConfig.Get(gconv.String(pay.PayItem))
baseRewards = gconv.Map(baseRewards)["BaseReward"]
// 如果有多条奖励
if strings.Contains(gconv.String(baseRewards), "|") {
split := strings.Split(gconv.String(baseRewards), "|")
for _, baseReward := range split {
//fmt.Println(baseReward)
goodsId := strings.Split(baseReward, "#")[0]
goodsName, _ := itemConfig.Get(gconv.String(goodsId))
// 还差付款渠道
data := gconv.String(pay.Billno) + "\t" + gconv.String(pay.OpenId) + "\t" + gconv.String(goodsId) + "\t" + gconv.String(goodsName) + "\t" + bugType + "\t" + gconv.String(pay.Creattime) + "\t" +
"" + gconv.String(serverInfo.Timezone) + "\t" + gconv.String(serverInfo.Currency) + "\t" + gconv.String(pay.PayWay) + "\t" + gconv.String(pay.Money)
2023-03-20 13:38:28 +08:00
w.Write([]string{data})
// 刷新缓冲
w.Flush()
}
} else {
// 只有一条奖励
if strings.Contains(gconv.String(baseRewards), "#") {
goodsId := strings.Split(gconv.String(baseRewards), "#")[0]
goodsName, _ := itemConfig.Get(gconv.String(goodsId))
// 还差付款渠道
data := gconv.String(pay.Billno) + "\t" + gconv.String(pay.OpenId) + "\t" + gconv.String(goodsId) + "\t" + gconv.String(goodsName) + "\t" + bugType + "\t" + gconv.String(pay.Creattime) + "\t" +
"" + gconv.String(serverInfo.Timezone) + "\t" + gconv.String(serverInfo.Currency) + "\t" + gconv.String(pay.PayWay) + "\t" + gconv.String(pay.Money)
2023-03-20 13:38:28 +08:00
w.Write([]string{data})
// 刷新缓冲
w.Flush()
}
}
}
// 关闭
itemConfig.Close()
rechargeCommodityConfig.Close()
file.Close()
//删除生成的文件
//os.Remove("./" + fileName)
fmt.Println("logCharge end")
}
}
// 道具消耗表: log_throw_{日期}_{区服号}_{游戏名}.txt (按天级别)
func logThrow() {
// 取配置中游戏名称
gameName := g.Config().GetString("game.name")
// 格式化时间
nowDate := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
daystartTime := gtime.NewFromStr(nowDate).Local().TimestampMilli()
dayendTime := gtime.NewFromStr(nowDate).Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
// 遍历需要导出表格的服务器
servers := gameinfo.ModelServer.FindAllBson(bson.D{{"exportdata", "1"}})
for servers.Next(context.TODO()) {
openIdMap := gmap.New()
var serverInfo gameinfo.ServerInfo
err := servers.Decode(&serverInfo)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
// 导出文件名称
fileName := "log_throw_" + nowDate + "_" + gconv.String(serverInfo.ServerId) + "_" + gameName + ".txt"
file, err := gfile.Create(fileName)
if err != nil {
fmt.Println("open file is failed, err: ", err.Error())
}
w := csv.NewWriter(file)
// 查询时间范围内的所有道具消耗信息
itemlogs := gameinfo.ModelItemlog.FindUser(bson.D{{"time", bson.D{{"$gte", gconv.String(daystartTime)}, {"$lte", gconv.String(dayendTime)}}}, {"type", gconv.Int32(1)}}, serverInfo.ServerId)
for itemlogs.Next(context.TODO()) {
var itemLog gameinfo.ItemLog
err := itemlogs.Decode(&itemLog)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
// 处理文本内容
openId := ""
// 根据uid查询对应openid 没有就去查然后放到map 下次用判断map中有没有对应uid
if openIdMap.Contains(itemLog.Uid) {
openId = gconv.String(openIdMap.Get(itemLog.Uid))
} else {
users := gameinfo.ModelUserInfo.FindAllBson(bson.D{{"serverId", gconv.String(serverInfo.ServerId)}, {"uid", gconv.Int32(itemLog.Uid)}})
for users.Next(context.TODO()) {
var userInfo gameinfo.UserInfo
err := users.Decode(&userInfo)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
openId = userInfo.OpenId
}
openIdMap.Set(itemLog.Uid, openId)
}
data := openId + "\t" + gconv.String(itemLog.ItemId) + "\t" + gconv.String(gtime.NewFromTimeStamp(gconv.Int64(itemLog.Time)).Format("Y-m-d H:i:s")) + "\t" + gconv.String(serverInfo.Timezone) + "\t" + gconv.String(itemLog.ItemNum)
2023-03-20 13:38:28 +08:00
w.Write([]string{data})
// 刷新缓冲
w.Flush()
}
}
fmt.Println("logThrow end")
}
// 道具发放表:log_goods_{日期_{区服号}_{游戏名}.txt (按天级别)
func logGoods() {
rechargeCommodityConfig := codeutil.GetRechargeCommodityConfig()
// 去配置中游戏名称
gameName := g.Config().GetString("game.name")
// 遍历需要导出表格的服务器
nowDate := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
stime := nowDate + " 00:00:00"
etime := nowDate + " 23:59:59"
daystartTime := gtime.NewFromStr(nowDate).Local().TimestampMilli()
dayendTime := gtime.NewFromStr(nowDate).Clone().AddDate(0, 0, 1).Local().TimestampMilli() - 1
// 遍历需要导出表格的服务器
servers := gameinfo.ModelServer.FindAllBson(bson.D{{"exportdata", "1"}})
for servers.Next(context.TODO()) {
openIdMap := gmap.New()
var serverInfo gameinfo.ServerInfo
err := servers.Decode(&serverInfo)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
// 导出文件名称
fileName := "log_goods_" + nowDate + "_" + gconv.String(serverInfo.ServerId) + "_" + gameName + ".txt"
file, err := gfile.Create(fileName)
if err != nil {
fmt.Println("open file is failed, err: ", err.Error())
}
w := csv.NewWriter(file)
// 查询支付订单信息
// 查询订单信息
filter := bson.D{
{"creattime", bson.D{{"$gte", stime}, {"$lte", etime}}},
{"status", gconv.Int32(1)},
{"region", gconv.Int32(serverInfo.ServerId)},
{"handlestatus", gconv.Int32(1)}}
payList := gameinfo.ModelPay.GetPayInfoTask(filter)
for payList.Next(context.TODO()) {
var pay gameinfo.Pay
err := payList.Decode(&pay)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
// 从缓存中取对应value
rechargeCommodity, _ := rechargeCommodityConfig.Get(gconv.String(pay.PayItem))
baseRewards := gconv.Map(rechargeCommodity)["BaseReward"]
// 如果有多条奖励
if strings.Contains(gconv.String(baseRewards), "|") {
split := strings.Split(gconv.String(baseRewards), "|")
priceWeights := strings.Split(gconv.String(gconv.Map(rechargeCommodity)["PriceWeight"]), "#")
var totalWeight float64
for _, priceWeight := range priceWeights {
totalWeight += gconv.Float64(priceWeight)
}
if len(split) == len(priceWeights) {
for i := 0; i < len(split); i++ {
percentage, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", gconv.Float64(priceWeights[i])/float64(totalWeight)), 64)
gainPrice, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", percentage*gconv.Float64(gconv.Map(rechargeCommodity)["Price"])), 64)
2023-03-20 13:38:28 +08:00
coinName := ""
data := pay.OpenId + "\t" + strings.Split(split[i], "#")[0] + "\t" + pay.Creattime + "\t" +
gconv.String(serverInfo.Timezone) + "\t" + gconv.String(strings.Split(split[i], "#")[1]) + "\t0" + "\t" + gconv.String(gainPrice) + "\t" + coinName + "\t0"
2023-03-20 13:38:28 +08:00
w.Write([]string{data})
w.Flush()
}
}
} else {
// 只有一条奖励
if strings.Contains(gconv.String(baseRewards), "#") {
coinName := ""
data := pay.OpenId + "\t" + strings.Split(gconv.String(baseRewards), "#")[0] + "\t" + pay.Creattime + "\t" +
gconv.String(serverInfo.Timezone) + "\t" + gconv.String(strings.Split(gconv.String(baseRewards), "#")[1]) + "\t0" + "\t" + gconv.String(gconv.Float64(gconv.Map(rechargeCommodity)["Price"])) + "\t" + coinName + "\t0"
2023-03-20 13:38:28 +08:00
w.Write([]string{data})
w.Flush()
}
}
}
// 查询日期范围内所有物品获取信息
itemlogs := gameinfo.ModelItemlog.FindUser(bson.D{{"time", bson.D{{"$gte", gconv.String(daystartTime)}, {"$lte", gconv.String(dayendTime)}}}, {"type", gconv.Int32(0)}}, serverInfo.ServerId)
for itemlogs.Next(context.TODO()) {
var itemLog gameinfo.ItemLog
err := itemlogs.Decode(&itemLog)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
openId := ""
if openIdMap.Contains(itemLog.Uid) {
openId = gconv.String(openIdMap.Get(itemLog.Uid))
} else {
users := gameinfo.ModelUserInfo.FindAllBson(bson.D{{"serverId", gconv.String(serverInfo.ServerId)}, {"uid", gconv.Int32(itemLog.Uid)}})
for users.Next(context.TODO()) {
var userInfo gameinfo.UserInfo
err := users.Decode(&userInfo)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
openId = userInfo.OpenId
}
openIdMap.Set(itemLog.Uid, openId)
}
//itemLog.Reason
// gainSource 0表示现金购买获取,1表示游戏币购买获取, 2表示通过活动等免费获取,3表示道具合成获取,4表示道具分解获取
gainSource := "gainSource"
// gain_price, — 获取道具时的价格,付费购买的道具标注用户实际支付的价格,免费获取道具价格统一为0,合成/分解获取的道具价格统一0.针对礼包涉及到多个道具的情况,拆成单独的道具,
// 价格为单个道具的商城价格*礼包整体折扣率.如果涉及到无价格的道具,需要cp估算出来一个价格
gainPrice := "gainPrice"
// coin_name 传游戏币名称,如果非游戏币购买获得,则传空
coinName := "coinName"
// coin_cost 游戏币消耗数量,如果非游戏币购买获得,则传0
coinCost := "coinCost"
data := openId + "\t" + gconv.String(itemLog.ItemId) + "\t" + gconv.String(gtime.NewFromTimeStamp(gconv.Int64(itemLog.Time)).Format("Y-m-d H:i:s")) + "\t" +
gconv.String(serverInfo.Timezone) + "\t" + gconv.String(itemLog.ItemNum) + "\t" + gainSource + "\t" + gainPrice + "\t" + coinName + "\t" + coinCost
2023-03-20 13:38:28 +08:00
w.Write([]string{data})
// 刷新缓冲
w.Flush()
}
}
rechargeCommodityConfig.Close()
fmt.Println("logGoods end")
}
//道具信息表:goods_info_{游戏名称}.txt (按照月度级别)
func goodsInfo() {
gameName := g.Config().GetString("game.name")
// 导出文件名称
fileName := "goods_info_" + gameName + ".txt"
fmt.Println(fileName)
fmt.Println("goodsInfo end")
}
// 用户账号与角色对应关系表: log_role_{游戏名}.txt (按照月度级别)
func logRole() {
// 取配置中的游戏名
gameName := g.Config().GetString("game.name")
// 导出文件名称
fileName := "log_role_" + gameName + ".txt"
file, err := gfile.Create(fileName)
if err != nil {
fmt.Println("open file is failed, err: ", err.Error())
}
w := csv.NewWriter(file)
// 所有用户信息
allUser := gameinfo.ModelUserInfo.FindAllBson(bson.D{})
for allUser.Next(context.TODO()) {
var userInfo gameinfo.UserInfo
err := allUser.Decode(&userInfo)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
// 在充值信息中查询有无充值记录
sort := bson.D{{"creattime", -1}}
filter := bson.D{{"status", gconv.Int32(1)}, {"handlestatus", gconv.Int32(1)}, {"openid", userInfo.OpenId}}
userPay := gameinfo.ModelPay.GetSortList(filter, sort)
list := g.ListAnyAny{}
for userPay.Next(context.TODO()) {
dayMap := gmap.New()
var pay gameinfo.Pay
err := userPay.Decode(&pay)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
dayMap.Set("creattime", pay.Creattime)
list = append(g.ListAnyAny{gconv.Map(dayMap)}, list...)
}
// 0:免费用户,1:付费用户
accountType := "0"
firstPayDay := ""
lastPayDay := ""
// 如果是付费用户
if len(list) > 0 {
// 排序后 取第一条和最后一条分别为首充时间和最后一次充值时间
firstPayDay = gconv.String(gconv.Map(list[0])["creattime"])
lastPayDay = gconv.String(gconv.Map(list[len(list)-1])["creattime"])
2023-03-20 13:38:28 +08:00
accountType = "1"
}
data := gconv.String(userInfo.OpenId) + "\t" + gconv.String(userInfo.OpenId) + "\t" + firstPayDay + "\t" + lastPayDay + "\t" + accountType
2023-03-20 13:38:28 +08:00
w.Write([]string{data})
// 刷新缓冲
w.Flush()
}
fmt.Println("logRole end")
}
func initServerPayInfo() {
bs := bson.D{}
serverInfoList := gameinfo.ModelServer.FindServerInfoList(bs)
for serverInfoList.Next(context.TODO()) {
var serverInfo gameinfo.ServerInfo
err := serverInfoList.Decode(&serverInfo)
if err != nil {
//log.Fatal(err)
log.Println(err)
continue
}
statisticalRecharge := gameinfo.StatisticalRecharge{}
tomorrow := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
statisticalRecharge.CreatDay = tomorrow
statisticalRecharge.Id = primitive.NewObjectID()
//统计新增充值(当日注册且付费的人数和金额、付费次数)
statisticalRecharge.NewRechargeNum = gconv.Int(0)
statisticalRecharge.NewRechargeAmount = gconv.Float64(0)
statisticalRecharge.NewRechargeCount = gconv.Int(0)
//统计充值总数(人数和金额、付费次数)
statisticalRecharge.RechargeNum = gconv.Int(0)
statisticalRecharge.RechargeAmount = gconv.Float64(0)
statisticalRecharge.RechargeCount = gconv.Int(0)
gameinfo.ModelStatisticalRecharge.InsertAndUpdateStatisticalRecharge(statisticalRecharge.Id, statisticalRecharge, serverInfo.ServerId)
}
}