log_list.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package log
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go.uber.org/zap"
  7. "log-server/global"
  8. "log-server/model/log"
  9. "log-server/model/log/request"
  10. "log-server/model/log/response"
  11. loging2 "log-server/service/log/loging"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. type ServiceLogList struct {
  17. ServiceLogCoding
  18. logical loging2.LogicalLog
  19. }
  20. func (s *ServiceLogList) GetCodeLogList(ctx context.Context, api log.Loging, info request.PageInfo, order string, desc bool, code int) (list interface{}, total int64, err error) {
  21. limit := info.PageSize
  22. offset := info.PageSize * (info.Page - 1)
  23. if api.GameId == 0 {
  24. return
  25. }
  26. var apiList []log.Loging
  27. var createDate string
  28. if api.CreateDate == "" {
  29. createDate = time.Now().Format("2006-01-02")
  30. } else {
  31. createDate = api.CreateDate
  32. }
  33. splitTime := strings.Split(createDate, "-")
  34. date := splitTime[0] + "-" + splitTime[1]
  35. db := global.GVA_DB.Model(&log.Loging{})
  36. if date != "2022-11" {
  37. table := "loging_" + splitTime[0] + "-" + splitTime[1]
  38. fmt.Println(table)
  39. db = global.GVA_DB.Table(table)
  40. }
  41. db = db.Where("create_date = ?", createDate)
  42. db = db.Where("game_id = ?", api.GameId)
  43. if api.Coding != 0 {
  44. db = db.Where("coding = ?", api.Coding)
  45. }
  46. if api.PcCode != "" {
  47. db = db.Where("pc_code = ?", api.PcCode)
  48. }
  49. if api.Account != "" {
  50. db = db.Where("account = ?", api.Account)
  51. }
  52. if api.LogUuid != "" {
  53. db = db.Where("log_uuid = ?", api.LogUuid)
  54. }
  55. if code != 0 {
  56. db = db.Where("type_coding = ?", code)
  57. }
  58. if api.Status != 0 {
  59. if api.Status == 2 {
  60. db = db.Where("status = ?", 0)
  61. } else {
  62. db = db.Where("status = ?", 1)
  63. }
  64. }
  65. err = db.Count(&total).Error
  66. if err != nil {
  67. return apiList, total, err
  68. } else {
  69. db = db.Limit(limit).Offset(offset)
  70. if order != "" {
  71. var OrderStr string
  72. // 设置有效排序key 防止sql注入
  73. // 感谢 Tom4t0 提交漏洞信息
  74. orderMap := make(map[string]bool, 4)
  75. orderMap["id"] = true
  76. orderMap["log_uuid"] = true
  77. orderMap["account"] = true
  78. orderMap["game_id"] = true
  79. if orderMap[order] {
  80. if desc {
  81. OrderStr = order + " desc"
  82. } else {
  83. OrderStr = order
  84. }
  85. } else { // didn't matched any order key in `orderMap`
  86. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  87. return apiList, total, err
  88. }
  89. err = db.Order(OrderStr).Find(&apiList).Error
  90. } else {
  91. err = db.Order("id").Find(&apiList).Error
  92. }
  93. }
  94. fmt.Println(time.Now().Unix())
  95. var apisReply []*response.GetLogReply
  96. if total != 0 {
  97. for _, apiInfo := range apiList {
  98. apiReply := new(response.GetLogReply)
  99. apiReply.Id = apiInfo.Id
  100. apiReply.Coding = strconv.Itoa(apiInfo.Coding)
  101. apiReply.GameId = apiInfo.GameId
  102. apiReply.Status = apiInfo.Status
  103. apiReply.Account = apiInfo.Account
  104. apiReply.DeviceId = apiInfo.DeviceId
  105. apiReply.ComputerType = apiInfo.ComputerType
  106. apiReply.EnvCode = apiInfo.EnvCode
  107. apiReply.LogUuid = apiInfo.LogUuid
  108. apiReply.Operator = apiInfo.Operator
  109. apiReply.PcCode = apiInfo.PcCode
  110. apiReply.PcIp = apiInfo.PcIp
  111. apiReply.SimulatorIp = apiInfo.SimulatorIp
  112. apiReply.SimulatorMac = apiInfo.SimulatorMac
  113. apiReply.PcMac = apiInfo.PcMac
  114. apiReply.AccountType = apiInfo.AccountType
  115. apiReply.CreateDate = apiInfo.CreateDate[:10]
  116. apiReply.CreateTime = apiInfo.CreateTime
  117. apiReply.Remarks = apiInfo.Remarks
  118. apiReply.TaskType = apiInfo.TaskType
  119. describe, _ := s.CodeListCache(ctx, apiReply.Coding)
  120. apiReply.Describe = describe
  121. apiReply.ScriptType = apiInfo.ScriptType
  122. apisReply = append(apisReply, apiReply)
  123. }
  124. }
  125. fmt.Println(time.Now().Unix())
  126. return apisReply, total, err
  127. }
  128. var failCacheKey = "%s:fail:list"
  129. func (s *ServiceLogList) CreateLog(c context.Context, request request.AddLogRequest, logical ServiceLoging, status, noLogStatus string) {
  130. var err error
  131. if status == "99" {
  132. err = logical.SuccessLog(c, request)
  133. } else if noLogStatus == "99" {
  134. err = logical.NoLogStatusData(c, request)
  135. } else {
  136. err = logical.FailLog(c, request)
  137. }
  138. if request.Coding == 4301001 || request.Coding == 4501001 {
  139. return
  140. }
  141. if err != nil {
  142. body, _ := json.Marshal(request)
  143. current := time.Now().Format("2006-01-02")
  144. key := fmt.Sprintf(failCacheKey, current)
  145. global.GVA_REDIS.LPush(c, key, body)
  146. global.GVA_LOG.Error("创建失败!: "+string(body), zap.Error(err))
  147. return
  148. }
  149. global.GVA_LOG.Info("创建成功!: " + request.LogUuid + " code: " + strconv.Itoa(request.Coding))
  150. }
  151. func (s *ServiceLogList) CreateFailLog() {
  152. c := context.Background()
  153. current := time.Now().Format("2006-01-02")
  154. key := fmt.Sprintf(failCacheKey, current)
  155. n, err := global.GVA_REDIS.LLen(c, key).Result()
  156. if err != nil {
  157. global.GVA_LOG.Error("corn 获取队列长度失败!"+key, zap.Error(err))
  158. return
  159. }
  160. if n == 0 {
  161. global.GVA_LOG.Info("corn 获取队列长度为0!")
  162. return
  163. }
  164. if n > 300 {
  165. n = 300
  166. }
  167. for i := 0; i < int(n); i++ {
  168. str, err := global.GVA_REDIS.RPop(c, key).Result()
  169. if err != nil {
  170. global.GVA_LOG.Error("rpop获取数据失败!", zap.Error(err))
  171. return
  172. }
  173. var data request.AddLogRequest
  174. err = json.Unmarshal([]byte(str), &data)
  175. if err != nil {
  176. global.GVA_LOG.Error("rpop获取数据失败!", zap.Error(err))
  177. continue
  178. }
  179. l, err := s.logical.GetLogByUuidCoding(data.LogUuid, data.Coding, data.ScriptType)
  180. if err != nil {
  181. global.GVA_LOG.Error("GetLogByUuidCoding获取数据失败!", zap.Error(err))
  182. continue
  183. }
  184. if l.GameId != 0 {
  185. global.GVA_LOG.Info("数据已存在!: " + data.LogUuid + " code: " + strconv.Itoa(data.Coding))
  186. continue
  187. }
  188. coding := strconv.Itoa(data.Coding)
  189. nodeCoding := coding[:3]
  190. status := coding[5:]
  191. noLogStatus := coding[3:5]
  192. var logical ServiceLoging
  193. switch nodeCoding {
  194. case "410":
  195. logical = new(loging2.PullAccountLog)
  196. break
  197. case "430":
  198. logical = new(loging2.SimulatorStartLog)
  199. break
  200. case "450":
  201. logical = new(loging2.GameStartLog)
  202. break
  203. case "460":
  204. logical = new(loging2.LoginLog)
  205. break
  206. case "470":
  207. logical = new(loging2.EnterMainLog)
  208. break
  209. case "480":
  210. logical = new(loging2.FeeLog)
  211. break
  212. default:
  213. logical = new(loging2.OtherLog)
  214. break
  215. }
  216. s.CreateLog(c, data, logical, status, noLogStatus)
  217. global.GVA_LOG.Info("重试!: " + data.LogUuid + " code: " + strconv.Itoa(data.Coding))
  218. }
  219. }
  220. func (s *ServiceLogList) RegularCreateLogingTable() {
  221. var loging log.Loging
  222. err := loging.CreateLogingTable()
  223. if err != nil {
  224. global.GVA_LOG.Error("RegularCreateLogingTable 建表失败!", zap.Error(err))
  225. }
  226. global.GVA_LOG.Info("建表成功")
  227. }