orderquery.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package events
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "strings"
  9. "time"
  10. "box-gm/utils"
  11. "github.com/astaxie/beego"
  12. "github.com/tealeg/xlsx"
  13. // "log"
  14. )
  15. type RecordOrder struct {
  16. EventTime string
  17. Uid string
  18. Name string
  19. OrderId string
  20. GoodsId int
  21. Price int
  22. BuyCount int
  23. }
  24. func OrderQuery_get_records(name, uid string, startTime, endTime int) ([]*RecordOrder, int, int) {
  25. // 日期列表
  26. var dateList []string
  27. times := 0
  28. cur_charge := 0
  29. tol_charge := 0
  30. for {
  31. ti := startTime + times*86400
  32. date := time.Unix(int64(ti), 0).Format("2006-01-02")
  33. dateList = append(dateList, date)
  34. times++
  35. if ti >= endTime {
  36. break
  37. }
  38. }
  39. // log.Printf("OrderQuery_get_records dateList[%v]", dateList)
  40. var records []*RecordOrder
  41. loc, _ := time.LoadLocation("Local")
  42. for _, date := range dateList {
  43. eventFile := fmt.Sprintf("%s/pay-%s.log", utils.GetKeyConf("events", "path"), date)
  44. f, err := os.Open(eventFile)
  45. if err != nil {
  46. log.Printf("eventFile[%s] err[%v]", eventFile, err)
  47. continue
  48. }
  49. defer f.Close()
  50. buff := bufio.NewReader(f)
  51. for {
  52. line, err := buff.ReadString('\n')
  53. if err != nil || io.EOF == err {
  54. break
  55. }
  56. line = strings.TrimSpace(line)
  57. arr := strings.Split(line, ";")
  58. uidEvent := arr[2]
  59. if uid == "" || uid == uidEvent {
  60. theTime, errTime := time.ParseInLocation("2006-01-02 15:04:05", arr[0], loc)
  61. if errTime == nil {
  62. ti := int(theTime.Unix())
  63. arrCnt := strings.Split(arr[3], ",")
  64. if len(arrCnt) > 4 && ti >= startTime && ti <= endTime {
  65. nickname := arrCnt[0]
  66. orderId := arrCnt[1]
  67. // if orderId != "GM" {
  68. // //"%s|%s|%s|%s", sServerKey, pid, iNowTime, iRanNumber
  69. // }
  70. goodsId := utils.StringToInt(arrCnt[2])
  71. // isFirstCharge := arrCnt[3]
  72. price := utils.StringToInt(arrCnt[4])
  73. if name == "" || nickname == name {
  74. tol_charge += price
  75. if utils.GetDiffDays(ti, int(time.Now().Unix())) == 0 {
  76. cur_charge += price
  77. }
  78. item := &RecordOrder{}
  79. item.EventTime = arr[0]
  80. item.Uid = uidEvent
  81. item.Name = nickname
  82. item.OrderId = orderId
  83. item.GoodsId = goodsId
  84. item.Price = price
  85. item.BuyCount = 1
  86. records = append(records, item)
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. // log.Printf("OrderQuery_get_records records[%v]", records)
  94. return records, cur_charge, tol_charge
  95. }
  96. // 保存文件
  97. func Save_orderquery_records(records []*RecordOrder, user string) string {
  98. var file *xlsx.File
  99. var sheet *xlsx.Sheet
  100. var row *xlsx.Row
  101. var err error
  102. file = xlsx.NewFile()
  103. sheet, err = file.AddSheet("订单数据")
  104. if err != nil {
  105. beego.Warn(err)
  106. return ""
  107. }
  108. row = sheet.AddRow()
  109. row.AddCell().Value = "时间"
  110. row.AddCell().Value = "玩家ID"
  111. row.AddCell().Value = "订单ID"
  112. row.AddCell().Value = "商品ID"
  113. row.AddCell().Value = "价格"
  114. row.AddCell().Value = "购买数量"
  115. for i := 0; i < len(records); i++ {
  116. item := records[i]
  117. row = sheet.AddRow()
  118. row.AddCell().SetString(item.EventTime)
  119. row.AddCell().SetString(item.Uid)
  120. row.AddCell().SetString(item.OrderId)
  121. row.AddCell().SetInt(item.GoodsId)
  122. row.AddCell().SetInt(item.Price)
  123. row.AddCell().SetInt(item.BuyCount)
  124. }
  125. name := fmt.Sprintf("./static/统计-订单查询-%s.xlsx", user)
  126. os.Remove(name)
  127. err = file.Save(name)
  128. if err != nil {
  129. beego.Warn(err)
  130. return ""
  131. }
  132. return name
  133. }