| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945 |
- 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 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) {
- 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)
- 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)
- 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)
- 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)
- 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"
- 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"
- 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
- 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"])
- accountType = "1"
- }
- data := gconv.String(userInfo.OpenId)+"\t"+gconv.String(userInfo.OpenId)+"\t"+firstPayDay+"\t"+lastPayDay+"\t"+accountType
- w.Write([]string{data})
- // 刷新缓冲
- w.Flush()
- }
- fmt.Println("logRole end")
- }
|