chatquery.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 RecordChat struct {
  16. EventTime string
  17. Uid string
  18. Name string
  19. ToUid string
  20. MsgType string
  21. ContentType string
  22. Content string
  23. }
  24. func ChatQuery_get_records(name, uid, ty_chat string, startTime, endTime int) []*RecordChat {
  25. // 日期列表
  26. var dateList []string
  27. times := 0
  28. for {
  29. ti := startTime + times*86400
  30. date := time.Unix(int64(ti), 0).Format("2006-01-02")
  31. dateList = append(dateList, date)
  32. times++
  33. if ti >= endTime {
  34. break
  35. }
  36. }
  37. // log.Printf("ChatQuery_get_records dateList[%v]", dateList)
  38. t := func(ty string) string {
  39. switch ty {
  40. case "1":
  41. return "PRIVATE"
  42. case "2":
  43. return "WORLD"
  44. case "3":
  45. return "CLUB"
  46. }
  47. return ""
  48. }
  49. var records []*RecordChat
  50. loc, _ := time.LoadLocation("Local")
  51. for _, date := range dateList {
  52. eventFile := fmt.Sprintf("%s/chat-%s.log", utils.GetKeyConf("events", "path"), date)
  53. f, err := os.Open(eventFile)
  54. if err != nil {
  55. log.Printf("eventFile[%s] err[%v]", eventFile, err)
  56. continue
  57. }
  58. defer f.Close()
  59. buff := bufio.NewReader(f)
  60. for {
  61. line, err := buff.ReadString('\n')
  62. if err != nil || io.EOF == err {
  63. break
  64. }
  65. line = strings.TrimSpace(line)
  66. /* string.format(
  67. "%s;%s;%s;%s;%s;%s;%s",
  68. get_time_string(),
  69. get_server_id(),
  70. _tostring(uid),
  71. _tostring(toUid),
  72. _tostring(msgty),
  73. _tostring(iCType),
  74. _tostring(cnt)*/
  75. arr := strings.Split(line, ";")
  76. uidEvent := arr[2]
  77. if uid == "" || uid == uidEvent {
  78. theTime, errTime := time.ParseInLocation("2006-01-02 15:04:05", arr[0], loc)
  79. if errTime == nil {
  80. ti := int(theTime.Unix())
  81. if len(arr) > 6 && ti >= startTime && ti <= endTime {
  82. params := strings.Split(arr[6], ",")
  83. nickname := ""
  84. content := ""
  85. ty := arr[4]
  86. if len(params) > 1 {
  87. nickname = params[0]
  88. content = params[1]
  89. } else {
  90. content = arr[6]
  91. }
  92. if name == "" || nickname == name {
  93. if ty_chat == "0" || ty_chat == ty {
  94. item := &RecordChat{}
  95. item.EventTime = arr[0]
  96. item.Uid = arr[2]
  97. item.Name = nickname
  98. item.ToUid = arr[3]
  99. item.MsgType = t(ty)
  100. item.ContentType = arr[5]
  101. item.Content = content
  102. records = append(records, item)
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. // log.Printf("ChatQuery_get_records records[%v]", records)
  111. return records
  112. }
  113. // 保存文件
  114. func Save_chatquery_records(records []*RecordChat, user string) string {
  115. var file *xlsx.File
  116. var sheet *xlsx.Sheet
  117. var row *xlsx.Row
  118. var err error
  119. file = xlsx.NewFile()
  120. sheet, err = file.AddSheet("聊天记录")
  121. if err != nil {
  122. beego.Warn(err)
  123. return ""
  124. }
  125. row = sheet.AddRow()
  126. row.AddCell().Value = "时间"
  127. row.AddCell().Value = "发送方ID"
  128. row.AddCell().Value = "发送方昵称"
  129. row.AddCell().Value = "接收方ID"
  130. row.AddCell().Value = "消息类型"
  131. row.AddCell().Value = "内容类型"
  132. row.AddCell().Value = "发送内容"
  133. for i := 0; i < len(records); i++ {
  134. item := records[i]
  135. row = sheet.AddRow()
  136. row.AddCell().SetString(item.EventTime)
  137. row.AddCell().SetString(item.Uid)
  138. row.AddCell().SetString(item.Name)
  139. row.AddCell().SetString(item.ToUid)
  140. row.AddCell().SetString(item.MsgType)
  141. row.AddCell().SetString(item.ContentType)
  142. row.AddCell().SetString(item.Content)
  143. }
  144. name := fmt.Sprintf("./static/统计-聊天查询-%s.xlsx", user)
  145. os.Remove(name)
  146. err = file.Save(name)
  147. if err != nil {
  148. beego.Warn(err)
  149. return ""
  150. }
  151. return name
  152. }