paynum.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package events
  2. import (
  3. "bufio"
  4. "fmt"
  5. "box-gm/utils"
  6. "io"
  7. "math"
  8. "os"
  9. "strings"
  10. "time"
  11. "github.com/astaxie/beego"
  12. "github.com/tealeg/xlsx"
  13. )
  14. func PayNum_get_day_time_count(date string, minutes int, serverId string) map[int]int {
  15. var timeCount = make(map[int]int)
  16. var payMark = make(map[int]map[int]int)
  17. f, err := os.Open(fmt.Sprintf("%s/pay-%s.log", utils.GetKeyConf("events", "path"), date))
  18. if err != nil {
  19. return timeCount
  20. }
  21. defer f.Close()
  22. startTime := utils.GetTime(date + " 00:00:00")
  23. buff := bufio.NewReader(f)
  24. for {
  25. line, err := buff.ReadString('\n')
  26. if err != nil || io.EOF == err {
  27. break
  28. }
  29. line = strings.TrimSpace(line)
  30. arr := strings.Split(line, ";")
  31. if serverId == "0" || arr[1] == serverId {
  32. eventTime := utils.GetTime(arr[0])
  33. sec := eventTime - startTime
  34. sec = int(math.Floor(float64(sec) / float64(minutes*60)))
  35. pid := utils.StringToInt(arr[2])
  36. params := strings.Split(arr[3], ",")
  37. price := 0
  38. if len(params) > 4 {
  39. price = utils.StringToInt(params[4])
  40. }
  41. if price > 0 {
  42. if payMark[sec] == nil {
  43. payMark[sec] = make(map[int]int)
  44. payMark[sec][pid] = 1
  45. } else {
  46. payMark[sec][pid] += 1
  47. }
  48. timeCount[sec] = len(payMark[sec])
  49. }
  50. }
  51. }
  52. return timeCount
  53. }
  54. func PayNum_get_range_day_time_count(days, minutes int, serverId string) ([]*tgSeries, []string) {
  55. var series []*tgSeries
  56. currTime := int(time.Now().Unix())
  57. dayCount := 0
  58. t := 24 * 60 / minutes
  59. for {
  60. tiEvent := currTime - 86400*dayCount
  61. date := time.Unix(int64(tiEvent), 0).Format("2006-01-02")
  62. // m, _ := time.ParseDuration("1m")
  63. item := &tgSeries{}
  64. item.Name = date
  65. glCount := PayNum_get_day_time_count(date, minutes, serverId)
  66. for i := 0; i < t; i++ {
  67. item.Data = append(item.Data, glCount[i])
  68. }
  69. item.MapCount = glCount
  70. // log.Printf("item[%v]", item)
  71. series = append(series, item)
  72. dayCount++
  73. if dayCount > days {
  74. break
  75. }
  76. }
  77. var arrCate []string
  78. loc, _ := time.LoadLocation("Local")
  79. theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02")+" 00:00:00", loc)
  80. for i := 0; i < t; i++ {
  81. ti := theTime.Add(time.Minute * time.Duration(minutes) * time.Duration(i))
  82. arrCate = append(arrCate, fmt.Sprintf("%02d:%02d:%02d", ti.Hour(), ti.Minute(), ti.Second()))
  83. }
  84. return series, arrCate
  85. }
  86. // 保存文件
  87. func Save_paynum_records(series []*tgSeries, minutes int, user string) string {
  88. var file *xlsx.File
  89. var sheet *xlsx.Sheet
  90. var row *xlsx.Row
  91. var err error
  92. file = xlsx.NewFile()
  93. date := time.Now().Format("2006-01-02")
  94. sheet, err = file.AddSheet("付费玩家人数统计")
  95. if err != nil {
  96. beego.Warn(err)
  97. return ""
  98. }
  99. row = sheet.AddRow()
  100. row.AddCell().Value = "日期"
  101. row.AddCell().Value = "时间"
  102. row.AddCell().Value = "人数"
  103. loc, _ := time.LoadLocation("Local")
  104. theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", date+" 00:00:00", loc)
  105. for i := 0; i < len(series); i++ {
  106. item := series[i]
  107. for k, v := range item.MapCount {
  108. row = sheet.AddRow()
  109. row.AddCell().SetString(item.Name)
  110. ti := theTime.Add(time.Minute * time.Duration(minutes) * time.Duration(k))
  111. row.AddCell().SetString(fmt.Sprintf("%02d:%02d:%02d", ti.Hour(), ti.Minute(), ti.Second()))
  112. row.AddCell().SetInt(v)
  113. }
  114. }
  115. name := fmt.Sprintf("./static/统计-付费玩家人数-%s.xlsx", user)
  116. os.Remove(name)
  117. err = file.Save(name)
  118. if err != nil {
  119. beego.Warn(err)
  120. return ""
  121. }
  122. return name
  123. }